Java Code Examples for jdk.internal.org.objectweb.asm.ClassReader#accept()

The following examples show how to use jdk.internal.org.objectweb.asm.ClassReader#accept() . 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
Source File: CheckClassAdapter.java    From dragonwell8_jdk with GNU General Public License v2.0 6 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 2
Source File: TestInstrumentation.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public byte[] transform(
        ClassLoader classLoader, String className, Class<?> classBeingRedefined,
        ProtectionDomain pd, byte[] bytes) throws IllegalClassFormatException {
    // Check if this class should be instrumented.
    if (!instrClassesTarget.contains(className)) {
        return null;
    }

    boolean isRedefinition = classBeingRedefined != null;
    log("instrument class(" + className + ") " + (isRedefinition ? "redef" : "load"));

    ClassReader reader = new ClassReader(bytes);
    ClassWriter writer = new ClassWriter(
            reader, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    CallbackClassVisitor classVisitor = new CallbackClassVisitor(writer);
    reader.accept(classVisitor, 0);
    instrClassesDone.add(className);
    return writer.toByteArray();
}
 
Example 3
Source File: TestInstrumentation.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public byte[] transform(
        ClassLoader classLoader, String className, Class<?> classBeingRedefined,
        ProtectionDomain pd, byte[] bytes) throws IllegalClassFormatException {
    // Check if this class should be instrumented.
    if (!instrClassesTarget.contains(className)) {
        return null;
    }

    boolean isRedefinition = classBeingRedefined != null;
    log("instrument class(" + className + ") " + (isRedefinition ? "redef" : "load"));

    ClassReader reader = new ClassReader(bytes);
    ClassWriter writer = new ClassWriter(
            reader, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    CallbackClassVisitor classVisitor = new CallbackClassVisitor(writer);
    reader.accept(classVisitor, 0);
    instrClassesDone.add(className);
    return writer.toByteArray();
}
 
Example 4
Source File: CheckClassAdapter.java    From openjdk-8-source with 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 5
Source File: RedefineAnnotations.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public byte[] asm(ClassLoader loader, String className,
        Class<?> classBeingRedefined,
        ProtectionDomain protectionDomain, byte[] classfileBuffer)
    throws IllegalClassFormatException {

    ClassWriter cw = new ClassWriter(0);
    ClassVisitor cv = new ReAddDummyFieldsClassVisitor(ASM5, cw) { };
    ClassReader cr = new ClassReader(classfileBuffer);
    cr.accept(cv, 0);
    return cw.toByteArray();
}
 
Example 6
Source File: JImageTask.java    From openjdk-jdk9 with 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 7
Source File: ScriptClassInfoCollector.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * External entry point for ScriptClassInfoCollector if invoked from the command line
 * @param args argument vector, args contains a class for which to collect info
 * @throws IOException if there were problems parsing args or class
 */
public static void main(final String[] args) throws IOException {
    if (args.length != 1) {
        System.err.println("Usage: " + ScriptClassInfoCollector.class.getName() + " <class>");
        System.exit(1);
    }

    args[0] = args[0].replace('.', '/');
    final ScriptClassInfoCollector scic = new ScriptClassInfoCollector();
    try (final BufferedInputStream bis = new BufferedInputStream(new FileInputStream(args[0] + ".class"))) {
        final ClassReader reader = new ClassReader(bis);
        reader.accept(scic, 0);
    }
    final ScriptClassInfo sci = scic.getScriptClassInfo();
    final PrintStream out = System.out;
    if (sci != null) {
        out.println("script class: " + sci.getName());
        out.println("===================================");
        for (final MemberInfo memInfo : sci.getMembers()) {
            out.println("kind : " + memInfo.getKind());
            out.println("name : " + memInfo.getName());
            out.println("attributes: " + memInfo.getAttributes());
            out.println("javaName: " + memInfo.getJavaName());
            out.println("javaDesc: " + memInfo.getJavaDesc());
            out.println("where: " + memInfo.getWhere());
            out.println("=====================================");
        }
    } else {
        out.println(args[0] + " is not a @ScriptClass");
    }
}
 
Example 8
Source File: ScriptClassInfoCollector.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * External entry point for ScriptClassInfoCollector if invoked from the command line
 * @param args argument vector, args contains a class for which to collect info
 * @throws IOException if there were problems parsing args or class
 */
public static void main(final String[] args) throws IOException {
    if (args.length != 1) {
        System.err.println("Usage: " + ScriptClassInfoCollector.class.getName() + " <class>");
        System.exit(1);
    }

    args[0] = args[0].replace('.', '/');
    final ScriptClassInfoCollector scic = new ScriptClassInfoCollector();
    try (final BufferedInputStream bis = new BufferedInputStream(new FileInputStream(args[0] + ".class"))) {
        final ClassReader reader = new ClassReader(bis);
        reader.accept(scic, 0);
    }
    final ScriptClassInfo sci = scic.getScriptClassInfo();
    final PrintStream out = System.out;
    if (sci != null) {
        out.println("script class: " + sci.getName());
        out.println("===================================");
        for (final MemberInfo memInfo : sci.getMembers()) {
            out.println("kind : " + memInfo.getKind());
            out.println("name : " + memInfo.getName());
            out.println("attributes: " + memInfo.getAttributes());
            out.println("javaName: " + memInfo.getJavaName());
            out.println("javaDesc: " + memInfo.getJavaDesc());
            out.println("where: " + memInfo.getWhere());
            out.println("=====================================");
        }
    } else {
        out.println(args[0] + " is not a @ScriptClass");
    }
}
 
Example 9
Source File: ScriptClassInstrumentor.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * External entry point for ScriptClassInfoCollector if run from the command line
 *
 * @param args arguments - one argument is needed, the name of the class to collect info from
 *
 * @throws IOException if there are problems reading class
 */
public static void main(final String[] args) throws IOException {
    if (args.length != 1) {
        System.err.println("Usage: " + ScriptClassInstrumentor.class.getName() + " <class>");
        System.exit(1);
    }

    final String fileName = args[0].replace('.', '/') + ".class";
    final ScriptClassInfo sci = ClassGenerator.getScriptClassInfo(fileName);
    if (sci == null) {
        System.err.println("No @ScriptClass in " + fileName);
        System.exit(2);
        throw new AssertionError(); //guard against warning that sci is null below
    }

    try {
        sci.verify();
    } catch (final Exception e) {
        System.err.println(e.getMessage());
        System.exit(3);
    }

    final ClassWriter writer = ClassGenerator.makeClassWriter();
    try (final BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileName))) {
        final ClassReader reader = new ClassReader(bis);
        final CheckClassAdapter checker = new CheckClassAdapter(writer);
        final ScriptClassInstrumentor instr = new ScriptClassInstrumentor(checker, sci);
        reader.accept(instr, 0);
    }

    try (FileOutputStream fos = new FileOutputStream(fileName)) {
        fos.write(writer.toByteArray());
    }
}
 
