Java Code Examples for jdk.internal.org.objectweb.asm.tree.ClassNode

The following examples show how to use jdk.internal.org.objectweb.asm.tree.ClassNode. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@SuppressWarnings("unchecked")
private static <T> T annotationValue(ClassNode classNode, String typeDescriptor, Class<?> type) {
    if (classNode.visibleAnnotations != null) {
        for (AnnotationNode a : classNode.visibleAnnotations) {
            if (typeDescriptor.equals(a.desc)) {
                List<Object> values = a.values;
                if (values != null && values.size() == 2) {
                    Object key = values.get(0);
                    Object value = values.get(1);
                    if (key instanceof String && value != null) {
                        if (type == value.getClass()) {
                            String keyName = (String) key;
                            if ("value".equals(keyName)) {
                               return (T) value;
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
 
Example 2
@SuppressWarnings("unchecked")
private static <T> T annotationValue(ClassNode classNode, String typeDescriptor, Class<?> type) {
    if (classNode.visibleAnnotations != null) {
        for (AnnotationNode a : classNode.visibleAnnotations) {
            if (typeDescriptor.equals(a.desc)) {
                List<Object> values = a.values;
                if (values != null && values.size() == 2) {
                    Object key = values.get(0);
                    Object value = values.get(1);
                    if (key instanceof String && value != null) {
                        if (type == value.getClass()) {
                            String keyName = (String) key;
                            if ("value".equals(keyName)) {
                               return (T) value;
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
 
Example 3
@SuppressWarnings("unchecked")
private static <T> T annotationValue(ClassNode classNode, String typeDescriptor, Class<?> type) {
    if (classNode.visibleAnnotations != null) {
        for (AnnotationNode a : classNode.visibleAnnotations) {
            if (typeDescriptor.equals(a.desc)) {
                List<Object> values = a.values;
                if (values != null && values.size() == 2) {
                    Object key = values.get(0);
                    Object value = values.get(1);
                    if (key instanceof String && value != null) {
                        if (type == value.getClass()) {
                            String keyName = (String) key;
                            if ("value".equals(keyName)) {
                               return (T) value;
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
 
Example 4
/**
 * Create a {@link ClassNode} with empty constructor.
 */
private static ClassNode emptyClass(String name) {
    ClassNode classNode = new ClassNode();
    classNode.visit(52, ACC_SUPER | ACC_PUBLIC, name.replace('.', '/'), null, "java/lang/Object", new String[]{});

    MethodVisitor mv = classNode.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitInsn(RETURN);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitMaxs(1, 1);
    mv.visitEnd();

    return classNode;
}
 
Example 5
/**
 * Methods in methodFilter that exist in cn will be merged into cv. If the method already exists,
 * the original method will be deleted.
 *
 * @param cv
 * @param cn - a ClassNode with Methods that will be merged into this class
 * @param methodFilter - only methods in this list will be merged
 * @param typeMappings - while merging, type references in the methods will be changed according to this map
 */
public JIMethodMergeAdapter(ClassVisitor cv, ClassNode cn, List<Method> methodFilter, JITypeMapping[] typeMappings) {
    super(Opcodes.ASM5, cv);
    this.cn = cn;
    this.methodFilter = methodFilter;

    this.typeMap = new HashMap<>();
    for (JITypeMapping tm : typeMappings) {
        typeMap.put(tm.from().replace('.', '/'), tm.to().replace('.', '/'));
    }
}
 
Example 6
private byte[] makeBytecode() throws IOException, ClassNotFoundException {

        // Find the methods to instrument and inline

        final List<Method> instrumentationMethods = new ArrayList<>();
        for (final Method m : instrumentor.getDeclaredMethods()) {
            JIInstrumentationMethod im = m.getAnnotation(JIInstrumentationMethod.class);
            if (im != null) {
                instrumentationMethods.add(m);
            }
        }

        // We begin by inlining the target's methods into the instrumentor

        ClassNode temporary = new ClassNode();
        ClassVisitor inliner = new JIInliner(
                Opcodes.ASM5,
                temporary,
                targetName,
                instrumentorName,
                targetClassReader,
                instrumentationMethods);
        instrClassReader.accept(inliner, ClassReader.EXPAND_FRAMES);

        // Now we have the target's methods inlined into the instrumentation code (in 'temporary').
        // We now need to replace the target's method with the code in the
        // instrumentation method.

        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
        JIMethodMergeAdapter ma = new JIMethodMergeAdapter(
                cw,
                temporary,
                instrumentationMethods,
                instrumentor.getAnnotationsByType(JITypeMapping.class));
        targetClassReader.accept(ma, ClassReader.EXPAND_FRAMES);

       return cw.toByteArray();
    }
 
Example 7
Source Project: dragonwell8_jdk   Source File: JIInliner.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * A ClassVisitor which will check all methods of the class it visits against the instrumentationMethods
 * list. If a method is on that list, the method will be further processed for inlining into that
 * method.
 */
JIInliner(int api, ClassVisitor cv, String targetClassName, String instrumentationClassName,
        ClassReader targetClassReader,
        List<Method> instrumentationMethods) {
    super(api, cv);
    this.targetClassName = targetClassName;
    this.instrumentationClassName = instrumentationClassName;
    this.instrumentationMethods = instrumentationMethods;

    ClassNode cn = new ClassNode(Opcodes.ASM5);
    targetClassReader.accept(cn, ClassReader.EXPAND_FRAMES);
    this.targetClassNode = cn;
}
 
Example 8
/**
 * Checks a given class.
 *
 * @param cr
 *            a <code>ClassReader</code> that contains bytecode for the
 *            analysis.
 * @param loader
 *            a <code>ClassLoader</code> which will be used to load
 *            referenced classes. This is useful if you are verifiying
 *            multiple interdependent classes.
 * @param dump
 *            true if bytecode should be printed out not only when errors
 *            are found.
 * @param pw
 *            write where results going to be printed
 */
public static void verify(final ClassReader cr, final ClassLoader loader,
        final boolean dump, final PrintWriter pw) {
    ClassNode cn = new ClassNode();
    cr.accept(new CheckClassAdapter(cn, false), ClassReader.SKIP_DEBUG);

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

    List<Type> interfaces = new ArrayList<Type>();
    for (Iterator<String> i = cn.interfaces.iterator(); i.hasNext();) {
        interfaces.add(Type.getObjectType(i.next()));
    }

    for (int i = 0; i < methods.size(); ++i) {
        MethodNode method = methods.get(i);
        SimpleVerifier verifier = new SimpleVerifier(
                Type.getObjectType(cn.name), syperType, interfaces,
                (cn.access & Opcodes.ACC_INTERFACE) != 0);
        Analyzer<BasicValue> a = new Analyzer<BasicValue>(verifier);
        if (loader != null) {
            verifier.setClassLoader(loader);
        }
        try {
            a.analyze(cn.name, method);
            if (!dump) {
                continue;
            }
        } catch (Exception e) {
            e.printStackTrace(pw);
        }
        printAnalyzerResult(method, a, pw);
    }
    pw.flush();
}
 
Example 9
/**
 * Methods in methodFilter that exist in cn will be merged into cv. If the method already exists,
 * the original method will be deleted.
 *
 * @param cv
 * @param cn - a ClassNode with Methods that will be merged into this class
 * @param methodFilter - only methods in this list will be merged
 * @param typeMappings - while merging, type references in the methods will be changed according to this map
 */
public JIMethodMergeAdapter(ClassVisitor cv, ClassNode cn, List<Method> methodFilter, JITypeMapping[] typeMappings) {
    super(Opcodes.ASM5, cv);
    this.cn = cn;
    this.methodFilter = methodFilter;

    this.typeMap = new HashMap<>();
    for (JITypeMapping tm : typeMappings) {
        typeMap.put(tm.from().replace('.', '/'), tm.to().replace('.', '/'));
    }
}
 
Example 10
private byte[] makeBytecode() throws IOException, ClassNotFoundException {

        // Find the methods to instrument and inline

        final List<Method> instrumentationMethods = new ArrayList<>();
        for (final Method m : instrumentor.getDeclaredMethods()) {
            JIInstrumentationMethod im = m.getAnnotation(JIInstrumentationMethod.class);
            if (im != null) {
                instrumentationMethods.add(m);
            }
        }

        // We begin by inlining the target's methods into the instrumentor

        ClassNode temporary = new ClassNode();
        ClassVisitor inliner = new JIInliner(
                Opcodes.ASM5,
                temporary,
                targetName,
                instrumentorName,
                targetClassReader,
                instrumentationMethods);
        instrClassReader.accept(inliner, ClassReader.EXPAND_FRAMES);

        // Now we have the target's methods inlined into the instrumentation code (in 'temporary').
        // We now need to replace the target's method with the code in the
        // instrumentation method.

        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
        JIMethodMergeAdapter ma = new JIMethodMergeAdapter(
                cw,
                temporary,
                instrumentationMethods,
                instrumentor.getAnnotationsByType(JITypeMapping.class));
        targetClassReader.accept(ma, ClassReader.EXPAND_FRAMES);

       return cw.toByteArray();
    }
 
Example 11
Source Project: TencentKona-8   Source File: JIInliner.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * A ClassVisitor which will check all methods of the class it visits against the instrumentationMethods
 * list. If a method is on that list, the method will be further processed for inlining into that
 * method.
 */
JIInliner(int api, ClassVisitor cv, String targetClassName, String instrumentationClassName,
        ClassReader targetClassReader,
        List<Method> instrumentationMethods) {
    super(api, cv);
    this.targetClassName = targetClassName;
    this.instrumentationClassName = instrumentationClassName;
    this.instrumentationMethods = instrumentationMethods;

    ClassNode cn = new ClassNode(Opcodes.ASM5);
    targetClassReader.accept(cn, ClassReader.EXPAND_FRAMES);
    this.targetClassNode = cn;
}
 
Example 12
/**
 * Checks a given class.
 *
 * @param cr
 *            a <code>ClassReader</code> that contains bytecode for the
 *            analysis.
 * @param loader
 *            a <code>ClassLoader</code> which will be used to load
 *            referenced classes. This is useful if you are verifiying
 *            multiple interdependent classes.
 * @param dump
 *            true if bytecode should be printed out not only when errors
 *            are found.
 * @param pw
 *            write where results going to be printed
 */
public static void verify(final ClassReader cr, final ClassLoader loader,
        final boolean dump, final PrintWriter pw) {
    ClassNode cn = new ClassNode();
    cr.accept(new CheckClassAdapter(cn, false), ClassReader.SKIP_DEBUG);

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

    List<Type> interfaces = new ArrayList<Type>();
    for (Iterator<String> i = cn.interfaces.iterator(); i.hasNext();) {
        interfaces.add(Type.getObjectType(i.next()));
    }

    for (int i = 0; i < methods.size(); ++i) {
        MethodNode method = methods.get(i);
        SimpleVerifier verifier = new SimpleVerifier(
                Type.getObjectType(cn.name), syperType, interfaces,
                (cn.access & Opcodes.ACC_INTERFACE) != 0);
        Analyzer<BasicValue> a = new Analyzer<BasicValue>(verifier);
        if (loader != null) {
            verifier.setClassLoader(loader);
        }
        try {
            a.analyze(cn.name, method);
            if (!dump) {
                continue;
            }
        } catch (Exception e) {
            e.printStackTrace(pw);
        }
        printAnalyzerResult(method, a, pw);
    }
    pw.flush();
}
 
Example 13
Source Project: jdk8u60   Source File: CheckClassAdapter.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks a given class.
 *
 * @param cr
 *            a <code>ClassReader</code> that contains bytecode for the
 *            analysis.
 * @param loader
 *            a <code>ClassLoader</code> which will be used to load
 *            referenced classes. This is useful if you are verifiying
 *            multiple interdependent classes.
 * @param dump
 *            true if bytecode should be printed out not only when errors
 *            are found.
 * @param pw
 *            write where results going to be printed
 */
public static void verify(final ClassReader cr, final ClassLoader loader,
        final boolean dump, final PrintWriter pw) {
    ClassNode cn = new ClassNode();
    cr.accept(new CheckClassAdapter(cn, false), ClassReader.SKIP_DEBUG);

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

    List<Type> interfaces = new ArrayList<Type>();
    for (Iterator<String> i = cn.interfaces.iterator(); i.hasNext();) {
        interfaces.add(Type.getObjectType(i.next()));
    }

    for (int i = 0; i < methods.size(); ++i) {
        MethodNode method = methods.get(i);
        SimpleVerifier verifier = new SimpleVerifier(
                Type.getObjectType(cn.name), syperType, interfaces,
                (cn.access & Opcodes.ACC_INTERFACE) != 0);
        Analyzer<BasicValue> a = new Analyzer<BasicValue>(verifier);
        if (loader != null) {
            verifier.setClassLoader(loader);
        }
        try {
            a.analyze(cn.name, method);
            if (!dump) {
                continue;
            }
        } catch (Exception e) {
            e.printStackTrace(pw);
        }
        printAnalyzerResult(method, a, pw);
    }
    pw.flush();
}
 
Example 14
/**
 * Methods in methodFilter that exist in cn will be merged into cv. If the method already exists,
 * the original method will be deleted.
 *
 * @param cv
 * @param cn - a ClassNode with Methods that will be merged into this class
 * @param methodFilter - only methods in this list will be merged
 * @param typeMappings - while merging, type references in the methods will be changed according to this map
 */
public JIMethodMergeAdapter(ClassVisitor cv, ClassNode cn, List<Method> methodFilter, JITypeMapping[] typeMappings) {
    super(Opcodes.ASM5, cv);
    this.cn = cn;
    this.methodFilter = methodFilter;

    this.typeMap = new HashMap<>();
    for (JITypeMapping tm : typeMappings) {
        typeMap.put(tm.from().replace('.', '/'), tm.to().replace('.', '/'));
    }
}
 
Example 15
private byte[] makeBytecode() throws IOException, ClassNotFoundException {

        // Find the methods to instrument and inline

        final List<Method> instrumentationMethods = new ArrayList<>();
        for (final Method m : instrumentor.getDeclaredMethods()) {
            JIInstrumentationMethod im = m.getAnnotation(JIInstrumentationMethod.class);
            if (im != null) {
                instrumentationMethods.add(m);
            }
        }

        // We begin by inlining the target's methods into the instrumentor

        ClassNode temporary = new ClassNode();
        ClassVisitor inliner = new JIInliner(
                Opcodes.ASM5,
                temporary,
                targetName,
                instrumentorName,
                targetClassReader,
                instrumentationMethods);
        instrClassReader.accept(inliner, ClassReader.EXPAND_FRAMES);

        // Now we have the target's methods inlined into the instrumentation code (in 'temporary').
        // We now need to replace the target's method with the code in the
        // instrumentation method.

        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
        JIMethodMergeAdapter ma = new JIMethodMergeAdapter(
                cw,
                temporary,
                instrumentationMethods,
                instrumentor.getAnnotationsByType(JITypeMapping.class));
        targetClassReader.accept(ma, ClassReader.EXPAND_FRAMES);

       return cw.toByteArray();
    }
 
Example 16
Source Project: openjdk-jdk8u   Source File: JIInliner.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * A ClassVisitor which will check all methods of the class it visits against the instrumentationMethods
 * list. If a method is on that list, the method will be further processed for inlining into that
 * method.
 */
JIInliner(int api, ClassVisitor cv, String targetClassName, String instrumentationClassName,
        ClassReader targetClassReader,
        List<Method> instrumentationMethods) {
    super(api, cv);
    this.targetClassName = targetClassName;
    this.instrumentationClassName = instrumentationClassName;
    this.instrumentationMethods = instrumentationMethods;

    ClassNode cn = new ClassNode(Opcodes.ASM5);
    targetClassReader.accept(cn, ClassReader.EXPAND_FRAMES);
    this.targetClassNode = cn;
}
 
Example 17
/**
 * Checks a given class.
 *
 * @param cr
 *            a <code>ClassReader</code> that contains bytecode for the
 *            analysis.
 * @param loader
 *            a <code>ClassLoader</code> which will be used to load
 *            referenced classes. This is useful if you are verifiying
 *            multiple interdependent classes.
 * @param dump
 *            true if bytecode should be printed out not only when errors
 *            are found.
 * @param pw
 *            write where results going to be printed
 */
public static void verify(final ClassReader cr, final ClassLoader loader,
        final boolean dump, final PrintWriter pw) {
    ClassNode cn = new ClassNode();
    cr.accept(new CheckClassAdapter(cn, false), ClassReader.SKIP_DEBUG);

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

    List<Type> interfaces = new ArrayList<Type>();
    for (Iterator<String> i = cn.interfaces.iterator(); i.hasNext();) {
        interfaces.add(Type.getObjectType(i.next()));
    }

    for (int i = 0; i < methods.size(); ++i) {
        MethodNode method = methods.get(i);
        SimpleVerifier verifier = new SimpleVerifier(
                Type.getObjectType(cn.name), syperType, interfaces,
                (cn.access & Opcodes.ACC_INTERFACE) != 0);
        Analyzer<BasicValue> a = new Analyzer<BasicValue>(verifier);
        if (loader != null) {
            verifier.setClassLoader(loader);
        }
        try {
            a.analyze(cn.name, method);
            if (!dump) {
                continue;
            }
        } catch (Exception e) {
            e.printStackTrace(pw);
        }
        printAnalyzerResult(method, a, pw);
    }
    pw.flush();
}
 
Example 18
Source Project: NullAway   Source File: AnnotationChecker.java    License: MIT License 5 votes vote down vote up
private static boolean checkMethodAnnotationsInClass(
    InputStream is, Map<String, String> expectedToActualAnnotations) throws IOException {
  ClassReader cr = new ClassReader(is);
  ClassNode cn = new ClassNode();
  cr.accept(cn, 0);

  for (MethodNode method : cn.methods) {
    if (!checkExpectedAnnotations(method.visibleAnnotations, expectedToActualAnnotations)
        && !checkTestMethodAnnotationByName(method)) {
      System.out.println(
          "Error: Invalid / Unexpected annotations found on method '" + method.name + "'");
      return false;
    }
    List<AnnotationNode>[] paramAnnotations = method.visibleParameterAnnotations;
    if (paramAnnotations == null) continue;
    for (List<AnnotationNode> annotations : paramAnnotations) {
      if (!checkExpectedAnnotations(annotations, expectedToActualAnnotations)
          && !checkTestMethodParamAnnotationByName(method)) {
        System.out.println(
            "Error: Invalid / Unexpected annotations found in a parameter of method '"
                + method.name
                + "'.");
        return false;
      }
    }
  }
  return true;
}
 
Example 19
/**
 * Checks a given class.
 *
 * @param cr
 *            a <code>ClassReader</code> that contains bytecode for the
 *            analysis.
 * @param loader
 *            a <code>ClassLoader</code> which will be used to load
 *            referenced classes. This is useful if you are verifiying
 *            multiple interdependent classes.
 * @param dump
 *            true if bytecode should be printed out not only when errors
 *            are found.
 * @param pw
 *            write where results going to be printed
 */
public static void verify(final ClassReader cr, final ClassLoader loader,
        final boolean dump, final PrintWriter pw) {
    ClassNode cn = new ClassNode();
    cr.accept(new CheckClassAdapter(cn, false), ClassReader.SKIP_DEBUG);

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

    List<Type> interfaces = new ArrayList<Type>();
    for (Iterator<String> i = cn.interfaces.iterator(); i.hasNext();) {
        interfaces.add(Type.getObjectType(i.next()));
    }

    for (int i = 0; i < methods.size(); ++i) {
        MethodNode method = methods.get(i);
        SimpleVerifier verifier = new SimpleVerifier(
                Type.getObjectType(cn.name), syperType, interfaces,
                (cn.access & Opcodes.ACC_INTERFACE) != 0);
        Analyzer<BasicValue> a = new Analyzer<BasicValue>(verifier);
        if (loader != null) {
            verifier.setClassLoader(loader);
        }
        try {
            a.analyze(cn.name, method);
            if (!dump) {
                continue;
            }
        } catch (Exception e) {
            e.printStackTrace(pw);
        }
        printAnalyzerResult(method, a, pw);
    }
    pw.flush();
}
 
Example 20
Source Project: Bytecoder   Source File: CheckClassAdapter.java    License: Apache License 2.0 5 votes vote down vote up
/**
  * Checks the given class.
  *
  * @param classReader the class to be checked.
  * @param loader a <code>ClassLoader</code> which will be used to load referenced classes. May be
  *     {@literal null}.
  * @param printResults whether to print the results of the bytecode verification.
  * @param printWriter where the results (or the stack trace in case of error) must be printed.
  */
public static void verify(
        final ClassReader classReader,
        final ClassLoader loader,
        final boolean printResults,
        final PrintWriter printWriter) {
    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<Type>();
    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<BasicValue>(verifier);
        if (loader != null) {
            verifier.setClassLoader(loader);
        }
        try {
            analyzer.analyze(classNode.name, method);
        } catch (AnalyzerException e) {
            e.printStackTrace(printWriter);
        }
        if (printResults) {
            printAnalyzerResult(method, analyzer, printWriter);
        }
    }
    printWriter.flush();
}
 
Example 21
/**
 * @return a {@link MethodNode} called {@code methodName} in the given class.
 */
private static MethodNode getMethodNode(Class<?> clazz, String methodName) throws IOException {
    ClassReader classReader = new ClassReader(clazz.getName());
    ClassNode classNode = new ClassNode();
    classReader.accept(classNode, ClassReader.SKIP_FRAMES);

    for (MethodNode methodNode : classNode.methods) {
        if (methodNode.name.equals(methodName)) {
            return methodNode;
        }
    }
    return null;
}
 
Example 22
public Class<?> instrument(Class<?> targetClass, String methodName, int opcode, boolean insertAfter) throws IOException, ClassNotFoundException {
    // create a container class
    String className = targetClass.getName() + "$$" + methodName;
    ClassNode classNode = emptyClass(className);
    // duplicate the target method and add to the container class
    MethodNode methodNode = getMethodNode(targetClass, methodName);
    MethodNode newMethodNode = new MethodNode(methodNode.access, methodNode.name, methodNode.desc, methodNode.signature, methodNode.exceptions.toArray(new String[methodNode.exceptions.size()]));
    methodNode.accept(newMethodNode);
    classNode.methods.add(newMethodNode);
    // perform bytecode instrumentation
    for (AbstractInsnNode instruction : selectAll(newMethodNode.instructions)) {
        if (instruction.getOpcode() == opcode) {
            InsnList instrumentation = cloneInstructions(instrumentationInstructions);
            shiftLocalSlots(instrumentation, newMethodNode.maxLocals);
            newMethodNode.maxLocals += instrumentationMaxLocal;
            if (insertAfter) {
                newMethodNode.instructions.insert(instruction, instrumentation);
            } else {
                newMethodNode.instructions.insertBefore(instruction, instrumentation);
            }
        }
    }
    // dump a byte array and load the class with a dedicated loader to separate the namespace
    ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    classNode.accept(classWriter);
    byte[] bytes = classWriter.toByteArray();
    return new Loader(className, bytes).findClass(className);
}
 
Example 23
/**
 * Checks a given class.
 *
 * @param cr
 *            a <code>ClassReader</code> that contains bytecode for the
 *            analysis.
 * @param loader
 *            a <code>ClassLoader</code> which will be used to load
 *            referenced classes. This is useful if you are verifiying
 *            multiple interdependent classes.
 * @param dump
 *            true if bytecode should be printed out not only when errors
 *            are found.
 * @param pw
 *            write where results going to be printed
 */
public static void verify(final ClassReader cr, final ClassLoader loader,
        final boolean dump, final PrintWriter pw) {
    ClassNode cn = new ClassNode();
    cr.accept(new CheckClassAdapter(cn, false), ClassReader.SKIP_DEBUG);

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

    List<Type> interfaces = new ArrayList<Type>();
    for (Iterator<String> i = cn.interfaces.iterator(); i.hasNext();) {
        interfaces.add(Type.getObjectType(i.next()));
    }

    for (int i = 0; i < methods.size(); ++i) {
        MethodNode method = methods.get(i);
        SimpleVerifier verifier = new SimpleVerifier(
                Type.getObjectType(cn.name), syperType, interfaces,
                (cn.access & Opcodes.ACC_INTERFACE) != 0);
        Analyzer<BasicValue> a = new Analyzer<BasicValue>(verifier);
        if (loader != null) {
            verifier.setClassLoader(loader);
        }
        try {
            a.analyze(cn.name, method);
            if (!dump) {
                continue;
            }
        } catch (Exception e) {
            e.printStackTrace(pw);
        }
        printAnalyzerResult(method, a, pw);
    }
    pw.flush();
}
 
Example 24
Source Project: openjdk-jdk9   Source File: JImageTask.java    License: GNU General Public License v2.0 5 votes vote down vote up
void verify(BasicImageReader reader, String name, ImageLocation location) {
    if (name.endsWith(".class") && !name.endsWith("module-info.class")) {
        try {
            byte[] bytes = reader.getResource(location);
            ClassReader cr = new ClassReader(bytes);
            ClassNode cn = new ClassNode();
            cr.accept(cn, 0);
        } catch (Exception ex) {
            log.println("Error(s) in Class: " + name);
        }
    }
}
 
Example 25
Source Project: jdk8u-jdk   Source File: CheckClassAdapter.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks a given class.
 *
 * @param cr
 *            a <code>ClassReader</code> that contains bytecode for the
 *            analysis.
 * @param loader
 *            a <code>ClassLoader</code> which will be used to load
 *            referenced classes. This is useful if you are verifiying
 *            multiple interdependent classes.
 * @param dump
 *            true if bytecode should be printed out not only when errors
 *            are found.
 * @param pw
 *            write where results going to be printed
 */
public static void verify(final ClassReader cr, final ClassLoader loader,
        final boolean dump, final PrintWriter pw) {
    ClassNode cn = new ClassNode();
    cr.accept(new CheckClassAdapter(cn, false), ClassReader.SKIP_DEBUG);

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

    List<Type> interfaces = new ArrayList<Type>();
    for (Iterator<String> i = cn.interfaces.iterator(); i.hasNext();) {
        interfaces.add(Type.getObjectType(i.next()));
    }

    for (int i = 0; i < methods.size(); ++i) {
        MethodNode method = methods.get(i);
        SimpleVerifier verifier = new SimpleVerifier(
                Type.getObjectType(cn.name), syperType, interfaces,
                (cn.access & Opcodes.ACC_INTERFACE) != 0);
        Analyzer<BasicValue> a = new Analyzer<BasicValue>(verifier);
        if (loader != null) {
            verifier.setClassLoader(loader);
        }
        try {
            a.analyze(cn.name, method);
            if (!dump) {
                continue;
            }
        } catch (Exception e) {
            e.printStackTrace(pw);
        }
        printAnalyzerResult(method, a, pw);
    }
    pw.flush();
}
 
Example 26
Source Project: hottub   Source File: CheckClassAdapter.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks a given class.
 *
 * @param cr
 *            a <code>ClassReader</code> that contains bytecode for the
 *            analysis.
 * @param loader
 *            a <code>ClassLoader</code> which will be used to load
 *            referenced classes. This is useful if you are verifiying
 *            multiple interdependent classes.
 * @param dump
 *            true if bytecode should be printed out not only when errors
 *            are found.
 * @param pw
 *            write where results going to be printed
 */
public static void verify(final ClassReader cr, final ClassLoader loader,
        final boolean dump, final PrintWriter pw) {
    ClassNode cn = new ClassNode();
    cr.accept(new CheckClassAdapter(cn, false), ClassReader.SKIP_DEBUG);

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

    List<Type> interfaces = new ArrayList<Type>();
    for (Iterator<String> i = cn.interfaces.iterator(); i.hasNext();) {
        interfaces.add(Type.getObjectType(i.next()));
    }

    for (int i = 0; i < methods.size(); ++i) {
        MethodNode method = methods.get(i);
        SimpleVerifier verifier = new SimpleVerifier(
                Type.getObjectType(cn.name), syperType, interfaces,
                (cn.access & Opcodes.ACC_INTERFACE) != 0);
        Analyzer<BasicValue> a = new Analyzer<BasicValue>(verifier);
        if (loader != null) {
            verifier.setClassLoader(loader);
        }
        try {
            a.analyze(cn.name, method);
            if (!dump) {
                continue;
            }
        } catch (Exception e) {
            e.printStackTrace(pw);
        }
        printAnalyzerResult(method, a, pw);
    }
    pw.flush();
}
 
Example 27
/**
 * Checks a given class.
 *
 * @param cr
 *            a <code>ClassReader</code> that contains bytecode for the
 *            analysis.
 * @param loader
 *            a <code>ClassLoader</code> which will be used to load
 *            referenced classes. This is useful if you are verifiying
 *            multiple interdependent classes.
 * @param dump
 *            true if bytecode should be printed out not only when errors
 *            are found.
 * @param pw
 *            write where results going to be printed
 */
public static void verify(final ClassReader cr, final ClassLoader loader,
        final boolean dump, final PrintWriter pw) {
    ClassNode cn = new ClassNode();
    cr.accept(new CheckClassAdapter(cn, false), ClassReader.SKIP_DEBUG);

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

    List<Type> interfaces = new ArrayList<Type>();
    for (Iterator<String> i = cn.interfaces.iterator(); i.hasNext();) {
        interfaces.add(Type.getObjectType(i.next()));
    }

    for (int i = 0; i < methods.size(); ++i) {
        MethodNode method = methods.get(i);
        SimpleVerifier verifier = new SimpleVerifier(
                Type.getObjectType(cn.name), syperType, interfaces,
                (cn.access & Opcodes.ACC_INTERFACE) != 0);
        Analyzer<BasicValue> a = new Analyzer<BasicValue>(verifier);
        if (loader != null) {
            verifier.setClassLoader(loader);
        }
        try {
            a.analyze(cn.name, method);
            if (!dump) {
                continue;
            }
        } catch (Exception e) {
            e.printStackTrace(pw);
        }
        printAnalyzerResult(method, a, pw);
    }
    pw.flush();
}
 
Example 28
Source Project: openjdk-8   Source File: CheckClassAdapter.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks a given class.
 *
 * @param cr
 *            a <code>ClassReader</code> that contains bytecode for the
 *            analysis.
 * @param loader
 *            a <code>ClassLoader</code> which will be used to load
 *            referenced classes. This is useful if you are verifiying
 *            multiple interdependent classes.
 * @param dump
 *            true if bytecode should be printed out not only when errors
 *            are found.
 * @param pw
 *            write where results going to be printed
 */
public static void verify(final ClassReader cr, final ClassLoader loader,
        final boolean dump, final PrintWriter pw) {
    ClassNode cn = new ClassNode();
    cr.accept(new CheckClassAdapter(cn, false), ClassReader.SKIP_DEBUG);

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

    List<Type> interfaces = new ArrayList<Type>();
    for (Iterator<String> i = cn.interfaces.iterator(); i.hasNext();) {
        interfaces.add(Type.getObjectType(i.next()));
    }

    for (int i = 0; i < methods.size(); ++i) {
        MethodNode method = methods.get(i);
        SimpleVerifier verifier = new SimpleVerifier(
                Type.getObjectType(cn.name), syperType, interfaces,
                (cn.access & Opcodes.ACC_INTERFACE) != 0);
        Analyzer<BasicValue> a = new Analyzer<BasicValue>(verifier);
        if (loader != null) {
            verifier.setClassLoader(loader);
        }
        try {
            a.analyze(cn.name, method);
            if (!dump) {
                continue;
            }
        } catch (Exception e) {
            e.printStackTrace(pw);
        }
        printAnalyzerResult(method, a, pw);
    }
    pw.flush();
}
 
Example 29
Source Project: jdk8u_jdk   Source File: CheckClassAdapter.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks a given class.
 *
 * @param cr
 *            a <code>ClassReader</code> that contains bytecode for the
 *            analysis.
 * @param loader
 *            a <code>ClassLoader</code> which will be used to load
 *            referenced classes. This is useful if you are verifiying
 *            multiple interdependent classes.
 * @param dump
 *            true if bytecode should be printed out not only when errors
 *            are found.
 * @param pw
 *            write where results going to be printed
 */
public static void verify(final ClassReader cr, final ClassLoader loader,
        final boolean dump, final PrintWriter pw) {
    ClassNode cn = new ClassNode();
    cr.accept(new CheckClassAdapter(cn, false), ClassReader.SKIP_DEBUG);

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

    List<Type> interfaces = new ArrayList<Type>();
    for (Iterator<String> i = cn.interfaces.iterator(); i.hasNext();) {
        interfaces.add(Type.getObjectType(i.next()));
    }

    for (int i = 0; i < methods.size(); ++i) {
        MethodNode method = methods.get(i);
        SimpleVerifier verifier = new SimpleVerifier(
                Type.getObjectType(cn.name), syperType, interfaces,
                (cn.access & Opcodes.ACC_INTERFACE) != 0);
        Analyzer<BasicValue> a = new Analyzer<BasicValue>(verifier);
        if (loader != null) {
            verifier.setClassLoader(loader);
        }
        try {
            a.analyze(cn.name, method);
            if (!dump) {
                continue;
            }
        } catch (Exception e) {
            e.printStackTrace(pw);
        }
        printAnalyzerResult(method, a, pw);
    }
    pw.flush();
}
 
Example 30
Source Project: jdk8u-jdk   Source File: CheckClassAdapter.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks a given class.
 *
 * @param cr
 *            a <code>ClassReader</code> that contains bytecode for the
 *            analysis.
 * @param loader
 *            a <code>ClassLoader</code> which will be used to load
 *            referenced classes. This is useful if you are verifiying
 *            multiple interdependent classes.
 * @param dump
 *            true if bytecode should be printed out not only when errors
 *            are found.
 * @param pw
 *            write where results going to be printed
 */
public static void verify(final ClassReader cr, final ClassLoader loader,
        final boolean dump, final PrintWriter pw) {
    ClassNode cn = new ClassNode();
    cr.accept(new CheckClassAdapter(cn, false), ClassReader.SKIP_DEBUG);

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

    List<Type> interfaces = new ArrayList<Type>();
    for (Iterator<String> i = cn.interfaces.iterator(); i.hasNext();) {
        interfaces.add(Type.getObjectType(i.next()));
    }

    for (int i = 0; i < methods.size(); ++i) {
        MethodNode method = methods.get(i);
        SimpleVerifier verifier = new SimpleVerifier(
                Type.getObjectType(cn.name), syperType, interfaces,
                (cn.access & Opcodes.ACC_INTERFACE) != 0);
        Analyzer<BasicValue> a = new Analyzer<BasicValue>(verifier);
        if (loader != null) {
            verifier.setClassLoader(loader);
        }
        try {
            a.analyze(cn.name, method);
            if (!dump) {
                continue;
            }
        } catch (Exception e) {
            e.printStackTrace(pw);
        }
        printAnalyzerResult(method, a, pw);
    }
    pw.flush();
}