Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
<copyright> Copyright (c) 2009, 2010 Springsite BV (The Netherlands) and others All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: Martin Taal - Initial API and implementation </copyright> $Id: ModelResolver.java,v 1.17 2011/08/28 12:34:59 mtaal Exp $
 
 
 package org.eclipse.emf.texo.model;
 
 import java.util.List;
 import java.util.Map;
 
 import  org.eclipse.emf.ecore.EClass;
 import  org.eclipse.emf.ecore.EClassifier;
 import  org.eclipse.emf.ecore.EDataType;
 import  org.eclipse.emf.ecore.EObject;
 import  org.eclipse.emf.ecore.EPackage;
 import  org.eclipse.emf.ecore.EReference;
 import  org.eclipse.emf.ecore.EStructuralFeature;
The ModelResolver is the central location for retrieving model information and setting and getting values on a target model-managed object for an EStructuralFeature. When a ModelPackage initializes then it registers itself in the model resolver. The ModelPackage also registers the generated classes and their corresponding EClass in the ModelResolver. ModelPackage instances can be retrieved by calling the getModelPackage(String) method. At runtime model information can be obtained for a model-managed pojo by calling the getModelObject(Object) method. The ModelObject wraps/adapts a model-managed pojo and provides access to runtime model information (the EClass) and generic set/get methods (see ModelObject.eGet(EStructuralFeature) and ModelObject.eSet(EStructuralFeature, Object)). It is also possible to retrieve model information for a Class. For this the class provides access to the ModelDescriptor maintained for each registered Class. The ModelDescriptor contains the EClass and ModelPackage. The ModelResolver is maintained in a static member (which can be overwritten with your own implementation). It is also maintainable by thread. If you want a specific thread to use a different ModelResolver then call ;ink .setThreadInstance(ModelResolver). Note that you should take care to call this method with a null value (to clear the ThreadLocal) at the end of the thread to clean up the thread ModelResolver (some environments, for example Tomcat, re-uses Thread objects!).

Author(s):
Martin Taal
See also:
EClassifier
EPackage
ModelPackage
ModelFactory
ModelObject
 
 public class ModelResolver implements TexoStaticSingleton {
 
   private static ModelResolver instance = new ModelResolver();
   private static ThreadLocal<ModelResolverthreadInstance = new ThreadLocal<ModelResolver>();

  
Get the ModelResolver stored as a static instance. If a ModelResolver is set for this thread (see setThreadInstance(ModelResolver) then that one is returned.

Returns:
an instance of a ModelResolver
 
   public static ModelResolver getInstance() {
     if (.get() != null) {
       return .get();
     }
     return ;
   }
 
   public static void setInstance(final ModelResolver instance) {
     . = instance;
   }

  
Set a specific ModelResolver for this thread. See the comments in the Class header above.

Parameters:
instance
 
   public static void setThreadInstance(final ModelResolver instance) {
     .set(instance);
   }
 
 
  private final Map<EClassifier, Class<?>> eClassifierToClassMapping = new ConcurrentHashMap<EClassifier, Class<?>>();
  private EPackage.Registry ePackageRegistry = EPackage.Registry.INSTANCE;

  
Registers a ModelPackage in this registry.

Parameters:
ecoreModelPackage to register, not NULL
  public void registerModelPackage(final ModelPackage modelPackage) {
    Check.isNull(.get(modelPackage.getNsURI()), "The EcoreModelPackage with NsUri " //$NON-NLS-1$
        + modelPackage.getNsURI() + " has already been registered!");//$NON-NLS-1$
    .put(modelPackage.getNsURI(), modelPackage);
  }

  
Return a ModelPackage on the basis of the NsUri

Parameters:
nsURI the namespace URI as defined in the EPackage.getNsURI()
Returns:
an ModelPackage instance or null if not found
See also:
ModelPackage.getNsURI()
  public ModelPackage getModelPackage(final String nsURI) {
    return .get(nsURI);
  }

  
Create a model enabled objecct for the EClass. If the related EPackage does not have a ModelPackage then a DynamicModelObject is returned.

See also:
ModelFactory.create(EClass)
  public Object create(EClass eClass) {
    final ModelPackage modelPackage = getModelPackage(eClass.getEPackage().getNsURI());
    // a dynamic ModelObject
    if (modelPackage == null) {
      final DynamicModelObject result = ComponentProvider.getInstance().newInstance(DynamicModelObject.class);
      result.setEClass(eClass);
      return result;
    }
    return modelPackage.getModelFactory().create(eClass);
  }

  
Return all registered model packages.
    return .values();
  }

  
Convert a value to a string using the ModelFactory. If there is not ModelPackage registered for the EDataType then the EFactory is used.

Parameters:
eDataType the EDataType to convert
value the value to convert
Returns:
the String representation of the value
  public String convertToString(EDataType eDataTypeObject value) {
    final ModelPackage modelPackage = getModelPackage(eDataType.getEPackage().getNsURI());
    if (modelPackage != null) {
      return modelPackage.getModelFactory().convertToString(eDataTypevalue);
    }
    return eDataType.getEPackage().getEFactoryInstance().convertToString(eDataTypevalue);
  }

  
Creates a value from a String representation using the ModelFactory.createFromString(EDataType, String) method. If there is no registered ModelPackage for the name space uri then the EFactory is used.

Parameters:
eDataType the EDataType of the to-be-converted value
strValue the String to convert
Returns:
the object representation of the string
  public Object createFromString(EDataType eDataTypeString strValue) {
    final ModelPackage modelPackage = getModelPackage(eDataType.getEPackage().getNsURI());
    if (modelPackage != null) {
      return modelPackage.getModelFactory().createFromString(eDataTypestrValue);
    }
    return eDataType.getEPackage().getEFactoryInstance().createFromString(eDataTypestrValue);
  }

  
Register a new mapping from a Class to its ModelDescriptor.

Parameters:
clazz the Class to register
eClassifier the EClassifier
modelPackage the ModelPackage giving access to the ModelAccessor
  public void registerClassModelMapping(final Class<?> clazzfinal EClassifier eClassifier,
      final ModelPackage modelPackage) {
        "The class " + clazz + " already has a model descriptor " + .get(clazz)); //$NON-NLS-1$ //$NON-NLS-2$
    final ModelDescriptor modelDescriptor = new ModelDescriptor();
    modelDescriptor.setEClassifier(eClassifier);
    modelDescriptor.setModelPackage(modelPackage);
    .put(clazzmodelDescriptor);
    .put(eClassifierclazz);
  }

  
