Search in sources :

Example 66 with ClassMetaData

use of org.datanucleus.metadata.ClassMetaData in project datanucleus-core by datanucleus.

the class DataNucleusEnhancer method enhance.

/**
 * Method to enhance all classes defined by addClass, addClasses, addJar, addPersistenceUnit, addFiles.
 * @return Number of classes enhanced
 */
public int enhance() {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Enhancing classes");
    }
    if (componentsToEnhance.isEmpty()) {
        // Nothing to enhance
        return 0;
    }
    // Load the meta-data for the registered components to enhance.
    long startTime = System.currentTimeMillis();
    Collection<FileMetaData> fileMetaData = getFileMetadataForInput();
    // Enhance the classes implied by the FileMetaData
    long inputTime = System.currentTimeMillis();
    Set<String> classNames = new HashSet<String>();
    Iterator<FileMetaData> filemdIter = fileMetaData.iterator();
    boolean success = true;
    while (filemdIter.hasNext()) {
        FileMetaData filemd = filemdIter.next();
        for (int packagenum = 0; packagenum < filemd.getNoOfPackages(); packagenum++) {
            PackageMetaData pmd = filemd.getPackage(packagenum);
            for (int classnum = 0; classnum < pmd.getNoOfClasses(); classnum++) {
                ClassMetaData cmd = pmd.getClass(classnum);
                if (classNames.contains(cmd.getFullClassName())) {
                    // Already processed, maybe via annotations and this is MetaData
                    continue;
                }
                classNames.add(cmd.getFullClassName());
                byte[] bytes = bytesForClassesToEnhanceByClassName != null ? bytesForClassesToEnhanceByClassName.get(cmd.getFullClassName()) : null;
                ClassEnhancer classEnhancer = getClassEnhancer(cmd, bytes);
                // Enhance, but don't store if based on input bytes
                boolean clsSuccess = enhanceClass(cmd, classEnhancer, bytes == null);
                if (!clsSuccess) {
                    success = false;
                }
            }
        }
    }
    if (!success) {
        throw new NucleusException("Failure during enhancement of classes - see the log for details");
    }
    // Log info about timings
    long enhanceTime = System.currentTimeMillis();
    String msg = null;
    if (verbose) {
        msg = Localiser.msg("005004", classNames.size(), "" + (inputTime - startTime), "" + (enhanceTime - inputTime), "" + (enhanceTime - startTime));
    } else {
        msg = Localiser.msg("005005", classNames.size());
    }
    addMessage(msg, false);
    // Remove the input specification
    if (bytesForClassesToEnhanceByClassName != null) {
        bytesForClassesToEnhanceByClassName.clear();
        bytesForClassesToEnhanceByClassName = null;
    }
    componentsToEnhance.clear();
    return classNames.size();
}
Also used : PackageMetaData(org.datanucleus.metadata.PackageMetaData) NucleusException(org.datanucleus.exceptions.NucleusException) FileMetaData(org.datanucleus.metadata.FileMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData) HashSet(java.util.HashSet)

Example 67 with ClassMetaData

use of org.datanucleus.metadata.ClassMetaData 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 68 with ClassMetaData

use of org.datanucleus.metadata.ClassMetaData 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)

Example 69 with ClassMetaData

use of org.datanucleus.metadata.ClassMetaData in project tests by datanucleus.

the class AnnotationPlusXMLTest method testOneToManyBiJoin.

/**
 * Test of JPA 1-N unidir FK relation.
 * This is really a 1-N uni join since JPA doesnt support 1-N uni FK.
 */
/*public void testOneToManyUniFK()
    {
        NucleusContext nucleusCtx = new NucleusContext("JPA", null);
        ClassLoaderResolver clr = new ClassLoaderResolverImpl();
        MetaDataManager metaDataMgr = new JPAMetaDataManager(pmfcontext);
        PersistenceUnitMetaData pumd = metaDataMgr.getMetaDataForPersistenceUnit("JPATest");
        metaDataMgr.initialise(pumd, clr);

        // owner side
        ClassMetaData cmd1 = (ClassMetaData)metaDataMgr.getMetaDataForClass(Site.class.getName(), clr);
        AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("offices");
        assertNotNull("Site.offices is null!", fmd1);
        assertEquals("Site.offices mapped-by is incorrect", fmd1.getMappedBy(), null);
        assertEquals("Site.offices relationType is incorrect",
            fmd1.getRelationType(clr), Relation.ONE_TO_MANY_UNI);
        assertEquals("Site.offices jointable name is incorrect", fmd1.getTable(), null);
        assertNotNull("Site.offices should have join but doesnt", fmd1.getJoinMetaData());

        ElementMetaData elemmd = fmd1.getElementMetaData();
        assertNotNull("Site.offices has no element column info but should", elemmd);
        ColumnMetaData[] colmds = elemmd.getColumnMetaData();
        assertNotNull("Site.offices has incorrect element columns", colmds);
        assertEquals("Site.offices has incorrect number of element columns", colmds.length, 1);
        assertEquals("Site.offices has incorrect element column name", colmds[0].getName(), "SITE_ID");
    }*/
