Example usage for org.objectweb.asm Opcodes ASM7

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

Introduction

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

Prototype

int ASM7

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

Click Source Link

Usage

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

License:Open Source License

private void filter(File file) {
    try {/*from  w  w  w. j  a  v a  2s . c o m*/
        ClassWriter cw = null;
        try (FileInputStream fis = new FileInputStream(file)) {
            ClassReader cr = new ClassReader(fis);
            cw = new ClassWriter(cr, 0);
            cr.accept(new ClassVisitor(Opcodes.ASM7, cw) {
                @Override
                public ModuleVisitor visitModule(String name, int access, String version) {
                    return new ModuleVisitor(Opcodes.ASM7, super.visitModule(name, access, version)) {
                        @Override
                        public void visitRequire(String module, int access, String version) {
                            if (!module.contains(mod)) {
                                super.visitRequire(module, access, version);
                            }
                        }
                    };
                }
            }, 0);
        }

        try (FileOutputStream fos = new FileOutputStream(file)) {
            fos.write(cw.toByteArray());
        }
    } catch (IOException ex) {
        throw new BuildException("Filterin module-info file " + file, ex);
    }
}

From source file:co.paralleluniverse.vtime.VirtualTimeClassTransformer.java

License:Open Source License

private ClassVisitor createVisitor(ClassVisitor next) {
    int api;//from ww  w  .j a v a  2s. c o  m
    if (System.getProperty("java.version").startsWith("1.8")) {
        api = Opcodes.ASM5;
    } else if (System.getProperty("java.version").startsWith("10")) {
        api = Opcodes.ASM6;
    } else {
        api = Opcodes.ASM7;
    }
    return new ClassVisitor(api, next) {
        @Override
        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 visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
                    if (!captureTimeCall(owner, name, desc)) {
                        super.visitMethodInsn(opcode, owner, name, desc, itf);
                    }
                }

                private boolean captureTimeCall(String owner, String name, String desc) {
                    switch (owner) {
                    case "java/lang/Object":
                        if ("wait".equals(name)) {
                            return callClockMethod("Object_wait", instanceToStatic(owner, desc));
                        }
                        break;
                    case "java/lang/System":
                        switch (name) {
                        case "nanoTime":
                            return callClockMethod("System_nanoTime", desc);
                        case "currentTimeMillis":
                            return callClockMethod("System_currentTimeMillis", desc);
                        }
                        break;
                    case "java/lang/Thread":
                        if ("sleep".equals(name)) {
                            return callClockMethod("Thread_sleep", desc);
                        }
                        break;
                    case "sun/misc/Unsafe":
                        if ("park".equals(name)) {
                            return callClockMethod("Unsafe_park", instanceToStatic(owner, desc));
                        }
                        break;
                    case "java/lang/management/RuntimeMXBean":
                        if ("getStartTime".equals(name)) {
                            return callClockMethod("RuntimeMXBean_getStartTime", instanceToStatic(owner, desc));
                        }
                        break;
                    }
                    return false;
                }

                private boolean callClockMethod(String name, String desc) {
                    if (includedMethods == null || includedMethods.contains(name)) {
                        super.visitMethodInsn(Opcodes.INVOKESTATIC, CLOCK, name, desc, false);
                        return true;
                    } else {
                        return false;
                    }
                }

                private String instanceToStatic(String owner, String desc) {
                    return "(L" + owner + ";" + desc.substring(1);
                }
            };
        }
    };
}

From source file:org.ballerinalang.compiler.backend.jvm.ClassVerifier.java

License:Open Source License

