Example usage for org.objectweb.asm Opcodes ARETURN

List of usage examples for org.objectweb.asm Opcodes ARETURN

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes ARETURN.

Prototype

int ARETURN

To view the source code for org.objectweb.asm Opcodes ARETURN.

Click Source Link

Usage

From source file:com.navercorp.pinpoint.profiler.instrument.ASMMethodVariables.java

License:Apache License

public void storeResultVar(final InsnList instructions, final int opcode) {
    assertInitializedInterceptorLocalVariables();
    if (opcode == Opcodes.RETURN) {
        // void.//  w ww .j ava2s.  c om
        loadNull(instructions);
    } else if (opcode == Opcodes.ARETURN) {
        // object.
        dup(instructions);
    } else {
        if (opcode == Opcodes.LRETURN || opcode == Opcodes.DRETURN) {
            // long or double.
            dup2(instructions);
        } else {
            dup(instructions);
        }
        final Type type = Type.getReturnType(this.methodNode.desc);
        box(instructions, type);
    }
    storeVar(instructions, this.resultVarIndex);
    loadNull(instructions);
    storeVar(instructions, this.throwableVarIndex);
}

From source file:com.navercorp.pinpoint.profiler.instrument.ASMMethodVariables.java

License:Apache License

boolean isReturnCode(final int opcode) {
    return opcode == Opcodes.IRETURN || opcode == Opcodes.LRETURN || opcode == Opcodes.FRETURN
            || opcode == Opcodes.DRETURN || opcode == Opcodes.ARETURN || opcode == Opcodes.RETURN;
}

From source file:com.nginious.http.serialize.JsonDeserializerCreator.java

License:Apache License

/**
 * Creates a JSON deserializer for the specified bean class unless a deserializer has already
 * been created. Created deserializers are cached and returned on subsequent calls to this method.
 * // ww  w .ja  v a2 s . c o  m
 * @param <T> class type for bean
 * @param beanClazz bean class for which a deserializer should be created
 * @return the created deserializer
 * @throws SerializerFactoryException if unable to create deserializer or class is not a bean
 */
