Package org.objectweb.asm.commons

Examples of org.objectweb.asm.commons.GeneratorAdapter


                new UnableToProxyException(superToCopy));
        mv = new CopyingMethodAdapter((GeneratorAdapter) weaver, superType, currentTransformMethod);
      }
      else {
        //For whatever reason we aren't weaving this method. The call to super.xxx() will always work
        mv = new CopyingMethodAdapter(new GeneratorAdapter(access, currentTransformMethod, mv),
             superType, currentTransformMethod);
      }
    }
   
    return mv;
View Full Code Here


        null, EXPRESSION_TYPE.getInternalName(), null);
    String clippedSourceText = (sourceText.length() <= MAX_SOURCE_LENGTH) ?
        sourceText : (sourceText.substring(0, MAX_SOURCE_LENGTH - 3) + "...");
    classWriter.visitSource(clippedSourceText, null);
   
    GeneratorAdapter constructor = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC,
        EXPRESSION_CTOR, null, null, classWriter);
    constructor.loadThis();
    constructor.loadArgs();
    constructor.invokeConstructor(EXPRESSION_TYPE, EXPRESSION_CTOR);
    constructor.returnValue();
    constructor.endMethod();
   
    gen = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC,
        EVALUATE_METHOD, null, null, classWriter);
  }
View Full Code Here

      ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
      classWriter.visit(Opcodes.V1_5, ACC_PUBLIC | ACC_SUPER | ACC_FINAL | ACC_SYNTHETIC,
          className.replace('.', '/'), null, Type.getInternalName(Object.class), null);
     
      org.objectweb.asm.commons.Method m = org.objectweb.asm.commons.Method.getMethod("void <init>()");
      GeneratorAdapter constructor = new GeneratorAdapter(ACC_PRIVATE | ACC_SYNTHETIC, m, null, null, classWriter);
      constructor.loadThis();
      constructor.loadArgs();
      constructor.invokeConstructor(Type.getType(Object.class), m);
      constructor.returnValue();
      constructor.endMethod();
     
      GeneratorAdapter gen = new GeneratorAdapter(ACC_STATIC | ACC_PUBLIC | ACC_SYNTHETIC,
          org.objectweb.asm.commons.Method.getMethod("double bar()"), null, null, classWriter);
      gen.push(2.0);
      gen.returnValue();
      gen.endMethod();     
     
      byte[] bc = classWriter.toByteArray();
      return defineClass(className, bc, 0, bc.length);
    }
