Search in sources :

Example 1 with ClassReader

use of org.apache.tapestry5.internal.plastic.asm.ClassReader in project tapestry-5 by apache.

the class CheckClassAdapter method main.

/**
 * Checks the given class.
 *
 * @param args the command line arguments.
 * @param logger where to log errors.
 * @throws IOException if the class cannot be found, or if an IO exception occurs.
 */
static void main(final String[] args, final PrintWriter logger) throws IOException {
    if (args.length != 1) {
        logger.println(USAGE);
        return;
    }
    ClassReader classReader;
    if (args[0].endsWith(".class")) {
        // Can't fix PMD warning for 1.5 compatibility.
        try (InputStream inputStream = new FileInputStream(args[0])) {
            // NOPMD(AvoidFileStream)
            classReader = new ClassReader(inputStream);
        }
    } else {
        classReader = new ClassReader(args[0]);
    }
    verify(classReader, false, logger);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ClassReader(org.apache.tapestry5.internal.plastic.asm.ClassReader) FileInputStream(java.io.FileInputStream)

Example 2 with ClassReader

use of org.apache.tapestry5.internal.plastic.asm.ClassReader in project tapestry-5 by apache.

the class CheckClassAdapter method verify.

/**
 * 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(/*latest*/
    Opcodes.ASM10_EXPERIMENTAL, 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 (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();
}
Also used : ClassNode(org.apache.tapestry5.internal.plastic.asm.tree.ClassNode) AnalyzerException(org.apache.tapestry5.internal.plastic.asm.tree.analysis.AnalyzerException) SimpleVerifier(org.apache.tapestry5.internal.plastic.asm.tree.analysis.SimpleVerifier) ArrayList(java.util.ArrayList) Analyzer(org.apache.tapestry5.internal.plastic.asm.tree.analysis.Analyzer) BasicValue(org.apache.tapestry5.internal.plastic.asm.tree.analysis.BasicValue) Type(org.apache.tapestry5.internal.plastic.asm.Type) MethodNode(org.apache.tapestry5.internal.plastic.asm.tree.MethodNode)

Example 3 with ClassReader

use of org.apache.tapestry5.internal.plastic.asm.ClassReader in project tapestry-5 by apache.

the class AbstractReloadableObjectCreator method doClassLoad.

public Class<?> doClassLoad(String className) throws IOException {
    ClassVisitor analyzer = new ClassVisitor(Opcodes.ASM9) {

        @Override
        public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
            String path = superName + ".class";
            URL url = baseClassLoader.getResource(path);
            if (isFileURL(url)) {
                add(PlasticInternalUtils.toClassName(superName));
            }
        }

        @Override
        public void visitInnerClass(String name, String outerName, String innerName, int access) {
            // the internal name of the containing class.
            if (outerName == null || classesToLoad.contains(PlasticInternalUtils.toClassName(outerName))) {
                add(PlasticInternalUtils.toClassName(name));
            }
        }
    };
    String path = PlasticInternalUtils.toClassPath(className);
    InputStream stream = baseClassLoader.getResourceAsStream(path);
    assert stream != null;
    ByteArrayOutputStream classBuffer = new ByteArrayOutputStream(5000);
    byte[] buffer = new byte[5000];
    while (true) {
        int length = stream.read(buffer);
        if (length < 0) {
            break;
        }
        classBuffer.write(buffer, 0, length);
    }
    stream.close();
    byte[] bytecode = classBuffer.toByteArray();
    new ClassReader(new ByteArrayInputStream(bytecode)).accept(analyzer, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
    return loader.defineClassWithBytecode(className, bytecode);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ClassReader(org.apache.tapestry5.internal.plastic.asm.ClassReader) ClassVisitor(org.apache.tapestry5.internal.plastic.asm.ClassVisitor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URL(java.net.URL)

Example 4 with ClassReader

use of org.apache.tapestry5.internal.plastic.asm.ClassReader in project tapestry-5 by apache.

the class PlasticClassPool method readClassNode.

static ClassNode readClassNode(String className, ClassLoader classLoader) throws IOException {
    ClassNode classNode = new ClassNode();
    final String location = PlasticInternalUtils.toInternalName(className) + ".class";
    InputStream inputStream = classLoader.getResourceAsStream(location);
    BufferedInputStream bis = new BufferedInputStream(inputStream);
    ClassReader classReader = new ClassReader(inputStream);
    inputStream.close();
    bis.close();
    classReader.accept(classNode, 0);
    return classNode;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ClassReader(org.apache.tapestry5.internal.plastic.asm.ClassReader)

Example 5 with ClassReader

use of org.apache.tapestry5.internal.plastic.asm.ClassReader in project tapestry-5 by apache.

the class PlasticInternalUtils method convertBytecodeToClassNode.

public static ClassNode convertBytecodeToClassNode(byte[] bytecode) {
    ClassReader cr = new ClassReader(bytecode);
    ClassNode result = new ClassNode();
    ClassVisitor adapter = new ClassVisitor(Opcodes.ASM9, result) {

        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
            MethodVisitor delegate = super.visitMethod(access, name, desc, signature, exceptions);
            return new JSRInlinerAdapter(delegate, access, name, desc, signature, exceptions);
        }
    };
    cr.accept(adapter, 0);
    return result;
}
Also used : ClassNode(org.apache.tapestry5.internal.plastic.asm.tree.ClassNode) TraceClassVisitor(org.apache.tapestry5.internal.plastic.asm.util.TraceClassVisitor) JSRInlinerAdapter(org.apache.tapestry5.internal.plastic.asm.commons.JSRInlinerAdapter)

Aggregations

InputStream (java.io.InputStream)4 ClassReader (org.apache.tapestry5.internal.plastic.asm.ClassReader)4 FileInputStream (java.io.FileInputStream)2 ClassNode (org.apache.tapestry5.internal.plastic.asm.tree.ClassNode)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 ClassVisitor (org.apache.tapestry5.internal.plastic.asm.ClassVisitor)1 Type (org.apache.tapestry5.internal.plastic.asm.Type)1 JSRInlinerAdapter (org.apache.tapestry5.internal.plastic.asm.commons.JSRInlinerAdapter)1 MethodNode (org.apache.tapestry5.internal.plastic.asm.tree.MethodNode)1 Analyzer (org.apache.tapestry5.internal.plastic.asm.tree.analysis.Analyzer)1 AnalyzerException (org.apache.tapestry5.internal.plastic.asm.tree.analysis.AnalyzerException)1 BasicValue (org.apache.tapestry5.internal.plastic.asm.tree.analysis.BasicValue)1 SimpleVerifier (org.apache.tapestry5.internal.plastic.asm.tree.analysis.SimpleVerifier)1 TraceClassVisitor (org.apache.tapestry5.internal.plastic.asm.util.TraceClassVisitor)1