Package org.objectweb.asm.tree

Examples of org.objectweb.asm.tree.MethodNode


* @author Eric Bruneton
*/
public class NullDereferenceAnalyzerTest extends AbstractTestCase {

  public void test() throws Exception {
    MethodNode mn = new MethodNode(ACC_PUBLIC, "m", "(ZZ)V", null, null);
    mn.visitCode();
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    Label l3 = new Label();
    Label l4 = new Label();
    Label l5 = new Label();
    Label l6 = new Label();
    mn.visitTryCatchBlock(l0, l1, l2, null);
    mn.visitTryCatchBlock(l2, l3, l2, null);
    mn.visitInsn(ACONST_NULL); // insn 0
    mn.visitVarInsn(ASTORE, 3);
    mn.visitInsn(ACONST_NULL); // insn 2
    mn.visitVarInsn(ASTORE, 4);
    mn.visitTypeInsn(NEW, "C");
    mn.visitInsn(DUP);
    mn.visitMethodInsn(INVOKESPECIAL, "C", "<init>", "()V");
    mn.visitVarInsn(ASTORE, 5);
    mn.visitVarInsn(ILOAD, 1);
    mn.visitJumpInsn(IFEQ, l4);
    mn.visitTypeInsn(NEW, "java/lang/Integer"); // insn
    // 10
    mn.visitInsn(DUP);
    mn.visitInsn(ICONST_1);
    mn.visitMethodInsn(INVOKESPECIAL, "java/lang/Integer", "<init>",
        "(I)V");
    mn.visitVarInsn(ASTORE, 3);
    mn.visitTypeInsn(NEW, "C"); // insn 15
    mn.visitInsn(DUP);
    mn.visitMethodInsn(INVOKESPECIAL, "C", "<init>", "()V");
    mn.visitVarInsn(ASTORE, 4);
    mn.visitLabel(l4);
    mn.visitVarInsn(ILOAD, 2);
    mn.visitJumpInsn(IFEQ, l5);
    mn.visitTypeInsn(NEW, "java/lang/Integer"); // insn
    // 22
    mn.visitInsn(DUP);
    mn.visitInsn(ICONST_1);
    mn.visitMethodInsn(INVOKESPECIAL, "java/lang/Integer", "<init>",
        "(I)V");
    mn.visitVarInsn(ASTORE, 3);
    mn.visitTypeInsn(NEW, "C"); // insn 27
    mn.visitInsn(DUP);
    mn.visitMethodInsn(INVOKESPECIAL, "C", "<init>", "()V");
    mn.visitVarInsn(ASTORE, 4);
    mn.visitLabel(l5);
    mn.visitVarInsn(ALOAD, 4);
    mn.visitInsn(DUP);
    mn.visitVarInsn(ASTORE, 6);
    mn.visitInsn(MONITORENTER); // insn 35
    mn.visitLabel(l0);
    mn.visitVarInsn(ALOAD, 4);
    mn.visitVarInsn(ALOAD, 4);
    mn.visitFieldInsn(GETFIELD, "C", "i", "I"); // insn
    // 39
    mn.visitVarInsn(ALOAD, 5);
    mn.visitFieldInsn(GETFIELD, "C", "i", "I");
    mn.visitInsn(IADD);
    mn.visitVarInsn(ALOAD, 3);
    mn.visitMethodInsn(INVOKEVIRTUAL, // insn 44
        "java/lang/Integer", "intValue", "()I");
    mn.visitInsn(IADD);
    mn.visitFieldInsn(PUTFIELD, "C", "i", "I"); // insn
    // 46
    mn.visitVarInsn(ALOAD, 6);
    mn.visitInsn(MONITOREXIT); // insn 48
    mn.visitLabel(l1);
    mn.visitJumpInsn(GOTO, l6);
    mn.visitLabel(l2);
    mn.visitVarInsn(ALOAD, 6);
    mn.visitInsn(MONITOREXIT); // insn 53
    mn.visitLabel(l3);
    mn.visitInsn(ATHROW);
    mn.visitLabel(l6);
    mn.visitInsn(RETURN);
    mn.visitMaxs(3, 7);
    mn.visitEnd();

    List<AbstractInsnNode> occurences;
    occurences = new NullDereferenceAnalyzer().findNullDereferences(
        "D", mn);
    assertEquals(6, occurences.size());
View Full Code Here


* @author Eric Bruneton
*/
public class CyclomaticComplexityTest extends AbstractTestCase {

  public void test() throws AnalyzerException {
    MethodNode mn = new MethodNode(ACC_PUBLIC, "checkAndSetF", "(I)V",
        null, null);
    InsnList il = mn.instructions;
    il.add(new VarInsnNode(ILOAD, 1));
    LabelNode label = new LabelNode();
    il.add(new JumpInsnNode(IFLT, label));
View Full Code Here

  protected ClassVisitor getClassAdapter(final ClassVisitor cv) {
    return new ClassNode() {
      public void visitEnd() {
        Iterator i = methods.iterator();
        while (i.hasNext()) {
          MethodNode mn = (MethodNode) i.next();
          try {
            CyclomaticComplexity cc = new CyclomaticComplexity();
            assert (cc.getCyclomaticComplexity(name, mn) > 0);
          } catch (AnalyzerException ignored) {
          }
View Full Code Here

public class RemoveUnusedCastTransformerTest extends
    RemoveUnusedCastAdapterTest {

  public void test() {
    TraceMethodVisitor tmv = new TraceMethodVisitor(null);
    MethodNode mn = new MethodNode(ACC_PUBLIC, "m",
        "(Ljava/lang/Integer;)I", null, null);
    mn.visitCode();
    mn.visitVarInsn(ALOAD, 1);
    mn.visitTypeInsn(CHECKCAST, "java/lang/Number");
    mn.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Number", "intValue",
        "()I");
    mn.visitInsn(IRETURN);
    mn.visitMaxs(1, 2);
    mn.visitEnd();
    new RemoveUnusedCastTransformer("C", null).transform(mn);
    mn.accept(tmv);
    checkMethod(tmv);
  }
View Full Code Here

public class RemoveGetFieldPutFieldTransformer2Test extends
    RemoveGetFieldPutFieldAdapterTest {

  public void test() {
    TraceMethodVisitor tmv = new TraceMethodVisitor(null);
    MethodNode mn = new MethodNode(0, null, null, null, null);
    mn.visitCode();
    mn.visitVarInsn(ALOAD, 0);
    mn.visitVarInsn(ALOAD, 0);
    mn.visitVarInsn(ALOAD, 0);
    mn.visitFieldInsn(GETFIELD, "C", "f", "I");
    mn.visitFieldInsn(PUTFIELD, "C", "f", "I");
    mn.visitFieldInsn(GETFIELD, "C", "f", "I");
    mn.visitInsn(IRETURN);
    mn.visitMaxs(0, 0);
    mn.visitEnd();
    new RemoveGetFieldPutFieldTransformer2(null).transform(mn);
    mn.accept(tmv);
    checkMethod(tmv);
  }
View Full Code Here

        ClassWriter cw = new ClassWriter(0);
        ClassNode cn = new ClassNode();
        new ClassReader(b).accept(cn, 0);
        Iterator it = cn.methods.iterator();
        while (it.hasNext()) {
          MethodNode mn = (MethodNode) it.next();
          new RemoveGetFieldPutFieldTransformer(null).transform(mn);
        }
        cn.accept(cw);
        cw.toByteArray();
      }
      t = System.currentTimeMillis() - t;
      times[12] = Math.min(t, times[12]);
      System.out.println("Time to deserialize and reserialize "
          + classes.size()
          + " classes with RemoveGetFieldPutFieldTransformer = " + t
          + " ms");
    }

    for (int i = 0; i < 10; ++i) {
      int errors = 0;
      long t = System.currentTimeMillis();
      for (int j = 0; j < classes.size() / 10; ++j) {
        byte[] b = (byte[]) classes.get(j);
        ClassReader cr = new ClassReader(b);
        ClassNode cn = new ClassNode();
        cr.accept(cn, 0);
        List methods = cn.methods;
        for (int k = 0; k < methods.size(); ++k) {
          MethodNode method = (MethodNode) methods.get(k);
          if (method.instructions.size() > 0) {
            Analyzer a = new Analyzer(new BasicInterpreter());
            try {
              a.analyze(cn.name, method);
            } catch (Throwable th) {
              ++errors;
            }
          }
        }
      }
      t = System.currentTimeMillis() - t;
      times[13] = Math.min(t, times[13]);
      System.out.println("Time to analyze " + classes.size() / 10
          + " classes with BasicInterpreter = " + t + " ms (" + errors
          + " errors)");
    }

    for (int i = 0; i < 10; ++i) {
      int errors = 0;
      long t = System.currentTimeMillis();
      for (int j = 0; j < classes.size() / 10; ++j) {
        byte[] b = (byte[]) classes.get(j);
        ClassReader cr = new ClassReader(b);
        ClassNode cn = new ClassNode();
        cr.accept(cn, 0);
        List methods = cn.methods;
        for (int k = 0; k < methods.size(); ++k) {
          MethodNode method = (MethodNode) methods.get(k);
          if (method.instructions.size() > 0) {
            Analyzer a = new Analyzer(new SimpleVerifier());
            try {
              a.analyze(cn.name, method);
            } catch (Throwable th) {
View Full Code Here

        "LESS", "I", null, new Integer(-1)));
    cn.fields.add(new FieldNode(ACC_PUBLIC + ACC_FINAL + ACC_STATIC,
        "EQUAL", "I", null, new Integer(0)));
    cn.fields.add(new FieldNode(ACC_PUBLIC + ACC_FINAL + ACC_STATIC,
        "GREATER", "I", null, new Integer(1)));
    cn.methods.add(new MethodNode(ACC_PUBLIC + ACC_ABSTRACT,
        "compareTo", "(Ljava/lang/Object;)I", null, null));
    ClassWriter cw = new ClassWriter(0);
    cn.accept(cw);
    return cw.toByteArray();
  }
View Full Code Here

  }

  public void transform(ClassNode cn) {
    Iterator i = cn.methods.iterator();
    while (i.hasNext()) {
      MethodNode mn = (MethodNode) i.next();
      if (methodName.equals(mn.name) && methodDesc.equals(mn.desc)) {
        i.remove();
      }
    }
    super.transform(cn);
View Full Code Here

    cn.name = "pkg/Bean";
    cn.superName = "java/lang/Object";
    cn.sourceDebug = "Bean.java";
    cn.fields.add(new FieldNode(ACC_PRIVATE, "f", "I", null, null));
    {
      MethodNode mn = new MethodNode(ACC_PUBLIC, "<init>", "()V", null,
          null);
      cn.methods.add(mn);
      InsnList il = mn.instructions;
      il.add(new VarInsnNode(ALOAD, 0));
      il.add(new MethodInsnNode(INVOKESPECIAL, "java/lang/Object",
          "<init>", "()V"));
      il.add(new InsnNode(RETURN));
      mn.maxStack = 1;
      mn.maxLocals = 1;
    }
    {
      MethodNode mn = new MethodNode(ACC_PUBLIC, "getF", "()I", null,
          null);
      cn.methods.add(mn);
      InsnList il = mn.instructions;
      il.add(new VarInsnNode(ALOAD, 0));
      il.add(new FieldInsnNode(GETFIELD, "pkg/Bean", "f", "I"));
      il.add(new InsnNode(IRETURN));
      mn.maxStack = 1;
      mn.maxLocals = 1;
    }
    {
      MethodNode mn = new MethodNode(ACC_PUBLIC, "setF", "(I)V", null,
          null);
      cn.methods.add(mn);
      InsnList il = mn.instructions;
      il.add(new VarInsnNode(ALOAD, 0));
      il.add(new VarInsnNode(ILOAD, 1));
      il.add(new FieldInsnNode(PUTFIELD, "pkg/Bean", "f", "I"));
      il.add(new InsnNode(RETURN));
      mn.maxStack = 2;
      mn.maxLocals = 2;
    }
    MethodNode mn = new MethodNode(ACC_PUBLIC, "checkAndSetF", "(I)V",
        null, null);
    cn.methods.add(mn);
    InsnList il = mn.instructions;
    il.add(new VarInsnNode(ILOAD, 1));
    LabelNode label = new LabelNode();
View Full Code Here

        IList eil = ((IList)((IConstructor)v).get(4));
        String ea[] = new String[eil.length()];
        for (int i = 0; i < eil.length(); i++) {
          ea[i] = ((IString)eil.get(i)).getValue();
        }
        MethodNode mn = new MethodNode(((IInteger)((IConstructor)v).get(0)).intValue(),
                         ((IString)((IConstructor)v).get(1)).getValue(),
                         ((IString)((IConstructor)v).get(2)).getValue(),
                         emptyIsNull(((IString)((IConstructor)v).get(3)).getValue()),
                         ea);
       
View Full Code Here

TOP

Related Classes of org.objectweb.asm.tree.MethodNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.