View Full Code Here

    String className = "com.github.mustachejava.codegen.RunCodes" + classId;
    String internalClassName = className.replace(".", "/");
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, internalClassName, null, "java/lang/Object", new String[]{CompiledCodes.class.getName().replace(".", "/")});
    cw.visitSource("runCodes", null);

    GeneratorAdapter cm = new GeneratorAdapter(Opcodes.ACC_PUBLIC, getMethod("void <init> (com.github.mustachejava.Code[])"), null, null, cw);
    cm.loadThis();
    cm.invokeConstructor(Type.getType(Object.class), getMethod("void <init> ()"));
    {
      GeneratorAdapter gm = new GeneratorAdapter(Opcodes.ACC_PUBLIC, getMethod("java.io.Writer runCodes(java.io.Writer, Object[])"), null, null, cw);
      int writerLocal = gm.newLocal(Type.getType(Writer.class));
      // Put the writer in our local
      gm.loadArg(0);
      gm.storeLocal(writerLocal);
      int fieldNum = 0;
      for (Code newcode : newcodes) {
        Class<? extends Code> codeClass = newcode.getClass();
        Class fieldClass = codeClass;
        while(fieldClass.isAnonymousClass() || fieldClass.isLocalClass() || (fieldClass.getModifiers() & Modifier.PUBLIC) == 0) {
          if (codeClass.getSuperclass() != Object.class && codeClass.getSuperclass().isAssignableFrom(Code.class)) {
            fieldClass = codeClass.getSuperclass();
          } else {
            fieldClass = Code.class;
          }
        }
        Type fieldType = Type.getType(fieldClass);

        // add a field for each one to the class
        String fieldName = "code" + fieldNum;
        cw.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, fieldName, fieldType.getDescriptor(), null, null);

        // set the fields to the passed in values in the constructor
        cm.loadThis();
        cm.loadArg(0);
        cm.push(fieldNum);
        cm.arrayLoad(Type.getType(Code.class));
        cm.checkCast(fieldType);
        cm.putField(Type.getType(internalClassName), fieldName, fieldType);

        // writer, scopes)
        gm.loadThis();
        gm.getField(Type.getType(internalClassName), fieldName, fieldType);
        gm.loadLocal(writerLocal);
        gm.loadArg(1);
        // code.execute(
        if (fieldClass.isInterface()) {
          gm.invokeInterface(fieldType, EXECUTE_METHOD);
        } else {
          gm.invokeVirtual(fieldType, EXECUTE_METHOD);
        }
        // writer =
        gm.storeLocal(writerLocal);

        fieldNum++;
      }
      cm.returnValue();
      cm.endMethod();

      // Load writer and return it
      gm.loadLocal(writerLocal);
      gm.returnValue();
      gm.endMethod();
    }

    cw.visitEnd();
    Class<?> aClass = GuardCompiler.defineClass(className, cw.toByteArray());
    try {
View Full Code Here

    String internalClassName = className.replace(".", "/");
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, internalClassName, null, "java/lang/Object", new String[]{Guard.class.getName().replace(".", "/")});
    cw.visitSource(source, null);

    // Constructor
    GeneratorAdapter cm = new GeneratorAdapter(Opcodes.ACC_PUBLIC, getMethod("void <init> (Object[])"), null, null, cw);
    cm.loadThis();
    cm.invokeConstructor(Type.getType(Object.class), getMethod("void <init> ()"));

    // Static initializer
    GeneratorAdapter sm = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, getMethod("void <clinit> ()"), null, null, cw);

    // Method implementation
    GeneratorAdapter gm = new GeneratorAdapter(Opcodes.ACC_PUBLIC, getMethod("boolean apply(Object[])"), null, null, cw);
    Label returnFalse = new Label();

    // Add each guard in the list
    List<Object> cargs = new ArrayList<Object>();
    for (CompilableGuard guard : guards) {
      guard.addGuard(returnFalse, gm, cm, sm, cw, id, cargs, Type.getType(internalClassName));
    }

    // Makes it through the guard, success
    gm.push(true);
    gm.returnValue();
    // Jumps to returnFalse, failure
    gm.visitLabel(returnFalse);
    gm.push(false);
    gm.returnValue();
    gm.endMethod();

    // Close the constructor
    cm.returnValue();
    cm.endMethod();
