org.eigenbase.jmi
Class JmiObjUtil

java.lang.Object
  extended by org.eigenbase.jmi.JmiObjUtil

public abstract class JmiObjUtil
extends Object

Static JMI utilities.

Version:
$Id: //open/dev/farrago/src/org/eigenbase/jmi/JmiObjUtil.java#23 $
Author:
John V. Sichi

Nested Class Summary
static interface JmiObjUtil.Namer
          Generates a name for a JMI element.
 
Field Summary
private static int DEFAULT_MAX_STRING_LENGTH
          Default maximum repository string length.
 
Constructor Summary
JmiObjUtil()
           
 
Method Summary
private static
<T extends StructuralFeature>
void
addFeatures(List<T> list, MofClass mofClass, Class<T> filterClass, boolean includeMultiValued)
           
static void assertConstraints(RefObject obj)
          Deprecated. use RefBaseObject.refVerifyConstraints(boolean) instead
private static boolean compositeEquals(RefObject obj1, RefObject obj2)
           
static void copyAttributes(RefObject dst, RefObject src)
          Copies attribute values from one RefObject to another compatible RefObject.
static void dumpGraph(JmiDependencyGraph graph, PrintWriter pw, JmiObjUtil.Namer namer)
          Prints a dependency graph to a given writer.
static void dumpGraph(JmiModelView view, PrintWriter pw)
          Prints a model view to a given writer.
static String exportToXmiString(Collection<?> collection)
          Exports a collection of JMI objects as XMI.
static String getAccessorName(ModelElement modelElement)
          Constructs the generated name of an accessor method.
static SortedMap<String,Object> getAttributeValues(RefObject src)
          Gets a SortedMap (from String to Object) containing the attribute values for a RefObject.
static Class<? extends RefObject> getClassForRefClass(ClassLoader classLoader, RefClass refClass, boolean nullIfNotFound)
          Finds the Java class generated for a particular RefClass.
static Class<? extends RefObject> getClassForRefClass(MDRepository repos, RefClass refClass)
          Finds the Java class generated for a particular RefClass, or RefObject.class if not found.
static Class<? extends RefObject> getClassForRefClass(RefClass refClass)
          Deprecated. use getClassForRefClass(MDRepository, RefClass)
static RefBaseObject getContainer(RefBaseObject refObject)
          Gets an object's container.
static String getEnumFieldName(String enumSymbol)
          Constructs the generated name of an enum symbol.
static
<T extends StructuralFeature>
List<T>
getFeatures(RefClass refClass, Class<T> filterClass, boolean includeMultiValued)
          Gets a List of instance-level StructuralFeatures for a RefClass.
private static
<T> Class<? extends T>
getJavaInterfaceForProxy(Class<?> proxyClass, Class<T> resultClass, String delimiter)
           
static Class<? extends RefAssociation> getJavaInterfaceForRefAssoc(RefAssociation refAssoc)
          Finds the Java interface corresponding to a JMI association
static Class<? extends RefClass> getJavaInterfaceForRefClass(RefClass refClass)
          Finds the Java interface corresponding to a JMI class.
static Class<? extends RefObject> getJavaInterfaceForRefObject(RefClass refClass)
          Finds the Java interface corresponding to object instances of a JMI class.
static Class<? extends RefPackage> getJavaInterfaceForRefPackage(RefPackage refPackage)
          Finds the Java interface corresponding to a JMI package.
static int getMaxLength(RefClass refClass, Attribute attr)
           
static String getMetaObjectName(RefBaseObject refObject)
          Gets the name of the model element corresponding to a RefBaseObject
static
<T extends StructuralFeature>
T
getNamedFeature(RefClass refClass, Class<T> filterClass, String featureName, boolean includeMultiValued)
          Limits the result of getFeatures(RefClass, Class, boolean) to the first feature with the given name.
static long getObjectId(RefObject refObject)
          Gets the 64-bit object ID for a JMI object.
static RefPackage getSubPackage(RefPackage rootPackage, String[] names, int prefix)
          Looks up a subpackage by name.
static String getTypeName(RefObject refObject)
          Returns the type name of an object.
static Collection<RefBaseObject> importFromXmiString(RefPackage extent, String string)
          Imports a collection of JMI objects from XMI.
static boolean isBlank(String value)
          Tests an attribute value to see if it is blank.
static RefObject newClone(RefObject refObject)
          Clones a RefObject.
static void setAttributeValues(RefObject dst, SortedMap<String,Object> map)
          Sets values for attributes of a RefObject.