@SuppressWarnings("unchecked")
protected <T> JsonDeserializer<T> create(Class<T> beanClazz) throws SerializerFactoryException {
    JsonDeserializer<T> deserializer = (JsonDeserializer<T>) deserializers.get(beanClazz);

    if (deserializer != null) {
        return deserializer;
    }

    try {
        synchronized (this) {
            deserializer = (JsonDeserializer<T>) deserializers.get(beanClazz);

            if (deserializer != null) {
                return deserializer;
            }

            checkDeserializability(beanClazz, "json");
            String intBeanClazzName = Serialization.createInternalClassName(beanClazz);
            Method[] methods = beanClazz.getMethods();

            String intDeserializerClazzName = new StringBuffer(intBeanClazzName).append("JsonDeserializer")
                    .toString();

            // Create class
            ClassWriter writer = new ClassWriter(0);
            String signature = Serialization
                    .createClassSignature("com/nginious/http/serialize/JsonDeserializer", intBeanClazzName);
            writer.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, intDeserializerClazzName, signature,
                    "com/nginious/http/serialize/JsonDeserializer", null);

            // Create constructor
            Serialization.createConstructor(writer, "com/nginious/http/serialize/JsonDeserializer");

            // Create deserialize method
            MethodVisitor visitor = createDeserializeMethod(writer, intBeanClazzName);

            for (Method method : methods) {
                Serializable info = method.getAnnotation(Serializable.class);
                boolean canDeserialize = info == null
                        || (info != null && info.deserialize() && info.types().indexOf("json") > -1);

                if (canDeserialize && method.getName().startsWith("set")
                        && method.getReturnType().equals(void.class)
                        && method.getParameterTypes().length == 1) {
                    Class<?>[] parameterTypes = method.getParameterTypes();
                    Class<?> parameterType = parameterTypes[0];

                    if (parameterType.isArray()) {
                        Class<?> arrayType = parameterType.getComponentType();

                        if (arrayType.equals(boolean.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeBooleanArray", "[Z", "[Z", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(double.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeDoubleArray", "[D", "[D", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(float.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeFloatArray", "[F", "[F", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(int.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeIntArray", "[I", "[I", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(long.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeLongArray", "[J", "[J", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(short.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeShortArray", "[S", "[S", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(String.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeStringArray", "[Ljava/lang/String;", "[Ljava/lang/String;",
                                    intBeanClazzName, method.getName());
                        }
                    } else if (parameterType.isPrimitive()) {
                        if (parameterType.equals(boolean.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeBoolean", "Z", "Z", intBeanClazzName, method.getName());
                        } else if (parameterType.equals(double.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeDouble", "D", "D", intBeanClazzName, method.getName());
                        } else if (parameterType.equals(float.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeFloat", "F", "F", intBeanClazzName, method.getName());
                        } else if (parameterType.equals(int.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeInt", "I", "I", intBeanClazzName, method.getName());
                        } else if (parameterType.equals(long.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeLong", "J", "J", intBeanClazzName, method.getName());
                        } else if (parameterType.equals(short.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeShort", "S", "S", intBeanClazzName, method.getName());
                        }
                    } else if (parameterType.equals(Calendar.class)) {
                        createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                "deserializeCalendar", "Ljava/util/Calendar;", "Ljava/util/Calendar;",
                                intBeanClazzName, method.getName());
                    } else if (parameterType.equals(Date.class)) {
                        createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeDate",
                                "Ljava/util/Date;", "Ljava/util/Date;", intBeanClazzName, method.getName());
                    } else if (parameterType.equals(String.class)) {
                        createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                "deserializeString", "Ljava/lang/String;", "Ljava/lang/String;",
                                intBeanClazzName, method.getName());
                    }
                }
            }

            visitor.visitVarInsn(Opcodes.ALOAD, 3);
            visitor.visitInsn(Opcodes.ARETURN);
            visitor.visitMaxs(5, 4);
            visitor.visitEnd();

            writer.visitEnd();
            byte[] clazzBytes = writer.toByteArray();
            ClassLoader controllerLoader = null;

            if (classLoader.hasLoaded(beanClazz)) {
                controllerLoader = beanClazz.getClassLoader();
            } else {
                controllerLoader = this.classLoader;
            }

            Class<?> clazz = Serialization.loadClass(controllerLoader,
                    intDeserializerClazzName.replace('/', '.'), clazzBytes);
            deserializer = (JsonDeserializer<T>) clazz.newInstance();
            deserializers.put(beanClazz, deserializer);
            return deserializer;
        }
    } catch (IllegalAccessException e) {
        throw new SerializerFactoryException(e);
    } catch (InstantiationException e) {
        throw new SerializerFactoryException(e);
    }
}

From source file:com.nginious.http.serialize.QueryDeserializerCreator.java

License:Apache License

/**
 * Creates a query deserializer for the specified bean class unless a deserializer has already
 * been created. Created deserializers are cached and returned on subsequent calls to this method.
 * //  ww w. j av a2  s  .  c om
 * @param <T> class type for bean
 * @param beanClazz bean class for which a deserializer should be created
 * @return the created deserializer
 * @throws SerializerFactoryException if unable to create deserializer or class is not a bean
 */
@SuppressWarnings("unchecked")
protected <T> QueryDeserializer<T> create(Class<T> beanClazz) throws SerializerFactoryException {
    QueryDeserializer<T> deserializer = (QueryDeserializer<T>) deserializers.get(beanClazz);

    if (deserializer != null) {
        return deserializer;
    }

    try {
        synchronized (this) {
            deserializer = (QueryDeserializer<T>) deserializers.get(beanClazz);

            if (deserializer != null) {
                return deserializer;
            }

            checkDeserializability(beanClazz, "query");
            String intBeanClazzName = Serialization.createInternalClassName(beanClazz);
            Method[] methods = beanClazz.getMethods();

            String intDeserializerClazzName = new StringBuffer(intBeanClazzName).append("QueryDeserializer")
                    .toString();

            // Create class
            ClassWriter writer = new ClassWriter(0);
            String signature = Serialization.createClassSignature("com/nginious/serialize/QueryDeserializer",
                    intBeanClazzName);
            writer.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, intDeserializerClazzName, signature,
                    "com/nginious/http/serialize/QueryDeserializer", null);

            // Create constructor
            Serialization.createConstructor(writer, "com/nginious/http/serialize/QueryDeserializer");

            // Create deserialize method
            MethodVisitor visitor = createDeserializeMethod(writer, intBeanClazzName);

            for (Method method : methods) {
                Serializable info = method.getAnnotation(Serializable.class);
                boolean canDeserialize = info == null
                        || (info != null && info.deserialize() && info.types().indexOf("query") > -1);

                if (canDeserialize && method.getName().startsWith("set")
                        && method.getReturnType().equals(void.class)
                        && method.getParameterTypes().length == 1) {
                    Class<?>[] parameterTypes = method.getParameterTypes();
                    Class<?> parameterType = parameterTypes[0];

                    if (parameterType.isArray()) {
                        Class<?> arrayType = parameterType.getComponentType();

                        if (arrayType.equals(boolean.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeBooleanArray", "[Z", "[Z", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(double.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeDoubleArray", "[D", "[D", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(float.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeFloatArray", "[F", "[F", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(int.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeIntArray", "[I", "[I", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(long.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeLongArray", "[J", "[J", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(short.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeShortArray", "[S", "[S", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(String.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeStringArray", "[Ljava/lang/String;", "[Ljava/lang/String;",
                                    intBeanClazzName, method.getName());
                        }
                    } else if (parameterType.isPrimitive()) {
                        if (parameterType.equals(boolean.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeBoolean", "Z", "Z", intBeanClazzName, method.getName());
                        } else if (parameterType.equals(double.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeDouble", "D", "D", intBeanClazzName, method.getName());
                        } else if (parameterType.equals(float.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeFloat", "F", "F", intBeanClazzName, method.getName());
                        } else if (parameterType.equals(int.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeInt", "I", "I", intBeanClazzName, method.getName());
                        } else if (parameterType.equals(long.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeLong", "J", "J", intBeanClazzName, method.getName());
                        } else if (parameterType.equals(short.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeShort", "S", "S", intBeanClazzName, method.getName());
                        }
                    } else if (parameterType.equals(Calendar.class)) {
                        createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                "deserializeCalendar", "Ljava/util/Calendar;", "Ljava/util/Calendar;",
                                intBeanClazzName, method.getName());
                    } else if (parameterType.equals(Date.class)) {
                        createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeDate",
                                "Ljava/util/Date;", "Ljava/util/Date;", intBeanClazzName, method.getName());
                    } else if (parameterType.equals(String.class)) {
                        createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                "deserializeString", "Ljava/lang/String;", "Ljava/lang/String;",
                                intBeanClazzName, method.getName());
                    }
                }
            }

            visitor.visitVarInsn(Opcodes.ALOAD, 3);
            visitor.visitInsn(Opcodes.ARETURN);
            visitor.visitMaxs(5, 4);
            visitor.visitEnd();

            writer.visitEnd();
            byte[] clazzBytes = writer.toByteArray();
            ClassLoader controllerLoader = null;

            if (classLoader.hasLoaded(beanClazz)) {
                controllerLoader = beanClazz.getClassLoader();
            } else {
                controllerLoader = this.classLoader;
            }

            Class<?> clazz = Serialization.loadClass(controllerLoader,
                    intDeserializerClazzName.replace('/', '.'), clazzBytes);
            deserializer = (QueryDeserializer<T>) clazz.newInstance();
            deserializers.put(beanClazz, deserializer);
            return deserializer;
        }
    } catch (IllegalAccessException e) {
        throw new SerializerFactoryException(e);
    } catch (InstantiationException e) {
        throw new SerializerFactoryException(e);
    }
}

From source file:com.nginious.http.serialize.XmlDeserializerCreator.java

License:Apache License

/**
 * Creates a XML deserializer for the specified bean class unless a deserializer has already
 * been created. Created deserializers are cached and returned on subsequent calls to this method.
 * //from  w w  w .ja v  a  2  s  . c o m
 * @param <T> class type for bean
 * @param beanClazz bean class for which a deserializer should be created
 * @return the created deserializer
 * @throws SerializerFactoryException if unable to create deserializer or class is not a bean
 */
@SuppressWarnings("unchecked")
protected <T> XmlDeserializer<T> create(Class<T> beanClazz) throws SerializerFactoryException {
    XmlDeserializer<T> deserializer = (XmlDeserializer<T>) deserializers.get(beanClazz);

    if (deserializer != null) {
        return deserializer;
    }

    try {
        synchronized (this) {
            deserializer = (XmlDeserializer<T>) deserializers.get(beanClazz);

            if (deserializer != null) {
                return deserializer;
            }

            checkDeserializability(beanClazz, "xml");
            String intBeanClazzName = Serialization.createInternalClassName(beanClazz);
            Method[] methods = beanClazz.getMethods();

            String intDeserializerClazzName = new StringBuffer(intBeanClazzName).append("XmlDeserializer")
                    .toString();

            // Create class
            ClassWriter writer = new ClassWriter(0);
            String signature = Serialization.createClassSignature("com/nginious/http/serialize/XmlDeserializer",
                    intBeanClazzName);
            writer.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, intDeserializerClazzName, signature,
                    "com/nginious/http/serialize/XmlDeserializer", null);

            // Create constructor
            Serialization.createConstructor(writer, "com/nginious/http/serialize/XmlDeserializer");

            // Create deserialize method
            MethodVisitor visitor = createDeserializeMethod(writer, intBeanClazzName);

            for (Method method : methods) {
                Serializable info = method.getAnnotation(Serializable.class);
                boolean canDeserialize = info == null
                        || (info != null && info.deserialize() && info.types().indexOf("xml") > -1);

                if (canDeserialize && method.getName().startsWith("set")
                        && method.getReturnType().equals(void.class)
                        && method.getParameterTypes().length == 1) {
                    Class<?>[] parameterTypes = method.getParameterTypes();
                    Class<?> parameterType = parameterTypes[0];

                    if (parameterType.isArray()) {
                        Class<?> arrayType = parameterType.getComponentType();

                        if (arrayType.equals(boolean.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeBooleanArray", "[Z", "[Z", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(double.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeDoubleArray", "[D", "[D", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(float.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeFloatArray", "[F", "[F", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(int.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeIntArray", "[I", "[I", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(long.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeLongArray", "[J", "[J", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(short.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeShortArray", "[S", "[S", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(String.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeStringArray", "[Ljava/lang/String;", "[Ljava/lang/String;",
                                    intBeanClazzName, method.getName());
                        }
                    } else if (parameterType.isPrimitive()) {
                        if (parameterType.equals(boolean.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeBoolean", "Z", "Z", intBeanClazzName, method.getName());
                        } else if (parameterType.equals(double.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeDouble", "D", "D", intBeanClazzName, method.getName());
                        } else if (parameterType.equals(float.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeFloat", "F", "F", intBeanClazzName, method.getName());
                        } else if (parameterType.equals(int.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeInt", "I", "I", intBeanClazzName, method.getName());
                        } else if (parameterType.equals(long.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeLong", "J", "J", intBeanClazzName, method.getName());
                        } else if (parameterType.equals(short.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeShort", "S", "S", intBeanClazzName, method.getName());
                        }
                    } else if (parameterType.equals(Calendar.class)) {
                        createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                "deserializeCalendar", "Ljava/util/Calendar;", "Ljava/util/Calendar;",
                                intBeanClazzName, method.getName());
                    } else if (parameterType.equals(Date.class)) {
                        createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeDate",
                                "Ljava/util/Date;", "Ljava/util/Date;", intBeanClazzName, method.getName());
                    } else if (parameterType.equals(String.class)) {
                        createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                "deserializeString", "Ljava/lang/String;", "Ljava/lang/String;",
                                intBeanClazzName, method.getName());
                    }
                }
            }

            visitor.visitVarInsn(Opcodes.ALOAD, 3);
            visitor.visitInsn(Opcodes.ARETURN);
            visitor.visitMaxs(5, 4);
            visitor.visitEnd();

            writer.visitEnd();
            byte[] clazzBytes = writer.toByteArray();
            ClassLoader controllerLoader = null;

            if (classLoader.hasLoaded(beanClazz)) {
                controllerLoader = beanClazz.getClassLoader();
            } else {
                controllerLoader = this.classLoader;
            }

            Class<?> clazz = Serialization.loadClass(controllerLoader,
                    intDeserializerClazzName.replace('/', '.'), clazzBytes);
            deserializer = (XmlDeserializer<T>) clazz.newInstance();
            deserializers.put(beanClazz, deserializer);
            return deserializer;
        }
    } catch (IllegalAccessException e) {
        throw new SerializerFactoryException(e);
    } catch (InstantiationException e) {
        throw new SerializerFactoryException(e);
    }
}

From source file:com.nginious.http.xsp.expr.ExpressionCompiler.java

License:Apache License

/**
 * Creates a compiled expression from the specified tree value node expression. The class bytecode for the 
 * compiled expression is generated at runtime.
 * /*from  w  ww .  j a v a 2s .c o m*/
 * @param uncompiled the uncompiled tree value node expression
 * @return the compiled expression
 * @throws ExpressionException if unable to compile expression
 */
public Expression compile(TreeExpression uncompiled) throws ExpressionException {
    ClassWriter writer = new ClassWriter(0);

    // Create class
    String className = classBaseName + classNameCounter.getAndIncrement();
    String classIdentifier = className.replace('.', '/');
    writer.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, classIdentifier, "L" + classIdentifier + ";",
            "com/nginious/http/xsp/expr/Expression", null);

    // Create constructor
    MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PROTECTED, "<init>", "()V", null, null);
    visitor.visitCode();
    visitor.visitVarInsn(Opcodes.ALOAD, 0);
    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nginious/http/xsp/expr/Expression", "<init>", "()V");
    visitor.visitInsn(Opcodes.RETURN);
    visitor.visitMaxs(1, 1);
    visitor.visitEnd();

    // protected abstract boolean evaluateBoolean();
    visitor = writer.visitMethod(Opcodes.ACC_PROTECTED, "evaluateBoolean", "()Z", null, null);

    if (uncompiled.getType() == Type.BOOLEAN) {
        uncompiled.compile(visitor);
    } else if (uncompiled.getType() == Type.DOUBLE) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateDouble", "()D");

        Label endLabel = new Label();
        Label falseLabel = new Label();
        visitor.visitLdcInsn(0.0d);
        visitor.visitInsn(Opcodes.DCMPL);
        visitor.visitJumpInsn(Opcodes.IFEQ, falseLabel);
        visitor.visitLdcInsn(true);
        visitor.visitJumpInsn(Opcodes.GOTO, endLabel);
        visitor.visitLabel(falseLabel);
        visitor.visitLdcInsn(false);
        visitor.visitLabel(endLabel);
    } else if (uncompiled.getType() == Type.INT) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateInt", "()I");

        Label endLabel = new Label();
        Label falseLabel = new Label();
        visitor.visitLdcInsn(0);
        visitor.visitJumpInsn(Opcodes.IFNE, falseLabel);
        visitor.visitLdcInsn(true);
        visitor.visitJumpInsn(Opcodes.GOTO, endLabel);
        visitor.visitLabel(falseLabel);
        visitor.visitLdcInsn(false);
        visitor.visitLabel(endLabel);
    } else if (uncompiled.getType() == Type.STRING) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateString",
                "()Ljava/lang/String;");
        visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "parseBoolean",
                "(Ljava/lang/String;)Z");
    }

    visitor.visitInsn(Opcodes.IRETURN);
    visitor.visitMaxs(5, 5);
    visitor.visitEnd();

    // protected abstract int evaluateInt();
    visitor = writer.visitMethod(Opcodes.ACC_PROTECTED, "evaluateInt", "()I", null, null);

    if (uncompiled.getType() == Type.BOOLEAN) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateBoolean", "()Z");

        Label endLabel = new Label();
        Label falseLabel = new Label();
        visitor.visitJumpInsn(Opcodes.IFEQ, falseLabel);
        visitor.visitLdcInsn(1);
        visitor.visitJumpInsn(Opcodes.GOTO, endLabel);
        visitor.visitLabel(falseLabel);
        visitor.visitLdcInsn(0);
        visitor.visitLabel(endLabel);
    } else if (uncompiled.getType() == Type.DOUBLE) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateDouble", "()D");
        visitor.visitInsn(Opcodes.D2I);
    } else if (uncompiled.getType() == Type.INT) {
        uncompiled.compile(visitor);
    } else if (uncompiled.getType() == Type.STRING) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateString",
                "()Ljava/lang/String;");
        visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "parseInt", "(Ljava/lang/String;)I");
    }

    visitor.visitInsn(Opcodes.IRETURN);
    visitor.visitMaxs(5, 5);
    visitor.visitEnd();

    // protected abstract double evaluateDouble();
    visitor = writer.visitMethod(Opcodes.ACC_PROTECTED, "evaluateDouble", "()D", null, null);

    if (uncompiled.getType() == Type.BOOLEAN) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateBoolean", "()Z");

        Label endLabel = new Label();
        Label falseLabel = new Label();
        visitor.visitJumpInsn(Opcodes.IFEQ, falseLabel);
        visitor.visitLdcInsn(1.0d);
        visitor.visitJumpInsn(Opcodes.GOTO, endLabel);
        visitor.visitLabel(falseLabel);
        visitor.visitLdcInsn(0.0d);
        visitor.visitLabel(endLabel);
    } else if (uncompiled.getType() == Type.DOUBLE) {
        uncompiled.compile(visitor);
    } else if (uncompiled.getType() == Type.INT) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateInt", "()I");
        visitor.visitInsn(Opcodes.I2D);
    } else if (uncompiled.getType() == Type.STRING) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateString",
                "()Ljava/lang/String;");
        visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "parseDouble",
                "(Ljava/lang/String;)D");
    }

    visitor.visitInsn(Opcodes.DRETURN);
    visitor.visitMaxs(5, 5);
    visitor.visitEnd();

    // protected abstract String evaluateString();
    visitor = writer.visitMethod(Opcodes.ACC_PROTECTED, "evaluateString", "()Ljava/lang/String;", null, null);

    if (uncompiled.getType() == Type.BOOLEAN) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateBoolean", "()Z");
        visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "toString", "(Z)Ljava/lang/String;");
    } else if (uncompiled.getType() == Type.DOUBLE) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateDouble", "()D");
        visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "toString", "(D)Ljava/lang/String;");
    } else if (uncompiled.getType() == Type.INT) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0); // this
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classIdentifier, "evaluateInt", "()I");
        visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "toString", "(I)Ljava/lang/String;");
    } else if (uncompiled.getType() == Type.STRING) {
        uncompiled.compile(visitor);
    }

    visitor.visitInsn(Opcodes.ARETURN);
    visitor.visitMaxs(6, 6);
    visitor.visitEnd();

    // public abstract Type getType();        
    visitor = writer.visitMethod(Opcodes.ACC_PUBLIC, "getType", "()Lcom/nginious/http/xsp/expr/Type;", null,
            null);

    if (uncompiled.getType() == Type.BOOLEAN) {
        visitor.visitFieldInsn(Opcodes.GETSTATIC, "com/nginious/http/xsp/expr/Type", "BOOLEAN",
                "Lcom/nginious/http/xsp/expr/Type;");
        visitor.visitInsn(Opcodes.ARETURN);
    } else if (uncompiled.getType() == Type.DOUBLE) {
        visitor.visitFieldInsn(Opcodes.GETSTATIC, "com/nginious/http/xsp/expr/Type", "DOUBLE",
                "Lcom/nginious/http/xsp/expr/Type;");
        visitor.visitInsn(Opcodes.ARETURN);
    } else if (uncompiled.getType() == Type.INT) {
        visitor.visitFieldInsn(Opcodes.GETSTATIC, "com/nginious/http/xsp/expr/Type", "INT",
                "Lcom/nginious/http/xsp/expr/Type;");
        visitor.visitInsn(Opcodes.ARETURN);
    } else if (uncompiled.getType() == Type.STRING) {
        visitor.visitFieldInsn(Opcodes.GETSTATIC, "com/nginious/http/xsp/expr/Type", "STRING",
                "Lcom/nginious/http/xsp/expr/Type;");
        visitor.visitInsn(Opcodes.ARETURN);
    }

    visitor.visitMaxs(1, 1);
    visitor.visitEnd();

    try {
        writer.visitEnd();
        byte[] clazzBytes = writer.toByteArray();
        Class<?> clazz = loadClass(className, clazzBytes);
        return (Expression) clazz.newInstance();
    } catch (Exception e) {
        throw new ExpressionException("Can't instantiate compiled expression", e);
    }
}

From source file:com.nway.spring.jdbc.bean.AsmBeanProcessor.java

License:Apache License

/**
 *
 * /* w w  w  .  j a  v  a 2s  .c  o  m*/
 *
 * @param mv MethodVisitor
 * @param processorName com/nway/commons/dbutils/DynamicBeanProcessorImpl
 * @param beanName com/nway/commons/dbutils/test/User
 */
private void endScript(MethodVisitor mv, Label processorLabel, Label beanStart, int lineNumber,
        String processorName, String beanName) {

    Label l10 = new Label();
    mv.visitLabel(l10);
    mv.visitLineNumber(lineNumber, l10);
    mv.visitVarInsn(Opcodes.ALOAD, 3);
    mv.visitInsn(Opcodes.ARETURN);
    Label l11 = new Label();
    mv.visitLabel(l11);
    mv.visitLocalVariable("this", "L" + processorName + ";", null, processorLabel, l11, 0);
    mv.visitLocalVariable("rs", "Ljava/sql/ResultSet;", null, processorLabel, l11, 1);
    mv.visitLocalVariable("type", "Ljava/lang/Class;", "Ljava/lang/Class<TT;>;", processorLabel, l11, 2);
    mv.visitLocalVariable("bean", "L" + beanName + ";", null, beanStart, l11, 3);
    mv.visitMaxs(5, 4);
    mv.visitEnd();
}

From source file:com.offbynull.coroutines.instrumenter.asm.InstructionUtils.java

License:Open Source License

/**
 * Generates instructions that returns a dummy value. Return values are as follows:
 * <ul>/*from  w w  w.  j  ava2 s. c om*/
 * <li>void -&gt; no value</li>
 * <li>boolean -&gt; false</li>
 * <li>byte/short/char/int -&gt; 0</li>
 * <li>long -&gt; 0L</li>
 * <li>float -&gt; 0.0f</li>
 * <li>double -&gt; 0.0</li>
 * <li>Object -&gt; null</li>
 * </ul>
 *
 * @param returnType return type of the method this generated bytecode is for
 * @return instructions to return a dummy value
 * @throws NullPointerException if any argument is {@code null}
 * @throws IllegalArgumentException if {@code returnType}'s sort is of {@link Type#METHOD}
 */
public static InsnList returnDummy(Type returnType) {
    Validate.notNull(returnType);
    Validate.isTrue(returnType.getSort() != Type.METHOD);

    InsnList ret = new InsnList();

    switch (returnType.getSort()) {
    case Type.VOID:
        ret.add(new InsnNode(Opcodes.RETURN));
        break;
    case Type.BOOLEAN:
    case Type.BYTE:
    case Type.SHORT:
    case Type.CHAR:
    case Type.INT:
        ret.add(new InsnNode(Opcodes.ICONST_0));
        ret.add(new InsnNode(Opcodes.IRETURN));
        break;
    case Type.LONG:
        ret.add(new InsnNode(Opcodes.LCONST_0));
        ret.add(new InsnNode(Opcodes.LRETURN));
        break;
    case Type.FLOAT:
        ret.add(new InsnNode(Opcodes.FCONST_0));
        ret.add(new InsnNode(Opcodes.FRETURN));
        break;
    case Type.DOUBLE:
        ret.add(new InsnNode(Opcodes.DCONST_0));
        ret.add(new InsnNode(Opcodes.DRETURN));
        break;
    case Type.OBJECT:
    case Type.ARRAY:
        ret.add(new InsnNode(Opcodes.ACONST_NULL));
        ret.add(new InsnNode(Opcodes.ARETURN));
        break;
    default:
        throw new IllegalStateException();
    }

    return ret;
}

From source file:com.offbynull.coroutines.instrumenter.asm.InstructionUtils.java

License:Open Source License

/**
 * Generates instructions that returns a value.
 *
 * @param returnType return type of the method this generated bytecode is for
 * @param returnValueInsnList instructions that produce the return value (should leave it on the top of the stack)
 * @return instructions to return a value
 * @throws NullPointerException if any argument is {@code null}
 * @throws IllegalArgumentException if {@code returnType}'s sort is of {@link Type#METHOD}
 *//* w w  w.  j ava 2  s  .co  m*/
public static InsnList returnValue(Type returnType, InsnList returnValueInsnList) {
    Validate.notNull(returnType);
    Validate.isTrue(returnType.getSort() != Type.METHOD);

    InsnList ret = new InsnList();

    ret.add(returnValueInsnList);

    switch (returnType.getSort()) {
    case Type.VOID:
        ret.add(new InsnNode(Opcodes.RETURN));
        break;
    case Type.BOOLEAN:
    case Type.BYTE:
    case Type.SHORT:
    case Type.CHAR:
    case Type.INT:
        ret.add(new InsnNode(Opcodes.IRETURN));
        break;
    case Type.LONG:
        ret.add(new InsnNode(Opcodes.LRETURN));
        break;
    case Type.FLOAT:
        ret.add(new InsnNode(Opcodes.FRETURN));
        break;
    case Type.DOUBLE:
        ret.add(new InsnNode(Opcodes.DRETURN));
        break;
    case Type.OBJECT:
    case Type.ARRAY:
        ret.add(new InsnNode(Opcodes.ARETURN));
        break;
    default:
        throw new IllegalStateException();
    }

    return ret;
}

From source file:com.offbynull.coroutines.instrumenter.ContinuationGenerators.java

License:Open Source License

/**
 * Generates instructions that returns a dummy value. Return values are as follows:
 * <ul>// w w  w .j av  a  2 s  .  co m
 * <li>void -&gt; no value</li>
 * <li>boolean -&gt; false</li>
 * <li>byte/short/char/int -&gt; 0</li>
 * <li>long -&gt; 0L</li>
 * <li>float -&gt; 0.0f</li>
 * <li>double -&gt; 0.0</li>
 * <li>Object -&gt; null</li>
 * </ul>
 *
 * @param returnType return type of the method this generated bytecode is for
 * @return instructions to return a dummy value
 * @throws NullPointerException if any argument is {@code null}
 * @throws IllegalArgumentException if {@code returnType}'s sort is of {@link Type#METHOD}
 */
private static InsnList returnDummy(Type returnType) {
    Validate.notNull(returnType);
    Validate.isTrue(returnType.getSort() != Type.METHOD);

    InsnList ret = new InsnList();

    switch (returnType.getSort()) {
    case Type.VOID:
        ret.add(new InsnNode(Opcodes.RETURN));
        break;
    case Type.BOOLEAN:
    case Type.BYTE:
    case Type.SHORT:
    case Type.CHAR:
    case Type.INT:
        ret.add(new InsnNode(Opcodes.ICONST_0));
        ret.add(new InsnNode(Opcodes.IRETURN));
        break;
    case Type.LONG:
        ret.add(new InsnNode(Opcodes.LCONST_0));
        ret.add(new InsnNode(Opcodes.LRETURN));
        break;
    case Type.FLOAT:
        ret.add(new InsnNode(Opcodes.FCONST_0));
        ret.add(new InsnNode(Opcodes.FRETURN));
        break;
    case Type.DOUBLE:
        ret.add(new InsnNode(Opcodes.DCONST_0));
        ret.add(new InsnNode(Opcodes.DRETURN));
        break;
    case Type.OBJECT:
    case Type.ARRAY:
        ret.add(new InsnNode(Opcodes.ACONST_NULL));
        ret.add(new InsnNode(Opcodes.ARETURN));
        break;
    default:
        throw new IllegalStateException();
    }

    return ret;
}