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:org.jruby.anno.IndyBinder.java

License:LGPL

public void adaptHandle(ExecutableElementDescriptor executableElementDescriptor, int implClass) {
    ExecutableElementDescriptor desc = executableElementDescriptor;

    // adapt handle
    mv.aload(RUNTIME);//from  ww  w  .j a  v  a  2 s  . c o  m
    mv.ldc(calculateActualRequired(desc.method, desc.method.getParameters().size(), desc.optional, desc.rest,
            desc.isStatic, desc.hasContext, desc.hasBlock));
    mv.ldc(desc.required);
    mv.ldc(desc.optional);
    mv.ldc(desc.rest);
    mv.ldc(desc.rubyName);
    mv.ldc(Type.getObjectType(desc.declaringClassPath));
    mv.ldc(desc.isStatic);
    mv.ldc(desc.hasContext);
    mv.ldc(desc.hasBlock);
    mv.ldc(desc.anno.frame());
    mv.aload(implClass);
    mv.invokestatic("org/jruby/internal/runtime/methods/InvokeDynamicMethodFactory", "adaptHandle",
            Method.getMethod(
                    "java.util.concurrent.Callable adaptHandle(java.lang.invoke.MethodHandle, org.jruby.Ruby, int, int, int, boolean, java.lang.String, java.lang.Class, boolean, boolean, boolean, boolean, org.jruby.RubyModule)")
                    .getDescriptor());
}

From source file:org.kjots.json.object.GenericMethod.java

License:Apache License

/**
 * Retrieve the generic method for the given Java method.
 *
 * @param javaMethod The Java method./*w  w  w  .  ja v a2s. c o  m*/
 * @return The generic method.
 */
public static GenericMethod getGenericMethod(java.lang.reflect.Method javaMethod) {
    Method method = Method.getMethod(javaMethod);
    Set<Integer> genericTypeIndices = new TreeSet<Integer>();

    java.lang.reflect.Type[] parameterTypes = javaMethod.getGenericParameterTypes();
    for (int i = 0; i < parameterTypes.length; i++) {
        if (isGenericType(parameterTypes[i])) {
            genericTypeIndices.add(i);
        }
    }

    return new GenericMethod(method, isGenericType(javaMethod.getGenericReturnType()), genericTypeIndices);
}

From source file:org.kjots.json.object.JsonObjectGeneratorBase.java

License:Apache License

/**
 * Generate the class.//  w ww.j  a  v  a  2  s.  c om
 *
 * @param classVisitor The class visitor.
 * @param jsonObjectClass The class of the JSON object.
 * @param jsonObjectType The type of the JSON object.
 * @param superJsonObjectImplType The type of the super JSON object implementation.
 */
private void generateClass(ClassVisitor classVisitor, Class<? extends JsonObject> jsonObjectClass,
        Type jsonObjectType, Type superJsonObjectImplType) {
    Type jsonObjectImplType = Type
            .getObjectType(jsonObjectType.getInternalName() + "$" + this.jsonObjectImplClass.getSimpleName());

    classVisitor.visit(V1_6, ACC_PUBLIC + ACC_SUPER, jsonObjectImplType, null, superJsonObjectImplType,
            jsonObjectType);

    this.generateConstructor(classVisitor, jsonObjectImplType, superJsonObjectImplType);

    for (java.lang.reflect.Method method : jsonObjectClass.getDeclaredMethods()) {
        if (method.getAnnotation(JsonFunction.class) != null) {
            this.generateFunctionMethod(classVisitor, jsonObjectImplType, jsonObjectClass,
                    Method.getMethod(method), method.getAnnotation(JsonFunction.class), method.isVarArgs());
        } else if (method.getAnnotation(JsonException.class) != null) {
            this.generateExceptionMethod(classVisitor, jsonObjectImplType, jsonObjectClass,
                    Method.getMethod(method), method.getAnnotation(JsonException.class), method.isVarArgs());
        } else if (method.getAnnotation(JsonProperty.class) != null) {
            this.generatePropertyMethod(classVisitor, jsonObjectImplType, method, Method.getMethod(method),
                    method.getAnnotation(JsonProperty.class));
        } else {
            throw new IllegalArgumentException(
                    method.getName() + "() is not annotated with suitable annotation");
        }
    }

    Set<Method> implementedMethods = new HashSet<Method>(classVisitor.getImplementedMethods());

    for (GenericMethod declaredMethod : this.getExtraInterfaceMethods(jsonObjectClass)) {
        Method implementedMethod = null;

        if (declaredMethod.getGenericReturnType() || !declaredMethod.getGenericTypeIndices().isEmpty()) {
            implementedMethod = declaredMethod.getCompatibleMethod(implementedMethods);
        } else if (!declaredMethod.getGenericReturnType() && !implementedMethods.contains(declaredMethod)) {
            int returnTypeSort = declaredMethod.getReturnType().getSort();
            if (returnTypeSort == Type.ARRAY) {
                returnTypeSort = declaredMethod.getReturnType().getElementType().getSort();
            }

            if (returnTypeSort == Type.OBJECT) {
                GenericMethod newDeclaredMethod = new GenericMethod(declaredMethod.getName(),
                        declaredMethod.getDescriptor(), true, declaredMethod.getGenericTypeIndices());

                implementedMethod = newDeclaredMethod.getCompatibleMethod(implementedMethods);
            }
        }

        if (implementedMethod != null && !implementedMethod.equals(declaredMethod)) {
            this.generateBridgeMethod(classVisitor, jsonObjectImplType, declaredMethod, implementedMethod);
        }
    }

    classVisitor.visitEnd();
}

