Search in sources :

Example 1 with EnhancerClassAdapter

use of org.datanucleus.enhancer.EnhancerClassAdapter in project datanucleus-core by datanucleus.

the class ClassEnhancerImpl method enhance.

/**
 * Method to enhance a classes definition.
 * @return Whether it was enhanced with no errors
 */
public boolean enhance() {
    if (cmd.getPersistenceModifier() != ClassPersistenceModifier.PERSISTENCE_CAPABLE && cmd.getPersistenceModifier() != ClassPersistenceModifier.PERSISTENCE_AWARE) {
        return false;
    }
    initialise();
    if (checkClassIsEnhanced(false)) {
        // Already enhanced
        DataNucleusEnhancer.LOGGER.info(Localiser.msg("005014", className));
        return true;
    }
    try {
        // Check for generation of PK
        if (cmd.getIdentityType() == IdentityType.APPLICATION && cmd.getObjectidClass() == null && cmd.getNoOfPrimaryKeyMembers() > 1) {
            if (hasOption(OPTION_GENERATE_PK)) {
                int[] pkMemberPositions = cmd.getPKMemberPositions();
                AbstractMemberMetaData pkMmd0 = cmd.getMetaDataForManagedMemberAtAbsolutePosition(pkMemberPositions[0]);
                if (pkMmd0 instanceof PropertyMetaData) {
                    // Throw exception for invalid metadata
                    throw new InvalidMetaDataException("044136", cmd.getFullClassName());
                }
                String pkClassName = cmd.getFullClassName() + AbstractClassMetaData.GENERATED_PK_SUFFIX;
                if (DataNucleusEnhancer.LOGGER.isDebugEnabled()) {
                    DataNucleusEnhancer.LOGGER.debug(Localiser.msg("005016", cmd.getFullClassName(), pkClassName));
                }
                cmd.setObjectIdClass(pkClassName);
                PrimaryKeyGenerator pkGen = new PrimaryKeyGenerator(cmd, this);
                pkClassBytes = pkGen.generate();
            } else {
                // Throw exception for invalid metadata
                throw new InvalidMetaDataException("044065", cmd.getFullClassName(), cmd.getNoOfPrimaryKeyMembers());
            }
        }
        // Create an adapter using a writer
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        EnhancerClassAdapter cv = new EnhancerClassAdapter(cw, this);
        ClassReader cr = null;
        InputStream classReaderInputStream = null;
        try {
            // Create a reader for the class and tell it to visit the adapter, performing the changes
            if (inputBytes != null) {
                cr = new ClassReader(inputBytes);
            } else {
                classReaderInputStream = clr.getResource(inputResourceName, null).openStream();
                cr = new ClassReader(classReaderInputStream);
            }
            cr.accept(cv, 0);
            // Save the bytes
            classBytes = cw.toByteArray();
        } finally {
            if (classReaderInputStream != null) {
                classReaderInputStream.close();
            }
        }
    } catch (Exception e) {
        DataNucleusEnhancer.LOGGER.error("Error thrown enhancing with ASMClassEnhancer", e);
        return false;
    }
    update = true;
    return true;
}
Also used : InvalidMetaDataException(org.datanucleus.metadata.InvalidMetaDataException) EnhancerClassAdapter(org.datanucleus.enhancer.EnhancerClassAdapter) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ClassReader(org.datanucleus.enhancer.asm.ClassReader) PropertyMetaData(org.datanucleus.metadata.PropertyMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) ClassWriter(org.datanucleus.enhancer.asm.ClassWriter) InvalidMetaDataException(org.datanucleus.metadata.InvalidMetaDataException) IOException(java.io.IOException)

Aggregations

FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 EnhancerClassAdapter (org.datanucleus.enhancer.EnhancerClassAdapter)1 ClassReader (org.datanucleus.enhancer.asm.ClassReader)1 ClassWriter (org.datanucleus.enhancer.asm.ClassWriter)1 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)1 InvalidMetaDataException (org.datanucleus.metadata.InvalidMetaDataException)1 PropertyMetaData (org.datanucleus.metadata.PropertyMetaData)1