Example usage for org.objectweb.asm.commons Method getMethod

List of usage examples for org.objectweb.asm.commons Method getMethod

Introduction

In this page you can find the example usage for org.objectweb.asm.commons Method getMethod.

Prototype

public static Method getMethod(final String method) 

Source Link

Document

Returns a Method corresponding to the given Java method declaration.

Usage

From source file:com.mulberry.athena.asm.ASMTest.java

License:Open Source License

@Test
public void generateClassUseClassVisitor() throws Exception {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    ClassVisitor cv = new TraceClassVisitor(cw, new PrintWriter(System.out));
    cv.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "Hello", null, "java/lang/Object", null);
    {//ww  w . jav  a  2s . c  o  m
        GeneratorAdapter clinitgen = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC, clinit, null, null, cv);
        clinitgen.visitCode();
        clinitgen.visitLineNumber(1, clinitgen.mark());
        clinitgen.returnValue();
        clinitgen.endMethod();
    }

    {
        GeneratorAdapter ctorgen = new GeneratorAdapter(ACC_PUBLIC, voidctor, null, null, cv);
        Label start = ctorgen.newLabel();
        Label end = ctorgen.newLabel();
        ctorgen.visitCode();
        ctorgen.visitLineNumber(2, ctorgen.mark());
        ctorgen.visitLabel(start);
        ctorgen.loadThis();
        ctorgen.invokeConstructor(Type.getObjectType("java/lang/Object"), voidctor);
        ctorgen.visitLabel(end);
        ctorgen.returnValue();
        ctorgen.endMethod();
    }
    {
        GeneratorAdapter gen = new GeneratorAdapter(ACC_PUBLIC, Method.getMethod("int getRequiredArity()"),
                null, null, cv);
        gen.visitCode();
        gen.push(3);
        gen.returnValue();
        gen.endMethod();
    }

    {
        Method method = Method.getMethod("void main(java/lang/String[])", true);
        GeneratorAdapter maingen = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC, method, null, null, cv);
        Label start = maingen.newLabel();
        maingen.visitCode();
        maingen.visitLineNumber(3, maingen.mark());
        maingen.visitLabel(start);
        maingen.getStatic(Type.getType("java/lang/System"), "out", Type.getType(PrintStream.class));
        maingen.push("IT WORKS!");
        maingen.invokeVirtual(Type.getType("java/io/PrintStream"),
                Method.getMethod("void println(java/lang/String)", true));
        Label end = maingen.newLabel();
        maingen.visitLabel(end);
        maingen.returnValue();
        maingen.endMethod();
    }

    cv.visitEnd();

    Class<?> clazz = new DynamicClassLoader().defineClass("Hello", cw.toByteArray());
    java.lang.reflect.Method method = clazz.getMethod("main", String[].class);
    final String[] arguments = new String[] { "hello", "world" };
    method.invoke(clazz, (Object) arguments);
}

From source file:com.mulberry.athena.asm.ASMTest.java

License:Open Source License

public static void main(String[] args) {
    System.out.println(Method.getMethod("void main([Ljava/lang/String;)"));
}

From source file:com.quartercode.jtimber.rh.agent.asm.InsertJAXBTweaksClassAdapter.java

License:Open Source License

private void generateAfterUnmarshalMethod(Method method) {

    GeneratorAdapter mg = new GeneratorAdapter(ACC_PUBLIC, method, null, null, cv);

    /*/*from  ww  w  . ja va 2  s  . c  o  m*/
     * Iterate through all fields annotated with "SubstituteWithWrapper" and replace their current value with their current value wrapped inside a wrapper.
     * This section first reads the current field value, then creates a new wrapper which wraps around that field value, and finally sets the field to the wrapper.
     */
    for (Triple<String, Type, Type> field : fieldsForWrapperSubstitution) {
        String fieldName = field.getLeft();
        Type fieldType = fields.get(fieldName);
        Type wrapperType = field.getMiddle();
        // Null means "default"; in that case, the field type is used as the type of the first argument of the wrapper constructor
        Type wrapperConstructorArgType = field.getRight() == null ? fieldType : field.getRight();

        // Note that this reference will be used for the PUTFIELD instruction later on
        mg.loadThis();

        // ----- Stack: [this]

        // Creates the wrapper using the current field value
        {
            // Create a new instance of the wrapper type and duplicate it for the constructor call later on
            mg.newInstance(wrapperType);
            mg.dup();

            // ----- Stack: [this, wrapper, wrapper]

            // Retrieve the current field value
            ASMUtils.generateGetField(mg, classType, fieldName, fieldType);

            // ----- Stack: [this, wrapper, wrapper, fieldValue]

            // Call the constructor of the new wrapper using the current field value as the first argument
            mg.invokeConstructor(wrapperType,
                    Method.getMethod("void <init> (" + wrapperConstructorArgType.getClassName() + ")"));

            // ----- Stack: [this, wrapper]
        }

        // Store the new wrapper in the field the value has been retrieved from before
        // The substitution is complete
        mg.putField(classType, fieldName, fieldType);

        // ----- Stack: []
    }

    /*
     * Iterate through all fields.
     * For each field, call the addParent() method with "this" as parent if the current field value is parent-aware
     */
    for (Entry<String, Type> field : fields.entrySet()) {
        String fieldName = field.getKey();
        Type fieldType = field.getValue();

        ASMUtils.generateGetField(mg, classType, fieldName, fieldType);

        // ----- Stack: [fieldValue]

        ASMUtils.generateAddOrRemoveThisAsParent(mg, "addParent");
    }

    // End the method
    mg.returnValue();
    mg.endMethod();
}

