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;
}
Aggregations