use of org.datanucleus.enhancer.asm.ClassReader in project datanucleus-core by datanucleus.
the class ClassEnhancerImpl method getClassNameForFileName.
/**
* Convenience accessor for the class name that is stored in a particular class.
* @param filename Name of the file
* @return The class name
*/
public static String getClassNameForFileName(String filename) {
MyClassVisitor vis = new MyClassVisitor();
FileInputStream fis = null;
try {
fis = new FileInputStream(filename);
new ClassReader(fis).accept(vis, 0);
return vis.getClassName();
} catch (IOException ioe) {
return null;
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
}
}
}
}
use of org.datanucleus.enhancer.asm.ClassReader 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;
}
use of org.datanucleus.enhancer.asm.ClassReader in project datanucleus-core by datanucleus.
the class ClassEnhancerImpl method checkClassIsEnhanced.
/**
* Convenience method to return if a class is enhanced.
* @param logErrors Whether to log any errors (missing methods etc) as errors (otherwise info/debug)
* @return Whether the class is enhanced
*/
protected boolean checkClassIsEnhanced(boolean logErrors) {
try {
// Create an adapter using a writer
EnhancerClassChecker checker = new EnhancerClassChecker(this, logErrors);
InputStream classReaderInputStream = null;
try {
// Create a reader for the class and visit it using the checker
ClassReader cr = null;
if (inputBytes != null) {
cr = new ClassReader(inputBytes);
} else {
classReaderInputStream = clr.getResource(inputResourceName, null).openStream();
cr = new ClassReader(classReaderInputStream);
}
// [ASM Note : In 2.2 this should be "cr.accept(checker, false);"]
cr.accept(checker, 0);
} finally {
if (classReaderInputStream != null) {
classReaderInputStream.close();
}
}
return checker.isEnhanced();
} catch (Exception e) {
DataNucleusEnhancer.LOGGER.error("Error thrown enhancing with ASMClassEnhancer", e);
}
return false;
}
Aggregations