Removes the artifacts of the ModelPackage and its sub packages from the internal data structures.
  public void deregister(ModelPackage modelPackage) {
    deregister(modelPackage.getEPackage());
  }

  

See also:
deregister(ModelPackage)
  public void deregister(EPackage ePackage) {
    .remove(ePackage.getNsURI());
    .remove(ePackage.getNsURI());
    final List<Class<?>> toRemove = new ArrayList<Class<?>>();
    for (Class<?> clz : .keySet()) {
      final ModelDescriptor modelDescriptor = .get(clz);
      if (modelDescriptor.getModelPackage().getEPackage() == ePackage) {
        toRemove.add(clz);
      }
    }
    for (Class<?> clz : toRemove) {
      .remove(clz);
    }
    for (EClassifier eClassifier : ePackage.getEClassifiers()) {
      .remove(eClassifier);
    }
    for (EPackage subEPackage : ePackage.getESubpackages()) {
      deregister(subEPackage);
    }
  }

  
Returns the implementation class for a certain EClassifier, can be a normal java class or an enum class.

Parameters:
eClassifier the EClassifier to get the implementation class for
Returns:
the implementation class
Throws:
IllegalArgumentException if there is no implementation class
  public Class<?> getImplementationClass(EClassifier eClassifier) {
    final Class<?> clz = .get(eClassifier);
    if (clz == null) {
      throw new IllegalArgumentException("No implementation class for classifier " + eClassifier); //$NON-NLS-1$
    }
    return clz;
  }

  
Is the EClassifier managed by the ModelResolver.
  public boolean isModeledEClassifier(EClassifier eClassifier) {
    return .containsKey(eClassifier);
  }

  

Parameters:
modelObject the object to check if it is managed by a ModelPackage. Note if null is passed then false is returned.
Returns:
true if this is an object managed by a ModelPackage, false if not or if null is passed
  public boolean isModelEnabled(final Object target) {
    if (target == null) {
      return false;
    }
    return getModelDescriptor(target.getClass(), false) != null;
  }

  