/**
 * This method is an extension of/* w  w w.  j  ava2  s . co m*/
 * {@code jdk.internal.org.objectweb.asm.util.CheckClassAdapter#verify(ClassReader, boolean, java.io.PrintWriter)}.
 * 
 * @param bytes Bytes stream of the class to be verified.
 * @return An optional error, if there are verification errors.
 */
private static Optional<ErrorValue> verify(byte[] bytes, ClassLoader classLoader) {
    ClassReader classReader = new ClassReader(bytes);
    ClassNode classNode = new ClassNode();
    classReader.accept(new CheckClassAdapter(Opcodes.ASM7, classNode, false) {
    }, ClassReader.SKIP_DEBUG);

    Type syperType = classNode.superName == null ? null : Type.getObjectType(classNode.superName);
    List<MethodNode> methods = classNode.methods;

    List<Type> interfaces = new ArrayList<>();
    for (String interfaceName : classNode.interfaces) {
        interfaces.add(Type.getObjectType(interfaceName));
    }

    for (MethodNode method : methods) {
        SimpleVerifier verifier = new SimpleVerifier(Type.getObjectType(classNode.name), syperType, interfaces,
                (classNode.access & Opcodes.ACC_INTERFACE) != 0);
        Analyzer<BasicValue> analyzer = new Analyzer<>(verifier);
        if (classLoader != null) {
            verifier.setClassLoader(classLoader);
        }
        try {
            analyzer.analyze(classNode.name, method);
        } catch (AnalyzerException e) {
            return Optional.of(BallerinaErrors.createError(e.getMessage()));
        }
    }

    return Optional.empty();
}

From source file:scouter.agent.AgentTransformer.java

License:Apache License

public byte[] transform(final ClassLoader loader, String className, final Class classBeingRedefined,
        ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    try {//from   w  ww  . j  ava2  s  .c om
        hookingCtx.set(loader);

        //            if(className != null && (className.indexOf("http") >= 0 || className.indexOf("Http") >= 0)) {
        //                System.out.println("[!!!!!!!!] loading ...http className = " + className);
        //            }

        if (className == null)
            return null;
        if (classBeingRedefined == null) {
            if (asynchook.contains(className.hashCode())) {
                AsyncRunner.getInstance().add(loader, className, classfileBuffer);
                return null;
            }
            if (loader == null) {
                if (conf._hook_boot_prefix == null || conf._hook_boot_prefix.length() == 0
                        || false == className.startsWith(conf._hook_boot_prefix)) {
                    return null;
                }
            }
        }
        if (className.startsWith("scouter/")) {
            return null;
        }
        //
        classfileBuffer = DirectPatch.patch(className, classfileBuffer);
        ObjTypeDetector.check(className);
        final ClassDesc classDesc = new ClassDesc();
        ClassReader cr = new ClassReader(classfileBuffer);
        cr.accept(new ClassVisitor(Opcodes.ASM7) {
            public void visit(int version, int access, String name, String signature, String superName,
                    String[] interfaces) {
                classDesc.set(version, access, name, signature, superName, interfaces);
                if (conf._hook_map_impl_enabled) {
                    classDesc.isMapImpl = isMapImpl(superName, interfaces, loader);
                }
                super.visit(version, access, name, signature, superName, interfaces);
            }

            @Override
            public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                classDesc.anotation += desc;
                return super.visitAnnotation(desc, visible);
            }
        }, 0);
        if (AsmUtil.isInterface(classDesc.access)) {
            return null;
        }
        classDesc.classBeingRedefined = classBeingRedefined;
        ClassWriter cw = getClassWriter(classDesc);
        ClassVisitor cv = cw;
        List<IASM> workAsms = asms;
        for (int i = workAsms.size() - 1; i >= 0; i--) {
            cv = workAsms.get(i).transform(cv, className, classDesc);
            if (cv != cw) {
                cr = new ClassReader(classfileBuffer);
                cr.accept(cv, ClassReader.EXPAND_FRAMES);
                classfileBuffer = cw.toByteArray();
                cv = cw = getClassWriter(classDesc);
                if (conf._log_asm_enabled) {
                    if (this.bciOut == null) {
                        this.bciOut = new Logger.FileLog("./scouter.bci");
                    }
                    this.bciOut.println(className + "\t\t[" + loader + "]");
                }
            }
        }
        return classfileBuffer;
    } catch (Throwable t) {
        Logger.println("A101", "Transformer Error : " + className, t);
        t.printStackTrace();
    } finally {
        hookingCtx.set(null);
    }
    return null;
}

From source file:scouter.agent.asm.JDBCConnectionOpenASM.java

License:Apache License

public DbcOpenMV(int access, String desc, MethodVisitor mv, String fullname, int fullname_hash) {
    super(Opcodes.ASM7, access, desc, mv);
    this.fullname = fullname;
    this.fullname_hash = fullname_hash;
    this.isStatic = (access & ACC_STATIC) != 0;
}

From source file:scouter.agent.DirectPatch.java

License:Apache License

private static String getClassName(byte[] bytes) {
    try {//  w w w . j a  v a 2s.c  om
        final ClassDesc classDesc = new ClassDesc();
        ClassReader cr = new ClassReader(bytes);
        cr.accept(new ClassVisitor(Opcodes.ASM7) {
            public void visit(int version, int access, String name, String signature, String superName,
                    String[] interfaces) {
                classDesc.set(version, access, name, signature, superName, interfaces);
            }
        }, 0);
        return classDesc.name.replace('.', '/');
    } catch (Throwable t) {
        return null;
    }
}

From source file:scouter.agent.LambdaFormTransformer.java

License:Apache License

public byte[] transform(final ClassLoader loader, String className, final Class classBeingRedefined,
        ProtectionDomain protectionDomain, byte[] classfileBuffer, String lambdaMethodName,
        String lambdaMethodDesc, String factoryMethodName, String factoryMethodDesc)
        throws IllegalClassFormatException {
    try {//from w  ww.  java2  s.c o  m
        if (className == null)
            return null;

        boolean scoped = false;

        for (int i = 0; i < scanScopePrefix.size(); i++) {
            if (className.indexOf(scanScopePrefix.get(i)) == 0) {
                scoped = true;
                break;
            }
        }

        if (!scoped) {
            return null;
        }

        final ClassDesc classDesc = new ClassDesc();
        ClassReader cr = new ClassReader(classfileBuffer);
        cr.accept(new ClassVisitor(Opcodes.ASM7) {
            public void visit(int version, int access, String name, String signature, String superName,
                    String[] interfaces) {
                classDesc.set(version, access, name, signature, superName, interfaces);
                super.visit(version, access, name, signature, superName, interfaces);
            }
        }, 0);
        if (AsmUtil.isInterface(classDesc.access)) {
            return null;
        }
        classDesc.classBeingRedefined = classBeingRedefined;
        ClassWriter cw = getClassWriter(classDesc);
        ClassVisitor cv = cw;
        List<ILASM> workAsms = asms;
        for (int i = workAsms.size() - 1; i >= 0; i--) {
            cv = workAsms.get(i).transform(cv, className, classDesc, lambdaMethodName, lambdaMethodDesc,
                    factoryMethodName, factoryMethodDesc);
            if (cv != cw) {
                cr = new ClassReader(classfileBuffer);
                cr.accept(cv, ClassReader.EXPAND_FRAMES);
                classfileBuffer = cw.toByteArray();
                cv = cw = getClassWriter(classDesc);
            }
        }
        return classfileBuffer;
    } catch (Throwable t) {
        Logger.println("B101", "LambdaFormTransformer Error", t);
        t.printStackTrace();
    } finally {
    }
    return null;
}