Search in sources :

Example 16 with InterfaceMetaData

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);
    }
}
Also used : NucleusUserException(org.datanucleus.exceptions.NucleusUserException) InterfaceMetaData(org.datanucleus.metadata.InterfaceMetaData) NucleusException(org.datanucleus.exceptions.NucleusException) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 17 with InterfaceMetaData

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();
}
Also used : PrivilegedAction(java.security.PrivilegedAction) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) InterfaceMetaData(org.datanucleus.metadata.InterfaceMetaData) ClassEnhancerImpl(org.datanucleus.enhancer.ClassEnhancerImpl) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Aggregations

InterfaceMetaData (org.datanucleus.metadata.InterfaceMetaData)17 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)8 ClassMetaData (org.datanucleus.metadata.ClassMetaData)6 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)5 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)4 PackageMetaData (org.datanucleus.metadata.PackageMetaData)4 NucleusException (org.datanucleus.exceptions.NucleusException)3 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)3 DiscriminatorMetaData (org.datanucleus.metadata.DiscriminatorMetaData)3 ForeignKeyMetaData (org.datanucleus.metadata.ForeignKeyMetaData)3 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)3 SQLException (java.sql.SQLException)2 Iterator (java.util.Iterator)2 JDOUserException (javax.jdo.JDOUserException)2 Label (org.datanucleus.enhancer.asm.Label)2 MethodVisitor (org.datanucleus.enhancer.asm.MethodVisitor)2 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)2 FetchGroupMetaData (org.datanucleus.metadata.FetchGroupMetaData)2 FileMetaData (org.datanucleus.metadata.FileMetaData)2 PropertyMetaData (org.datanucleus.metadata.PropertyMetaData)2