View Full Code Here

     * @param paramAnnotations : the parameter annotations to move to this method.
     */
    private void generateMethodWrapper(int access, String name, String desc, String signature, String[] exceptions,
                                       List<LocalVariableNode> localVariables, List<ClassChecker.AnnotationDescriptor> annotations,
                                       Map<Integer, List<ClassChecker.AnnotationDescriptor>> paramAnnotations) {
        GeneratorAdapter mv = new GeneratorAdapter(cv.visitMethod(access, name, desc, signature, exceptions), access, name, desc);

        // If we have variables, we wraps the code within labels. The `lifetime` of the variables are bound to those
        // two variables.
        boolean hasArgumentLabels = localVariables != null && !localVariables.isEmpty();
        Label start = null;
        if (hasArgumentLabels) {
            start = new Label();
            mv.visitLabel(start);
        }

        mv.visitCode();

        Type returnType = Type.getReturnType(desc);

        // Compute result and exception stack location
        int result = -1;
        int exception;

        //int arguments = mv.newLocal(Type.getType((new Object[0]).getClass()));

        if (returnType.getSort() != Type.VOID) {
            // The method returns something
            result = mv.newLocal(returnType);
            exception = mv.newLocal(Type.getType(Throwable.class));
        } else {
            exception = mv.newLocal(Type.getType(Throwable.class));
        }

        Label l0 = new Label();
        Label l1 = new Label();
        Label l2 = new Label();

        mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Throwable");

        // Access the flag from the outer class
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_name, "this$0", "L" + m_outer + ";");
        mv.visitFieldInsn(GETFIELD, m_outer, getMethodFlagName(name, desc), "Z");
        mv.visitJumpInsn(IFNE, l0);

        mv.visitVarInsn(ALOAD, 0);
        mv.loadArgs();
        mv.visitMethodInsn(INVOKESPECIAL, m_name, ClassManipulator.PREFIX + name, desc, false);
        mv.visitInsn(returnType.getOpcode(IRETURN));

        // end of the non intercepted method invocation.

        mv.visitLabel(l0);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_name, "this$0", "L" + m_outer + ";");
        mv.visitFieldInsn(GETFIELD, m_outer, ClassManipulator.IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(getMethodId(name, desc));
        mv.loadArgArray();
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", ClassManipulator.ENTRY,
                "(Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;)V", false);

        mv.visitVarInsn(ALOAD, 0);

        // Do not allow argument modification : just reload arguments.
        mv.loadArgs();
        mv.visitMethodInsn(INVOKESPECIAL, m_name, ClassManipulator.PREFIX + name, desc, false);

        if (returnType.getSort() != Type.VOID) {
            mv.visitVarInsn(returnType.getOpcode(ISTORE), result);
        }

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_name, "this$0", "L" + m_outer + ";");
        mv.visitFieldInsn(GETFIELD, m_outer, ClassManipulator.IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(getMethodId(name, desc));
        if (returnType.getSort() != Type.VOID) {
            mv.visitVarInsn(returnType.getOpcode(ILOAD), result);
            mv.box(returnType);
        } else {
            mv.visitInsn(ACONST_NULL);
        }
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager",
                ClassManipulator.EXIT, "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V", false);

        mv.visitLabel(l1);
        Label l7 = new Label();
        mv.visitJumpInsn(GOTO, l7);
        mv.visitLabel(l2);

        mv.visitVarInsn(ASTORE, exception);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_name, "this$0", "L" + m_outer + ";");
        mv.visitFieldInsn(GETFIELD, m_outer, ClassManipulator.IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(getMethodId(name, desc));
        mv.visitVarInsn(ALOAD, exception);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", ClassManipulator.ERROR,
                "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Throwable;)V", false);
        mv.visitVarInsn(ALOAD, exception);
        mv.visitInsn(ATHROW);

        mv.visitLabel(l7);
        if (returnType.getSort() != Type.VOID) {
            mv.visitVarInsn(returnType.getOpcode(ILOAD), result);
        }
        mv.visitInsn(returnType.getOpcode(IRETURN));

        // If we had arguments, we mark the end of the lifetime.
        Label end = null;
        if (hasArgumentLabels) {
            end = new Label();
            mv.visitLabel(end);
        }

        // Move annotations
        if (annotations != null) {
            for (ClassChecker.AnnotationDescriptor ad : annotations) {
                ad.visitAnnotation(mv);
            }
        }

        // Move parameter annotations
        if (paramAnnotations != null  && ! paramAnnotations.isEmpty()) {
            for (Integer id : paramAnnotations.keySet()) {
                List<ClassChecker.AnnotationDescriptor> ads = paramAnnotations.get(id);
                for (ClassChecker.AnnotationDescriptor ad : ads) {
                    ad.visitParameterAnnotation(id, mv);
                }
            }
        }

        // Write the arguments name.
        if (hasArgumentLabels) {
            for (LocalVariableNode var : localVariables) {
                mv.visitLocalVariable(var.name, var.desc, var.signature, start, end, var.index);
            }
        }

        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
