use of org.objectweb.asm.ClassVisitor in project atlas by alibaba.
the class AsmTest method test.
@Test
public void test() throws Throwable {
ClassReader cr = new ClassReader(Config.class.getName());
ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
ClassVisitor cv = new MethodChangeClassAdapter(cw);
cr.accept(cv, Opcodes.ASM5);
// Add a new method
MethodVisitor mw = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "add", "([Ljava/lang/String;)V", null, null);
// pushes the 'out' field (of type PrintStream) of the System class
mw.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
// pushes the "Hello World!" String constant
mw.visitLdcInsn("this is add method print!");
// invokes the 'println' method (defined in the PrintStream class)
mw.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
mw.visitInsn(RETURN);
// this code uses a maximum of two stack elements and two local
// variables
mw.visitMaxs(0, 0);
mw.visitEnd();
// Type.getDescriptor(AdviceFlowOuterHolder.class)
FieldVisitor fv = cw.visitField(ACC_PUBLIC + ACC_STATIC + ACC_FINAL, "age", Type.INT_TYPE.toString(), null, 1);
fv.visitEnd();
FieldVisitor fv2 = cw.visitField(ACC_PUBLIC + ACC_STATIC + ACC_FINAL, "name2", Type.getDescriptor(String.class), null, "name2");
fv2.visitEnd();
ModifyClassVisiter cv2 = new ModifyClassVisiter(Opcodes.ASM5);
cv2.addRemoveField("name");
cr.accept(cv2, Opcodes.ASM5);
FieldVisitor fv3 = cw.visitField(ACC_PUBLIC + ACC_STATIC + ACC_FINAL, "name", Type.getDescriptor(String.class), null, "name");
fv3.visitEnd();
byte[] code = cw.toByteArray();
File file = new File("Config.class");
System.out.println(file.getAbsolutePath());
FileOutputStream fos = new FileOutputStream(file);
fos.write(code);
fos.close();
}
use of org.objectweb.asm.ClassVisitor in project Glowstone by GlowstoneMC.
the class LinkstonePluginLoader method newPluginLoader.
@Override
protected PluginClassLoader newPluginLoader(JavaPluginLoader loader, ClassLoader parent, PluginDescriptionFile description, File dataFolder, File file, ClassLoader libraryLoader) throws Exception {
return new PluginClassLoader(loader, parent, description, dataFolder, file, libraryLoader) {
@Override
protected byte[] transformBytecode(byte[] bytecode) {
if (LinkstoneRuntimeData.getFields().isEmpty() && LinkstoneRuntimeData.getBoxes().isEmpty()) {
// so there's no need for runtime support
return bytecode;
}
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
ClassVisitor cv = cw;
cv = new DirectFieldAccessReplaceVisitor(LinkstoneRuntimeData.getFields(), cv);
if (!FieldAccessorUtility.isSupported() || !MethodAccessorUtility.isSupported()) {
cv = new ReflectionReplaceVisitor(cv);
}
cv = new ClassInitInvokeVisitor(cv);
cv = new BoxPatchVisitor(LinkstoneRuntimeData.getBoxes(), cv);
new ClassReader(bytecode).accept(cv, 0);
return cw.toByteArray();
}
};
}
use of org.objectweb.asm.ClassVisitor in project Glowstone by GlowstoneMC.
the class LinkstonePluginScanner method scanPlugin.
private void scanPlugin(File pluginJar) throws IOException {
ZipInputStream zin = new ZipInputStream(new FileInputStream(pluginJar));
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null) {
if (entry.isDirectory() || !entry.getName().endsWith(".class")) {
continue;
}
ClassVisitor cv = new AnnotatedFieldCollectVisitor(this.fields);
cv = new BoxCollectVisitor(this.boxes, cv);
new ClassReader(zin).accept(cv, ClassReader.SKIP_CODE);
zin.closeEntry();
}
zin.close();
}
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