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

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

Introduction

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

Prototype

public String getName() 

Source Link

Document

Returns the name of the method described by this object.

Usage

From source file:com.google.code.nanorm.internal.introspect.asm.MapperBuilder.java

License:Apache License

/**
 * Generate mapper method./*from   w w w  . j a v  a  2s  . c  om*/
 * 
 * @param owner self type
 * @param cw class writer
 * @param config method configuration
 */
private static void visitMethod(Type owner, ClassWriter cw, MethodConfig config) {
    java.lang.reflect.Method ifaceMethod = config.getMethod();
    Type returnType = Type.getType(ifaceMethod.getReturnType());
    Type[] args = new Type[ifaceMethod.getParameterTypes().length];
    for (int i = 0; i < args.length; ++i) {
        args[i] = Type.getType(ifaceMethod.getParameterTypes()[i]);
    }

    Method method = new Method(ifaceMethod.getName(), returnType, args);

    GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, method, null, null, cw);

    // Factory
    mg.loadThis();
    mg.getField(owner, "delegate", QUERY_DELEGATE_TYPE);

    // Statement config
    mg.loadThis();
    mg.getField(owner, "configs", STATEMENT_CONFIGS_ARR_TYPE);
    mg.push(config.getIndex());
    mg.arrayLoad(Type.getType(StatementConfig.class));

    // Arguments
    mg.loadArgArray();

    mg.invokeInterface(QUERY_DELEGATE_TYPE, QUERY_METHOD);
    mg.unbox(returnType);
    mg.returnValue();
    mg.endMethod();

    // Copy the annotations
    copyAnnotations(ifaceMethod, null, mg);
}

From source file:com.google.gwt.dev.shell.rewrite.RewriteSingleJsoImplDispatches.java

License:Apache License

/**
 * For regular Java objects that implement a SingleJsoImpl interface, write
 * instance trampoline dispatchers for mangled method names to the
 * implementing method./*from  w w  w .  java 2 s  .  co  m*/
 */
private void writeTrampoline(String stubIntr) {
    /*
     * This is almost the same kind of trampoline as the ones generated in
     * WriteJsoImpl, however there are enough small differences between the
     * semantics of the dispatches that would make a common implementation far
     * more awkward than the duplication of code.
     */
    for (String mangledName : toImplement(stubIntr)) {
        for (Method method : jsoData.getDeclarations(mangledName)) {

            Method toCall = new Method(method.getName(), method.getDescriptor());

            // Must not be final
            MethodVisitor mv = super.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, mangledName,
                    method.getDescriptor(), null, null);
            if (mv != null) {
                mv.visitCode();

                /*
                 * It just so happens that the stack and local variable sizes are the
                 * same, but they're kept distinct to aid in clarity should the
                 * dispatch logic change.
                 *
                 * These start at 1 because we need to load "this" onto the stack
                 */
                int var = 1;
                int size = 1;

                // load this
                mv.visitVarInsn(Opcodes.ALOAD, 0);

                // then the rest of the arguments
                for (Type t : toCall.getArgumentTypes()) {
                    size += t.getSize();
                    mv.visitVarInsn(t.getOpcode(Opcodes.ILOAD), var);
                    var += t.getSize();
                }

                // Make sure there's enough room for the return value
                size = Math.max(size, toCall.getReturnType().getSize());

                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, currentTypeName, toCall.getName(),
                        toCall.getDescriptor(), false);
                mv.visitInsn(toCall.getReturnType().getOpcode(Opcodes.IRETURN));
                mv.visitMaxs(size, var);
                mv.visitEnd();
            }
        }
    }
}

From source file:com.google.template.soy.jbcsrc.CodeBuilder.java

License:Apache License

CodeBuilder(int access, Method method, MethodVisitor mv) {
    this(mv, access, method.getName(), method.getDescriptor());
}

From source file:com.google.template.soy.jbcsrc.CodeBuilder.java

License:Apache License

