Example usage for org.objectweb.asm MethodVisitor visitLocalVariable

List of usage examples for org.objectweb.asm MethodVisitor visitLocalVariable

Introduction

In this page you can find the example usage for org.objectweb.asm MethodVisitor visitLocalVariable.

Prototype

public void visitLocalVariable(final String name, final String descriptor, final String signature,
        final Label start, final Label end, final int index) 

Source Link

Document

Visits a local variable declaration.

Usage

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

License:Apache License

/**
 * Generate a has property method./*w  w w.  j  av  a  2  s.  com*/
 *
 * @param classVisitor The class visitor.
 * @param jsonObjectImplType The type of the JSON object implementation.
 * @param method The method.
 * @param jsonPropertyAnnotation The JSON property annotation.
 */
private void generateHasPropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType, Method method,
        JsonProperty jsonPropertyAnnotation) {
    if (!method.getReturnType().equals(Type.BOOLEAN_TYPE)) {
        throw new IllegalArgumentException(method.getName() + "() must have a boolean return type");
    }

    if (method.getArgumentTypes().length != 0) {
        throw new IllegalArgumentException(method.getName() + "() must have no parameters");
    }

    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(jsonPropertyAnnotation.name());
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonObjectImplType, this.getHasJsonPropertyMethod());
    methodVisitor.visitInsn(IRETURN);
    methodVisitor.visitLabel(end);

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

    methodVisitor.visitEnd();
}

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

License:Apache License

/**
 * Generate an is null property method.//from  w  w w.  j  a v a2s . co  m
 *
 * @param classVisitor The class visitor.
 * @param jsonObjectImplType The type of the JSON object implementation.
 * @param method The method.
 * @param jsonPropertyAnnotation The JSON property annotation.
 */
private void generateIsNullPropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType, Method method,
        JsonProperty jsonPropertyAnnotation) {
    if (!method.getReturnType().equals(Type.BOOLEAN_TYPE)) {
        throw new IllegalArgumentException(method.getName() + "() must have a boolean return type");
    }

    if (method.getArgumentTypes().length != 0) {
        throw new IllegalArgumentException(method.getName() + "() must have no parameters");
    }

    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(jsonPropertyAnnotation.name());
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonObjectImplType, this.getIsNullJsonPropertyMethod());
    methodVisitor.visitInsn(IRETURN);
    methodVisitor.visitLabel(end);

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

    methodVisitor.visitEnd();
}

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

License:Apache License

/**
 * Generate a delete property method.//ww w . ja  v  a2  s. com
 *
 * @param classVisitor The class visitor.
 * @param jsonObjectImplType The type of the JSON object implementation.
 * @param method The method.
 * @param jsonPropertyAnnotation The JSON property annotation.
 */
private void generateDeletePropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType, Method method,
        JsonProperty jsonPropertyAnnotation) {
    if (!method.getReturnType().equals(Type.BOOLEAN_TYPE)) {
        throw new IllegalArgumentException(method.getName() + "() must have a boolean return type");
    }

    if (method.getArgumentTypes().length != 0) {
        throw new IllegalArgumentException(method.getName() + "() must have no parameters");
    }

    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(jsonPropertyAnnotation.name());
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonObjectImplType, this.getDeleteJsonPropertyMethod());
    methodVisitor.visitInsn(IRETURN);
    methodVisitor.visitLabel(end);

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

    methodVisitor.visitEnd();
}

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

License:Apache License

/**
 * Generate a get JSON 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 a 2 s  . co  m
 * @param propertyName The name of the property.
 * @param jsonPrimitiveType The type of the JSON primitive.
 */
private void generateGetJsonPrimitivePropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType,
        Method method, String propertyName, Type jsonPrimitiveType) {
    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.visitInsn(ARETURN);
    methodVisitor.visitLabel(end);

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

    methodVisitor.visitEnd();
}

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

License:Apache License

/**
 * Generate a set JSON 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  va 2  s.c o m*/
 * @param propertyName The name of the property.
 * @param jsonPrimitiveType The type of the JSON primitive.
 */
private void generateSetJsonPrimitivePropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType,
        Method method, String propertyName, Type jsonPrimitiveType) {
    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(ALOAD, 1);
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonObjectImplType,
            this.getSetJsonPrimitivePropertyMethod(jsonPrimitiveType));
    methodVisitor.visitInsn(RETURN);
    methodVisitor.visitLabel(end);

    methodVisitor.visitLocalVariable("this", jsonObjectImplType, null, start, end, 0);
    methodVisitor.visitLocalVariable(propertyName, jsonPrimitiveType, 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 Java primitive property method.
 *
 * @param classVisitor The class visitor.
 * @param jsonObjectImplType The type of the JSON object implementation.
 * @param method The method./* www .j  a va  2 s.  c  o m*/
 * @param propertyName The name of the property.
 * @param jsonPrimitiveType The type of the JSON primitive.
 * @param javaPrimitiveType The type of the Java primitive.
 */
private void generateGetJavaPrimitivePropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType,
        Method method, String propertyName, Type jsonPrimitiveType, Type javaPrimitiveType) {
    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(jsonPrimitiveType));
    methodVisitor.visitVarInsn(ASTORE, 1);
    methodVisitor.visitVarInsn(ALOAD, 1);
    methodVisitor.visitJumpInsn(IFNULL, l0);
    methodVisitor.visitVarInsn(ALOAD, 1);
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonPrimitiveType,
            this.getToJavaPrimitiveMethod(javaPrimitiveType));
    methodVisitor.visitJumpInsn(GOTO, l1);
    methodVisitor.visitLabel(l0);
    methodVisitor.visitFrame(Opcodes.F_APPEND, 1, new Object[] { jsonPrimitiveType }, 0, null);
    methodVisitor.visitInsn(this.getDefaultConstOpcode(javaPrimitiveType));
    methodVisitor.visitLabel(l1);
    methodVisitor.visitFrame(Opcodes.F_SAME1, 0, null, 1,
            new Object[] { this.getStackElement(javaPrimitiveType) });
    methodVisitor.visitInsn(javaPrimitiveType.getOpcode(IRETURN));
    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 Java primitive property method.
 *
 * @param classVisitor The class visitor.
 * @param jsonObjectImplType The type of the JSON object implementation.
 * @param method The method.// ww  w.  ja va  2 s. c om
 * @param propertyName The name of the property.
 * @param jsonPrimitiveType The type of the JSON primitive.
 * @param javaPrimitiveType The type of the Java primitive.
 */
private void generateSetJavaPrimitivePropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType,
        Method method, String propertyName, Type jsonPrimitiveType, Type javaPrimitiveType) {
    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(javaPrimitiveType.getOpcode(ILOAD), 1);
    methodVisitor.visitMethodInsn(INVOKESTATIC, this.getWrapperType(javaPrimitiveType),
            this.getFromJavaPrimitiveMethod(javaPrimitiveType));
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, jsonObjectImplType,
            this.getSetJsonPrimitivePropertyMethod(jsonPrimitiveType));
    methodVisitor.visitInsn(RETURN);
    methodVisitor.visitLabel(end);

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

    methodVisitor.visitEnd();
}

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./*from  ww w  .  j  a v a2 s .  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  w  w w.j  av a  2 s.c  om
 * @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 JSON object property method.
 *
 * @param classVisitor The class visitor.
 * @param jsonObjectImplType The type of the JSON object implementation.
 * @param method The method.//from   w  w  w  .java 2 s .c  o m
 * @param propertyName The name of the property.
 * @param jsonObjectType The type of the JSON object.
 */
private void generateGetJsonObjectPropertyMethod(ClassVisitor classVisitor, Type jsonObjectImplType,
        Method method, String propertyName, Type jsonObjectType) {
    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.visitInsn(ARETURN);
    methodVisitor.visitLabel(end);

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

    methodVisitor.visitEnd();
}