Example usage for org.objectweb.asm Opcodes ASM4

List of usage examples for org.objectweb.asm Opcodes ASM4

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes ASM4.

Prototype

int ASM4

To view the source code for org.objectweb.asm Opcodes ASM4.

Click Source Link

Usage

From source file:cn.annoreg.asm.RegistryTransformer.java

License:Open Source License

@Override
public byte[] transform(String arg0, String arg1, byte[] data) {
    if (data == null)
        return data;
    try {//from  w ww . j a  v a  2  s  . c om
        if (arg0.startsWith("cn.annoreg.")) {
            return data;
        }
        ClassReader cr = new ClassReader(data);

        //Get inner class list for each class
        {
            InnerClassVisitor cv = new InnerClassVisitor(Opcodes.ASM4);
            cr.accept(cv, 0);
            List<String> inner = cv.getInnerClassList();
            if (inner != null) {
                RegistrationManager.INSTANCE.addInnerClassList(arg0, inner);
            }
        }
        //Transform network-calls for each class
        {
            NetworkCallVisitor cv = new NetworkCallVisitor(Opcodes.ASM4, arg0);
            cr.accept(cv, 0);
            if (cv.needTransform()) {
                ClassWriter cw = new ClassWriter(Opcodes.ASM4);
                cr.accept(cv.getTransformer(cw), 0);
                data = cw.toByteArray();
            }
        }
    } catch (Throwable t) {
        t.printStackTrace();
        throw new RuntimeException(t);
    }
    return data;
}

From source file:co.paralleluniverse.common.reflection.AnnotationUtil.java

License:Open Source License

private static boolean hasClassAnnotation(Class<? extends Annotation> annClass, ClassReader r) {
    // annotationName = annotationName.replace('.', '/');
    final String annDesc = Type.getDescriptor(annClass);
    final AtomicBoolean res = new AtomicBoolean(false);
    r.accept(new ClassVisitor(Opcodes.ASM4) {
        @Override/*from w  w w .j a v a  2  s . co  m*/
        public void visit(int version, int access, String name, String signature, String superName,
                String[] interfaces) {
        }

        @Override
        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
            if (desc.equals(annDesc))
                res.set(true);
            return null;
        }
    }, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG);
    return res.get();
}

From source file:co.paralleluniverse.fibers.instrument.CheckInstrumentationVisitor.java

License:Open Source License

public CheckInstrumentationVisitor(MethodDatabase db) {
    super(Opcodes.ASM4);
    this.db = db;
    this.classifier = db.getClassifier();
}

From source file:co.paralleluniverse.fibers.instrument.CheckInstrumentationVisitor.java

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, final String name, final String desc, String signature,
        String[] exceptions) {//ww w.  j a  va 2s. co  m
    SuspendableType suspendable = null;
    if (suspendableInterface)
        suspendable = SuspendableType.SUSPENDABLE_SUPER;
    if (suspendable == null)
        suspendable = classEntry.check(name, desc);
    if (suspendable == null)
        suspendable = classifier.isSuspendable(db, className, classEntry.getSuperName(),
                classEntry.getInterfaces(), name, desc, signature, exceptions);
    if (suspendable == SuspendableType.SUSPENDABLE) {
        hasSuspendable = true;
        // synchronized methods can't be made suspendable
        if ((access & Opcodes.ACC_SYNCHRONIZED) == Opcodes.ACC_SYNCHRONIZED) {
            if (!className.equals("clojure/lang/LazySeq"))
                throw new UnableToInstrumentException("synchronized method", className, name, desc);
        }
    }
    classEntry.set(name, desc, suspendable);

    if (suspendable == null) // look for @Suspendable annotation
        return new MethodVisitor(Opcodes.ASM4) {
            private boolean susp = false;

            @Override
            public AnnotationVisitor visitAnnotation(String adesc, boolean visible) {
                if (adesc.equals(ANNOTATION_DESC))
                    susp = true;
                return null;
            }

            @Override
            public void visitEnd() {
                super.visitEnd();
                classEntry.set(name, desc,
                        susp ? SuspendableType.SUSPENDABLE : SuspendableType.NON_SUSPENDABLE);
                hasSuspendable = hasSuspendable | susp;
            }
        };
    else
        return null;
}

From source file:co.paralleluniverse.fibers.instrument.InstrumentClass.java

License:Open Source License

public InstrumentClass(ClassVisitor cv, MethodDatabase db, boolean forceInstrumentation) {
    super(Opcodes.ASM4, cv);
    this.db = db;
    this.classifier = db.getClassifier();
    this.forceInstrumentation = forceInstrumentation;
    this.suspendableInterface = false;
}

From source file:co.paralleluniverse.fibers.instrument.InstrumentClass.java

License:Open Source License

