List of usage examples for org.objectweb.asm.tree AbstractInsnNode getNext
public AbstractInsnNode getNext()
From source file:org.jooby.internal.apitool.BytecodeRouteParser.java
License:Apache License
private java.lang.reflect.Type parameterType(final ClassLoader loader, final AbstractInsnNode n) { if (n instanceof MethodInsnNode) { MethodInsnNode node = (MethodInsnNode) n; if (mutantValue().test(node)) { /** value(); intValue(); booleanValue(); */ return TypeDescriptorParser.parse(loader, node.desc); } else if (mutantToSomething().test(node) || getOrCreateKotlinClass().test(node)) { /** to(String.class); toOptional; toList(); */ String owner = Type.getReturnType(node.desc).getClassName(); AbstractInsnNode prev = node.getPrevious(); if (prev instanceof FieldInsnNode && ((MethodInsnNode) n).name.equals("toEnum")) { /** toEnum(Letter.A); */ return loadType(loader, ((FieldInsnNode) prev).owner); }/* www . j ava 2 s . c o m*/ java.lang.reflect.Type toType = String.class; if (prev instanceof LdcInsnNode) { /** to(Foo.class); */ Object cst = ((LdcInsnNode) prev).cst; if (cst instanceof Type) { toType = loadType(loader, ((Type) cst).getClassName()); } } else if (prev instanceof FieldInsnNode) { toType = loadType(loader, ((FieldInsnNode) prev).owner); } // JoobyKt.toOptional AbstractInsnNode next = node.getNext(); if (next instanceof MethodInsnNode) { String joobyKt = ((MethodInsnNode) next).owner; String methodName = ((MethodInsnNode) next).name; if ("toOptional".equals(methodName) && "org/jooby/JoobyKt".equals(joobyKt)) { owner = Optional.class.getName(); } } Set<String> skipOwners = ImmutableSet.of(Object.class.getName(), Enum.class.getName(), "kotlin.reflect.KClass"); if (skipOwners.contains(owner)) { return toType; } /** toList(Foo.class); */ return Types.newParameterizedType(loadType(loader, owner), toType); } } else if (n instanceof VarInsnNode) { return new Insn<>(null, n).prev().filter(is(MethodInsnNode.class)).findFirst() .map(MethodInsnNode.class::cast).filter(file()).map(m -> { return m.name.equals("files") ? Types.newParameterizedType(List.class, File.class) : File.class; }).orElse(Object.class); } else if (n instanceof TypeInsnNode) { TypeInsnNode typeInsn = (TypeInsnNode) n; if (typeInsn.getOpcode() == Opcodes.CHECKCAST) { return loadType(loader, typeInsn.desc); } } else if (n != null && Opcodes.DUP == n.getOpcode()) { // Kotlin 1.2.x // mv.visitInsn(DUP); // mv.visitLdcInsn("req.param(\"p1\")"); // mv.visitMethodInsn(INVOKESTATIC, "kotlin/jvm/internal/Intrinsics", "checkExpressionValueIsNotNull", "(Ljava/lang/Object;Ljava/lang/String;)V", false); // mv.visitMethodInsn(INVOKESTATIC, "org/jooby/JoobyKt", "getValue", "(Lorg/jooby/Mutant;)Ljava/lang/String;", false); AbstractInsnNode next = new Insn<>(null, n).next().filter(MethodInsnNode.class::isInstance).skip(1) .findFirst().orElse(null); java.lang.reflect.Type result = parameterType(loader, next); if (result == Object.class) { next = new Insn<>(null, n).next().filter(TypeInsnNode.class::isInstance).findFirst().orElse(null); result = parameterType(loader, next); } return result; } else if (n instanceof FieldInsnNode) { AbstractInsnNode next = n.getNext(); if (next instanceof MethodInsnNode) { if (((MethodInsnNode) next).name.equals("toOptional")) { return Types.newParameterizedType(Optional.class, loadType(loader, ((FieldInsnNode) n).owner)); } else if (((MethodInsnNode) next).name.equals("getOrCreateKotlinClass")) { return loadType(loader, ((FieldInsnNode) n).owner); } } } return Object.class; }
From source file:org.jooby.internal.apitool.Insn.java
License:Apache License
public static List<LdcInsnNode> ldcFor(MethodInsnNode node) { AbstractInsnNode it = new Insn<>(null, node.getPrevious()).prev().filter(is(MethodInsnNode.class)) .findFirst().orElseGet(() -> Insn.first(node)); /** Collect all ldc between a previous method invocation and current invocation: */ List<LdcInsnNode> ldc = new ArrayList<>(); while (it != node) { if (it instanceof LdcInsnNode) { ldc.add((LdcInsnNode) it);//from w w w .j a va2s. com } it = it.getNext(); } return ldc; }
From source file:org.parboiled.transform.process.SuperCallRewriter.java
License:Apache License
@Override public void process(@Nonnull final ParserClassNode classNode, @Nonnull final RuleMethod method) throws Exception { Objects.requireNonNull(classNode, "classNode"); Objects.requireNonNull(method, "method"); final InsnList instructions = method.instructions; AbstractInsnNode insn = instructions.getFirst(); while (insn.getOpcode() != ARETURN) { if (insn.getOpcode() == INVOKESPECIAL) process(classNode, method, (MethodInsnNode) insn); insn = insn.getNext(); }//from w w w. j ava2 s. com }
From source file:org.parboiled.transform.process.UnusedLabelsRemover.java
License:Apache License
@Override public void process(@Nonnull final ParserClassNode classNode, @Nonnull final RuleMethod method) throws Exception { Objects.requireNonNull(classNode, "classNode"); Objects.requireNonNull(method, "method"); AbstractInsnNode current = method.instructions.getFirst(); AbstractInsnNode next;/*w ww. j av a2 s. c o m*/ boolean doRemove; while (current != null) { next = current.getNext(); //noinspection SuspiciousMethodCalls doRemove = current.getType() == AbstractInsnNode.LABEL && !method.getUsedLabels().contains(current); if (doRemove) method.instructions.remove(current); current = next; } }
From source file:org.spoofax.interpreter.adapter.asm.ASMFactory.java
License:LGPL
private static void dumpChain(AbstractInsnNode node) { if (node == null) return;/*from w w w . j a v a 2 s . c om*/ do { System.out.println(node.getClass().getName()); node = node.getNext(); } while (node != null); }
From source file:org.summer.aop.ltw.AspectWeaver.java
License:Open Source License
@SuppressWarnings("unchecked") private ClassNode mergeClassHierarchy(String className, List<String> interfaces) { ClassNode classNode = null;/*from w w w. ja va2s.c o m*/ try { ClassReader cr = new ClassReader(className); cr.accept(classNode = new ClassNode(), ClassReader.SKIP_DEBUG + ClassReader.SKIP_FRAMES); if (!classNode.superName.equals("java/lang/Object")) { boolean flag; ClassNode superClassNode = mergeClassHierarchy(classNode.superName, interfaces); for (Object superMethodNode : superClassNode.methods) { flag = false; MethodNode smn = (MethodNode) superMethodNode; for (Object methodNode : classNode.methods) { MethodNode mn = (MethodNode) methodNode; if (mn.name.equals(smn.name) && mn.desc.equals(smn.desc)) { flag = true; switch (mn.name) { case "<clinit>": mergeInitializingInstructions(superClassNode.name, smn.maxStack, smn.maxLocals, classNode.name, mn, smn.instructions.getFirst()); break; case "<init>": AbstractInsnNode insn = smn.instructions.getFirst(); while (!(insn instanceof MethodInsnNode) || !((MethodInsnNode) insn).owner.equals(superClassNode.superName) || ((MethodInsnNode) insn).getOpcode() != INVOKESPECIAL || !((MethodInsnNode) insn).name.equals("<init>")) { insn = insn.getNext(); } mergeInitializingInstructions(superClassNode.name, smn.maxStack, smn.maxLocals, classNode.name, mn, insn.getNext()); break; default: break; } break; } } if (!flag) { reviseMethodNode(superClassNode, smn, classNode.name); classNode.methods.add(smn); } } } for (Object inter : classNode.interfaces) { if (!interfaces.contains(inter)) interfaces.add((String) inter); } } catch (IOException ex) { ex.printStackTrace(); } return classNode; }
From source file:org.summer.aop.ltw.AspectWeaver.java
License:Open Source License
private void mergeInitializingInstructions(String superClassName, int maxStack, int maxLocals, String className, MethodNode methodNode, AbstractInsnNode firstSuperInsn) { InsnList superInsnList = new InsnList(); while (!(firstSuperInsn instanceof InsnNode) || (((InsnNode) firstSuperInsn).getOpcode() != RETURN && ((InsnNode) firstSuperInsn).getOpcode() != ATHROW)) { if (firstSuperInsn instanceof MethodInsnNode && ((MethodInsnNode) firstSuperInsn).owner.equals(superClassName)) ((MethodInsnNode) firstSuperInsn).owner = className; else if (firstSuperInsn instanceof FieldInsnNode && ((FieldInsnNode) firstSuperInsn).owner.equals(superClassName)) ((FieldInsnNode) firstSuperInsn).owner = className; superInsnList.add(firstSuperInsn); firstSuperInsn = firstSuperInsn.getNext(); }/*from www .j a v a2 s . c o m*/ firstSuperInsn = methodNode.instructions.getFirst(); while (!(firstSuperInsn instanceof MethodInsnNode) || !((MethodInsnNode) firstSuperInsn).owner.equals(superClassName) || ((MethodInsnNode) firstSuperInsn).getOpcode() != INVOKESPECIAL || !((MethodInsnNode) firstSuperInsn).name.equals("<init>")) { firstSuperInsn = firstSuperInsn.getNext(); } firstSuperInsn = firstSuperInsn.getNext(); methodNode.instructions.insertBefore(firstSuperInsn, superInsnList); methodNode.maxStack = Math.max(methodNode.maxStack, maxStack); methodNode.maxLocals = Math.max(methodNode.maxLocals, maxLocals); for (Iterator<?> it = methodNode.localVariables.iterator(); it.hasNext();) { LocalVariableNode localVar = (LocalVariableNode) it.next(); if (localVar.desc.equals("L" + superClassName + ";")) localVar.desc = "L" + className + ";"; } }
From source file:org.summer.aop.ltw.AspectWeaver.java
License:Open Source License
/** * Displaces the original method body to a corresponding private redirection method. However, if the visited class * has been mocked and the mock class overwrites this method, the original method body will be replaced entirely by * the corresponding mock method body./*from w w w . ja v a2s .com*/ * <p/> * Note: As constructors are nothing else than methods, they are also taken into account. * * @param access the original method access * @param name the original method name * @param desc the original method description * @param signature the original method signature * @param exceptions the original exceptions * @return a method visitor that points to the private redirected method */ @Override public MethodVisitor visitMethod(int access, final String name, final String desc, String signature, String[] exceptions) { // If this class should be replaced, skip all original methods if (replacingClassNode != null && !isVisitEndReached) return null; // If this class should be merged and this method isn't the static initializer, and a corresponding merging method is defined, merge their exceptions if (mergingClassNode != null && !name.equals("<clinit>")) { for (Object methodNode : mergingClassNode.methods) { MethodNode mn = (MethodNode) methodNode; if (mn.name.equals(name) && mn.desc.equals(desc)) { List<String> mergedExceptions = exceptions == null ? new ArrayList<String>() : Arrays.asList(exceptions); for (Object e : mn.exceptions) { String ex = (String) e; if (!mergedExceptions.contains(ex)) mergedExceptions.add(ex); } exceptions = mergedExceptions.toArray(new String[mergedExceptions.size()]); break; } } } MethodVisitor mv; //Currently ALL constructors and methods will be transformed for retransformation purposes, because //the Java Instrumentation API doesn't yet allow the addition of new methods (or constructors). //If future Java versions cancel this restriction you can safely enable the following three lines in place of //the three lines after next! // List<Aspect> matchingAspects = getMatchingAspects(access,name,desc,signature,exceptions); // if(!isInterface && !matchingAspects.isEmpty()) // { if (!isInterface && !name.equals("<clinit>")) { List<Aspect> matchingAspects = getMatchingAspects(access, name, desc, signature, exceptions); if (name.equals("<init>")) { // Transform the constructor and add a constructor redirected method mv = new ConstructorSplitter(className, cv, access, ++constructorCounter, desc, signature, exceptions, matchingAspects); } else { // Rename original non-constructor methods mv = cv.visitMethod(toPrivateAccess(access), AOPContext.toRedirectedMethodName(name, -1), desc, signature, exceptions); } // Remember the intercepted original method details interceptedMethodInfos.add(new AspectWeaver.MethodInfo(access, name, name.equals("<init>") ? constructorCounter : -1, desc, signature, exceptions, matchingAspects)); // If this class should be merged... if (mergingClassNode != null) { if (name.equals("<init>")) { visitedMergingClassMethods.add(name + desc); // Merge the original constructor body with ALL those defined in the merging class return new AdviceAdapter(ASM4, mv, access, "<init>", desc) { @Override protected void onMethodExit(int opcode) { for (Object methodNode : mergingClassNode.methods) { MethodNode mn = (MethodNode) methodNode; if (mn.name.equals("<init>")) { // Insert everything between the super call invocation and RETURN or ATHROW instruction AbstractInsnNode insn = mn.instructions.getFirst(); while (!(insn instanceof MethodInsnNode) || !((MethodInsnNode) insn).owner.equals(mergingClassNode.superName) || ((MethodInsnNode) insn).getOpcode() != INVOKESPECIAL || !((MethodInsnNode) insn).name.equals("<init>")) { insn = insn.getNext(); } insn = insn.getNext(); while (!(insn instanceof InsnNode) || (((InsnNode) insn).getOpcode() != RETURN && ((InsnNode) insn).getOpcode() != ATHROW)) { if (insn instanceof MethodInsnNode && ((MethodInsnNode) insn).owner.equals(mergingClassNode.name)) ((MethodInsnNode) insn).owner = className; else if (insn instanceof FieldInsnNode && ((FieldInsnNode) insn).owner.equals(mergingClassNode.name)) ((FieldInsnNode) insn).owner = className; insn.accept(this); insn = insn.getNext(); } maxStack = Math.max(maxStack, mn.maxStack); maxLocals = Math.max(maxLocals, mn.maxLocals); for (Iterator<?> it = mn.localVariables.iterator(); it.hasNext();) { LocalVariableNode localVar = (LocalVariableNode) it.next(); if (localVar.desc.equals("L" + mergingClassNode.name + ";")) localVar.desc = "L" + className + ";"; } } } } @Override public void visitMaxs(int nStack, int nLocals) { super.visitMaxs(Math.max(nStack, maxStack), Math.max(nLocals, maxLocals)); } private int maxStack; private int maxLocals; }; } else { for (Object methodNode : mergingClassNode.methods) { MethodNode mn = (MethodNode) methodNode; if (mn.name.equals(name) && mn.desc.equals(desc)) { visitedMergingClassMethods.add(name + desc); if (shouldInsertRedirectionCondition) { // Insert a redirection condition, which is based on the STATIC_IS_MERGING_REVERTED_FIELD boolean value. // If the flag is false then call the original method and skip the merging one, otherwise do the opposite. Type returnType = Type.getReturnType(desc); boolean isVoid = returnType.getSort() == Type.VOID; int returnOpcode = isVoid ? RETURN : returnType.getOpcode(IRETURN); AbstractInsnNode first = mn.instructions.getFirst(); mn.instructions.insertBefore(first, new FieldInsnNode(GETSTATIC, className, AOPContext.STATIC_IS_MERGING_REVERTED_FIELD, "Z")); LabelNode labelNode = new LabelNode(); mn.instructions.insertBefore(first, new JumpInsnNode(IFEQ, labelNode)); boolean isStatic = (access & ACC_STATIC) != 0; int inc = 0; if (!isStatic) { mn.instructions.insertBefore(first, new VarInsnNode(ALOAD, 0)); mn.maxStack++; inc = 1; } Type[] argTypes = Type.getArgumentTypes(desc); for (int i = 0; i < argTypes.length; ++i) { mn.instructions.insertBefore(first, new VarInsnNode(argTypes[i].getOpcode(ILOAD), inc + i)); if (argTypes[i].getInternalName().equals("J") || argTypes[i].getInternalName().equals("D")) ++inc; } mn.instructions.insertBefore(first, new MethodInsnNode(isStatic ? INVOKESTATIC : INVOKEVIRTUAL, className, AOPContext.toMergedMethodName(name, -1), desc)); mn.instructions.insertBefore(first, new InsnNode(returnOpcode)); mn.instructions.insertBefore(first, labelNode); } // A corresponding merging method has been found, so let's replace the original method body // with the merging method body mn.accept(mv); // Rename the original method if existent, otherwise return null return shouldInsertRedirectionCondition ? cv.visitMethod(access, AOPContext.toMergedMethodName(name, -1), desc, signature, exceptions) : null; } } } } } else { mv = cv.visitMethod(access, name, desc, signature, exceptions); if (mergingClassNode != null) visitedMergingClassMethods.add(name + desc); if (name.equals("<clinit>")) { isStaticBlockVisited = true; mv = new AdviceAdapter(ASM4, mv, access, name, desc) { @Override protected void onMethodEnter() { initializeStaticAspectsField(this); if (mergingClassNode != null) initializeStaticIsMergingActiveField(this); } @Override protected void onMethodExit(int opcode) { if (mergingClassNode != null) { for (Object methodNode : mergingClassNode.methods) { MethodNode mn = (MethodNode) methodNode; if (mn.name.equals("<clinit>")) { AbstractInsnNode insn = mn.instructions.getFirst(); while (!(insn instanceof InsnNode) || (((InsnNode) insn).getOpcode() != RETURN && ((InsnNode) insn).getOpcode() != ATHROW)) { if (insn instanceof MethodInsnNode && ((MethodInsnNode) insn).owner.equals(mergingClassNode.name)) ((MethodInsnNode) insn).owner = className; else if (insn instanceof FieldInsnNode && ((FieldInsnNode) insn).owner.equals(mergingClassNode.name)) ((FieldInsnNode) insn).owner = className; insn.accept(this); insn = insn.getNext(); } maxStack = Math.max(maxStack, mn.maxStack); maxLocals = Math.max(maxLocals, mn.maxLocals); for (Iterator<?> it = mn.localVariables.iterator(); it.hasNext();) { LocalVariableNode localVar = (LocalVariableNode) it.next(); if (localVar.desc.equals("L" + mergingClassNode.name + ";")) localVar.desc = "L" + className + ";"; } break; } } } } @Override public void visitMaxs(int nStack, int nLocals) { if (mergingClassNode != null) super.visitMaxs(Math.max(nStack, maxStack), Math.max(nLocals, maxLocals)); else super.visitMaxs(nStack + 2, nLocals); } private int maxStack; private int maxLocals; }; } } return mv; }
From source file:pl.clareo.coroutines.core.MethodTransformer.java
License:Apache License
private static FrameNode findNextFrame(AbstractInsnNode insn) { AbstractInsnNode nextInsn = insn.getNext(); while (nextInsn != null) { if (nextInsn.getType() == AbstractInsnNode.FRAME) { return (FrameNode) nextInsn; }// w w w.j ava 2 s . co m nextInsn = nextInsn.getNext(); } return null; }
From source file:pxb.android.dex2jar.optimize.B.java
License:Apache License
private void cut() { // long timeStart = System.currentTimeMillis(); // log.debug("enter {}", m); int blockIndex = 0; // dump();//ww w .j a v a 2 s . co m Map<Integer, AbstractInsnNode> in = new HashMap<Integer, AbstractInsnNode>(); Map<Integer, AbstractInsnNode> out = new HashMap<Integer, AbstractInsnNode>(); AbstractInsnNode p = method.instructions.getFirst(); if (!(p instanceof LabelNode)) { insnList.insertBefore(p, new LabelNode()); } AbstractInsnNode q = method.instructions.getLast(); if (!(q instanceof LabelNode)) { insnList.insert(q, new LabelNode()); } AbstractInsnNode first = method.instructions.getFirst(); p = first.getNext(); // Blocks while (p != null) { if (isRead(p)) { int r = var(p); if (in.get(r) == null) { if (out.get(r) == null) { in.put(r, p); } } } else if (isWrite(p)) { int r = var(p); out.put(r, p); } else if (needBreak(p)) { if (p.getType() != AbstractInsnNode.LABEL) { q = p.getNext(); if (q != null && q.getType() == AbstractInsnNode.LABEL) { p = q; } else { method.instructions.insert(p, new LabelNode()); p = p.getNext(); } } Block block = new Block(blockIndex++); block.first = (LabelNode) first; block.last = (LabelNode) p; block.in = in; block.out = out; blocks.add(block); blockMaps.put(block.first.getLabel(), block); first = p; in = new HashMap<Integer, AbstractInsnNode>(); out = new HashMap<Integer, AbstractInsnNode>(); } p = p.getNext(); } }