Example 10
Source File: ConstructorTracerWriter.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static byte[] generateBytes(Class<?> clz, byte[] oldBytes) throws IOException {
    InputStream in = new ByteArrayInputStream(oldBytes);
    ClassReader cr = new ClassReader(in);
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    ConstructorTracerWriter ctw = new ConstructorTracerWriter(cw, clz);
    cr.accept(ctw, 0);
    return cw.toByteArray();
}
 
Example 11
Source File: CheckClassAdapter.java    From jdk8u-jdk with 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 12
Source File: CheckClassAdapter.java    From hottub with 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 13
Source File: ScriptClassInfoCollector.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * External entry point for ScriptClassInfoCollector if invoked from the command line
 * @param args argument vector, args contains a class for which to collect info
 * @throws IOException if there were problems parsing args or class
 */
public static void main(final String[] args) throws IOException {
    if (args.length != 1) {
        System.err.println("Usage: " + ScriptClassInfoCollector.class.getName() + " <class>");
        System.exit(1);
    }

    args[0] = args[0].replace('.', '/');
    final ScriptClassInfoCollector scic = new ScriptClassInfoCollector();
    try (final BufferedInputStream bis = new BufferedInputStream(new FileInputStream(args[0] + ".class"))) {
        final ClassReader reader = new ClassReader(bis);
        reader.accept(scic, 0);
    }
    final ScriptClassInfo sci = scic.getScriptClassInfo();
    final PrintStream out = System.out;
    if (sci != null) {
        out.println("script class: " + sci.getName());
        out.println("===================================");
        for (final MemberInfo memInfo : sci.getMembers()) {
            out.println("kind : " + memInfo.getKind());
            out.println("name : " + memInfo.getName());
            out.println("attributes: " + memInfo.getAttributes());
            out.println("javaName: " + memInfo.getJavaName());
            out.println("javaDesc: " + memInfo.getJavaDesc());
            out.println("where: " + memInfo.getWhere());
            out.println("=====================================");
        }
    } else {
        out.println(args[0] + " is not a @ScriptClass");
    }
}
 
