Package org.objectweb.asm

Examples of org.objectweb.asm.ClassVisitor


public class AddTimerAdapterTest extends AbstractTestCase {

  public void test() throws Exception {
    ClassWriter cw = new ClassWriter(0);
    CheckClassAdapter ca = new CheckClassAdapter(cw);
    ClassVisitor cv = getClassAdapter(ca);
    generateBasicClass(cv);
    checkClass(defineClass("C", cw.toByteArray()));
  }
View Full Code Here


        ClassReader cr = new ClassReader(is);
        int crFlags = 0;
        if(modes.get(BCOConstants.F_EXPAND_STACKMAP)) {
            crFlags |= ClassReader.EXPAND_FRAMES;
        }
        ClassVisitor cv;
        if (modes.get(BCOConstants.F_SHOW_ASMIFIER_CODE)) {
            cv = new CommentedASMifierClassVisitor(modes);
        } else {
            cv = new CommentedClassVisitor(modes);
        }
View Full Code Here

            } catch (Throwable ignored) {
              continue;
            }
          }
          ClassWriter cw = new ClassWriter(0);
          ClassVisitor cv = getClassAdapter(cw);
          try {
            cr.accept(cv, ClassReader.EXPAND_FRAMES);
          } catch (UnsatisfiedLinkError ignored) {
          } catch (NoClassDefFoundError ignored) {
          }
