Search in sources :

Example 1 with MethodNode

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

the class Analyzer method computeInitialFrame.

/**
 * Computes the initial execution stack frame of the given method.
 *
 * @param owner the internal name of the class to which 'method' belongs.
 * @param method the method to be analyzed.
 * @return the initial execution stack frame of the 'method'.
 */
private Frame<V> computeInitialFrame(final String owner, final MethodNode method) {
    Frame<V> frame = newFrame(method.maxLocals, method.maxStack);
    int currentLocal = 0;
    boolean isInstanceMethod = (method.access & ACC_STATIC) == 0;
    if (isInstanceMethod) {
        Type ownerType = Type.getObjectType(owner);
        frame.setLocal(currentLocal, interpreter.newParameterValue(isInstanceMethod, currentLocal, ownerType));
        currentLocal++;
    }
    Type[] argumentTypes = Type.getArgumentTypes(method.desc);
    for (Type argumentType : argumentTypes) {
        frame.setLocal(currentLocal, interpreter.newParameterValue(isInstanceMethod, currentLocal, argumentType));
        currentLocal++;
        if (argumentType.getSize() == 2) {
            frame.setLocal(currentLocal, interpreter.newEmptyValue(currentLocal));
            currentLocal++;
        }
    }
    while (currentLocal < method.maxLocals) {
        frame.setLocal(currentLocal, interpreter.newEmptyValue(currentLocal));
        currentLocal++;
    }
    frame.setReturn(interpreter.newReturnTypeValue(Type.getReturnType(method.desc)));
    return frame;
}
Also used : Type(org.apache.tapestry5.internal.plastic.asm.Type)

Example 2 with MethodNode

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

the class CheckClassAdapter method printAnalyzerResult.

static void printAnalyzerResult(final MethodNode method, final Analyzer<BasicValue> analyzer, final PrintWriter printWriter) {
    Textifier textifier = new Textifier();
    TraceMethodVisitor traceMethodVisitor = new TraceMethodVisitor(textifier);
    printWriter.println(method.name + method.desc);
    for (int i = 0; i < method.instructions.size(); ++i) {
        method.instructions.get(i).accept(traceMethodVisitor);
        StringBuilder stringBuilder = new StringBuilder();
        Frame<BasicValue> frame = analyzer.getFrames()[i];
        if (frame == null) {
            stringBuilder.append('?');
        } else {
            for (int j = 0; j < frame.getLocals(); ++j) {
                stringBuilder.append(getUnqualifiedName(frame.getLocal(j).toString())).append(' ');
            }
            stringBuilder.append(" : ");
            for (int j = 0; j < frame.getStackSize(); ++j) {
                stringBuilder.append(getUnqualifiedName(frame.getStack(j).toString())).append(' ');
            }
        }
        while (stringBuilder.length() < method.maxStack + method.maxLocals + 1) {
            stringBuilder.append(' ');
        }
        printWriter.print(Integer.toString(i + 100000).substring(1));
        printWriter.print(" " + stringBuilder + " : " + textifier.text.get(textifier.text.size() - 1));
    }
    for (TryCatchBlockNode tryCatchBlock : method.tryCatchBlocks) {
        tryCatchBlock.accept(traceMethodVisitor);
        printWriter.print(" " + textifier.text.get(textifier.text.size() - 1));
    }
    printWriter.println();
}
Also used : TryCatchBlockNode(org.apache.tapestry5.internal.plastic.asm.tree.TryCatchBlockNode) BasicValue(org.apache.tapestry5.internal.plastic.asm.tree.analysis.BasicValue)

Example 3 with MethodNode

use of org.apache.tapestry5.internal.plastic.asm.tree.MethodNode 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 4 with MethodNode

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

the class MethodAdviceManager method createNewMethod.

private void createNewMethod() {
    String[] exceptions = advisedMethodNode.exceptions == null ? null : advisedMethodNode.exceptions.toArray(new String[0]);
    // Remove the private flag, so that the MethodInvocation implementation (in the same package)
    // can directly access the method without an additional access method.
    MethodNode mn = new MethodNode(advisedMethodNode.access & ~Opcodes.ACC_PRIVATE, newMethodName, advisedMethodNode.desc, advisedMethodNode.signature, exceptions);
    // Copy everything else about the advisedMethodNode over to the new node
    advisedMethodNode.accept(mn);
    // Add this new method, with the same implementation as the original method, to the
    // PlasticClass
    plasticClass.classNode.methods.add(mn);
}
Also used : MethodNode(org.apache.tapestry5.internal.plastic.asm.tree.MethodNode)

Example 5 with MethodNode

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

the class PlasticClassImpl method getJavaParameterTypes.

private static List<Class> getJavaParameterTypes(MethodNode methodNode) {
    final ClassLoader classLoader = PlasticInternalUtils.class.getClassLoader();
    Type[] parameterTypes = Type.getArgumentTypes(methodNode.desc);
    List<Class> list = new ArrayList<>();
    for (Type type : parameterTypes) {
        try {
            list.add(PlasticInternalUtils.toClass(classLoader, type.getClassName()));
        } catch (ClassNotFoundException e) {
            // shouldn't happen anyway
            throw new RuntimeException(e);
        }
    }
    return list;
}
Also used : Type(org.apache.tapestry5.internal.plastic.asm.Type)

Aggregations

MethodNode (org.apache.tapestry5.internal.plastic.asm.tree.MethodNode)10 Type (org.apache.tapestry5.internal.plastic.asm.Type)4 ArrayList (java.util.ArrayList)2 AbstractInsnNode (org.apache.tapestry5.internal.plastic.asm.tree.AbstractInsnNode)2 IincInsnNode (org.apache.tapestry5.internal.plastic.asm.tree.IincInsnNode)2 TryCatchBlockNode (org.apache.tapestry5.internal.plastic.asm.tree.TryCatchBlockNode)2 VarInsnNode (org.apache.tapestry5.internal.plastic.asm.tree.VarInsnNode)2 BasicValue (org.apache.tapestry5.internal.plastic.asm.tree.analysis.BasicValue)2 HashMap (java.util.HashMap)1 List (java.util.List)1 AnnotationVisitor (org.apache.tapestry5.internal.plastic.asm.AnnotationVisitor)1 ClassNode (org.apache.tapestry5.internal.plastic.asm.tree.ClassNode)1 InsnList (org.apache.tapestry5.internal.plastic.asm.tree.InsnList)1 JumpInsnNode (org.apache.tapestry5.internal.plastic.asm.tree.JumpInsnNode)1 LabelNode (org.apache.tapestry5.internal.plastic.asm.tree.LabelNode)1 LookupSwitchInsnNode (org.apache.tapestry5.internal.plastic.asm.tree.LookupSwitchInsnNode)1 TableSwitchInsnNode (org.apache.tapestry5.internal.plastic.asm.tree.TableSwitchInsnNode)1 Analyzer (org.apache.tapestry5.internal.plastic.asm.tree.analysis.Analyzer)1 AnalyzerException (org.apache.tapestry5.internal.plastic.asm.tree.analysis.AnalyzerException)1 SimpleVerifier (org.apache.tapestry5.internal.plastic.asm.tree.analysis.SimpleVerifier)1