Search in sources :

Example 61 with ClassVisitor

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();
}
Also used : ModifyClassVisiter(com.taobao.android.builder.tools.asm.field.ModifyClassVisiter) FileOutputStream(java.io.FileOutputStream) ClassReader(org.objectweb.asm.ClassReader) MethodChangeClassAdapter(com.taobao.asm.AsmExample.MethodChangeClassAdapter) ClassVisitor(org.objectweb.asm.ClassVisitor) FieldVisitor(org.objectweb.asm.FieldVisitor) File(java.io.File) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor) Test(org.junit.Test)

Example 62 with ClassVisitor

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();
        }
    };
}
Also used : BoxPatchVisitor(net.glowstone.linkstone.runtime.boxing.BoxPatchVisitor) DirectFieldAccessReplaceVisitor(net.glowstone.linkstone.runtime.direct.DirectFieldAccessReplaceVisitor) ClassInitInvokeVisitor(net.glowstone.linkstone.runtime.inithook.ClassInitInvokeVisitor) ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor) ReflectionReplaceVisitor(net.glowstone.linkstone.runtime.reflectionreplace.ReflectionReplaceVisitor) ClassWriter(org.objectweb.asm.ClassWriter) PluginClassLoader(org.bukkit.plugin.java.PluginClassLoader)

Example 63 with ClassVisitor

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();
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) AnnotatedFieldCollectVisitor(net.glowstone.linkstone.runtime.collect.AnnotatedFieldCollectVisitor) ZipEntry(java.util.zip.ZipEntry) ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor) FileInputStream(java.io.FileInputStream) BoxCollectVisitor(net.glowstone.linkstone.runtime.collect.BoxCollectVisitor)

Example 64 with ClassVisitor

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();
}
Also used : AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor)

Example 65 with ClassVisitor

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;
        }
    });
}
Also used : ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) ClassReader(org.objectweb.asm.ClassReader)

Aggregations

ClassVisitor (org.objectweb.asm.ClassVisitor)133 ClassReader (org.objectweb.asm.ClassReader)97 ClassWriter (org.objectweb.asm.ClassWriter)80 MethodVisitor (org.objectweb.asm.MethodVisitor)45 IOException (java.io.IOException)19 InputStream (java.io.InputStream)19 Type (org.objectweb.asm.Type)14 TraceClassVisitor (org.objectweb.asm.util.TraceClassVisitor)14 File (java.io.File)11 PrintWriter (java.io.PrintWriter)11 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)10 Label (org.objectweb.asm.Label)10 FileInputStream (java.io.FileInputStream)9 Test (org.junit.Test)9 FileOutputStream (java.io.FileOutputStream)6 FieldVisitor (org.objectweb.asm.FieldVisitor)6 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)6 ArrayList (java.util.ArrayList)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 FieldList (net.bytebuddy.description.field.FieldList)5