001/*
002 * Copyright c 2018 Rusi Popov, MDA Tools.net All rights reserved.
003 *
004 * This program and the accompanying materials are made available under the terms of the
005 * Eclipse Public License v2.0 which accompanies this distribution, and is available at
006 * http://www.eclipse.org/legal/epl-v20.html
007 */
008package net.mdatools.modelant.core.operation.element;
009
010import java.util.ArrayList;
011import java.util.Collection;
012import java.util.Iterator;
013import java.util.List;
014
015import javax.jmi.model.Attribute;
016import javax.jmi.model.MofClass;
017import javax.jmi.reflect.RefObject;
018
019import net.mdatools.modelant.core.api.Function;
020
021/**
022 * Retrieve all attributes of the model element, either defined in it or inherited
023 * @author Rusi Popov (popovr@mdatools.net)
024 */
025public class RetrieveAttributes implements Function<RefObject, Collection<String>> {
026
027  /**
028   * This method retrieves the names of all attributes of the model element provided
029   * @param element is non-null object from the first model
030   * @return a non-null list of names of attributes
031   */
032  public Collection<String> execute(RefObject element) {
033    List<String> result;
034    List<MofClass> metaClasses;
035    MofClass metaClass;
036
037    Iterator featuresIterator;
038    Iterator<MofClass> superclassIterator;
039    Object contained;
040
041    result = new ArrayList<String>();
042    metaClass = (MofClass) element.refMetaObject();
043
044    // check all inherited attributes
045    metaClasses = new ArrayList<MofClass>();
046    metaClasses.add( metaClass );
047    metaClasses.addAll( metaClass.allSupertypes() );
048
049    superclassIterator = metaClasses.iterator();
050    while ( superclassIterator.hasNext() ) {
051      metaClass = superclassIterator.next();
052
053      featuresIterator = metaClass.getContents().iterator();
054      while ( featuresIterator.hasNext() ) {
055        contained = featuresIterator.next();
056
057        if ( contained instanceof Attribute ) {
058          result.add( ((Attribute) contained).getName() );
059        }
060      }
061    }
062    return result;
063  }
064}