CodeBuilder(int access, Method method, @Nullable Type[] exceptions, ClassVisitor cv) {
    this(access, method, cv.visitMethod(access, method.getName(), method.getDescriptor(),
            null /* generic signature */, getInternalNames(exceptions)));
}

From source file:com.google.template.soy.jbcsrc.ConstructorRef.java

License:Apache License

/** 
 * Returns a new {@link ConstructorRef} that refers to a constructor on the given type with the
 * given parameter types.//from  w  w  w  .ja  v a 2 s  . co  m
 */
static ConstructorRef create(TypeInfo type, Method init) {
    checkArgument(init.getName().equals("<init>") && init.getReturnType().equals(Type.VOID_TYPE),
            "'%s' is not a valid constructor", init);
    return new AutoValue_ConstructorRef(type, init, ImmutableList.copyOf(init.getArgumentTypes()));
}

From source file:com.google.template.soy.jbcsrc.restricted.CodeBuilder.java

License:Apache License

public CodeBuilder(int access, Method method, MethodVisitor mv) {
    this(mv, access, method.getName(), method.getDescriptor());
}

From source file:com.google.template.soy.jbcsrc.restricted.CodeBuilder.java

License:Apache License

public CodeBuilder(int access, Method method, @Nullable Type[] exceptions, ClassVisitor cv) {
    this(access, method, cv.visitMethod(access, method.getName(), method.getDescriptor(),
            null /* generic signature */, getInternalNames(exceptions)));
}

From source file:com.google.template.soy.jbcsrc.restricted.ConstructorRef.java

License:Apache License

/**
 * Returns a new {@link ConstructorRef} that refers to a constructor on the given type with the
 * given parameter types./* w  w w  .  j  a va 2  s .c  o m*/
 */
public static ConstructorRef create(TypeInfo type, Method init) {
    checkArgument(init.getName().equals("<init>") && init.getReturnType().equals(Type.VOID_TYPE),
            "'%s' is not a valid constructor", init);
    return new AutoValue_ConstructorRef(type, init, ImmutableList.copyOf(init.getArgumentTypes()));
}

From source file:com.googlecode.gwt.test.internal.rewrite.RewriteSingleJsoImplDispatches.java

License:Apache License

/**
 * For regular Java objects that implement a SingleJsoImpl interface, write instance trampoline
 * dispatchers for mangled method names to the implementing method.
 *//*from w  ww.ja va  2  s  . co  m*/
private void writeTrampoline(String stubIntr) {
    /*
     * This is almost the same kind of trampoline as the ones generated in WriteJsoImpl, however
     * there are enough small differences between the semantics of the dispatches that would make
     * a common implementation far more awkward than the duplication of code.
     */
    for (String mangledName : toImplement(stubIntr)) {
        for (Method method : jsoData.getDeclarations(mangledName)) {

            Method toCall = new Method(method.getName(), method.getDescriptor());

            // Must not be final
            MethodVisitor mv = super.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, mangledName,
                    method.getDescriptor(), null, null);
            if (mv != null) {
                mv.visitCode();

                /*
                 * It just so happens that the stack and local variable sizes are the same, but
                 * they're kept distinct to aid in clarity should the dispatch logic change.
                 *
                 * These start at 1 because we need to load "this" onto the stack
                 */
                int var = 1;
                int size = 1;

                // load this
                mv.visitVarInsn(Opcodes.ALOAD, 0);

                // then the rest of the arguments
                for (Type t : toCall.getArgumentTypes()) {
                    size += t.getSize();
                    mv.visitVarInsn(t.getOpcode(Opcodes.ILOAD), var);
                    var += t.getSize();
                }

                // Make sure there's enough room for the return value
                size = Math.max(size, toCall.getReturnType().getSize());

                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, currentTypeName, toCall.getName(),
                        toCall.getDescriptor());
                mv.visitInsn(toCall.getReturnType().getOpcode(Opcodes.IRETURN));
                mv.visitMaxs(size, var);
                mv.visitEnd();
            }
        }
    }
}

From source file:com.mogujie.instantrun.IncrementalChangeVisitor.java