@Override
public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature,
        final String[] exceptions) {
    SuspendableType markedSuspendable = null;
    if (suspendableInterface)
        markedSuspendable = SuspendableType.SUSPENDABLE_SUPER;
    if (markedSuspendable == null)
        markedSuspendable = classifier.isSuspendable(db, className, classEntry.getSuperName(),
                classEntry.getInterfaces(), name, desc, signature, exceptions);
    final SuspendableType setSuspendable = classEntry.check(name, desc);

    if (setSuspendable == null)
        classEntry.set(name, desc,//from w w w. ja v  a  2 s  .  c o m
                markedSuspendable != null ? markedSuspendable : SuspendableType.NON_SUSPENDABLE);

    final boolean suspendable = markedSuspendable == SuspendableType.SUSPENDABLE
            | setSuspendable == SuspendableType.SUSPENDABLE;

    if (checkAccess(access) && !isYieldMethod(className, name)) {
        if (isSynchronized(access)) {
            if (!db.isAllowMonitors())
                throw new UnableToInstrumentException("synchronization", className, name, desc);
            else
                db.log(LogLevel.WARNING, "Method %s#%s%s is synchronized", className, name, desc);
        }

        if (methods == null)
            methods = new ArrayList<MethodNode>();
        final MethodNode mn = new MethodNode(access, name, desc, signature, exceptions);

        //            if (suspendable) {
        //                if (db.isDebug())
        //                    db.log(LogLevel.INFO, "Method %s#%s suspendable: %s (markedSuspendable: %s setSuspendable: %s)", className, name, suspendable, markedSuspendable, setSuspendable);
        //
        //                methods.add(mn);
        //                return mn; // this causes the mn to be initialized
        //            } else { // look for @Suspendable or @DontInstrument annotation
        return new MethodVisitor(Opcodes.ASM4, mn) {
            private boolean susp = suspendable;
            private boolean commited = false;

            @Override
            public AnnotationVisitor visitAnnotation(String adesc, boolean visible) {
                if (adesc.equals(ANNOTATION_DESC))
                    susp = true;
                else if (adesc.equals(DONT_INSTRUMENT_ANNOTATION_DESC))
                    susp = false;

                return super.visitAnnotation(adesc, visible);
            }

            @Override
            public void visitCode() {
                commit();
                super.visitCode();
            }

            @Override
            public void visitEnd() {
                if (exception != null)
                    return;

                commit();
                try {
                    super.visitEnd();
                } catch (RuntimeException e) {
                    exception = e;
                }
            }

            private void commit() {
                if (commited)
                    return;
                commited = true;

                if (db.isDebug())
                    db.log(LogLevel.INFO,
                            "Method %s#%s suspendable: %s (markedSuspendable: %s setSuspendable: %s)",
                            className, name, susp, susp, setSuspendable);
                classEntry.set(name, desc,
                        susp ? SuspendableType.SUSPENDABLE : SuspendableType.NON_SUSPENDABLE);

                if (susp)
                    methods.add(mn);
                else {
                    MethodVisitor _mv = makeOutMV(mn);
                    _mv = new JSRInlinerAdapter(_mv, access, name, desc, signature, exceptions);
                    mn.accept(new MethodVisitor(Opcodes.ASM4, _mv) {
                        @Override
                        public void visitEnd() {
                            // don't call visitEnd on MV
                        }
                    }); // write method as-is
                    this.mv = _mv;
                }
            }
        };
        //            }
    }
    return super.visitMethod(access, name, desc, signature, exceptions);
}

From source file:co.paralleluniverse.fibers.instrument.JavaAgent.java

License:Open Source License

public static byte[] crazyClojureOnceDisable(ClassLoader loader, String className, Class<?> classBeingRedefined,
        ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    if (!Boolean.parseBoolean(System.getProperty("co.paralleluniverse.pulsar.disableOnce", "false")))
        return classfileBuffer;

    ClassReader cr = new ClassReader(classfileBuffer);
    ClassWriter cw = new ClassWriter(cr, 0);
    ClassVisitor cv = new ClassVisitor(Opcodes.ASM4, cw) {

        @Override/*from  www.j  a v a 2 s  .  c om*/
        public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                String[] exceptions) {
            return new MethodVisitor(api, super.visitMethod(access, name, desc, signature, exceptions)) {

                @Override
                public void visitLdcInsn(Object cst) {
                    if (cst instanceof String && cst.equals("once")) {
                        super.visitLdcInsn("once$disabled-by-pulsar");
                    } else
                        super.visitLdcInsn(cst);
                }

            };
        }
    };
    cr.accept(cv, 0);
    return cw.toByteArray();
}

From source file:com.alibaba.hotswap.processor.basic.BaseClassVisitor.java

License:Open Source License

public BaseClassVisitor(ClassVisitor cv) {
    super(Opcodes.ASM4, cv);
}

From source file:com.alibaba.hotswap.processor.constructor.modifier.ConstructorLVTAdjustModifier.java

License:Open Source License

public ConstructorLVTAdjustModifier(MethodVisitor mv, int delta) {
    super(Opcodes.ASM4, mv);
    this.delta = delta;
}

From source file:com.alibaba.hotswap.processor.jdk.classloader.modifier.DefineClassMethodModifier.java

License:Open Source License

public DefineClassMethodModifier(MethodVisitor mv, int access, String name, String desc) {
    super(Opcodes.ASM4, mv, access, name, desc);
}