Example #1
0
 @Override
 public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
   if (api < Opcodes.ASM5) {
     super.visitMethodInsn(opcode, owner, name, desc, itf);
     return;
   }
   instructions.add(new MethodInsnNode(opcode, owner, name, desc, itf));
 }
Example #2
0
 @Deprecated
 @Override
 public void visitMethodInsn(int opcode, String owner, String name, String desc) {
   if (api >= Opcodes.ASM5) {
     super.visitMethodInsn(opcode, owner, name, desc);
     return;
   }
   instructions.add(new MethodInsnNode(opcode, owner, name, desc));
 }
Example #3
0
 /**
  * Checks that this method node is compatible with the given ASM API version. This methods checks
  * that this node, and all its nodes recursively, do not contain elements that were introduced in
  * more recent versions of the ASM API than the given version.
  *
  * @param api an ASM API version. Must be one of {@link Opcodes#ASM4} or {@link Opcodes#ASM5}.
  */
 public void check(final int api) {
   if (api == Opcodes.ASM4) {
     if (visibleTypeAnnotations != null && visibleTypeAnnotations.size() > 0) {
       throw new RuntimeException();
     }
     if (invisibleTypeAnnotations != null && invisibleTypeAnnotations.size() > 0) {
       throw new RuntimeException();
     }
     int n = tryCatchBlocks == null ? 0 : tryCatchBlocks.size();
     for (int i = 0; i < n; ++i) {
       TryCatchBlockNode tcb = tryCatchBlocks.get(i);
       if (tcb.visibleTypeAnnotations != null && tcb.visibleTypeAnnotations.size() > 0) {
         throw new RuntimeException();
       }
       if (tcb.invisibleTypeAnnotations != null && tcb.invisibleTypeAnnotations.size() > 0) {
         throw new RuntimeException();
       }
     }
     for (int i = 0; i < instructions.size(); ++i) {
       AbstractInsnNode insn = instructions.get(i);
       if (insn.visibleTypeAnnotations != null && insn.visibleTypeAnnotations.size() > 0) {
         throw new RuntimeException();
       }
       if (insn.invisibleTypeAnnotations != null && insn.invisibleTypeAnnotations.size() > 0) {
         throw new RuntimeException();
       }
       if (insn instanceof MethodInsnNode) {
         boolean itf = ((MethodInsnNode) insn).itf;
         if (itf != (insn.opcode == Opcodes.INVOKEINTERFACE)) {
           throw new RuntimeException();
         }
       }
     }
     if (visibleLocalVariableAnnotations != null && visibleLocalVariableAnnotations.size() > 0) {
       throw new RuntimeException();
     }
     if (invisibleLocalVariableAnnotations != null
         && invisibleLocalVariableAnnotations.size() > 0) {
       throw new RuntimeException();
     }
   }
 }
Example #4
0
 @Override
 public void visitFrame(
     final int type,
     final int nLocal,
     final Object[] local,
     final int nStack,
     final Object[] stack) {
   instructions.add(
       new FrameNode(
           type,
           nLocal,
           local == null ? null : getLabelNodes(local),
           nStack,
           stack == null ? null : getLabelNodes(stack)));
 }
Example #5
0
 @Override
 public AnnotationVisitor visitInsnAnnotation(
     int typeRef, TypePath typePath, String desc, boolean visible) {
   // Finds the last real instruction, i.e. the instruction targeted by
   // this annotation.
   AbstractInsnNode insn = instructions.getLast();
   while (insn.getOpcode() == -1) {
     insn = insn.getPrevious();
   }
   // Adds the annotation to this instruction.
   TypeAnnotationNode an = new TypeAnnotationNode(typeRef, typePath, desc);
   if (visible) {
     if (insn.visibleTypeAnnotations == null) {
       insn.visibleTypeAnnotations = new ArrayList<TypeAnnotationNode>(1);
     }
     insn.visibleTypeAnnotations.add(an);
   } else {
     if (insn.invisibleTypeAnnotations == null) {
       insn.invisibleTypeAnnotations = new ArrayList<TypeAnnotationNode>(1);
     }
     insn.invisibleTypeAnnotations.add(an);
   }
   return an;
 }
Example #6
0
 @Override
 public void visitMultiANewArrayInsn(final String desc, final int dims) {
   instructions.add(new MultiANewArrayInsnNode(desc, dims));
 }
Example #7
0
 @Override
 public void visitLookupSwitchInsn(final Label dflt, final int[] keys, final Label[] labels) {
   instructions.add(new LookupSwitchInsnNode(getLabelNode(dflt), keys, getLabelNodes(labels)));
 }
Example #8
0
 @Override
 public void visitTableSwitchInsn(
     final int min, final int max, final Label dflt, final Label... labels) {
   instructions.add(new TableSwitchInsnNode(min, max, getLabelNode(dflt), getLabelNodes(labels)));
 }
Example #9
0
 @Override
 public void visitIincInsn(final int var, final int increment) {
   instructions.add(new IincInsnNode(var, increment));
 }
Example #10
0
 @Override
 public void visitLdcInsn(final Object cst) {
   instructions.add(new LdcInsnNode(cst));
 }
Example #11
0
 @Override
 public void visitInsn(final int opcode) {
   instructions.add(new InsnNode(opcode));
 }
Example #12
0
 @Override
 public void visitInvokeDynamicInsn(String name, String desc, Handle bsm, Object... bsmArgs) {
   instructions.add(new InvokeDynamicInsnNode(name, desc, bsm, bsmArgs));
 }