Example 14
Source File: CheckClassAdapter.java    From openjdk-8 with 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 15
Source File: ScriptClassInstrumentor.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * External entry point for ScriptClassInfoCollector if run from the command line
 *
 * @param args arguments - one argument is needed, the name of the class to collect info from
 *
 * @throws IOException if there are problems reading class
 */
public static void main(final String[] args) throws IOException {
    if (args.length != 1) {
        System.err.println("Usage: " + ScriptClassInfoCollector.class.getName() + " <class>");
        System.exit(1);
    }

    final String fileName = args[0].replace('.', '/') + ".class";
    final ScriptClassInfo sci = ClassGenerator.getScriptClassInfo(fileName);
    if (sci == null) {
        System.err.println("No @ScriptClass in " + fileName);
        System.exit(2);
        throw new AssertionError(); //guard against warning that sci is null below
    }

    try {
        sci.verify();
    } catch (final Exception e) {
        System.err.println(e.getMessage());
        System.exit(3);
    }

    final ClassWriter writer = ClassGenerator.makeClassWriter();
    try (final BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileName))) {
        final ClassReader reader = new ClassReader(bis);
        final CheckClassAdapter checker = new CheckClassAdapter(writer);
        final ScriptClassInstrumentor instr = new ScriptClassInstrumentor(checker, sci);
        reader.accept(instr, 0);
    }

    try (FileOutputStream fos = new FileOutputStream(fileName)) {
        fos.write(writer.toByteArray());
    }
}
 
Example 16
Source File: RedefineAnnotations.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public byte[] asm(ClassLoader loader, String className,
        Class<?> classBeingRedefined,
        ProtectionDomain protectionDomain, byte[] classfileBuffer)
    throws IllegalClassFormatException {

    ClassWriter cw = new ClassWriter(0);
    ClassVisitor cv = new ReAddDummyFieldsClassVisitor(ASM5, cw) { };
    ClassReader cr = new ClassReader(classfileBuffer);
    cr.accept(cv, 0);
    return cw.toByteArray();
}
 
Example 17
Source File: ScriptClassInstrumentor.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * External entry point for ScriptClassInfoCollector if run from the command line
 *
 * @param args arguments - one argument is needed, the name of the class to collect info from
 *
 * @throws IOException if there are problems reading class
 */
public static void main(final String[] args) throws IOException {
    if (args.length != 1) {
        System.err.println("Usage: " + ScriptClassInstrumentor.class.getName() + " <class>");
        System.exit(1);
    }

    final String fileName = args[0].replace('.', '/') + ".class";
    final ScriptClassInfo sci = ClassGenerator.getScriptClassInfo(fileName);
    if (sci == null) {
        System.err.println("No @ScriptClass in " + fileName);
        System.exit(2);
        throw new AssertionError(); //guard against warning that sci is null below
    }

    try {
        sci.verify();
    } catch (final Exception e) {
        System.err.println(e.getMessage());
        System.exit(3);
    }

    final ClassWriter writer = ClassGenerator.makeClassWriter();
    try (final BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileName))) {
        final ClassReader reader = new ClassReader(bis);
        final CheckClassAdapter checker = new CheckClassAdapter(writer);
        final ScriptClassInstrumentor instr = new ScriptClassInstrumentor(checker, sci);
        reader.accept(instr, 0);
    }

    try (FileOutputStream fos = new FileOutputStream(fileName)) {
        fos.write(writer.toByteArray());
    }
}
 
Example 18
Source File: ClassGenerator.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
private static ScriptClassInfo getScriptClassInfo(final ClassReader reader) {
    final ScriptClassInfoCollector scic = new ScriptClassInfoCollector();
    reader.accept(scic, 0);
    return scic.getScriptClassInfo();
}
 
Example 19
Source File: ClassGenerator.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static ScriptClassInfo getScriptClassInfo(final ClassReader reader) {
    final ScriptClassInfoCollector scic = new ScriptClassInfoCollector();
    reader.accept(scic, 0);
    return scic.getScriptClassInfo();
}
 
Example 20
Source File: ClassGenerator.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
private static ScriptClassInfo getScriptClassInfo(final ClassReader reader) {
    final ScriptClassInfoCollector scic = new ScriptClassInfoCollector();
    reader.accept(scic, 0);
    return scic.getScriptClassInfo();
}