View Full Code Here

     * @param locals : the local variables from the original constructors.
     */
    private void generateConstructor(int access, String descriptor, String signature, String[] exceptions,
                                     List<AnnotationDescriptor> annotations, Map<Integer,
            List<AnnotationDescriptor>> paramAnnotations, LinkedHashMap<Integer, LocalVariableNode> locals) {
         GeneratorAdapter mv = new GeneratorAdapter(
                 cv.visitMethod(access, "<init>", descriptor, signature, exceptions),
                 access, "<init>", descriptor);
         // Compute the new signature
         String newDesc = descriptor.substring(1); // Remove the first (
         newDesc = "(Lorg/apache/felix/ipojo/InstanceManager;" + newDesc;

         mv.visitCode();
         Label start = new Label();
         mv.visitLabel(start);
         mv.visitVarInsn(ALOAD, 0);
         mv.visitInsn(ACONST_NULL);
         mv.loadArgs();
         mv.visitMethodInsn(INVOKESPECIAL, m_owner, "<init>", newDesc, false);
         mv.visitInsn(RETURN);
         Label stop = new Label();
         mv.visitLabel(stop);

         // Move annotations
         if (annotations != null) {
             for (AnnotationDescriptor ad : annotations) {
                 ad.visitAnnotation(mv);
             }
         }

         // Move parameter annotations if any
         if (paramAnnotations != null  && ! paramAnnotations.isEmpty()) {
             for (Integer id : paramAnnotations.keySet()) {
                 List<AnnotationDescriptor> ads = paramAnnotations.get(id);
                 for (AnnotationDescriptor ad : ads) {
                     ad.visitParameterAnnotation(id, mv);
                 }
             }
         }

         // Add local variables for the arguments.
        for (Map.Entry<Integer, LocalVariableNode> local : locals.entrySet()) {
            // Write the parameter name. Only write the local variable that are either `this` or parameters from the
            // initial descriptor.
            if (local.getValue().index <= Type.getArgumentTypes(descriptor).length) {
                mv.visitLocalVariable(local.getValue().name, local.getValue().desc, local.getValue().signature, start,stop,
                        local.getValue().index);
            }
        }

         mv.visitMaxs(0, 0);
         mv.visitEnd();
    }