Example #13
0
 @Override
 public void visitFieldInsn(
     final int opcode, final String owner, final String name, final String desc) {
   instructions.add(new FieldInsnNode(opcode, owner, name, desc));
 }
Example #14
0
 @Override
 public void visitTypeInsn(final int opcode, final String type) {
   instructions.add(new TypeInsnNode(opcode, type));
 }
Example #15
0
 @Override
 public void visitVarInsn(final int opcode, final int var) {
   instructions.add(new VarInsnNode(opcode, var));
 }
Example #16
0
 @Override
 public void visitIntInsn(final int opcode, final int operand) {
   instructions.add(new IntInsnNode(opcode, operand));
 }
Example #17
0
 @Override
 public void visitLineNumber(final int line, final Label start) {
   instructions.add(new LineNumberNode(line, getLabelNode(start)));
 }
Example #18
0
 @Override
 public void visitJumpInsn(final int opcode, final Label label) {
   instructions.add(new JumpInsnNode(opcode, getLabelNode(label)));
 }
Example #19
0
 /**
  * Makes the given method visitor visit this method.
  *
  * @param mv a method visitor.
  */
 public void accept(final MethodVisitor mv) {
   // visits the method parameters
   int i, j, n;
   n = parameters == null ? 0 : parameters.size();
   for (i = 0; i < n; i++) {
     ParameterNode parameter = parameters.get(i);
     mv.visitParameter(parameter.name, parameter.access);
   }
   // visits the method attributes
   if (annotationDefault != null) {
     AnnotationVisitor av = mv.visitAnnotationDefault();
     AnnotationNode.accept(av, null, annotationDefault);
     if (av != null) {
       av.visitEnd();
     }
   }
   n = visibleAnnotations == null ? 0 : visibleAnnotations.size();
   for (i = 0; i < n; ++i) {
     AnnotationNode an = visibleAnnotations.get(i);
     an.accept(mv.visitAnnotation(an.desc, true));
   }
   n = invisibleAnnotations == null ? 0 : invisibleAnnotations.size();
   for (i = 0; i < n; ++i) {
     AnnotationNode an = invisibleAnnotations.get(i);
     an.accept(mv.visitAnnotation(an.desc, false));
   }
   n = visibleTypeAnnotations == null ? 0 : visibleTypeAnnotations.size();
   for (i = 0; i < n; ++i) {
     TypeAnnotationNode an = visibleTypeAnnotations.get(i);
     an.accept(mv.visitTypeAnnotation(an.typeRef, an.typePath, an.desc, true));
   }
   n = invisibleTypeAnnotations == null ? 0 : invisibleTypeAnnotations.size();
   for (i = 0; i < n; ++i) {
     TypeAnnotationNode an = invisibleTypeAnnotations.get(i);
     an.accept(mv.visitTypeAnnotation(an.typeRef, an.typePath, an.desc, false));
   }
   n = visibleParameterAnnotations == null ? 0 : visibleParameterAnnotations.length;
   for (i = 0; i < n; ++i) {
     List<?> l = visibleParameterAnnotations[i];
     if (l == null) {
       continue;
     }
     for (j = 0; j < l.size(); ++j) {
       AnnotationNode an = (AnnotationNode) l.get(j);
       an.accept(mv.visitParameterAnnotation(i, an.desc, true));
     }
   }
   n = invisibleParameterAnnotations == null ? 0 : invisibleParameterAnnotations.length;
   for (i = 0; i < n; ++i) {
     List<?> l = invisibleParameterAnnotations[i];
     if (l == null) {
       continue;
     }
     for (j = 0; j < l.size(); ++j) {
       AnnotationNode an = (AnnotationNode) l.get(j);
       an.accept(mv.visitParameterAnnotation(i, an.desc, false));
     }
   }
   if (visited) {
     instructions.resetLabels();
   }
   n = attrs == null ? 0 : attrs.size();
   for (i = 0; i < n; ++i) {
     mv.visitAttribute(attrs.get(i));
   }
   // visits the method's code
   if (instructions.size() > 0) {
     mv.visitCode();
     // visits try catch blocks
     n = tryCatchBlocks == null ? 0 : tryCatchBlocks.size();
     for (i = 0; i < n; ++i) {
       tryCatchBlocks.get(i).updateIndex(i);
       tryCatchBlocks.get(i).accept(mv);
     }
     // visits instructions
     instructions.accept(mv);
     // visits local variables
     n = localVariables == null ? 0 : localVariables.size();
     for (i = 0; i < n; ++i) {
       localVariables.get(i).accept(mv);
     }
     // visits local variable annotations
     n = visibleLocalVariableAnnotations == null ? 0 : visibleLocalVariableAnnotations.size();
     for (i = 0; i < n; ++i) {
       visibleLocalVariableAnnotations.get(i).accept(mv, true);
     }
     n = invisibleLocalVariableAnnotations == null ? 0 : invisibleLocalVariableAnnotations.size();
     for (i = 0; i < n; ++i) {
       invisibleLocalVariableAnnotations.get(i).accept(mv, false);
     }
     // visits maxs
     mv.visitMaxs(maxStack, maxLocals);
     visited = true;
   }
   mv.visitEnd();
 }
Example #20
0
 @Override
 public void visitLabel(final Label label) {
   instructions.add(getLabelNode(label));
 }