From source file:org.kjots.json.object.JsonObjectGeneratorBase.java

License:Apache License

/**
 * Generate the constructor.//from www.  j a  v a2s. com
 *
 * @param classVisitor The class visitor.
 * @param jsonObjectImplType The type of the JSON object implementation.
 * @param superJsonObjectImplType The type of the super JSON object implementation.
 */
private void generateConstructor(ClassVisitor classVisitor, Type jsonObjectImplType,
        Type superJsonObjectImplType) {
    Method constructor = Method.getMethod(this.getJsonObjectImplConstructor());
    Type[] argumentTypes = constructor.getArgumentTypes();

    int maxLocals = 1;

    MethodVisitor methodVisitor = classVisitor.visitMethod(ACC_PUBLIC, constructor, null, null);

    Label start = new Label();
    Label end = new Label();

    methodVisitor.visitCode();

    methodVisitor.visitLabel(start);
    methodVisitor.visitVarInsn(ALOAD, 0);
    for (int i = 0, index = 1; i < argumentTypes.length; i++) {
        Type argumentType = argumentTypes[i];

        methodVisitor.visitVarInsn(argumentType.getOpcode(ILOAD), index);

        index += argumentType.getSize();
    }
    methodVisitor.visitMethodInsn(INVOKESPECIAL, superJsonObjectImplType, constructor);
    methodVisitor.visitInsn(RETURN);
    methodVisitor.visitLabel(end);

    methodVisitor.visitLocalVariable("this", jsonObjectImplType, null, start, end, 0);
    for (int i = 0; i < argumentTypes.length; i++) {
        Type argumentType = argumentTypes[i];

        methodVisitor.visitLocalVariable("arg" + i, argumentType, null, start, end, maxLocals);

        maxLocals += argumentType.getSize();
    }
    methodVisitor.visitMaxs(maxLocals, maxLocals);

    methodVisitor.visitEnd();
}

From source file:org.kjots.json.object.JsonObjectGeneratorBase.java

License:Apache License

/**
 * Retrieve the JSON function method with the given name for the given JSON
 * object method./*  www.  j a v a2s . c  o  m*/
 *
 * @param jsonObjectClass The class of the JSON object.
 * @param jsonObjectMethod The JSON object method.
 * @param jsonFunctionAnnotation The JSON function annotation.
 * @return The JSON function method.
 */
private Method getJsonFunctionMethod(Class<?> jsonObjectClass, Method jsonObjectMethod,
        JsonFunction jsonFunctionAnnotation) {
    Set<Method> methods = new HashSet<Method>();
    for (java.lang.reflect.Method javaMethod : jsonFunctionAnnotation.klass().getDeclaredMethods()) {
        if (Modifier.isStatic(javaMethod.getModifiers())
                && javaMethod.getName().equals(jsonFunctionAnnotation.method())) {
            methods.add(Method.getMethod(javaMethod));
        }
    }

    if (!methods.isEmpty()) {
        Type[] argumentTypes = jsonObjectMethod.getArgumentTypes();

        while (jsonObjectClass != null) {
            Type[] jsonFunctionArgumentTypes = new Type[argumentTypes.length + 1];

            jsonFunctionArgumentTypes[0] = Type.getType(jsonObjectClass);
            System.arraycopy(argumentTypes, 0, jsonFunctionArgumentTypes, 1, argumentTypes.length);

            Method method = new Method(jsonFunctionAnnotation.method(), jsonObjectMethod.getReturnType(),
                    jsonFunctionArgumentTypes);
            if (methods.contains(method)) {
                return method;
            }

            jsonObjectClass = !jsonObjectClass.equals(JsonObject.class) ? jsonObjectClass.getInterfaces()[0]
                    : null;
        }
    }

    throw new IllegalArgumentException(jsonFunctionAnnotation.klass().getName()
            + " does not contain a suitable method named " + jsonFunctionAnnotation.method());
}

From source file:org.spongepowered.test.decompile.SwitchTests.java

License:Open Source License

@Test
public void testTableSwitchEclipse() {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    cw.visit(V1_8, ACC_PUBLIC | ACC_SUPER, "SwitchTests_Class", null, "java/lang/Object", null);

    FieldVisitor fv = cw.visitField(ACC_PRIVATE + ACC_STATIC + ACC_SYNTHETIC,
            "$SWITCH_TABLE$org$spongepowered$test$decompile$SwitchTests$TestEnum", "[I", null, null);
    fv.visitEnd();/*from  w w w  .j a  v a2 s  .c om*/

    {
        Method m = Method.getMethod("void <init> ()");
        GeneratorAdapter mg = new GeneratorAdapter(ACC_PUBLIC, m, null, null, cw);
        mg.loadThis();
        mg.invokeConstructor(Type.getType(Object.class), m);
        mg.returnValue();
        mg.endMethod();
    }

    {
        Method m = Method.getMethod("void test_mth (" + TEST_ENUM_TYPE.getClassName() + ")");
        GeneratorAdapter mv = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC, m, null, null, cw);
        Label start = mv.newLabel();
        mv.visitLabel(start);
        Label l1 = mv.newLabel();
        Label l2 = mv.newLabel();
        Label def = mv.newLabel();
        Label end = mv.newLabel();
        mv.invokeStatic(THIS_TYPE, Method
                .getMethod("int[] $SWITCH_TABLE$org$spongepowered$test$decompile$SwitchTests$TestEnum ()"));
        mv.loadArg(0);
        mv.invokeVirtual(TEST_ENUM_TYPE, Method.getMethod("int ordinal ()"));
        mv.arrayLoad(Type.INT_TYPE);
        mv.visitTableSwitchInsn(1, 2, def, l1, l2);
        mv.visitLabel(l1);
        mv.invokeStatic(THIS_TYPE, Method.getMethod("void body ()"));
        mv.goTo(end);
        mv.visitLabel(l2);
        mv.invokeStatic(THIS_TYPE, Method.getMethod("void body ()"));
        mv.goTo(end);
        mv.visitLabel(def);
        mv.invokeStatic(THIS_TYPE, Method.getMethod("void body ()"));
        mv.visitLabel(end);
        mv.visitInsn(RETURN);
        mv.visitLocalVariable("ie", TEST_ENUM_TYPE.getDescriptor(), null, start, end, 0);
        mv.endMethod();
    }
    generateSwitchSyntheticEclipse(cw);

    cw.visitEnd();
    String insn = TestHelper.getAsString(cw.toByteArray(), "test_mth");
    String good = "switch (ie) {\n" + "case ONE:\n"
            + "    org.spongepowered.test.decompile.SwitchTests.body();\n" + "    break;\n" + "case TWO:\n"
            + "    org.spongepowered.test.decompile.SwitchTests.body();\n" + "    break;\n" + "default:\n"
            + "    org.spongepowered.test.decompile.SwitchTests.body();\n" + "}";
    Assert.assertEquals(good, insn);
}

From source file:org.spongepowered.test.decompile.SwitchTests.java

License:Open Source License

