Java Code Examples for org.objectweb.asm.tree.MethodNode#accept()
The following examples show how to use
org.objectweb.asm.tree.MethodNode#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: AsmTestUtils.java From grappa with Apache License 2.0 | 7 votes |
public static String getMethodInstructionList(final MethodNode methodNode) { Preconditions.checkNotNull(methodNode, "methodNode"); final Printer printer = new NonMaxTextifier(); final TraceMethodVisitor traceMethodVisitor = new TraceMethodVisitor(printer); methodNode.accept(traceMethodVisitor); final StringWriter stringWriter = new StringWriter(); final PrintWriter printWriter = new PrintWriter(stringWriter); printer.print(printWriter); printWriter.flush(); final String[] lines = PATTERN.split(stringWriter.toString()); int lineNr = 0; for (int i = 0; i < lines.length; i++) { if (!lines[i].startsWith(" @")) { lines[i] = String.format("%2d %s", lineNr++, lines[i]); } } return "Method '" + methodNode.name + "':\n" + NEWLINE.join(lines) + '\n'; }
Example 2
Source File: MethodHasher.java From coroutines with GNU Lesser General Public License v3.0 | 6 votes |
private static byte[] dumpBytecode(MethodNode methodNode) { // Calculate label offsets -- required for hash calculation // we only care about where the labels are in relation to the opcode instructions -- we don't care about things like // LocalVariableNode or other ancillary data because these can change without the actual logic changing List<AbstractInsnNode> onlyInstructionsAndLabels = Arrays.stream(methodNode.instructions.toArray()) .filter(x -> x instanceof LabelNode || x.getOpcode() != -1) .collect(Collectors.toList()); Map<Label, Integer> labelOffsets = onlyInstructionsAndLabels.stream() .filter(x -> x instanceof LabelNode) .map(x -> (LabelNode) x) .collect(Collectors.toMap(x -> x.getLabel(), x -> onlyInstructionsAndLabels.indexOf(x))); // Hash based on overall structures and instructions+operands try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream daos = new DataOutputStream(baos);) { MethodVisitor daosDumpMethodVisitor = new DumpToDaosMethodVisitor(daos, labelOffsets); methodNode.accept(daosDumpMethodVisitor); daos.flush(); // doesn't really need it -- just incase return baos.toByteArray(); } catch (IOException ioe) { throw new IllegalStateException(ioe); // should never happen } }
Example 3
Source File: MCStripTransformer.java From CodeChickenLib with GNU Lesser General Public License v2.1 | 6 votes |
public static byte[] transform(byte[] bytes) { ClassNode cnode = ASMHelper.createClassNode(bytes, ClassReader.EXPAND_FRAMES); boolean changed = false; Iterator<MethodNode> it = cnode.methods.iterator(); while(it.hasNext()) { MethodNode mnode = it.next(); ReferenceDetector r = new ReferenceDetector(); mnode.accept(new RemappingMethodAdapter(mnode.access, mnode.desc, new MethodVisitor(Opcodes.ASM4) {}, r)); if(r.found) { it.remove(); changed = true; } } if(changed) bytes = ASMHelper.createBytes(cnode, 0); return bytes; }
Example 4
Source File: MCStripTransformer.java From CodeChickenLib with GNU Lesser General Public License v2.1 | 6 votes |
public static byte[] transform(byte[] bytes) { ClassNode cnode = ASMHelper.createClassNode(bytes, ClassReader.EXPAND_FRAMES); boolean changed = false; Iterator<MethodNode> it = cnode.methods.iterator(); while (it.hasNext()) { MethodNode mnode = it.next(); ReferenceDetector r = new ReferenceDetector(); mnode.accept(new RemappingMethodAdapter(mnode.access, mnode.desc, new MethodVisitor(Opcodes.ASM4) { }, r)); if (r.found) { it.remove(); changed = true; } } if (changed) { bytes = ASMHelper.createBytes(cnode, 0); } return bytes; }
Example 5
Source File: ByteCodeUtils.java From Stark with Apache License 2.0 | 5 votes |
/** * Converts the given method to a String. */ public static String textify(@NonNull MethodNode method) { Textifier textifier = new Textifier(); TraceMethodVisitor trace = new TraceMethodVisitor(textifier); method.accept(trace); String ret = ""; for (Object line : textifier.getText()) { ret += line; } return ret; }
Example 6
Source File: AsmTestUtils.java From grappa with Apache License 2.0 | 5 votes |
public static void assertTraceDumpEquality( final MethodNode method, final String traceDump) throws Exception { Preconditions.checkNotNull(method, "method"); final Printer printer = new NonMaxTextifier(); final TraceMethodVisitor traceMethodVisitor = new TraceMethodVisitor(printer); // MethodAdapter checkMethodAdapter = new MethodAdapter(traceMethodVisitor); final MethodVisitor checkMethodAdapter = new CheckMethodAdapter(traceMethodVisitor); method.accept(checkMethodAdapter); final StringWriter stringWriter = new StringWriter(); final PrintWriter printWriter = new PrintWriter(stringWriter); printer.print(printWriter); printWriter.flush(); assertEquals(stringWriter.toString(), traceDump); }
Example 7
Source File: ImportantInsnVisitor.java From CodeChickenLib with GNU Lesser General Public License v2.1 | 5 votes |
@Override public void visitEnd() { super.visitEnd(); MethodNode mnode = (MethodNode)mv; mnode.instructions = InsnComparator.getImportantList(mnode.instructions); mnode.accept(delegate); }
Example 8
Source File: MergeAdapter.java From JReFrameworker with MIT License | 5 votes |
/** * Adds the method to the base class * @param methodNode */ private void addMethod(MethodNode methodNode){ String[] exceptions = new String[methodNode.exceptions.size()]; methodNode.exceptions.toArray(exceptions); MethodVisitor mv = cv.visitMethod(methodNode.access, methodNode.name, methodNode.desc, methodNode.signature, exceptions); methodNode.instructions.resetLabels(); // SimpleRemapper -> maps old name to new name // updates owners and descriptions appropriately methodNode.accept(new RemappingMethodAdapter(methodNode.access, methodNode.desc, mv, new SimpleRemapper(classToMerge.name, baseClassName))); }
Example 9
Source File: MergeAdapter.java From JReFrameworker with MIT License | 5 votes |
/** * Adds the method to the base class * @param methodNode */ private void addMethod(MethodNode methodNode){ String[] exceptions = new String[methodNode.exceptions.size()]; methodNode.exceptions.toArray(exceptions); MethodVisitor mv = cv.visitMethod(methodNode.access, methodNode.name, methodNode.desc, methodNode.signature, exceptions); methodNode.instructions.resetLabels(); // SimpleRemapper -> maps old name to new name // updates owners and descriptions appropriately methodNode.accept(new RemappingMethodAdapter(methodNode.access, methodNode.desc, mv, new SimpleRemapper(classToMerge.name, baseClassName))); }
Example 10
Source File: ASMHelper.java From NOVA-Core with GNU Lesser General Public License v3.0 | 5 votes |
public static void replaceMethodCode(MethodNode original, MethodNode replacement) { original.instructions.clear(); if (original.localVariables != null) { original.localVariables.clear(); } if (original.tryCatchBlocks != null) { original.tryCatchBlocks.clear(); } replacement.accept(original); }
Example 11
Source File: ASMHelper.java From NOVA-Core with GNU Lesser General Public License v3.0 | 5 votes |
public static void replaceMethodCode(MethodNode original, MethodNode replacement) { original.instructions.clear(); if (original.localVariables != null) { original.localVariables.clear(); } if (original.tryCatchBlocks != null) { original.tryCatchBlocks.clear(); } replacement.accept(original); }
Example 12
Source File: ASMHelper.java From NOVA-Core with GNU Lesser General Public License v3.0 | 5 votes |
public static void replaceMethodCode(MethodNode original, MethodNode replacement) { original.instructions.clear(); if (original.localVariables != null) { original.localVariables.clear(); } if (original.tryCatchBlocks != null) { original.tryCatchBlocks.clear(); } replacement.accept(original); }
Example 13
Source File: IncrementalSupportVisitor.java From Aceso with Apache License 2.0 | 5 votes |
@Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { AcesoProguardMap.instance().putMethod(visitedClassName, IncrementalTool.getMtdSig(name, desc)); access = IncrementalTool.transformAccessForInstantRun(access); MethodVisitor defaultVisitor = super.visitMethod(access, name, desc, signature, exceptions); MethodNode method = getMethodByNameInClass(name, desc, classNode); // does the method use blacklisted APIs. boolean hasIncompatibleChange = InstantRunMethodVerifier.verifyMethod(method); if (hasIncompatibleChange || disableRedirectionForClass || !isAccessCompatibleWithInstantRun(access) || name.equals(ByteCodeUtils.CLASS_INITIALIZER)) { return defaultVisitor; } else { ArrayList<Type> args = new ArrayList<Type>(Arrays.asList(Type.getArgumentTypes(desc))); boolean isStatic = (access & Opcodes.ACC_STATIC) != 0; if (!isStatic) { args.add(0, Type.getType(Object.class)); } ISMethodVisitor mv = new ISMethodVisitor(defaultVisitor, access, name, desc); if (name.equals(ByteCodeUtils.CONSTRUCTOR)) { } else { mv.addRedirection(new MethodRedirection( new LabelNode(mv.getStartLabel()), visitedClassName, name, desc, args, Type.getReturnType(desc), isStatic)); } method.accept(mv); return null; } }
Example 14
Source File: StarkMethodVerifier.java From Stark with Apache License 2.0 | 5 votes |
/** * Verifies a method implementation against the blacklisted list of APIs. * @param method the method to verify * @return a {@link StarkVerifierStatus} instance or null if the method is not making any * blacklisted calls. */ @NonNull public static StarkVerifierStatus verifyMethod(MethodNode method) { VerifierMethodVisitor mv = new VerifierMethodVisitor(method); method.accept(mv); return mv.incompatibleChange.or(StarkVerifierStatus.COMPATIBLE); }
Example 15
Source File: BytecodeTraceUtil.java From tascalate-async-await with BSD 2-Clause "Simplified" License | 4 votes |
public static String toString(MethodNode mn) { Textifier t = new Textifier(); TraceMethodVisitor tmv = new TraceMethodVisitor(t); mn.accept(tmv); return t.toString(); }
Example 16
Source File: IncrementalSupportVisitor.java From AnoleFix with MIT License | 4 votes |
/** * Insert Constructor specific logic({@link ConstructorArgsRedirection} and * {@link ConstructorDelegationDetector}) for constructor redirecting or * normal method redirecting ({@link MethodRedirection}) for other methods. */ @Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { access = transformAccessForInstantRun(access); MethodVisitor defaultVisitor = super.visitMethod(access, name, desc, signature, exceptions); MethodNode method = getMethodByNameInClass(name, desc, classNode); // does the method use blacklisted APIs. boolean hasIncompatibleChange = InstantRunMethodVerifier.verifyMethod(method) != InstantRunVerifierStatus.COMPATIBLE; if (hasIncompatibleChange || disableRedirectionForClass || !isAccessCompatibleWithInstantRun(access) || name.equals(AsmUtils.CLASS_INITIALIZER)) { return defaultVisitor; } else { ISMethodVisitor mv = new ISMethodVisitor(defaultVisitor, access, name, desc); if (name.equals(AsmUtils.CONSTRUCTOR)) { ConstructorDelegationDetector.Constructor constructor = ConstructorDelegationDetector.deconstruct(visitedClassName, method); LabelNode start = new LabelNode(); LabelNode after = new LabelNode(); method.instructions.insert(constructor.loadThis, start); if (constructor.lineForLoad != -1) { // Record the line number from the start of LOAD_0 for uninitialized 'this'. // This allows a breakpoint to be set at the line with this(...) or super(...) // call in the constructor. method.instructions.insert(constructor.loadThis, new LineNumberNode(constructor.lineForLoad, start)); } method.instructions.insert(constructor.delegation, after); mv.addRedirection( new ConstructorArgsRedirection( start, visitedClassName, constructor.args.name + "." + constructor.args.desc, after, Type.getArgumentTypes(constructor.delegation.desc))); mv.addRedirection(new MethodRedirection(after, constructor.body.name + "." + constructor.body.desc, Type.getReturnType(desc))); } else { mv.addRedirection(new MethodRedirection( new LabelNode(mv.getStartLabel()), name + "." + desc, Type.getReturnType(desc))); } method.accept(mv); return null; } }
Example 17
Source File: InstantRunMethodVerifier.java From Aceso with Apache License 2.0 | 4 votes |
/** * Verifies a method implementation against the blacklisted list of APIs. */ public static boolean verifyMethod(MethodNode method) { VerifierMethodVisitor mv = new VerifierMethodVisitor(method); method.accept(mv); return (mv.incompatibleChange == InstantRunVerifierStatus.INCOMPATIBLE); }
Example 18
Source File: RedirectionVisitor.java From Stark with Apache License 2.0 | 4 votes |
/** * Insert Constructor specific logic({@link ConstructorRedirection} and * {@link ConstructorBuilder}) for constructor redirecting or * normal method redirecting ({@link MethodRedirection}) for other methods. */ @Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { access = transformAccessForStark(access); MethodVisitor defaultVisitor = super.visitMethod(access, name, desc, signature, exceptions); MethodNode method = checkNotNull( getMethodByNameInClass(name, desc, classAndInterfaceNode), "Method found by visitor but not in the pre-parsed class node."); // does the method use blacklisted APIs. boolean hasIncompatibleChange = StarkMethodVerifier.verifyMethod(method) != StarkVerifierStatus.COMPATIBLE; if (hasIncompatibleChange || disableRedirectionForClass || !isAccessCompatibleWithStark(access)) { return defaultVisitor; } if (name.equals(ByteCodeUtils.CLASS_INITIALIZER)) { classInitializerAdded = true; return isInterface ? new ISInterfaceStaticInitializerMethodVisitor( defaultVisitor, access, name, desc) : defaultVisitor; } ArrayList<Type> args = new ArrayList<>(Arrays.asList(Type.getArgumentTypes(desc))); boolean isStatic = (access & Opcodes.ACC_STATIC) != 0; if (!isStatic) { args.add(0, Type.getType(Object.class)); } // Install the Jsr/Ret inliner adapter, we have had reports of code still using the // Jsr/Ret deprecated byte codes. // see https://code.google.com/p/android/issues/detail?id=220019 JSRInlinerAdapter jsrInlinerAdapter = new JSRInlinerAdapter(defaultVisitor, access, name, desc, signature, exceptions); ISAbstractMethodVisitor mv = isInterface ? new ISDefaultMethodVisitor(jsrInlinerAdapter, access, name, desc) : new ISMethodVisitor(jsrInlinerAdapter, access, name, desc); if (name.equals(ByteCodeUtils.CONSTRUCTOR)) { if ((access & Opcodes.ACC_SYNTHETIC) != 0 || ByteCodeUtils.isAnnotatedWith(method, "Lkotlin/jvm/JvmOverloads;")) { return defaultVisitor; } Constructor constructor = ConstructorBuilder.build(visitedClassName, method); LabelNode start = new LabelNode(); method.instructions.insert(constructor.loadThis, start); if (constructor.lineForLoad != -1) { // Record the line number from the start of LOAD_0 for uninitialized 'this'. // This allows a breakpoint to be set at the line with this(...) or super(...) // call in the constructor. method.instructions.insert( constructor.loadThis, new LineNumberNode(constructor.lineForLoad, start)); } mv.addRedirection(new ConstructorRedirection(start, constructor, args)); } else { mv.addRedirection( new MethodRedirection( new LabelNode(mv.getStartLabel()), name + "." + desc, args, Type.getReturnType(desc))); } method.accept(mv); return null; }
Example 19
Source File: MergeAdapter.java From Bats with Apache License 2.0 | 4 votes |
@Override public void visitEnd() { // add all the fields of the class we're going to merge. for (Iterator<?> it = classToMerge.fields.iterator(); it.hasNext();) { // Special handling for nested classes. Drill uses non-static nested // "inner" classes in some templates. Prior versions of Drill would // create the generated nested classes as static, then this line // would copy the "this$0" field to convert the static nested class // into a non-static inner class. However, that approach is not // compatible with plain-old Java compilation. Now, Drill generates // the nested classes as non-static inner classes. As a result, we // do not want to copy the hidden fields; we'll end up with two if // we do. FieldNode field = (FieldNode) it.next(); if (! field.name.startsWith("this$")) { field.accept(this); } } // add all the methods that we to include. for (Iterator<?> it = classToMerge.methods.iterator(); it.hasNext();) { MethodNode mn = (MethodNode) it.next(); if (mn.name.equals("<init>")) { continue; } String[] exceptions = new String[mn.exceptions.size()]; mn.exceptions.toArray(exceptions); MethodVisitor mv = cv.visitMethod(mn.access | Modifier.FINAL, mn.name, mn.desc, mn.signature, exceptions); if (verifyBytecode) { mv = new CheckMethodVisitorFsm(api, mv); } mn.instructions.resetLabels(); // mn.accept(new RemappingMethodAdapter(mn.access, mn.desc, mv, new // SimpleRemapper("org.apache.drill.exec.compile.ExampleTemplate", "Bunky"))); ClassSet top = set; while (top.parent != null) { top = top.parent; } mn.accept(new RemappingMethodAdapter(mn.access, mn.desc, mv, new SimpleRemapper(top.precompiled.slash, top.generated.slash))); } super.visitEnd(); }
Example 20
Source File: InstantRunMethodVerifier.java From AnoleFix with MIT License | 3 votes |
/** * Verifies a method implementation against the blacklisted list of APIs. * * @param method the method to verify * @return a {@link InstantRunVerifierStatus} instance or null if the method is not making any * blacklisted calls. */ public static InstantRunVerifierStatus verifyMethod(MethodNode method) { VerifierMethodVisitor mv = new VerifierMethodVisitor(method); method.accept(mv); return mv.incompatibleChange.or(InstantRunVerifierStatus.COMPATIBLE); }