use of org.objectweb.asm.ClassVisitor in project Lucee by lucee.
the class SourceLastModifiedClassAdapter method setSourceLastModified.
public static byte[] setSourceLastModified(byte[] barr, long lastModified) {
ClassReader cr = new ClassReader(barr);
ClassWriter cw = ASMUtil.getClassWriter();
ClassVisitor ca = new SourceLastModifiedClassAdapter(cw, lastModified);
cr.accept(ca, 0);
return cw.toByteArray();
}
use of org.objectweb.asm.ClassVisitor in project quasar by puniverse.
the class QuasarInstrumentor method instrumentClass.
@VisibleForTesting
byte[] instrumentClass(ClassLoader loader, String className, InputStream is, boolean forceInstrumentation) throws IOException {
className = className != null ? className.replace('.', '/') : null;
byte[] cb = toByteArray(is);
MethodDatabase db = getMethodDatabase(loader);
if (className != null) {
log(LogLevel.INFO, "TRANSFORM: %s %s", className, (db.getClassEntry(className) != null && db.getClassEntry(className).requiresInstrumentation()) ? "request" : "");
examine(className, "quasar-1-preinstr", cb);
} else {
log(LogLevel.INFO, "TRANSFORM: null className");
}
// Phase 1, add a label before any suspendable calls, event API is enough
final ClassReader r1 = new ClassReader(cb);
final ClassWriter cw1 = new ClassWriter(r1, 0);
final LabelSuspendableCallSitesClassVisitor ic1 = new LabelSuspendableCallSitesClassVisitor(cw1, db);
r1.accept(ic1, 0);
cb = cw1.toByteArray();
examine(className, "quasar-2", cb);
// Phase 2, instrument, tree API
final ClassReader r2 = new ClassReader(cb);
final ClassWriter cw2 = new DBClassWriter(db, r2);
final ClassVisitor cv2 = (check && EXAMINED_CLASS == null) ? new CheckClassAdapter(cw2) : cw2;
final InstrumentClass ic2 = new InstrumentClass(cv2, db, forceInstrumentation);
try {
r2.accept(ic2, ClassReader.SKIP_FRAMES);
cb = cw2.toByteArray();
} catch (final Exception e) {
if (ic2.hasSuspendableMethods()) {
error("Unable to instrument class " + className, e);
throw e;
} else {
if (!MethodDatabase.isProblematicClass(className))
log(LogLevel.DEBUG, "Unable to instrument class " + className);
return null;
}
}
examine(className, "quasar-4", cb);
// Phase 4, fill suspendable call offsets, event API is enough
final OffsetClassReader r3 = new OffsetClassReader(cb);
final ClassWriter cw3 = new ClassWriter(r3, 0);
final SuspOffsetsAfterInstrClassVisitor ic3 = new SuspOffsetsAfterInstrClassVisitor(cw3, db);
r3.accept(ic3, 0);
cb = cw3.toByteArray();
// DEBUG
if (EXAMINED_CLASS != null) {
examine(className, "quasar-5-final", cb);
if (check) {
ClassReader r4 = new ClassReader(cb);
ClassVisitor cv4 = new CheckClassAdapter(new TraceClassVisitor(null), true);
r4.accept(cv4, 0);
}
}
return cb;
}
use of org.objectweb.asm.ClassVisitor in project cdap by caskdata.
the class ArtifactInspector method isPlugin.
/**
* Detects if a class is annotated with {@link Plugin} without loading the class.
*
* @param className name of the class
* @param classLoader ClassLoader for loading the class file of the given class
* @return true if the given class is annotated with {@link Plugin}
*/
private boolean isPlugin(String className, ClassLoader classLoader) {
try (InputStream is = classLoader.getResourceAsStream(className.replace('.', '/') + ".class")) {
if (is == null) {
return false;
}
// Use ASM to inspect the class bytecode to see if it is annotated with @Plugin
final boolean[] isPlugin = new boolean[1];
ClassReader cr = new ClassReader(is);
cr.accept(new ClassVisitor(Opcodes.ASM5) {
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
if (Plugin.class.getName().equals(Type.getType(desc).getClassName()) && visible) {
isPlugin[0] = true;
}
return super.visitAnnotation(desc, visible);
}
}, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
return isPlugin[0];
} catch (IOException e) {
// If failed to open the class file, then it cannot be a plugin
LOG.warn("Failed to open class file for {}", className, e);
return false;
}
}
use of org.objectweb.asm.ClassVisitor in project j2objc by google.
the class PackageInfoLookup method parseDataFromClassFile.
private PackageData parseDataFromClassFile(InputFile file) throws IOException {
PackageDataBuilder builder = new PackageDataBuilder();
ClassReader classReader = new ClassReader(file.getInputStream());
classReader.accept(new ClassVisitor(Opcodes.ASM5) {
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
if (desc.equals("Lcom/google/j2objc/annotations/ObjectiveCName;")) {
return new AnnotationVisitor(Opcodes.ASM5) {
@Override
public void visit(String name, Object value) {
if (name.equals("value")) {
builder.setObjectiveCName(value.toString());
}
}
};
} else if (desc.equals("Ljavax/annotation/ParametersAreNonnullByDefault;")) {
builder.setParametersAreNonnullByDefault();
} else if (desc.equals("Lcom/google/j2objc/annotations/ReflectionSupport;")) {
return new AnnotationVisitor(Opcodes.ASM5) {
@Override
public void visitEnum(String name, String desc, String value) {
if (desc.equals("Lcom/google/j2objc/annotations/ReflectionSupport$Level;") && name.equals("value")) {
builder.setReflectionSupportLevel(ReflectionSupport.Level.valueOf(value));
}
}
};
}
return null;
}
}, 0);
return builder.build();
}
use of org.objectweb.asm.ClassVisitor in project blade by biezhi.
the class DebuggingClassWriter method toByteArray.
public byte[] toByteArray() {
return (byte[]) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
public Object run() {
byte[] b = ((ClassWriter) DebuggingClassWriter.super.cv).toByteArray();
if (debugLocation != null) {
String dirs = className.replace('.', File.separatorChar);
try {
new File(debugLocation + File.separatorChar + dirs).getParentFile().mkdirs();
File file = new File(new File(debugLocation), dirs + ".class");
OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
try {
out.write(b);
} finally {
out.close();
}
if (traceCtor != null) {
file = new File(new File(debugLocation), dirs + ".asm");
out = new BufferedOutputStream(new FileOutputStream(file));
try {
ClassReader cr = new ClassReader(b);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
ClassVisitor tcv = (ClassVisitor) traceCtor.newInstance(new Object[] { null, pw });
cr.accept(tcv, 0);
pw.flush();
} finally {
out.close();
}
}
} catch (Exception e) {
throw new CodeGenerationException(e);
}
}
return b;
}
});
}
Aggregations