static void setMandatoryPrimitiveDefaults(RefObject obj)
          Sets default values for mandatory attributes of primitive type (e.g.
static String toMofId(long objectId)
          Gets the MofId for a given 64-bit object ID for a JMI object.
static String toString(RefClass refClass)
          Returns the name of a class.
static String toString(RefObject refObject)
          Generates a string describing an object and its attributes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_MAX_STRING_LENGTH

private static final int DEFAULT_MAX_STRING_LENGTH
Default maximum repository string length. Must match the value used at the time the repository was generated.

See Also:
Constant Field Values
Constructor Detail

JmiObjUtil

public JmiObjUtil()
Method Detail

getAttributeValues

public static SortedMap<String,Object> getAttributeValues(RefObject src)
Gets a SortedMap (from String to Object) containing the attribute values for a RefObject. Multi-valued attributes are not included.

Parameters:
src - RefObject to query
Returns:
map with attribute names as ordering keys

setAttributeValues

public static void setAttributeValues(RefObject dst,
                                      SortedMap<String,Object> map)
Sets values for attributes of a RefObject.

Parameters:
dst - object to modify
map - see return of getAttributeValues

compositeEquals

private static boolean compositeEquals(RefObject obj1,
                                       RefObject obj2)

getFeatures

public static <T extends StructuralFeature> List<T> getFeatures(RefClass refClass,
                                                                Class<T> filterClass,
                                                                boolean includeMultiValued)
Gets a List of instance-level StructuralFeatures for a RefClass.

Parameters:
refClass - class of interest
filterClass - only objects which are instances of this Class will be returned; so, for example, pass Attribute.class if you want only attributes, or StructuralFeature.class if you want everything
includeMultiValued - if true, multi-valued attributes will be included; otherwise, they will be filtered out
Returns:
attribute list

addFeatures

private static <T extends StructuralFeature> void addFeatures(List<T> list,
                                                              MofClass mofClass,
                                                              Class<T> filterClass,
                                                              boolean includeMultiValued)

copyAttributes

public static void copyAttributes(RefObject dst,
                                  RefObject src)
Copies attribute values from one RefObject to another compatible RefObject.

Parameters:
dst - RefObject to copy to
src - RefObject to copy from

exportToXmiString

public static String exportToXmiString(Collection<?> collection)
Exports a collection of JMI objects as XMI.

Parameters:
collection - JMI objects to be exported
Returns:
string representation of XMI

importFromXmiString

public static Collection<RefBaseObject> importFromXmiString(RefPackage extent,
                                                            String string)
Imports a collection of JMI objects from XMI.

Parameters:
extent - target
string - string representation of XMI
Returns:
outermost JMI objects imported

newClone

public static RefObject newClone(RefObject refObject)
Clones a RefObject.

Parameters:
refObject - RefObject to clone; must have neither associations nor composite types
Returns:
cloned instance

getJavaInterfaceForRefClass

public static Class<? extends RefClass> getJavaInterfaceForRefClass(RefClass refClass)
                                                             throws ClassNotFoundException
Finds the Java interface corresponding to a JMI class.

Parameters:
refClass - the JMI class
Returns:
corresponding Java interface
Throws:
ClassNotFoundException

getJavaInterfaceForRefAssoc

public static Class<? extends RefAssociation> getJavaInterfaceForRefAssoc(RefAssociation refAssoc)
                                                                   throws ClassNotFoundException
Finds the Java interface corresponding to a JMI association

Parameters:
refAssoc - the JMI association
Returns:
corresponding Java interface
Throws:
ClassNotFoundException

getJavaInterfaceForRefObject

public static Class<? extends RefObject> getJavaInterfaceForRefObject(RefClass refClass)
                                                               throws ClassNotFoundException
Finds the Java interface corresponding to object instances of a JMI class.

Parameters:
refClass - the JMI class
Returns:
corresponding Java interface
Throws:
ClassNotFoundException

getJavaInterfaceForRefPackage

public static Class<? extends RefPackage> getJavaInterfaceForRefPackage(RefPackage refPackage)
                                                                 throws ClassNotFoundException
Finds the Java interface corresponding to a JMI package.

Parameters:
refPackage - the JMI package
Returns:
corresponding Java interface
Throws:
ClassNotFoundException

getJavaInterfaceForProxy

private static <T> Class<? extends T> getJavaInterfaceForProxy(Class<?> proxyClass,
                                                               Class<T> resultClass,
                                                               String delimiter)
                                                    throws ClassNotFoundException
Throws:
ClassNotFoundException

getObjectId

public static long getObjectId(RefObject refObject)
Gets the 64-bit object ID for a JMI object. This is taken from the last 8 bytes of the MofId. REVIEW: need to make sure this is locally unique within a repository.

Parameters:
refObject - JMI object
Returns:
object ID

toMofId

public static String toMofId(long objectId)
Gets the MofId for a given 64-bit object ID for a JMI object. Generates the MofId from the long without validating whether the associated object exists.

Parameters:
objectId - JMI object id (as from getObjectId(RefObject))
Returns:
object's MofId

getTypeName

public static String getTypeName(RefObject refObject)
Returns the type name of an object. For example, "FemLocalView".

Parameters:
refObject - Object
Returns:
type name

toString

public static String toString(RefClass refClass)
Returns the name of a class.

Parameters:
refClass - Class
Returns:
Name of class

toString

public static String toString(RefObject refObject)
Generates a string describing an object and its attributes.

Useful for debugging. Typical result:

FemDataWrapper(creationTimestamp='2007-09-02 16:57:22.252', description='null', foreign='true', language='JAVA', libraryFile='class net.sf.farrago.namespace.mdr.MedMdrForeignDataWrapper', lineageId='1dacd3af-9181-47fd-94f3-64fb3a0506a4', modificationTimestamp='2007-09-02 17:21:08.846', name='SYS_MDR', visibility='vk_public')

Parameters:
refObject - Object
Returns:
Description of object

getSubPackage

public static RefPackage getSubPackage(RefPackage rootPackage,
                                       String[] names,
                                       int prefix)
Looks up a subpackage by name.

Parameters:
rootPackage - starting package from which to descend
names - array of package names representing path
prefix - number of elements of names to use
Returns:
subpackage or null if not found

getMetaObjectName

public static String getMetaObjectName(RefBaseObject refObject)
Gets the name of the model element corresponding to a RefBaseObject

Parameters:
refObject - RefBaseObject representation of a model element
Returns:
model element name

getAccessorName

public static String getAccessorName(ModelElement modelElement)
Constructs the generated name of an accessor method.

Parameters:
modelElement - ModelElement to be accessed
Returns:
constructed accessor name

getEnumFieldName

public static String getEnumFieldName(String enumSymbol)
Constructs the generated name of an enum symbol.

Parameters:
enumSymbol - name of enumeration symbol
Returns:
constructed field name

getClassForRefClass

public static Class<? extends RefObject> getClassForRefClass(MDRepository repos,
                                                             RefClass refClass)
Finds the Java class generated for a particular RefClass, or RefObject.class if not found.

Parameters:
repos - the MDRepository that the given RefClass belongs to
refClass - the reflective JMI class representation
Returns:
the generated Java class, or RefObject.class if no Java class has been generated

getClassForRefClass

@Deprecated
public static Class<? extends RefObject> getClassForRefClass(RefClass refClass)
Deprecated. use getClassForRefClass(MDRepository, RefClass)


getClassForRefClass

public static Class<? extends RefObject> getClassForRefClass(ClassLoader classLoader,
                                                             RefClass refClass,
                                                             boolean nullIfNotFound)
Finds the Java class generated for a particular RefClass.

Parameters:
classLoader - Class loader. Must not be null: if in doubt, use ClassLoader.getSystemClassLoader()
refClass - the reflective JMI class representation
nullIfNotFound - If true, return null if not found; if false, return RefObject.class if not found
Returns:
the generated Java class, or RefObject.class if no Java class has been generated

getContainer

public static RefBaseObject getContainer(RefBaseObject refObject)
Gets an object's container. If the object is a ModelElement, its container will be too; otherwise, its container will be a RefPackage.

Parameters:
refObject - object for which to find the container
Returns:
container

assertConstraints

public static void assertConstraints(RefObject obj)
Deprecated. use RefBaseObject.refVerifyConstraints(boolean) instead

Asserts that constraints are satisfied. This method exists because refVerifyConstraints didn't used to work MDR. Now it does, so this method is deprecated.

Parameters:
obj - the object to be verified

setMandatoryPrimitiveDefaults

public static void setMandatoryPrimitiveDefaults(RefObject obj)
Sets default values for mandatory attributes of primitive type (e.g. false for boolean). MDR actually doesn't require this (it synthesizes the default values on demand), except in refVerifyConstraints, so we call this just before invoking that method.

Parameters:
obj - the object being updated

getNamedFeature

public static <T extends StructuralFeature> T getNamedFeature(RefClass refClass,
                                                              Class<T> filterClass,
                                                              String featureName,
                                                              boolean includeMultiValued)
Limits the result of getFeatures(RefClass, Class, boolean) to the first feature with the given name.

Parameters:
refClass - class of interest
filterClass - only objects which are instances of this Class will be returned; so, for example, pass Attribute.class if you want only attributes, or StructuralFeature.class if you want everything
featureName - name of the feature to return
includeMultiValued - if true, multi-valued attributes will be included; otherwise, they will be filtered out
Returns:
the first feature matching the given parameters or null if none are found

getMaxLength

public static int getMaxLength(RefClass refClass,
                               Attribute attr)

isBlank

public static boolean isBlank(String value)
Tests an attribute value to see if it is blank.

Parameters:
value - Value
Returns:
true if value is either null or the empty string

dumpGraph

public static void dumpGraph(JmiDependencyGraph graph,
                             PrintWriter pw,
                             JmiObjUtil.Namer namer)
Prints a dependency graph to a given writer.

Parameters:
graph - Dependency graph
pw - Writer
namer - Maps JMI objects in the graph to a descriptive string

dumpGraph

public static void dumpGraph(JmiModelView view,
                             PrintWriter pw)
Prints a model view to a given writer.

Parameters:
view - Model view
pw - Writer