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

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

Introduction

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

Prototype

public Method(final String name, final Type returnType, final Type[] argumentTypes) 

Source Link

Document

Constructs a new Method .

Usage

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

License:Apache License

/**
 * Retrieve the constructor with the given argument types.
 *
 * @param argumentTypes The argument types.
 * @return The constructor.//w w w.  j a v  a  2  s .  c o m
 */
private static Method getConstructor(Type... argumentTypes) {
    return new Method("<init>", Type.VOID_TYPE, argumentTypes);
}

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

License:Apache License

/**
 * Generate a get Java character primitive property method.
 *
 * @param classVisitor The class visitor.
 * @param jsonObjectImplType The type of the JSON object implementation.
 * @param method The method.//w w  w  .ja v  a 2s . c o m
 * @param propertyName The name of the property.
 */
private void generateGetJavaCharacterPrimitivePropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType,
        Method method, String propertyName) {
    Type jsonStringPrimitiveType = Type.getType(String.class);

    MethodVisitor methodVisitor = classVisitor.visitMethod(ACC_PUBLIC + ACC_FINAL, method, null, null);

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

    methodVisitor.visitCode();

    methodVisitor.visitLabel(start);
    methodVisitor.visitVarInsn(ALOAD, 0);
    methodVisitor.visitLdcInsn(propertyName);
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonObjectImplType,
            this.getGetJsonPrimitivePropertyMethod(jsonStringPrimitiveType));
    methodVisitor.visitVarInsn(ASTORE, 1);
    methodVisitor.visitVarInsn(ALOAD, 1);
    methodVisitor.visitJumpInsn(IFNULL, l0);
    methodVisitor.visitVarInsn(ALOAD, 1);
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonStringPrimitiveType,
            new Method("isEmpty", Type.BOOLEAN_TYPE, new Type[] {}));
    methodVisitor.visitJumpInsn(IFNE, l0);
    methodVisitor.visitVarInsn(ALOAD, 1);
    methodVisitor.visitInsn(ICONST_0);
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonStringPrimitiveType,
            new Method("charAt", Type.CHAR_TYPE, new Type[] { Type.INT_TYPE }));
    methodVisitor.visitJumpInsn(GOTO, l1);
    methodVisitor.visitLabel(l0);
    methodVisitor.visitFrame(Opcodes.F_APPEND, 1, new Object[] { jsonStringPrimitiveType }, 0, null);
    methodVisitor.visitInsn(ICONST_0);
    methodVisitor.visitLabel(l1);
    methodVisitor.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { Opcodes.INTEGER });
    methodVisitor.visitInsn(IRETURN);
    methodVisitor.visitLabel(end);

    methodVisitor.visitLocalVariable("this", jsonObjectImplType, null, start, end, 0);
    methodVisitor.visitLocalVariable("_" + propertyName, jsonStringPrimitiveType, null, start, end, 1);
    methodVisitor.visitMaxs(2, 2);

    methodVisitor.visitEnd();
}

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

License:Apache License

/**
 * Generate a set Java character primitive property method.
 *
 * @param classVisitor The class visitor.
 * @param jsonObjectImplType The type of the JSON object implementation.
 * @param method The method.//from ww w. jav  a  2s .  c o m
 * @param propertyName The name of the property.
 */
private void generateSetJavaCharacterPrimitivePropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType,
        Method method, String propertyName) {
    Type jsonStringPrimitiveType = Type.getType(String.class);
    MethodVisitor methodVisitor = classVisitor.visitMethod(ACC_PUBLIC + ACC_FINAL, method, null, null);

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

    methodVisitor.visitCode();

    methodVisitor.visitLabel(start);
    methodVisitor.visitVarInsn(ALOAD, 0);
    methodVisitor.visitLdcInsn(propertyName);
    methodVisitor.visitVarInsn(ILOAD, 1);
    methodVisitor.visitMethodInsn(INVOKESTATIC, Type.getType(Character.class),
            new Method("toString", jsonStringPrimitiveType, new Type[] { Type.CHAR_TYPE }));
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonObjectImplType,
            this.getSetJsonPrimitivePropertyMethod(jsonStringPrimitiveType));
    methodVisitor.visitInsn(RETURN);
    methodVisitor.visitLabel(end);

    methodVisitor.visitLocalVariable("this", jsonObjectImplType, null, start, end, 0);
    methodVisitor.visitLocalVariable(propertyName, Type.CHAR_TYPE, null, start, end, 1);
    methodVisitor.visitMaxs(3, 2);

    methodVisitor.visitEnd();
}

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

License:Apache License

/**
 * Generate a get adapted primitive property method.
 *
 * @param classVisitor The class visitor.
 * @param jsonObjectImplType The type of the JSON object implementation.
 * @param method The method./*from w w  w. j a v  a 2  s  .  c om*/
 * @param propertyName The name of the property.
 * @param jsonPrimitiveType The type of the JSON primitive.
 * @param adapterType The type of the adapter.
 * @param propertyType The type of the property.
 */
private void generateGetAdaptedPrimitivePropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType,
        Method method, String propertyName, Type jsonPrimitiveType, Type adapterType, Type propertyType) {
    MethodVisitor methodVisitor = classVisitor.visitMethod(ACC_PUBLIC + ACC_FINAL, method, null, null);

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

    methodVisitor.visitCode();

    methodVisitor.visitLabel(start);
    methodVisitor.visitVarInsn(ALOAD, 0);
    methodVisitor.visitLdcInsn(propertyName);
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonObjectImplType,
            this.getGetJsonPrimitivePropertyMethod(jsonPrimitiveType));
    methodVisitor.visitVarInsn(ASTORE, 1);
    methodVisitor.visitMethodInsn(INVOKESTATIC, Type.getType(JsonObjectFactory.class),
            new Method("get", Type.getType(JsonObjectFactory.class), new Type[] {}));
    methodVisitor.visitLdcInsn(adapterType);
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, Type.getType(JsonObjectFactory.class),
            new Method("getJsonPropertyAdapter", Type.getType(JsonPropertyAdapter.class),
                    new Type[] { Type.getType(Class.class) }));
    methodVisitor.visitTypeInsn(CHECKCAST, adapterType);
    methodVisitor.visitVarInsn(ALOAD, 1);
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, adapterType,
            this.getFromJsonPropertyMethod(jsonPrimitiveType, propertyType));
    methodVisitor.visitInsn(ARETURN);
    methodVisitor.visitLabel(end);

    methodVisitor.visitLocalVariable("this", jsonObjectImplType, null, start, end, 0);
    methodVisitor.visitLocalVariable("_" + propertyName, jsonPrimitiveType, null, start, end, 1);
    methodVisitor.visitMaxs(2, 2);

    methodVisitor.visitEnd();
}

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

License:Apache License

/**
 * Generate a set adapted primitive property method.
 *
 * @param classVisitor The class visitor.
 * @param jsonObjectImplType The type of the JSON object implementation.
 * @param method The method./* w  w  w  .  j  a v  a2s. c  o m*/
 * @param propertyName The name of the property.
 * @param jsonPrimitiveType The type of the JSON primitive.
 * @param adapterType The type of the adapter.
 * @param propertyType The type of the property.
 */
private void generateSetAdaptedPrimitivePropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType,
        Method method, String propertyName, Type jsonPrimitiveType, Type adapterType, Type propertyType) {
    MethodVisitor methodVisitor = classVisitor.visitMethod(ACC_PUBLIC + ACC_FINAL, method, null, null);

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

    methodVisitor.visitCode();
    methodVisitor.visitLabel(start);
    methodVisitor.visitMethodInsn(INVOKESTATIC, Type.getType(JsonObjectFactory.class),
            new Method("get", Type.getType(JsonObjectFactory.class), new Type[] {}));
    methodVisitor.visitLdcInsn(adapterType);
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, Type.getType(JsonObjectFactory.class),
            new Method("getJsonPropertyAdapter", Type.getType(JsonPropertyAdapter.class),
                    new Type[] { Type.getType(Class.class) }));
    methodVisitor.visitTypeInsn(CHECKCAST, adapterType);
    methodVisitor.visitVarInsn(ALOAD, 1);
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, adapterType,
            this.getToJsonPropertyMethod(jsonPrimitiveType, propertyType));
    methodVisitor.visitVarInsn(ASTORE, 2);
    methodVisitor.visitVarInsn(ALOAD, 0);
    methodVisitor.visitLdcInsn(propertyName);
    methodVisitor.visitVarInsn(ALOAD, 2);
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonObjectImplType,
            this.getSetJsonPrimitivePropertyMethod(jsonPrimitiveType));
    methodVisitor.visitInsn(RETURN);
    methodVisitor.visitLabel(end);

    methodVisitor.visitLocalVariable("this", jsonObjectImplType, null, start, end, 0);
    methodVisitor.visitLocalVariable(propertyName, propertyType, null, start, end, 1);
    methodVisitor.visitLocalVariable("_" + propertyName, jsonPrimitiveType, null, start, end, 2);
    methodVisitor.visitMaxs(3, 3);

    methodVisitor.visitEnd();
}

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

License:Apache License

/**
 * Generate a get adapted object property method.
 *
 * @param classVisitor The class visitor.
 * @param jsonObjectImplType The type of the JSON object implementation.
 * @param method The method.// w ww  . ja v a 2 s.  c  om
 * @param propertyName The name of the property.
 * @param jsonObjectType The type of the JSON object.
 * @param adapterType The type of the adapter.
 * @param propertyType The type of the property.
 */
private void generateGetAdaptedObjectPropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType,
        Method method, String propertyName, Type jsonObjectType, Type adapterType, Type propertyType) {
    MethodVisitor methodVisitor = classVisitor.visitMethod(ACC_PUBLIC + ACC_FINAL, method, null, null);

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

    methodVisitor.visitCode();

    methodVisitor.visitLabel(start);
    methodVisitor.visitVarInsn(ALOAD, 0);
    methodVisitor.visitLdcInsn(propertyName);
    methodVisitor.visitLdcInsn(jsonObjectType);
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonObjectImplType, this.getGetJsonObjectPropertyMethod());
    methodVisitor.visitTypeInsn(CHECKCAST, jsonObjectType);
    methodVisitor.visitVarInsn(ASTORE, 1);
    methodVisitor.visitMethodInsn(INVOKESTATIC, Type.getType(JsonObjectFactory.class),
            new Method("get", Type.getType(JsonObjectFactory.class), new Type[] {}));
    methodVisitor.visitLdcInsn(adapterType);
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, Type.getType(JsonObjectFactory.class),
            new Method("getJsonPropertyAdapter", Type.getType(JsonPropertyAdapter.class),
                    new Type[] { Type.getType(Class.class) }));
    methodVisitor.visitTypeInsn(CHECKCAST, adapterType);
    methodVisitor.visitVarInsn(ALOAD, 1);
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, adapterType,
            this.getFromJsonPropertyMethod(jsonObjectType, propertyType));
    methodVisitor.visitInsn(ARETURN);
    methodVisitor.visitLabel(end);

    methodVisitor.visitLocalVariable("this", jsonObjectImplType, null, start, end, 0);
    methodVisitor.visitLocalVariable("_" + propertyName, jsonObjectType, null, start, end, 1);
    methodVisitor.visitMaxs(3, 2);

    methodVisitor.visitEnd();
}

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

License:Apache License

/**
 * Generate a set adapted object property method.
 *
 * @param classVisitor The class visitor.
 * @param jsonObjectImplType The type of the JSON object implementation.
 * @param method The method./*from   w  ww .j a v  a 2 s.  c  o m*/
 * @param propertyName The name of the property.
 * @param jsonObjectType The type of the JSON object.
 * @param adapterType The type of the adapter.
 * @param propertyType The type of the property.
 */
private void generateSetAdaptedObjectPropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType,
        Method method, String propertyName, Type jsonObjectType, Type adapterType, Type propertyType) {
    MethodVisitor methodVisitor = classVisitor.visitMethod(ACC_PUBLIC + ACC_FINAL, method, null, null);

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

    methodVisitor.visitCode();

    methodVisitor.visitLabel(start);
    methodVisitor.visitMethodInsn(INVOKESTATIC, Type.getType(JsonObjectFactory.class),
            new Method("get", Type.getType(JsonObjectFactory.class), new Type[] {}));
    methodVisitor.visitLdcInsn(adapterType);
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, Type.getType(JsonObjectFactory.class),
            new Method("getJsonPropertyAdapter", Type.getType(JsonPropertyAdapter.class),
                    new Type[] { Type.getType(Class.class) }));
    methodVisitor.visitTypeInsn(CHECKCAST, adapterType);
    methodVisitor.visitVarInsn(ALOAD, 1);
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, adapterType,
            this.getToJsonPropertyMethod(jsonObjectType, propertyType));
    methodVisitor.visitVarInsn(ASTORE, 2);
    methodVisitor.visitVarInsn(ALOAD, 0);
    methodVisitor.visitLdcInsn(propertyName);
    methodVisitor.visitVarInsn(ALOAD, 2);
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonObjectImplType, this.getSetJsonObjectPropertyMethod());
    methodVisitor.visitInsn(RETURN);
    methodVisitor.visitLabel(end);

    methodVisitor.visitLocalVariable("this", jsonObjectImplType, null, start, end, 0);
    methodVisitor.visitLocalVariable(propertyName, propertyType, null, start, end, 1);
    methodVisitor.visitLocalVariable("_" + propertyName, jsonObjectType, null, start, end, 2);
    methodVisitor.visitMaxs(3, 3);

    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./*from   w w  w  . ja v  a  2  s  . c om*/
 *
 * @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.kjots.json.object.JsonObjectGeneratorBase.java

License:Apache License

/**
 * Retrieve the has JSON property method.
 *
 * @return The has JSON property method.
 *//* w  w  w . j  a  v a  2 s.c o m*/
private Method getHasJsonPropertyMethod() {
    return new Method("hasProperty", Type.BOOLEAN_TYPE, new Type[] { Type.getType(String.class) });
}

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

License:Apache License

/**
 * Retrieve the is null JSON property method.
 *
 * @return The is null JSON property method.
 */// ww  w.j ava2  s . c o m
private Method getIsNullJsonPropertyMethod() {
    return new Method("isNullProperty", Type.BOOLEAN_TYPE, new Type[] { Type.getType(String.class) });
}