View Full Code Here

    for (int i = 0; i < 10; ++i) {
      long t = System.currentTimeMillis();
      for (int j = 0; j < classes.size(); ++j) {
        byte[] b = (byte[]) classes.get(j);
        ClassWriter cw = new ClassWriter(0);
        ClassVisitor cv = new AddTimerAdapter(cw);
        new ClassReader(b).accept(cv, 0);
        cw.toByteArray();
      }
      t = System.currentTimeMillis() - t;
      times[3] = Math.min(t, times[3]);
      System.out.println("Time to deserialize and reserialize "
          + classes.size() + " classes with AddTimerAdapter = " + t
          + " ms");
    }

    for (int i = 0; i < 10; ++i) {
      long t = System.currentTimeMillis();
      for (int j = 0; j < classes.size(); ++j) {
        byte[] b = (byte[]) classes.get(j);
        ClassReader cr = new ClassReader(b);
        ClassWriter cw = new ClassWriter(cr, 0);
        ClassVisitor cv = new AddTimerAdapter(cw);
        cr.accept(cv, 0);
        cw.toByteArray();
      }
      t = System.currentTimeMillis() - t;
      times[4] = Math.min(t, times[4]);
      System.out
          .println("Time to deserialize and reserialize "
              + classes.size()
              + " classes with AddTimerAdapter and copyPool = " + t
              + " ms");
    }

    for (int i = 0; i < 10; ++i) {
      long t = System.currentTimeMillis();
      for (int j = 0; j < classes.size(); ++j) {
        byte[] b = (byte[]) classes.get(j);
        ClassWriter cw = new ClassWriter(0);
        ClassVisitor cv = new ClassAdapter(cw) {
          public MethodVisitor visitMethod(int access, String name,
              String desc, String signature, String[] exceptions) {
            return new RemoveGetFieldPutFieldAdapter(cv.visitMethod(
                access, name, desc, signature, exceptions));
          }
        };
        new ClassReader(b).accept(cv, 0);
        cw.toByteArray();
      }
      t = System.currentTimeMillis() - t;
      times[5] = Math.min(t, times[5]);
      System.out.println("Time to deserialize and reserialize "
          + classes.size()
          + " classes with RemoveGetFieldPutFieldAdapter = " + t
          + " ms");
    }

    for (int i = 0; i < 10; ++i) {
      long t = System.currentTimeMillis();
      for (int j = 0; j < classes.size(); ++j) {
        byte[] b = (byte[]) classes.get(j);
        ClassReader cr = new ClassReader(b);
        ClassWriter cw = new ClassWriter(cr, 0);
        ClassVisitor cv = new ClassAdapter(cw) {
          public MethodVisitor visitMethod(int access, String name,
              String desc, String signature, String[] exceptions) {
            return new RemoveGetFieldPutFieldAdapter(cv.visitMethod(
                access, name, desc, signature, exceptions));
          }
        };
        cr.accept(cv, 0);
        cw.toByteArray();
View Full Code Here

//        public Object invoke(Object receiver, Object arg0, Object arg1, Object arg2, Object arg3, Object arg4) { throw fail(); }
        public Object invoke(Object receiver, Object... args) { throw fail(); }
    }
   
    public static Handle createHandle(JRubyClassLoader classLoader, Method method, boolean debug) {
        ClassVisitor cv;
        if (debug) {
            cv = new ASMifierClassVisitor(new PrintWriter(System.out));
        } else {
            cv = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        }

        Class returnType = method.getReturnType();
        Class[] paramTypes = method.getParameterTypes();
       
        String name = "H" + (method.getName() + pretty(returnType, paramTypes)).hashCode();
       
        try {
            Class existing = classLoader.loadClass(name);
            return (Handle)existing.newInstance();
        } catch (Exception e) {
        }
        cv.visit(ACC_PUBLIC | ACC_FINAL | ACC_SUPER, V1_5, name, null, p(Handle.class), null);
       
        SkinnyMethodAdapter m;
        String signature;
        switch (paramTypes.length) {
        case 0:
            signature = sig(Object.class, Object.class);
            break;
        case 1:
            signature = sig(Object.class, Object.class, Object.class);
            break;
        case 2:
            signature = sig(Object.class, Object.class, Object.class, Object.class);
            break;
        case 3:
            signature = sig(Object.class, Object.class, Object.class, Object.class, Object.class);
            break;
//        case 4:
//            signature = sig(Object.class, Object.class, Object.class, Object.class, Object.class);
//            break;
//        case 5:
//            signature = sig(Object.class, Object.class, Object.class, Object.class, Object.class, Object.class);
//            break;
        default:
            signature = sig(Object.class, Object.class, Object[].class);
            break;
        }
        m = new SkinnyMethodAdapter(cv.visitMethod(ACC_PUBLIC | ACC_FINAL | ACC_SYNTHETIC, "invoke", signature, null, null));
       
        m.start();
       
        // load receiver
        if (!Modifier.isStatic(method.getModifiers())) {
            m.aload(1); // receiver
            if (method.getDeclaringClass() != Object.class) {
                m.checkcast(p(method.getDeclaringClass()));
            }
        }
       
        // load arguments
        switch (paramTypes.length) {
        case 0:
        case 1:
        case 2:
        case 3:
//        case 4:
//        case 5:
            for (int i = 0; i < paramTypes.length; i++) {
                loadUnboxedArgument(m, i + 2, paramTypes[i]);
            }
            break;
        default:
            for (int i = 0; i < paramTypes.length; i++) {
                m.aload(2); // Object[] args
                m.pushInt(i);
                m.aaload();
                Class paramClass = paramTypes[i];
                if (paramClass.isPrimitive()) {
                    Class boxType = getBoxType(paramClass);
                    m.checkcast(p(boxType));
                    m.invokevirtual(p(boxType), paramClass.toString() + "Value", sig(paramClass));
                } else if (paramClass != Object.class) {
                    m.checkcast(p(paramClass));
                }
            }
            break;
        }
       
        if (Modifier.isStatic(method.getModifiers())) {
            m.invokestatic(p(method.getDeclaringClass()), method.getName(), sig(returnType, paramTypes));
        } else if (Modifier.isInterface(method.getDeclaringClass().getModifiers())) {
            m.invokeinterface(p(method.getDeclaringClass()), method.getName(), sig(returnType, paramTypes));
        } else {
            m.invokevirtual(p(method.getDeclaringClass()), method.getName(), sig(returnType, paramTypes));
        }
       
        if (returnType == void.class) {
            m.aload(1);
        } else if (returnType.isPrimitive()) {
            Class boxType = getBoxType(returnType);
            m.invokestatic(p(boxType), "valueOf", sig(boxType, returnType));
        }
        m.areturn();
        m.end();
       
        // constructor
        m = new SkinnyMethodAdapter(cv.visitMethod(ACC_PUBLIC, "<init>", sig(void.class), null, null));
        m.start();
        m.aload(0);
        m.invokespecial(p(Handle.class), "<init>", sig(void.class));
        m.voidreturn();
        m.end();
       
        cv.visitEnd();
       
        if (debug) {
            ((ASMifierClassVisitor)cv).print(new PrintWriter(System.out));
            return createHandle(classLoader, method, false);
        } else {
View Full Code Here

            break;
        }
    }

    private void beginInit() {
        ClassVisitor cv = getClassVisitor();

        initMethod = new SkinnyMethodAdapter(cv.visitMethod(ACC_PUBLIC, "<init>", sig(Void.TYPE), null, null));
        initMethod.start();
        initMethod.aload(THIS);
        initMethod.invokespecial(p(AbstractScript.class), "<init>", sig(Void.TYPE));
       
        cv.visitField(ACC_PRIVATE | ACC_FINAL, "$class", ci(Class.class), null, null);
       
        // FIXME: this really ought to be in clinit, but it doesn't matter much
        initMethod.aload(THIS);
        initMethod.ldc(c(getClassname()));
        initMethod.invokestatic(p(Class.class), "forName", sig(Class.class, params(String.class)));
View Full Code Here

        initMethod.voidreturn();
        initMethod.end();
    }

    private void beginClassInit() {
        ClassVisitor cv = getClassVisitor();

        clinitMethod = new SkinnyMethodAdapter(cv.visitMethod(ACC_PUBLIC | ACC_STATIC, "<clinit>", sig(Void.TYPE), null, null));
        clinitMethod.start();

        if (invDynSupportInstaller != null) {
            // install invokedynamic bootstrapper
            // TODO need to abstract this setup behind another compiler interface
View Full Code Here

    public String getNewConstant(String type, String name_prefix) {
        return getNewConstant(type, name_prefix, null);
    }

    public String getNewConstant(String type, String name_prefix, Object init) {
        ClassVisitor cv = getClassVisitor();

        String realName;
        synchronized (this) {
            realName = "_" + constants++;
        }

        // declare the field
        cv.visitField(ACC_PRIVATE, realName, type, null, null).visitEnd();

        if(init != null) {
            initMethod.aload(THIS);
            initMethod.ldc(init);
            initMethod.putfield(getClassname(),realName, type);
View Full Code Here

        return realName;
    }

    public String getNewField(String type, String name, Object init) {
        ClassVisitor cv = getClassVisitor();

        // declare the field
        cv.visitField(ACC_PRIVATE, name, type, null, null).visitEnd();

        if(init != null) {
            initMethod.aload(THIS);
            initMethod.ldc(init);
            initMethod.putfield(getClassname(),name, type);
View Full Code Here

        return name;
    }

    public String getNewStaticConstant(String type, String name_prefix) {
        ClassVisitor cv = getClassVisitor();

        String realName;
        synchronized (this) {
            realName = "__" + constants++;
        }

        // declare the field
        cv.visitField(ACC_PRIVATE | ACC_STATIC | ACC_FINAL, realName, type, null, null).visitEnd();
        return realName;
    }
View Full Code Here

TOP

Related Classes of org.objectweb.asm.ClassVisitor

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.