use of jdk.internal.org.objectweb.asm.ClassReader in project openj9 by eclipse.
the class Cmvc196982 method addAnnotation.
/*
* Transform classToTransform by adding the given annotation to all already-annotated methods, fields and constructors in the class.
* Return the modified class file bytes.
*/
private byte[] addAnnotation(Class<?> classToTransform, final Class<?> annotation) throws IOException {
ClassWriter writer = new ClassWriter(0);
new ClassReader(Type.getInternalName(classToTransform)).accept(new ClassVisitor(ASM4, writer) {
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
return new MethodVisitor(ASM4, cv.visitMethod(access, name, desc, signature, exceptions)) {
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
mv.visitAnnotation(Type.getDescriptor(annotation), true).visitEnd();
return mv.visitAnnotation(desc, visible);
}
};
}
@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
return new FieldVisitor(ASM4, cv.visitField(access, name, desc, signature, value)) {
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
fv.visitAnnotation(Type.getDescriptor(annotation), true).visitEnd();
return fv.visitAnnotation(desc, visible);
}
};
}
}, 0);
return writer.toByteArray();
}
use of jdk.internal.org.objectweb.asm.ClassReader in project Bytecoder by mirkosertic.
the class Textifier method main.
/**
* Prints a disassembled view of the given class to the standard output.
* <p>
* Usage: Textifier [-debug] <binary class name or class file name >
*
* @param args
* the command line arguments.
*
* @throws Exception
* if the class cannot be found, or if an IO exception occurs.
*/
public static void main(final String[] args) throws Exception {
int i = 0;
int flags = ClassReader.SKIP_DEBUG;
boolean ok = true;
if (args.length < 1 || args.length > 2) {
ok = false;
}
if (ok && "-debug".equals(args[0])) {
i = 1;
flags = 0;
if (args.length != 2) {
ok = false;
}
}
if (!ok) {
System.err.println("Prints a disassembled view of the given class.");
System.err.println("Usage: Textifier [-debug] " + "<fully qualified class name or class file name>");
return;
}
ClassReader cr;
if (args[i].endsWith(".class") || args[i].indexOf('\\') > -1 || args[i].indexOf('/') > -1) {
cr = new ClassReader(new FileInputStream(args[i]));
} else {
cr = new ClassReader(args[i]);
}
cr.accept(new TraceClassVisitor(new PrintWriter(System.out)), flags);
}
use of jdk.internal.org.objectweb.asm.ClassReader in project Bytecoder by mirkosertic.
the class Module method loadModuleInfoClass.
/**
* Loads module-info.class as a package-private interface in a class loader
* that is a child of this module's class loader.
*/
private Class<?> loadModuleInfoClass(InputStream in) throws IOException {
final String MODULE_INFO = "module-info";
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
ClassVisitor cv = new ClassVisitor(Opcodes.ASM5, cw) {
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
cw.visit(version, Opcodes.ACC_INTERFACE + Opcodes.ACC_ABSTRACT + Opcodes.ACC_SYNTHETIC, MODULE_INFO, null, "java/lang/Object", null);
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
// keep annotations
return super.visitAnnotation(desc, visible);
}
@Override
public void visitAttribute(Attribute attr) {
// drop non-annotation attributes
}
};
ClassReader cr = new ClassReader(in);
cr.accept(cv, 0);
byte[] bytes = cw.toByteArray();
ClassLoader cl = new ClassLoader(loader) {
@Override
protected Class<?> findClass(String cn) throws ClassNotFoundException {
if (cn.equals(MODULE_INFO)) {
return super.defineClass(cn, bytes, 0, bytes.length);
} else {
throw new ClassNotFoundException(cn);
}
}
};
try {
return cl.loadClass(MODULE_INFO);
} catch (ClassNotFoundException e) {
throw new InternalError(e);
}
}
use of jdk.internal.org.objectweb.asm.ClassReader in project openj9 by eclipse.
the class PluginManagerImpl method scanClassFile.
/*[ENDIF] Sidecar19-SE */
private ClassInfo scanClassFile(InputStream file, URL url) throws IOException {
ClassScanner scanner = new ClassScanner(url, listeners);
ClassReader cr = new ClassReader(file);
cr.accept(scanner, 0);
return scanner.getClassInfo();
}
use of jdk.internal.org.objectweb.asm.ClassReader in project OSJR by zeruth.
the class JarLoader method loadJar.
private void loadJar() {
try {
if (Settings.DOWNLOAD_GAMEPACK) {
long start = System.currentTimeMillis();
ReadableByteChannel rbc = Channels.newChannel(new URL(gamepackUrl).openStream());
FileOutputStream fos = new FileOutputStream("gamepack.jar");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
System.out.println("Gamepack download took " + (System.currentTimeMillis() - start) + "ms");
} else {
}
JarFile jar = new JarFile(new File("./export/gamepack.jar"));
Enumeration<JarEntry> en = jar.entries();
while (en.hasMoreElements()) {
JarEntry entry = en.nextElement();
if (entry.getName().endsWith(".class")) {
ClassReader cr = new ClassReader(jar.getInputStream(entry));
ClassNode cn = new ClassNode();
cr.accept(cn, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
classnodes.put(cn.name, cn);
}
}
jar.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations