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:blue.origami.asm.OAsmUtils.java

License:Apache License

public static final Method asmMethod(OType r, String methodName, OType... p) {
    StringBuilder sb = new StringBuilder();
    sb.append(r.getName());/*from   w  ww .  j  av  a2s .  com*/
    sb.append(" ");
    sb.append(methodName);
    sb.append("(");
    int c = 0;
    for (OType t : p) {
        if (c > 0) {
            sb.append(",");
        }
        sb.append(t.getName());
        c++;
    }
    sb.append(")");
    return Method.getMethod(sb.toString());
}

From source file:blue.origami.asm.OAsmUtils.java

License:Apache License

public static final Method asmMethod(java.lang.reflect.Method m) {
    return Method.getMethod(m);
}

From source file:ch.raffael.contracts.processor.cel.skeletons.Skeleton.java

License:Apache License

public Integer getLineNumber(java.lang.reflect.Method method) {
    if (!method.getDeclaringClass().equals(skeletonClass)) {
        return null;
    } else {//from  w  w  w .  jav  a2 s.  c  om
        return lineNumbers.get(Method.getMethod(method));
    }
}

From source file:co.cask.cdap.internal.io.DatumWriterGenerator.java

License:Apache License

private Method getGetter(TypeToken<?> outputType, String fieldName) {
    Class<?> rawType = outputType.getRawType();
    try {/* w  ww.j  a v  a 2 s .  c  o m*/
        return Method.getMethod(rawType.getMethod(
                String.format("get%c%s", Character.toUpperCase(fieldName.charAt(0)), fieldName.substring(1))));
    } catch (NoSuchMethodException e) {
        try {
            return Method.getMethod(rawType.getMethod(String.format("is%c%s",
                    Character.toUpperCase(fieldName.charAt(0)), fieldName.substring(1))));
        } catch (NoSuchMethodException ex) {
            throw new IllegalArgumentException("Getter method not found for field " + fieldName, ex);
        }
    }
}

From source file:co.cask.common.internal.asm.Methods.java

License:Apache License

public static Method getMethod(Class<?> returnType, String name, Class<?>... args) {
    StringBuilder builder = new StringBuilder(returnType.getName()).append(' ').append(name).append(" (");
    Joiner.on(',').appendTo(builder,
            Iterators.transform(Iterators.forArray(args), new Function<Class<?>, String>() {
                @Override/*from   w w w  .  j a  v  a 2  s  . co  m*/
                public String apply(Class<?> input) {
                    if (input.isArray()) {
                        return Type.getType(input.getName()).getClassName();
                    }
                    return input.getName();
                }
            }));
    builder.append(')');
    return Method.getMethod(builder.toString());
}

From source file:com.alibaba.hotswap.processor.constructor.ConstructorVisitor.java

License:Open Source License

private void addEmptyUniformConstructor() {
    int access = Opcodes.ACC_PUBLIC + Opcodes.ACC_SYNTHETIC;
    String name = HotswapConstants.INIT;
    String desc = HotswapConstants.UNIFORM_CONSTRUCTOR_DESC;

    MethodVisitor hotswapInit = cv.visitMethod(access, name, desc, null, null);
    GeneratorAdapter hotswapInitAdapter = new GeneratorAdapter(hotswapInit, access, name, desc);
    hotswapInitAdapter.visitCode();// ww  w. j  a va  2s.  c  o m
    hotswapInitAdapter.push(this.className);
    hotswapInitAdapter.loadArg(1);
    hotswapInitAdapter.invokeStatic(Type.getType(HotswapMethodUtil.class),
            Method.getMethod("Throwable noSuchMethodError(String, int)"));
    hotswapInitAdapter.throwException();
    hotswapInitAdapter.endMethod();
}

From source file:com.alibaba.hotswap.processor.constructor.ConstructorVisitor.java

License:Open Source License

@SuppressWarnings({ "unchecked" })
private void addUniformConstructor(ClassMeta classMeta) {
    int access = Opcodes.ACC_PUBLIC + Opcodes.ACC_SYNTHETIC;
    String name = HotswapConstants.INIT;
    String desc = HotswapConstants.UNIFORM_CONSTRUCTOR_DESC;

    MethodVisitor hotswapInit = new ConstructorInvokeModifier(cv.visitMethod(access, name, desc, null, null),
            access, name, desc);/*from  w  w  w . j  ava2 s  .c o  m*/
    GeneratorAdapter hotswapInitAdapter = new GeneratorAdapter(hotswapInit, access, name, desc);

    hotswapInitAdapter.visitCode();

    TreeMap<MethodMeta, MethodNode> initMethodMap = new TreeMap<MethodMeta, MethodNode>(
            new ConstructorIndexComparator());

    for (MethodNode node : initNodes.values()) {
        MethodMeta meta = new MethodMeta(node.access, node.name, node.desc, node.signature,
                ((String[]) node.exceptions.toArray(new String[node.exceptions.size()])));
        meta.setIndex(HotswapMethodIndexHolder.getMethodIndex(className, node.name, node.desc));
        classMeta.refreshInitMeta(meta, true);
        initMethodMap.put(meta, node);
    }

    List<MethodMeta> keys = new ArrayList<MethodMeta>(initMethodMap.keySet());
    List<MethodNode> values = new ArrayList<MethodNode>(initMethodMap.values());

    Label defaultLabel = new Label();
    int[] indexes = new int[keys.size()];
    Label[] labels = new Label[keys.size()];

    for (int i = 0; i < keys.size(); i++) {
        indexes[i] = keys.get(i).getIndex();
        labels[i] = new Label();
    }

    for (int i = 0; i < values.size(); i++) {
        MethodNode node = values.get(i);
        for (int j = 0; j < node.tryCatchBlocks.size(); j++) {
            ((TryCatchBlockNode) node.tryCatchBlocks.get(j)).accept(hotswapInitAdapter);
        }
    }

    hotswapInitAdapter.loadArg(1);
    hotswapInitAdapter.visitLookupSwitchInsn(defaultLabel, indexes, labels);

    for (int i = 0; i < keys.size(); i++) {
        MethodMeta methodMeta = keys.get(i);
        hotswapInitAdapter.visitLabel(labels[i]);
        MethodNode node = values.get(i);

        storeArgs(hotswapInitAdapter, hotswapInit, methodMeta);
        MethodVisitor methodVisitor = new ConstructorLVTAdjustModifier(hotswapInit, 3);

        node.instructions.accept(methodVisitor);

        for (int j = 0; j < (node.localVariables == null ? 0 : node.localVariables.size()); j++) {
            ((LocalVariableNode) node.localVariables.get(j)).accept(methodVisitor);
        }
    }
    hotswapInitAdapter.mark(defaultLabel);

    hotswapInitAdapter.push(this.className);
    hotswapInitAdapter.loadArg(1);
    hotswapInitAdapter.invokeStatic(Type.getType(HotswapMethodUtil.class),
            Method.getMethod("Throwable noSuchMethodError(String, int)"));
    hotswapInitAdapter.throwException();
    hotswapInitAdapter.endMethod();
}

From source file:com.android.build.gradle.internal.incremental.ByteCodeUtilsTest.java

License:Apache License

@Test
public void testShortUnbox() {
    ByteCodeUtils.unbox(generator, Type.SHORT_TYPE);
    verify(generator, times(1)).checkCast(Type.getObjectType("java/lang/Number"));
    verify(generator, times(1)).invokeVirtual(Type.getObjectType("java/lang/Number"),
            Method.getMethod("short shortValue()"));
}

From source file:com.android.build.gradle.internal.incremental.ByteCodeUtilsTest.java

License:Apache License

@Test
public void testByteUnbox() {
    ByteCodeUtils.unbox(generator, Type.BYTE_TYPE);
    verify(generator, times(1)).checkCast(Type.getObjectType("java/lang/Number"));
    verify(generator, times(1)).invokeVirtual(Type.getObjectType("java/lang/Number"),
            Method.getMethod("byte byteValue()"));
}

From source file:com.android.build.gradle.internal.incremental.ConstructorRedirection.java

License:Apache License

@Override
protected void doRedirect(GeneratorAdapter mv, int change) {
    mv.loadLocal(change);/*from   w  w  w.j a va2  s  . c  o  m*/
    mv.push("init$args." + constructor.args.desc);

    Type arrayType = Type.getType("[Ljava/lang/Object;");
    // init$args args (including this) + locals
    mv.push(types.size() + 1);
    mv.newArray(Type.getType(Object.class));

    int array = mv.newLocal(arrayType);
    mv.dup();
    mv.storeLocal(array);

    // "this" is not ready yet, use null instead.
    mv.dup();
    mv.push(0);
    mv.visitInsn(Opcodes.ACONST_NULL);
    mv.arrayStore(Type.getType(Object.class));

    // Set the arguments in positions 1..(n-1);
    ByteCodeUtils.loadVariableArray(mv, ByteCodeUtils.toLocalVariables(types), 1); // Skip the this value

    // Add the locals array at the last position.
    mv.dup();
    // The index of the last position of the array.
    mv.push(types.size());
    // Create the array with all the local variables declared up to this point.
    ByteCodeUtils.newVariableArray(mv, constructor.variables.subList(0, constructor.localsAtLoadThis));
    mv.arrayStore(Type.getType(Object.class));

    mv.invokeInterface(IncrementalVisitor.CHANGE_TYPE,
            Method.getMethod("Object access$dispatch(String, Object[])"));
    mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;");
    //// At this point, init$args has been called and the result Object is on the stack.
    //// The value of that Object is Object[] with exactly n + 2 elements.
    //// The first element is the resulting local variables
    //// The second element is a string with the qualified name of the constructor to call.
    //// The remaining elements are the constructor arguments.

    // Keep a reference to the new locals array
    mv.dup();
    mv.push(0);
    mv.arrayLoad(Type.getType("[Ljava/lang/Object;"));
    mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;");
    mv.storeLocal(array);

    // Call super constructor
    // Put this behind the returned array
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.swap();
    // Push a null for the marker parameter.
    mv.visitInsn(Opcodes.ACONST_NULL);
    // Invoke the constructor
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, constructor.owner, "<init>", DISPATCHING_THIS_SIGNATURE, false);

    // Dispatch to init$body
    mv.loadLocal(change);
    mv.push("init$body." + constructor.body.desc);
    mv.loadLocal(array);

    // Now "this" can be set
    mv.dup();
    mv.push(0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.arrayStore(Type.getType(Object.class));

    mv.invokeInterface(IncrementalVisitor.CHANGE_TYPE,
            Method.getMethod("Object access$dispatch(String, Object[])"));
    mv.pop();
}