View Full Code Here

     * @param paramAnnotations : the parameter annotations to move to this method.
     */
    private void generateMethodHeader(int access, String name, String desc, String signature, String[] exceptions,
                                      List<LocalVariableNode> localVariables, List<AnnotationDescriptor> annotations,
                                      Map<Integer, List<AnnotationDescriptor>> paramAnnotations) {
        GeneratorAdapter mv = new GeneratorAdapter(cv.visitMethod(access, name, desc, signature, exceptions), access, name, desc);
        mv.visitCode();

        // If we have variables, we wraps the code within labels. The `lifetime` of the variables are bound to those
        // two variables.
        boolean hasArgumentLabels = localVariables != null && !localVariables.isEmpty();
        Label start = null;
        if (hasArgumentLabels) {
            start = new Label();
            mv.visitLabel(start);
        }

        mv.visitCode();

        Type returnType = Type.getReturnType(desc);

        // Compute result and exception stack location
        int result = -1;
        int exception;

        //int arguments = mv.newLocal(Type.getType((new Object[0]).getClass()));

        if (returnType.getSort() != Type.VOID) {
            // The method returns something
            result = mv.newLocal(returnType);
            exception = mv.newLocal(Type.getType(Throwable.class));
        } else {
            exception = mv.newLocal(Type.getType(Throwable.class));
        }

        Label l0 = new Label();
        Label l1 = new Label();
        Label l2 = new Label();

        mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Throwable");

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, generateMethodFlag(name, desc), "Z");
        mv.visitJumpInsn(IFNE, l0);

        mv.visitVarInsn(ALOAD, 0);
        mv.loadArgs();
        mv.visitMethodInsn(INVOKESPECIAL, m_owner, PREFIX + name, desc, false);
        mv.visitInsn(returnType.getOpcode(IRETURN));

        // end of the non intercepted method invocation.

        mv.visitLabel(l0);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(generateMethodId(name, desc));
        mv.loadArgArray();
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", ENTRY,
                "(Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;)V", false);

        mv.visitVarInsn(ALOAD, 0);

        // Do not allow argument modification : just reload arguments.
        mv.loadArgs();
        mv.visitMethodInsn(INVOKESPECIAL, m_owner, PREFIX + name, desc, false);

        if (returnType.getSort() != Type.VOID) {
            mv.visitVarInsn(returnType.getOpcode(ISTORE), result);
        }

        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(generateMethodId(name, desc));
        if (returnType.getSort() != Type.VOID) {
            mv.visitVarInsn(returnType.getOpcode(ILOAD), result);
            mv.box(returnType);
        } else {
            mv.visitInsn(ACONST_NULL);
        }
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", EXIT,
                "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V", false);

        mv.visitLabel(l1);
        Label l7 = new Label();
        mv.visitJumpInsn(GOTO, l7);
        mv.visitLabel(l2);

        mv.visitVarInsn(ASTORE, exception);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;");
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(generateMethodId(name, desc));
        mv.visitVarInsn(ALOAD, exception);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", ERROR,
                "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Throwable;)V", false);
        mv.visitVarInsn(ALOAD, exception);
        mv.visitInsn(ATHROW);

        mv.visitLabel(l7);
        if (returnType.getSort() != Type.VOID) {
            mv.visitVarInsn(returnType.getOpcode(ILOAD), result);
        }
        mv.visitInsn(returnType.getOpcode(IRETURN));

        // If we had arguments, we mark the end of the lifetime.
        Label end = null;
        if (hasArgumentLabels) {
            end = new Label();
            mv.visitLabel(end);
        }

        // Move annotations
        if (annotations != null) {
            for (AnnotationDescriptor ad : annotations) {
                ad.visitAnnotation(mv);
            }
        }

        // Move parameter annotations
        if (paramAnnotations != null  && ! paramAnnotations.isEmpty()) {
            for (Integer id : paramAnnotations.keySet()) {
                List<AnnotationDescriptor> ads = paramAnnotations.get(id);
                for (AnnotationDescriptor ad : ads) {
                    ad.visitParameterAnnotation(id, mv);
                }
            }
        }

        // Write the arguments name.
        if (hasArgumentLabels) {
            for (LocalVariableNode var : localVariables) {
                mv.visitLocalVariable(var.name, var.desc, var.signature, start, end, var.index);
            }
        }

        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
View Full Code Here

        method().invokeVirtual(Type.getType(Object.class), Method.getMethod("Object " + call.getMethodAddr() + " ()"));
    }

    public void emitDEF_INST_METH(DefineInstanceMethodInstr instr) {
        IRMethod irMethod = instr.method;
        GeneratorAdapter adapter = new GeneratorAdapter(ACC_PUBLIC, Method.getMethod("void " + irMethod.getName() + " ()"), null, null, cls());
        adapter.loadThis();
        adapter.loadArgs();
        adapter.invokeStatic(Type.getType(Object.class), Method.getMethod("Object __ruby__" + irMethod.getName() + " (Object)"));
        adapter.returnValue();
        adapter.endMethod();
    }
View Full Code Here

        adapter.endMethod();
    }

    public void emitDEF_CLS_METH(DefineClassMethodInstr instr) {
        IRMethod irMethod = instr.method;
        GeneratorAdapter adapter = new GeneratorAdapter(ACC_PUBLIC | ACC_STATIC, Method.getMethod("void " + irMethod.getName() + " ()"), null, null, cls());
        adapter.returnValue();
        adapter.endMethod();
    }
View Full Code Here

TOP

Related Classes of org.objectweb.asm.commons.GeneratorAdapter

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.