use of org.eclipse.persistence.internal.libraries.asm.ClassVisitor in project eclipselink by eclipse-ee4j.
the class PersistenceWeaver method transform.
/**
* INTERNAL:
* Perform dynamic byte code weaving of class.
* @param loader The defining loader of the class to be transformed, may be {@code null}
* if the bootstrap loader.
* @param className The name of the class in the internal form of fully qualified class and interface
* names.
* @param classBeingRedefined If this is a redefine, the class being redefined, otherwise {@code null}.
* @param protectionDomain The protection domain of the class being defined or redefined.
* @param classfileBuffer The input byte buffer in class file format (must not be modified).
* @return A well-formed class file buffer (the result of the transform), or {@code null} if no transform
* is performed.
*/
@Override
public byte[] transform(final ClassLoader loader, final String className, final Class<?> classBeingRedefined, final ProtectionDomain protectionDomain, final byte[] classfileBuffer) throws TransformerException {
final SessionLog log = AbstractSessionLog.getLog();
// PERF: Is finest logging on weaving turned on?
final boolean shouldLogFinest = log.shouldLog(SessionLog.FINEST, SessionLog.WEAVER);
final Map<String, ClassDetails> classDetailsMap = this.classDetailsMap;
// Check if cleared already.
if (classDetailsMap == null) {
return null;
}
try {
/*
* The ClassFileTransformer callback - when called by the JVM's
* Instrumentation implementation - is invoked for every class loaded.
* Thus, we must check the classDetailsMap to see if we are 'interested'
* in the class.
*/
final ClassDetails classDetails = classDetailsMap.get(Helper.toSlashedClassName(className));
if (classDetails != null) {
if (shouldLogFinest) {
log.log(SessionLog.FINEST, SessionLog.WEAVER, "begin_weaving_class", className);
}
ClassReader classReader = null;
try {
classReader = new ClassReader(classfileBuffer);
} catch (IllegalArgumentException iae) {
// in such case log a warning and try to re-read the class without class version check
if (log.shouldLog(SessionLog.FINE, SessionLog.WEAVER)) {
SessionLogEntry entry = new SessionLogEntry(null, SessionLog.FINE, SessionLog.WEAVER, iae);
entry.setMessage(ExceptionLocalization.buildMessage("unsupported_classfile_version", new Object[] { className }));
log.log(entry);
}
classReader = new EclipseLinkClassReader(classfileBuffer);
}
final String reflectiveIntrospectionProperty = PrivilegedAccessHelper.getSystemProperty(SystemProperties.WEAVING_REFLECTIVE_INTROSPECTION);
final ClassWriter classWriter = reflectiveIntrospectionProperty != null ? new ClassWriter(ClassWriter.COMPUTE_FRAMES) : new ComputeClassWriter(loader, ClassWriter.COMPUTE_FRAMES);
final ClassWeaver classWeaver = new ClassWeaver(classWriter, classDetails);
final ClassVisitor sv = new SerialVersionUIDAdder(classWeaver);
classReader.accept(sv, 0);
if (classWeaver.alreadyWeaved) {
if (shouldLogFinest) {
log.log(SessionLog.FINEST, SessionLog.WEAVER, "end_weaving_class", className);
}
return null;
}
if (classWeaver.weaved) {
final byte[] bytes = classWriter.toByteArray();
final String outputPath = PrivilegedAccessHelper.getSystemProperty(SystemProperties.WEAVING_OUTPUT_PATH, "");
if (!outputPath.equals("")) {
Helper.outputClassFile(className, bytes, outputPath);
}
// PERF: Don't execute this set of if statements with logging turned off.
if (shouldLogFinest) {
if (classWeaver.weavedPersistenceEntity) {
log.log(SessionLog.FINEST, SessionLog.WEAVER, "weaved_persistenceentity", className);
}
if (classWeaver.weavedChangeTracker) {
log.log(SessionLog.FINEST, SessionLog.WEAVER, "weaved_changetracker", className);
}
if (classWeaver.weavedLazy) {
log.log(SessionLog.FINEST, SessionLog.WEAVER, "weaved_lazy", className);
}
if (classWeaver.weavedFetchGroups) {
log.log(SessionLog.FINEST, SessionLog.WEAVER, "weaved_fetchgroups", className);
}
if (classWeaver.weavedRest) {
log.log(SessionLog.FINEST, SessionLog.WEAVER, "weaved_rest", className);
}
log.log(SessionLog.FINEST, SessionLog.WEAVER, "end_weaving_class", className);
}
return bytes;
}
if (shouldLogFinest) {
log.log(SessionLog.FINEST, SessionLog.WEAVER, "end_weaving_class", className);
}
} else {
if (shouldLogFinest) {
log.log(SessionLog.FINEST, SessionLog.WEAVER, "transform_missing_class_details", className);
}
}
} catch (Throwable exception) {
if (log.shouldLog(SessionLog.WARNING, SessionLog.WEAVER)) {
log.log(SessionLog.WARNING, SessionLog.WEAVER, "exception_while_weaving", new Object[] { className, exception.getLocalizedMessage() });
if (shouldLogFinest) {
log.logThrowable(SessionLog.FINEST, SessionLog.WEAVER, exception);
}
}
}
if (shouldLogFinest) {
log.log(SessionLog.FINEST, SessionLog.WEAVER, "transform_existing_class_bytes", className);
}
// Returning null means 'use existing class bytes'.
return null;
}
Aggregations