Returns:
the EClass represented by this object
Throws:
IllegalArgumentException if no EClass can be found
  public ModelObject<?> getModelObject(final Object target) {
    Check.isNotNullArgument(target"target"); //$NON-NLS-1$
    if (target instanceof ModelObject<?>) {
      return (ModelObject<?>) target;
    }
    if (target instanceof EObject) {
      final ModelEObject modelEObject = ComponentProvider.getInstance().newInstance(ModelEObject.class);
      modelEObject.setTarget((EObject) target);
      return modelEObject;
    }
    final ModelDescriptor modelDescriptor = getModelDescriptor(target.getClass(), true);
    return modelDescriptor.createAdapter(target);
  }

  
Wrap an object (a pojo feature map entry) in a ModelFeatureMapEntry.

Parameters:
eFeature the feature used to find/create the ModelFeatureMapEntry.
adaptee the pojo entry to wrap
Returns:
the wrapper
  public ModelFeatureMapEntry<?> getModelFeatureMapEntry(EStructuralFeature eFeatureObject adaptee) {
    final String nsuri = eFeature.getEContainingClass().getEPackage().getNsURI();
    final ModelPackage modelPackage = getModelPackage(nsuri);
    if (modelPackage == null) {
      final DynamicModelFeatureMapEntry fmEntry = ComponentProvider.getInstance().newInstance(
          DynamicModelFeatureMapEntry.class);
      fmEntry.setEStructuralFeature(eFeature);
      return fmEntry;
    }
    return modelPackage.getModelFactory().createModelFeatureMapEntry(eFeatureadaptee);
  }

  
Creates a feature map entry instance for a certain EStructuralFeature.

Parameters:
eStructuralFeature the feature map feature
Returns:
the pojo feature map entry
Generated:
  public Object createFeatureMapEntry(EStructuralFeature eFeature) {
    final String nsuri = eFeature.getEContainingClass().getEPackage().getNsURI();
    final ModelPackage modelPackage = getModelPackage(nsuri);
    if (modelPackage == null) {
      final DynamicModelFeatureMapEntry fmEntry = ComponentProvider.getInstance().newInstance(
          DynamicModelFeatureMapEntry.class);
      fmEntry.setEStructuralFeature(eFeature);
      return fmEntry;
    }
    Check.isNotNull(modelPackage"No modelpackage found for EPackage nsuri: " + nsuri); //$NON-NLS-1$
    return modelPackage.getModelFactory().createFeatureMapEntry(eFeature);
  }

  
Find the ModelDescriptor for a Class. The system tries to find one for the Class itself or for its super Classes or implemented interfaces. If no ModelDescriptor can be found then an IllegalArgumentException is thrown (depends on the exceptionOnNotFound parameter).

Parameters:
clazz the Class to find the ModelDescriptor for.
exceptionOnNotFound if true then an IllegalArgumentException is thrown when no ModelDescriptor can be found, if false then null is returned (if no ModelDescriptor can be found).
Returns:
the found ModelDescriptor.
Throws:
IllegalArgumentException if no ModelDescriptor can be found and exceptionOnNotFound is true
  public ModelDescriptor getModelDescriptor(final Class<?> clazzfinal boolean exceptionOnNotFound) {
    ModelDescriptor modelDescriptor = .get(clazz);
    if (!exceptionOnNotFound && modelDescriptor == null) {
      return null;
    }
    Check.isNotNull(modelDescriptor"The class " + clazz + " is not managed by this ModelResolver"); //$NON-NLS-1$ //$NON-NLS-2$
    if (modelDescriptor != null) {
      return modelDescriptor;
    }
    // not there, try superclasses first
    modelDescriptor = findModelDescriptorSuperClasses(clazz.getSuperclass());
    // then try interfaces
    if (modelDescriptor == null) {
      modelDescriptor = findModelDescriptorInterfaces(clazz);
    }
    // found register it so that it is found quicker the next time
    if (modelDescriptor != null) {
      .put(clazzmodelDescriptor);
      return modelDescriptor;
    }
    if (exceptionOnNotFound) {
      throw new IllegalArgumentException("The class " + clazz //$NON-NLS-1$
          + " is not managed by this ModelResolver"); //$NON-NLS-1$
    }
    return null;
  }

  
