use of org.datanucleus.metadata.InterfaceMetaData in project datanucleus-core by datanucleus.
the class ImplementationCreatorImpl method newInstance.
/**
* Method to generate an instance of an interface, abstract class, or concrete PC class.
* @param cls The class of the interface, abstract class, or concrete class defined in MetaData
* @param clr ClassLoader resolver
* @return The instance of this type
*/
public Object newInstance(Class cls, ClassLoaderResolver clr) {
try {
if (Persistable.class.isAssignableFrom(cls)) {
if (Modifier.isAbstract(cls.getModifiers())) {
// Abstract class, so we need an implementation
ClassMetaData cmd = (ClassMetaData) metaDataMgr.getMetaDataForClass(cls, clr);
if (cmd == null) {
throw new NucleusException("Could not find metadata for class " + cls.getName()).setFatal();
}
Object obj = newInstance(cmd, clr);
if (!metaDataMgr.hasMetaDataForClass(obj.getClass().getName())) {
// No metadata yet present for the implementation so register it
metaDataMgr.registerImplementationOfAbstractClass(cmd, obj.getClass(), clr);
}
return obj;
}
// Concrete class that is PC so just create an instance using its no args constructor
return cls.newInstance();
}
// Interface, so we need an implemenation
InterfaceMetaData imd = metaDataMgr.getMetaDataForInterface(cls, clr);
if (imd == null) {
throw new NucleusException("Could not find metadata for class/interface " + cls.getName()).setFatal();
}
Object obj = newInstance(imd, clr);
if (!metaDataMgr.hasMetaDataForClass(obj.getClass().getName())) {
// No metadata yet present for the implementation so register it
metaDataMgr.registerPersistentInterface(imd, obj.getClass(), clr);
}
return obj;
} catch (ClassNotFoundException e) {
throw new NucleusUserException(e.toString(), e);
} catch (InstantiationException e) {
throw new NucleusUserException(e.toString(), e);
} catch (IllegalAccessException e) {
throw new NucleusUserException(e.toString(), e);
}
}
use of org.datanucleus.metadata.InterfaceMetaData in project datanucleus-core by datanucleus.
the class ImplementationGenerator method enhance.
/**
* Enhance the implementation of the class/interface.
* @param clr ClassLoader resolver
*/
public void enhance(final ClassLoaderResolver clr) {
// define the generated class in the classloader so we populate the metadata
final EnhancerClassLoader loader = new EnhancerClassLoader();
loader.defineClass(fullClassName, getBytes(), clr);
// Create MetaData for implementation of interface
final ClassLoaderResolver genclr = new ClassLoaderResolverImpl(loader);
final ClassMetaData implementationCmd;
if (inputCmd instanceof InterfaceMetaData) {
implementationCmd = new ClassMetaData((InterfaceMetaData) inputCmd, className, true);
} else {
implementationCmd = new ClassMetaData((ClassMetaData) inputCmd, className);
}
// Do as PrivilegedAction since populate()/initialise() use reflection to get additional fields
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
implementationCmd.populate(genclr, null, metaDataMgr);
implementationCmd.initialise(genclr);
return null;
}
});
// enhance the class and update the byte definition
ClassEnhancer gen = new ClassEnhancerImpl(implementationCmd, genclr, metaDataMgr, JDOEnhancementNamer.getInstance(), getBytes());
gen.enhance();
bytes = gen.getClassBytes();
}
Aggregations