Methods Summary |
---|
protected java.io.BufferedOutputStream | createFile(java.lang.String className, java.lang.String baseFileName, java.lang.String extension)Creates a file with the given base file name and extension
parallel to the supplied class (if it does not yet exist).
char extensionCharacter = '.";
File file = getFile(className,
baseFileName + extensionCharacter + extension);
if (file == null)
{
Class classElement = (Class)getClass(className);
if (classElement != null)
{
// need to find the path before the package name
String path = classElement.getResource(
getShortClassName(className) + extensionCharacter +
CLASS_EXTENSION).getFile();
int index = path.lastIndexOf(extensionCharacter) + 1;
file = new File(path.substring(0, index) + extension);
file.createNewFile();
}
}
return ((file != null)
? (new BufferedOutputStream(new FileOutputStream(file)))
: null);
|
protected void | deleteFile(java.lang.String className, java.lang.String fileName)Deletes the file with the given file name which is parallel
to the supplied class.
File file = getFile(className, fileName);
if ((file != null) && file.exists())
file.delete();
|
protected java.lang.ClassLoader | findClassLoader(java.lang.String className, java.lang.ClassLoader classLoader)This method returns the class loader used to find mapping information
for the specified className. If the classLoader argument is not null,
the method updates the classLoaders cache and returns the specified
classLoader. Otherwise it checks the cache for the specified className
and returns this class loader. If there is no cached class loader it
returns the current class loader.
ClassLoader cached = null;
if (className == null)
return null;
else if (className.startsWith(JAVA_PACKAGE) || isPrimitive(className))
// Use current class loader for java packages or primitive types
// - these classes cannot have the multiple class loader conflict
// - these classes should not show up in the classLoaders map
return getClass().getClassLoader();
synchronized (classLoaders)
{
cached = (ClassLoader)classLoaders.get(className);
if (classLoader == null)
{
// Case 1: specified class loader is null =>
// return cached class loader if available, if not
// take current class loader
classLoader =
(cached != null) ? cached : getClass().getClassLoader();
}
else if (cached == null)
{
// Case 2: specified class loader is NOT null AND
// no class loader cached for the class name =>
// put specified class loader in cache
classLoaders.put(className, classLoader);
}
else if (classLoader != cached)
{
// Case 3: specified class loader is NOT null AND
// cache contains class loader for this class name AND
// both class loaders are not identical =>
// pontential conflict
Class clazz = null;
Class cachedClazz = null;
try
{
String prop = ClassLoaderStrategy.getStrategy();
// Load the class using specified and cached class loader.
// NOTE, do not change the order of the next two lines, the
// catch block relies on it!
clazz = Class.forName(className, true, classLoader);
cachedClazz = Class.forName(className, true, cached);
if (clazz.getClassLoader() == cachedClazz.getClassLoader())
{
// Case 3a: both class loaders are the same =>
// return it
return cached;
}
else if (ClassLoaderStrategy.MULTIPLE_CLASS_LOADERS_IGNORE.equals(prop))
{
// Case 3b: both class loaders are different and
// the system property is defined as ignore =>
// ignore the specified class loader and return
// the cached class loader
return cached;
}
else if (ClassLoaderStrategy.MULTIPLE_CLASS_LOADERS_RELOAD.equals(prop))
{
// Case 3c: both class loaders are different and
// the system property is defined as reload =>
// discard the cached class loader and replace it
// by the specified class loader
removeResourcesFromCaches(cachedClazz.getClassLoader());
classLoaders.put(className, classLoader);
return classLoader;
}
else
{
// Case 3d: both class loaders are different and
// the system property is defined as error or
// any other value =>
// throw exception
throw new IllegalArgumentException(I18NHelper.getMessage(
getMessages(), "classloader.multiple", // NOI18N
className));
}
}
catch (ClassNotFoundException ex)
{
// At least one of the class loader could not find the class.
// Update the classLoader map, if the specified class loader
// could find it, but the cached could not.
if ((clazz != null) && (cachedClazz == null))
classLoaders.put(className, classLoader);
}
}
}
return classLoader;
|
protected java.lang.String | findPenultimateSuperclass(java.lang.String className)Returns the name of the second to top (top excluding java.lang.Object)
superclass for the given class name.
Class classElement = (Class)getClass(className);
Class objectClass = java.lang.Object.class;
Class testClass = null;
if (classElement == null)
return className;
while ((testClass = classElement.getSuperclass()) != null)
{
if (testClass.equals(objectClass))
break;
classElement = testClass;
}
return classElement.getName();
|
public java.lang.Object | getClass(java.lang.String className, java.lang.ClassLoader classLoader)Returns the class element with the specified className. The class is
found using Class.forName .
if (className == null)
return null;
try
{
classLoader = findClassLoader(className, classLoader);
return Class.forName(className, true, classLoader);
}
catch (ClassNotFoundException e)
{
return null;
}
|
public java.util.Map | getClassLoaderCache()Returns an unmodifiable copy of the ClassLoader cache.
return Collections.unmodifiableMap(classLoaders);
|
public java.lang.Object | getConstructor(java.lang.String className, java.lang.String[] argTypeNames)Returns the constructor element for the specified argument types
in the class with the specified name. Types are specified as type
names for primitive type such as int, float or as fully qualified
class names.
final Class classElement = (Class)getClass(className);
if (classElement != null)
{
final Class[] argTypes = getTypesForNames(argTypeNames);
return AccessController.doPrivileged(new PrivilegedAction()
{
public Object run ()
{
try
{
return ((Class)classElement).getDeclaredConstructor(
argTypes);
}
catch (NoSuchMethodException ex)
{
// constructor not found => return null
return null;
}
}
});
}
return null;
|
public java.lang.String | getDeclaringClass(java.lang.Object memberElement)Returns the string representation of declaring class of
the specified member element. Note, the member element is
either a class element as returned by getClass, a field element
as returned by getField, a constructor element as returned by
getConstructor, or a method element as returned by getMethod
executed on the same model instance. This implementation
expects the member element to be a reflection instance.
Class classElement = null;
if ((memberElement != null) && (memberElement instanceof Member))
classElement = ((Member)memberElement).getDeclaringClass();
return ((classElement != null) ? classElement.getName() : null);
|
public java.lang.Object | getField(java.lang.String className, java.lang.String fieldName)Returns the field element for the specified fieldName in the class
with the specified className.
final Class classElement = (Class)getClass(className);
if (classElement != null)
{
return AccessController.doPrivileged(new PrivilegedAction()
{
public Object run ()
{
try
{
return classElement.getDeclaredField(fieldName);
}
catch (NoSuchFieldException e)
{
// field not found => return null;
return null;
}
}
});
}
return null;
|
public java.util.List | getFields(java.lang.String className)Returns a list of names of all the declared field elements in the
class with the specified name.
List returnList = new ArrayList();
final Class classElement = (Class)getClass(className);
if (classElement != null)
{
Field[] fields = (Field[]) AccessController.doPrivileged(
new PrivilegedAction()
{
public Object run ()
{
return classElement.getDeclaredFields();
}
});
int i, count = fields.length;
for (i = 0; i < count; i++)
returnList.add(fields[i].getName());
}
return returnList;
|
protected java.io.File | getFile(java.lang.String className, java.lang.String fileName)Returns a file with the given file name which is parallel to the
supplied class.
Class classElement = (Class)getClass(className);
if (classElement != null)
{
// need to find the path before the package name
URL path = classElement.getResource(fileName.substring(
fileName.lastIndexOf(getShortClassName(className))));
return ((path != null) ? (new File(path.getFile())) : null);
}
return null;
|
protected java.io.BufferedInputStream | getInputStreamForResource(java.lang.String className, java.lang.ClassLoader classLoader, java.lang.String resourceName)Returns the input stream with the supplied resource name found with
the supplied class name.
NOTE, this implementation assumes the specified class loader is not null
and needs not to be validated. Any validation is done by getMappingClass
which is the only caller of this method.
InputStream is = ((className != null) ?
classLoader.getResourceAsStream(resourceName) : null);
BufferedInputStream rc = null;
if (is != null && !(is instanceof BufferedInputStream)) {
rc = new BufferedInputStream(is);
} else {
rc = (BufferedInputStream)is;
}
return rc;
|
public com.sun.jdo.api.persistence.model.mapping.MappingClassElement | getMappingClass(java.lang.String className, java.lang.ClassLoader classLoader)Returns the MappingClassElement created for the specified class name.
This method looks up the class in the internal cache. If not present
it loads the corresponding xml file containing the mapping information.
MappingClassElement mappingClass = null;
// First check class loader. This has to be done before the super call!
// Method Model.getMappingClass will check the MappingClassElement cache
// and will find an entry in the case of a multiple class loader for the
// same class name. So we have to check the multiple class loader first.
classLoader = findClassLoader(className, classLoader);
mappingClass = super.getMappingClass(className, classLoader);
if ((mappingClass != null) && (classLoader != null))
{
// Lookup the SchemElement connected to mappingClass. This reads
// the .dbschema file using the specified classLoader and stores the
// SchemaElement in the SchemaElement cache. Any subsequent
// SchemaElement.forName or TableElement.forName lookups will use
// the cached version.
String databaseRoot = mappingClass.getDatabaseRoot();
// An unmapped mapping class is allowed in which case the
// databaseRoot will be null (never mapped) or empty
// (mapped once, unmapped now), but if the databaseRoot is
// not null or empty and we can't find the schema, throw a
// RuntimeException to notify the user that something is wrong.
if (!StringHelper.isEmpty(databaseRoot) &&
(SchemaElement.forName(databaseRoot, classLoader) == null))
{
throw new RuntimeException(I18NHelper.getMessage(
getMessages(), "dbschema.not_found", // NOI18N
databaseRoot, className));
}
}
return mappingClass;
|
public java.lang.Object | getMethod(java.lang.String className, java.lang.String methodName, java.lang.String[] argTypeNames)Returns the method element for the specified method name and argument
types in the class with the specified name. Types are specified as
type names for primitive type such as int, float or as fully qualified
class names. Note, the method does not return inherited methods.
final Class classElement = (Class)getClass(className);
if (classElement != null)
{
final Class[] argTypes = getTypesForNames(argTypeNames);
return AccessController.doPrivileged(new PrivilegedAction()
{
public Object run ()
{
try
{
return classElement.getDeclaredMethod(
methodName, argTypes);
}
catch (NoSuchMethodException ex)
{
// method not found => return null
return null;
}
}
});
}
return null;
|
public int | getModifiers(java.lang.Object memberElement)Returns the modifier mask for the specified member element.
Note, the member element is either a class element as returned by
getClass, a field element as returned by getField, a constructor element
as returned by getConstructor, or a method element as returned by
getMethod executed on the same model instance. This implementation
expects the member element to be a reflection instance.
int modifiers = 0;
if (memberElement != null)
{
if (memberElement instanceof Class)
{
modifiers = ((Class)memberElement).getModifiers();
}
else if (memberElement instanceof Member)
{
modifiers = ((Member)memberElement).getModifiers();
}
}
return modifiers;
|
private java.lang.String | getNameForType(java.lang.Class type)
String typeName = null;
if (type != null)
{
if (type.isArray())
{
typeName = getNameForType(
type.getComponentType()) + "[]"; // NOI18N
}
else
typeName = type.getName();
}
return typeName;
|
private java.lang.String | getShortClassName(java.lang.String className)Computes the class name (without package) for the supplied
class name.
return JavaTypeHelper.getShortClassName(className);
|
protected java.lang.String | getSuperclass(java.lang.String className)Returns the name of the superclass for the given class name.
Class classElement = (Class)getClass(className);
if (classElement != null)
classElement = classElement.getSuperclass();
return ((classElement != null) ? classElement.getName() : null);
|
public java.lang.String | getType(java.lang.Object element)Returns the string representation of type of the specified element.
If element denotes a field, it returns the type of the field.
If element denotes a method, it returns the return type of the method.
Note, element is either a field element as returned by getField, or a
method element as returned by getMethod executed on the same model
instance. This implementation expects the element being a reflection
instance.
return getNameForType(getTypeObject(element));
|
private java.lang.Class | getTypeForName(java.lang.String typeName)Converts the specified type name into its corresponding java.lang.Class
representation.
Class clazz = JavaTypeHelper.getPrimitiveClass(typeName);
if (clazz == null)
clazz = (Class)getClass(typeName);
return clazz;
|
protected java.lang.Class | getTypeObject(java.lang.Object element)Returns the Class type of the specified element.
If element denotes a field, it returns the type of the field.
If element denotes a method, it returns the return type of the method.
Note, element is either a field element as returned by getField, or a
method element as returned by getMethod executed on the same model
instance.
Class type = null;
if (element != null)
{
if (element instanceof Field)
type = ((Field)element).getType();
else if (element instanceof Method)
type = ((Method)element).getReturnType();
}
return type;
|
private java.lang.Class[] | getTypesForNames(java.lang.String[] typeNames)Converts the array of type names into an array of Class objects.
Class[] classes = new Class[typeNames.length];
for (int i = 0; i < classes.length; i++)
classes[i] = getTypeForName(typeNames[i]);
return classes;
|
public boolean | hasConstructor(java.lang.String className)Determines if the class with the specified name declares a constructor.
final Class classElement = (Class)getClass(className);
if (classElement != null)
{
Boolean b = (Boolean)AccessController.doPrivileged(
new PrivilegedAction()
{
public Object run ()
{
return JavaTypeHelper.valueOf(((Class)classElement).
getDeclaredConstructors().length != 0);
}
});
return b.booleanValue();
}
return false;
|
public boolean | implementsInterface(java.lang.Object classElement, java.lang.String interfaceName)Determines if the specified class implements the specified interface.
Note, class element is a model specific class representation as returned
by a getClass call executed on the same model instance. This
implementation expects the class element being a reflection instance.
Class interfaceClass = (Class)getClass(interfaceName);
if ((classElement == null) || !(classElement instanceof Class) ||
(interfaceClass == null))
return false;
return interfaceClass.isAssignableFrom((Class)classElement);
|
public boolean | isArray(java.lang.String className, java.lang.String fieldName)Determines if a field with the specified fieldName in the class
with the specified className is an array.
Object fieldElement = getField(className, fieldName);
return ((fieldElement != null) ?
getTypeObject(fieldElement).isArray() : false);
|
public boolean | isInterface(java.lang.String className)Determines if the specified className represents an interface type.
Class classElement = (Class)getClass(className);
return ((classElement != null) ? classElement.isInterface() : false);
|
public boolean | isSerializable(java.lang.Object fieldElement)Determines if the specified field element has a serializable type.
A type is serializable if it is a primitive type, a class that implements
java.io.Serializable or an interface that inherits from
java.io.Serializable.
Note, the field element is a model specific field representation as
returned by a getField call executed on the same model instance. This
implementation expects the field element being a reflection instance.
Class type = getTypeObject(fieldElement);
// check if the topmost element type is serializable
while ((type != null) && type.isArray())
type = type.getComponentType();
return ((type != null) ?
(type.isPrimitive() || implementsInterface(type, SERIALIZABLE)) :
false);
|
public void | removeResourcesFromCaches(java.lang.ClassLoader classLoader)Removes the classes cached with the specified class loader from all
caches. The method iterates the ClassLoader cache to find classes
cached with the specified class loader. These classes are removed
from the ClassLoader cache, the cache of MappingClassElements and
the set of classes known to be non PC. The associated SchemaElements
are removed from the SchemaElement cache.
Collection classNames = new HashSet();
synchronized(classLoaders)
{
for (Iterator i = classLoaders.entrySet().iterator(); i.hasNext();)
{
Map.Entry next = (Map.Entry)i.next();
// check the cached class loader
if (next.getValue() == classLoader)
{
// add className to the collection of classNames to be
// removed
classNames.add(next.getKey());
// remove this entry from the classLoaders cache
i.remove();
}
}
}
removeResourcesFromCaches(classNames);
|