Checks the super class of a class for the existence of a ModelDescriptor.

Parameters:
clazz the Class to find the ModelDescriptor for
Returns:
the ModelDescriptor or null if none found
  protected ModelDescriptor findModelDescriptorSuperClasses(final Class<?> clazz) {
    if (clazz == null) {
      // end of the inheritance tree
      return null;
    }
    final ModelDescriptor modelDescriptor = .get(clazz);
    if (modelDescriptor == null) {
    }
    return modelDescriptor;
  }

  
Checks the interfaces of a class. Depth first, so for each implemented interface, first its inherited interfaces are checked before moving on to the next implemented interface.

Parameters:
clazz the Class to find the ModelDescriptor for
Returns:
the ModelDescriptor or null if none found
  protected ModelDescriptor findModelDescriptorInterfaces(final Class<?> clazz) {
    if (clazz == null) {
      return null;
    }
    for (final Class<?> interf : clazz.getInterfaces()) {
      ModelDescriptor modelDescriptor = .get(clazz);
      // ask the super interfaces
      if (modelDescriptor == null) {
        modelDescriptor = findModelDescriptorInterfaces(interf);
      }
      if (modelDescriptor != null) {
        return modelDescriptor;
      }
    }
    return null;
  }
  public EPackage.Registry getEPackageRegistry() {
    return ;
  }
  public void setEPackageRegistry(EPackage.Registry ePackageRegistry) {
    this. = ePackageRegistry;
  }

  
Returns the list of EReference instances which refer to the EClass of the object. Note that the EReference can also refer to a super EClass of the actual EClass of the object.

Parameters:
object the object for which the ereferences need to be returned.
includeContainmentReferences also return refering objects which reference the target through an EReference with containment==true
Returns:
the list of EReferences which refer to the object.
  public List<EReference> getReferingEReferences(EClass eClassboolean includeContainerReferences) {
    final List<EReference> result = new ArrayList<EReference>();
    for (EClassifier eClassifier : .keySet()) {
      if (eClassifier instanceof EClass) {
        final EClass referingEClass = (EClass) eClassifier;
        for (EReference eReference : referingEClass.getEAllReferences()) {
          if (eReference.isVolatile() || eReference.isTransient()) {
            continue;
          }
          if (eReference.isContainer() && !includeContainerReferences) {
            continue;
          }
          if (refersTo(eReferenceeClass)) {
            result.add(eReference);
          }
        }
      }
    }
    return result;
  }
  private boolean refersTo(EReference eReference, EClass eClass) {
    final EClass referedEClass = eReference.getEReferenceType();
    return referedEClass == eClass || referedEClass.isSuperTypeOf(eClass);
  }

  
Returns the name of the java member in the class which represents this EStructuralFeature.

Parameters:
eFeature the EStructuralFeature for which to get the java property name.
Returns:
the java member name
  public String getJavaPropertyName(EStructuralFeature eFeature) {
    return eFeature.getName();
  }

  
Combines model information for a specific EClassifier, provides a convenient api to get the adapter ModelObject for an object.

Author(s):
mtaal
  public static class ModelDescriptor {
    private EClassifier eClassifier;
    private ModelPackage modelPackage;
    private ModelFactory modelFactory;

    
Retrieve/create a ModelObject instance for the object passed in as a parameter.

Parameters:
object the object for which a model adapter needs to be provided
Returns:
the ModelObject instance.
    public ModelObject<?> createAdapter(final Object object) {
      Check.isTrue(getEClassifier() instanceof EClass, "The model descriptor: " + this //$NON-NLS-1$
          + " does not represent an EClass"); //$NON-NLS-1$
      return .createModelObject((EClass) object);
    }
    public ModelPackage getModelPackage() {
      return ;
    }
    public void setModelPackage(final ModelPackage modelPackage) {
      this. = modelPackage;
       = modelPackage.getModelFactory();
    }
    public EClassifier getEClassifier() {
      return ;
    }
    public void setEClassifier(final EClassifier eClassifier) {
      this. = eClassifier;
    }
    @Override
    public String toString() {
      return  + " " + //$NON-NLS-1$
    }
  }
    return ;
  }
  protected Map<EClassifier, Class<?>> getEClassifierToClassMapping() {
  }
    return ;
  }
New to GrepCode? Check out our FAQ X