From source file:com.xruby.compiler.codegen.ClassGeneratorForNoArgRubyMethod.java

License:BSD License

protected MethodGenerator createMethodGenerator() {
    return new MethodGenerator(Opcodes.ACC_PROTECTED, Method.getMethod(this.getRunMethodName()), cv_, null,
            null, false) {//  w  ww . j  a  va  2 s .c  o  m
        public void loadCurrentBlock() {
            this.loadArg(1);
        }

        public void loadMethodArg() {
        }
    };
}

From source file:com.xruby.compiler.codegen.FieldManager.java

License:BSD License

private MethodGenerator visitRubyBlock() {
    cv_.visit(CgConfig.TARGET_VERSION, Opcodes.ACC_PUBLIC, //need to modify fields when doing Proc#call, see RubyProc.java
            name_, null, // signature
            this.helper.getSuperName(), // superName
            null // interface
    );/*from ww  w.ja  va2s .  c o  m*/

    // set source file's name, for debug
    if (file_name_ != null) {
        cv_.visitSource(file_name_, null);
    }

    return new MethodGenerator(Opcodes.ACC_PROTECTED, Method.getMethod(this.helper.getRunMethodName()), cv_,
            null, symbol_table_of_the_current_scope_, false);
}

From source file:com.xruby.compiler.codegen.FieldManager.java

License:BSD License

private void createConstructorOfRubyBlock() {

    MethodGenerator mg = new MethodGenerator(Opcodes.ACC_PUBLIC, Method.getMethod(buildContructorSignature()),
            cv_, null, symbol_table_of_the_current_scope_, false);

    mg.loadThis();/*from  w w  w.  j a v  a 2 s  . c  o  m*/

    this.helper.pushBasciArgForSuperArg(mg, argc_, has_asterisk_parameter_, default_argc_);

    final int FIXED_PARAMETERS = 7;
    for (int i = 0; i < FIXED_PARAMETERS; ++i) {
        mg.loadArg(i);
    }

    mg.invokeConstructor(this.helper.getSuperClassType(), Method.getMethod(this.helper.getSuperCtorName()));

    mg.returnValue();
    mg.endMethod();
}

From source file:com.xruby.compiler.codegen.ClassGeneratorForRubyMethod.java

License:BSD License

protected MethodGenerator createMethodGenerator() {
    return new MethodGenerator(Opcodes.ACC_PROTECTED, Method.getMethod(this.getRunMethodName()), cv_, null,
            null, false);//  w ww  .  ja v  a 2 s  .c o m
}

From source file:com.xruby.compiler.codegen.ClassGeneratorForRubyMethod.java

License:BSD License

private void createCtorForRubyMethod(int argc, boolean has_asterisk_parameter, int default_argc) {
    MethodGenerator mg = new MethodGenerator(Opcodes.ACC_PUBLIC, CgUtil.CONSTRUCTOR, cv_, null, null, false);
    mg.loadThis();// w  w w .  j a  v a2 s .  co m

    this.pushBasciArgForSuperArg(mg, argc, has_asterisk_parameter, default_argc);

    mg.invokeConstructor(getSuperClassType(), Method.getMethod(this.getSuperCtorName()));
    mg.returnValue();
    mg.endMethod();
}

From source file:com.xruby.compiler.codegen.ClassGeneratorForRubyProgram.java

License:BSD License

private void createStaticVoidMain(ClassVisitor cv) {
    MethodGenerator mg = new MethodGenerator(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC,
            Method.getMethod("void main (String[])"), cv, null, null, false);

    mg.loadArg(0);/*from  w  w  w.j  a va  2 s.co  m*/
    mg.invokeStatic(Types.RUBY_RUNTIME_TYPE, Method.getMethod("void init(String[])"));

    Type program = Type.getType("L" + name_ + ";");
    mg.newInstance(program);
    mg.dup();
    mg.invokeConstructor(program, CgUtil.CONSTRUCTOR);
    mg.invokeVirtual(program, CgUtil.getMethod("invoke", Types.RUBY_VALUE_TYPE));
    mg.pop();

    mg.invokeStatic(Types.RUBY_RUNTIME_TYPE, CgUtil.getMethod("fini", Type.VOID_TYPE));

    mg.returnValue();
    mg.endMethod();
}

From source file:com.xruby.compiler.codegen.MethodGenerator.java

License:BSD License

public void returnIfBlockReturned() {
    dup();//w  w w .j a v a  2s  . c o  m
    invokeVirtual(Types.RUBY_VALUE_TYPE, Method.getMethod("boolean returnedInBlock()"));
    Label after_return = new Label();
    ifZCmp(GeneratorAdapter.EQ, after_return);
    returnValue();//TODO more error checking, may not in the method context
    mark(after_return);
    /*TODO if it is going to return any way, should not not check.
      Right now the code may look like:
      if(!rubyvalue5.returnedInBlock()) goto _L2; else goto _L1
      _L1:
              return;
      _L2:
              return;
      */
}