License:Apache License

/**
 * To each class, add the dispatch method called by the original code that acts as a trampoline to
 * invoke the changed methods.//  w w  w .  j  av a  2s .  co m
 * <p/>
 * Pseudo code:
 * <code>
 * Object access$dispatch(String name, object[] args) {
 * if (name.equals(
 * "firstMethod.(L$type;Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;")) {
 * return firstMethod(($type)arg[0], (String)arg[1], arg[2]);
 * }
 * if (name.equals("secondMethod.(L$type;Ljava/lang/String;I;)V")) {
 * secondMethod(($type)arg[0], (String)arg[1], (int)arg[2]);
 * return;
 * }
 * ...
 * StringBuilder $local1 = new StringBuilder();
 * $local1.append("Method not found ");
 * $local1.append(name);
 * $local1.append(" in " + visitedClassName +
 * "$dispatch implementation, restart the application");
 * throw new $package/InstantReloadException($local1.toString());
 * }
 * </code>
 */
private void addDispatchMethod() {
    int access = Opcodes.ACC_PUBLIC | Opcodes.ACC_VARARGS;
    Method m = new Method("access$dispatch", "(I[Ljava/lang/Object;)Ljava/lang/Object;");
    MethodVisitor visitor = super.visitMethod(access, m.getName(), m.getDescriptor(), null, null);

    final GeneratorAdapter mv = new GeneratorAdapter(access, m, visitor);

    if (TRACING_ENABLED) {
        mv.push("Redirecting ");
        mv.loadArg(0);
        trace(mv, 2);
    }

    List<MethodNode> allMethods = new ArrayList();

    // if we are disabled, do not generate any dispatch, the method will throw an exception
    // if invoked which should never happen.
    if (!instantRunDisabled) {
        //noinspection unchecked
        allMethods.addAll(classNode.methods);
        allMethods.addAll(addedMethods);
    }

    final Map<String, MethodNode> methods = new HashMap();
    for (MethodNode methodNode : allMethods) {
        if (methodNode.name.equals("<clinit>") || methodNode.name.equals("<init>")) {
            continue;
        }
        if (!isAccessCompatibleWithInstantRun(methodNode.access)) {
            continue;
        }
        methods.put(methodNode.name + "." + methodNode.desc, methodNode);
    }

    new IntSwitch() {
        @Override
        void visitString() {
            mv.visitVarInsn(Opcodes.ALOAD, 1);
        }

        @Override
        void visitInt() {
            mv.visitVarInsn(Opcodes.ILOAD, 1);
        }

        @Override
        void visitCase(String methodName) {
            MethodNode methodNode = methods.get(methodName);
            String name = methodNode.name;
            boolean isStatic = (methodNode.access & Opcodes.ACC_STATIC) != 0;
            String newDesc = computeOverrideMethodDesc(methodNode.desc, isStatic);

            if (TRACING_ENABLED) {
                trace(mv, "M: " + name + " P:" + newDesc);
            }
            Type[] args = Type.getArgumentTypes(newDesc);
            int argc = 0;
            for (Type t : args) {
                mv.visitVarInsn(Opcodes.ALOAD, 2);
                mv.push(argc);
                mv.visitInsn(Opcodes.AALOAD);
                ByteCodeUtils.unbox(mv, t);
                argc++;
            }
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, visitedClassName + "$override",
                    isStatic ? computeOverrideMethodName(name, methodNode.desc) : name, newDesc, false);
            Type ret = Type.getReturnType(methodNode.desc);
            if (ret.getSort() == Type.VOID) {
                mv.visitInsn(Opcodes.ACONST_NULL);
            } else {
                mv.box(ret);
            }
            mv.visitInsn(Opcodes.ARETURN);
        }

        @Override
        void visitDefault() {
            writeMissingMessageWithHash(mv, visitedClassName);
        }
    }.visit(mv, methods.keySet(), visitedClassName);

    mv.visitMaxs(0, 0);
    mv.visitEnd();

}