/**
 * Test of JPA 1-N bidir JoinTable relation
 */
public void testOneToManyBiJoin() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
    metaDataMgr.loadPersistenceUnit(pumd, null);
    // owner side
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Manager.class.getName(), clr);
    assertEquals("Manager has incorrect table name", cmd1.getTable(), "JPA_AX_MANAGER");
    AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("subordinates");
    assertNotNull("Manager.subordinates is null!", fmd1);
    assertEquals("Manager.subordinates mapped-by is incorrect", fmd1.getMappedBy(), "manager");
    assertEquals("Manager.subordinates relationType is incorrect", fmd1.getRelationType(clr), RelationType.ONE_TO_MANY_BI);
    assertEquals("Manager.subordinates jointable name is incorrect", fmd1.getTable(), "JPA_AX_MGR_EMPLOYEES");
    // non-owner side
    ClassMetaData cmd2 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Employee.class.getName(), clr);
    assertEquals("Employee has incorrect table name", cmd2.getTable(), "JPA_AX_EMPLOYEE");
    AbstractMemberMetaData fmd2 = cmd2.getMetaDataForMember("manager");
    assertNotNull("Employee.manager is null!", fmd2);
    assertEquals("Employee.manager mapped-by is incorrect", fmd2.getMappedBy(), null);
    assertEquals("Employee.manager relationType is incorrect", fmd2.getRelationType(clr), RelationType.MANY_TO_ONE_BI);
    assertEquals("Employee.manager jointable name is incorrect", fmd2.getTable(), null);
    // join-table
    JoinMetaData joinmd = fmd1.getJoinMetaData();
    assertNotNull("Manager.subordinates has no join table!", joinmd);
    assertNotNull("Manager.subordinates has incorrect join columns", joinmd.getColumnMetaData());
    assertEquals("Manager.subordinates has incorrect number of join columns", 1, joinmd.getColumnMetaData().length);
    assertEquals("Manager.subordinates has incorrect owner join column name", "MGR_ID", joinmd.getColumnMetaData()[0].getName());
    ElementMetaData elemmd = fmd1.getElementMetaData();
    assertNotNull("Manager.subordinates has no element column info but should", elemmd);
    assertNotNull("Manager.subordinates has incorrect element columns", elemmd.getColumnMetaData());
    assertEquals("Manager.subordinates has incorrect number of element columns", 1, elemmd.getColumnMetaData().length);
    assertEquals("Manager.subordinates has incorrect element join column name", "EMP_ID", elemmd.getColumnMetaData()[0].getName());
}
Also used : JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) ElementMetaData(org.datanucleus.metadata.ElementMetaData) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) JoinMetaData(org.datanucleus.metadata.JoinMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 70 with ClassMetaData

use of org.datanucleus.metadata.ClassMetaData in project tests by datanucleus.

the class AnnotationPlusXMLTest method testSequenceGenerator.

/**
 * Test of JPA <sequence-generator>
 */
public void testSequenceGenerator() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
    metaDataMgr.loadPersistenceUnit(pumd, null);
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Department.class.getName(), clr);
    PackageMetaData pmd = cmd1.getPackageMetaData();
    assertEquals("Number of Sequences registered for Department class is wrong", pmd.getNoOfSequences(), 1);
    SequenceMetaData seqmd = pmd.getSequences()[0];
    assertEquals("SequenceGenerator has incorrect name", seqmd.getName(), "DepartmentGenerator");
    assertEquals("SequenceGenerator has incorrect sequence name", seqmd.getDatastoreSequence(), "DEPT_SEQ");
    assertEquals("SequenceGenerator has incorrect initial value", seqmd.getInitialValue(), 1);
    assertEquals("SequenceGenerator has incorrect allocation size", seqmd.getAllocationSize(), 50);
}
Also used : JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) PackageMetaData(org.datanucleus.metadata.PackageMetaData) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData) SequenceMetaData(org.datanucleus.metadata.SequenceMetaData)

Aggregations

ClassMetaData (org.datanucleus.metadata.ClassMetaData)109 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)55 MetaDataManager (org.datanucleus.metadata.MetaDataManager)55 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)53 NucleusContext (org.datanucleus.NucleusContext)52 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)51 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)51 PersistenceUnitMetaData (org.datanucleus.metadata.PersistenceUnitMetaData)38 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)18 ClassLoaderResolverImpl (org.datanucleus.ClassLoaderResolverImpl)14 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)14 ElementMetaData (org.datanucleus.metadata.ElementMetaData)14 JoinMetaData (org.datanucleus.metadata.JoinMetaData)14 PackageMetaData (org.datanucleus.metadata.PackageMetaData)14 Label (org.datanucleus.enhancer.asm.Label)6 FileMetaData (org.datanucleus.metadata.FileMetaData)6 InterfaceMetaData (org.datanucleus.metadata.InterfaceMetaData)6 PropertyMetaData (org.datanucleus.metadata.PropertyMetaData)6 KeyMetaData (org.datanucleus.metadata.KeyMetaData)5 MapMetaData (org.datanucleus.metadata.MapMetaData)5