@Test
public void testLookupSwitchEclipse() {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    cw.visit(V1_8, ACC_PUBLIC | ACC_SUPER, "SwitchTests_Class", null, "java/lang/Object", null);

    FieldVisitor fv = cw.visitField(ACC_PRIVATE + ACC_STATIC + ACC_SYNTHETIC,
            "$SWITCH_TABLE$org$spongepowered$test$decompile$SwitchTests$TestEnum", "[I", null, null);
    fv.visitEnd();//from  w w  w.  java 2 s. co m

    {
        Method m = Method.getMethod("void <init> ()");
        GeneratorAdapter mg = new GeneratorAdapter(ACC_PUBLIC, m, null, null, cw);
        mg.loadThis();
        mg.invokeConstructor(Type.getType(Object.class), m);
        mg.returnValue();
        mg.endMethod();
    }

    {
        Method m = Method.getMethod("void test_mth (" + TEST_ENUM_TYPE.getClassName() + ")");
        GeneratorAdapter mv = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC, m, null, null, cw);
        Label start = mv.newLabel();
        mv.visitLabel(start);
        Label l1 = mv.newLabel();
        Label l2 = mv.newLabel();
        Label def = mv.newLabel();
        Label end = mv.newLabel();
        mv.invokeStatic(THIS_TYPE, Method
                .getMethod("int[] $SWITCH_TABLE$org$spongepowered$test$decompile$SwitchTests$TestEnum ()"));
        mv.loadArg(0);
        mv.invokeVirtual(TEST_ENUM_TYPE, Method.getMethod("int ordinal ()"));
        mv.arrayLoad(Type.INT_TYPE);
        mv.visitLookupSwitchInsn(def, new int[] { 1, 8 }, new Label[] { l1, l2 });
        mv.visitLabel(l1);
        mv.invokeStatic(THIS_TYPE, Method.getMethod("void body ()"));
        mv.goTo(end);
        mv.visitLabel(l2);
        mv.invokeStatic(THIS_TYPE, Method.getMethod("void body ()"));
        mv.goTo(end);
        mv.visitLabel(def);
        mv.invokeStatic(THIS_TYPE, Method.getMethod("void body ()"));
        mv.visitLabel(end);
        mv.visitInsn(RETURN);
        mv.visitLocalVariable("ie", TEST_ENUM_TYPE.getDescriptor(), null, start, end, 0);
        mv.endMethod();
    }
    generateSwitchSyntheticEclipse(cw);

    cw.visitEnd();
    String insn = TestHelper.getAsString(cw.toByteArray(), "test_mth");
    String good = "switch (ie) {\n" + "case ONE:\n"
            + "    org.spongepowered.test.decompile.SwitchTests.body();\n" + "    break;\n" + "case EIGHT:\n"
            + "    org.spongepowered.test.decompile.SwitchTests.body();\n" + "    break;\n" + "default:\n"
            + "    org.spongepowered.test.decompile.SwitchTests.body();\n" + "}";
    Assert.assertEquals(good, insn);
}

From source file:org.spongepowered.test.decompile.SwitchTests.java

License:Open Source License

@Test
public void testReturnSwitchEclipse() {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    cw.visit(V1_8, ACC_PUBLIC | ACC_SUPER, "SwitchTests_Class", null, "java/lang/Object", null);

    FieldVisitor fv = cw.visitField(ACC_PRIVATE + ACC_STATIC + ACC_SYNTHETIC,
            "$SWITCH_TABLE$org$spongepowered$test$decompile$SwitchTests$TestEnum", "[I", null, null);
    fv.visitEnd();//from  w  w  w.  j a va  2  s .  c om

    {
        Method m = Method.getMethod("void <init> ()");
        GeneratorAdapter mg = new GeneratorAdapter(ACC_PUBLIC, m, null, null, cw);
        mg.loadThis();
        mg.invokeConstructor(Type.getType(Object.class), m);
        mg.returnValue();
        mg.endMethod();
    }

    {
        Method m = Method.getMethod("void test_mth (" + TEST_ENUM_TYPE.getClassName() + ")");
        GeneratorAdapter mv = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC, m, null, null, cw);
        Label start = mv.newLabel();
        mv.visitLabel(start);
        Label l1 = mv.newLabel();
        Label l2 = mv.newLabel();
        Label def = mv.newLabel();
        Label end = mv.newLabel();
        mv.invokeStatic(THIS_TYPE, Method
                .getMethod("int[] $SWITCH_TABLE$org$spongepowered$test$decompile$SwitchTests$TestEnum ()"));
        mv.loadArg(0);
        mv.invokeVirtual(TEST_ENUM_TYPE, Method.getMethod("int ordinal ()"));
        mv.arrayLoad(Type.INT_TYPE);
        mv.visitLookupSwitchInsn(def, new int[] { 1, 8 }, new Label[] { l1, l2 });
        mv.visitLabel(l1);
        mv.invokeStatic(THIS_TYPE, Method.getMethod("void body ()"));
        mv.visitInsn(RETURN);
        mv.visitLabel(l2);
        mv.invokeStatic(THIS_TYPE, Method.getMethod("void body ()"));
        mv.visitInsn(RETURN);
        mv.visitLabel(def);
        mv.invokeStatic(THIS_TYPE, Method.getMethod("void body ()"));
        mv.visitLabel(end);
        mv.visitInsn(RETURN);
        mv.visitLocalVariable("ie", TEST_ENUM_TYPE.getDescriptor(), null, start, end, 0);
        mv.endMethod();
    }
    generateSwitchSyntheticEclipse(cw);

    cw.visitEnd();
    String insn = TestHelper.getAsString(cw.toByteArray(), "test_mth");
    String good = "switch (ie) {\n" + "case ONE:\n"
            + "    org.spongepowered.test.decompile.SwitchTests.body();\n" + "    return;\n" + "case EIGHT:\n"
            + "    org.spongepowered.test.decompile.SwitchTests.body();\n" + "    return;\n" + "default:\n"
            + "    org.spongepowered.test.decompile.SwitchTests.body();\n" + "    return;\n" + "}";
    Assert.assertEquals(good, insn);
}