Example usage for org.objectweb.asm.commons GeneratorAdapter valueOf

List of usage examples for org.objectweb.asm.commons GeneratorAdapter valueOf

Introduction

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

Prototype

public void valueOf(final Type type) 

Source Link

Document

Generates the instructions to box the top stack value using Java 5's valueOf() method.

Usage

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

License:Apache License

/**
 * Generates a getter that get the value by directly accessing the class field.
 * @param field The reflection field object.
 *///  www .  j a v a 2s  .c  o  m
private void directGetter(Field field) {
    GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, getMethod(Object.class, "get", Object.class),
            getterSignature(), new Type[0], classWriter);
    // Simply access by field
    // return ((classType)object).fieldName;
    mg.loadArg(0);
    mg.checkCast(Type.getType(field.getDeclaringClass()));
    mg.getField(Type.getType(field.getDeclaringClass()), field.getName(), Type.getType(field.getType()));
    if (field.getType().isPrimitive()) {
        mg.valueOf(Type.getType(field.getType()));
    }
    mg.returnValue();
    mg.endMethod();
}

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

License:Apache License

/**
 * Generates the primitive setter (setXXX) based on the field type.
 * @param field The reflection field object.
 *//*from  w  w  w. j  a v  a2 s .  c o m*/
private void primitiveSetter(Field field) {
    String typeName = field.getType().getName();
    String methodName = String.format("set%c%s", Character.toUpperCase(typeName.charAt(0)),
            typeName.substring(1));

    GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC,
            getMethod(void.class, methodName, Object.class, field.getType()), null, new Type[0], classWriter);
    if (isPrivate) {
        // set the value using the generic void get(Object, Object) method with boxing the value.
        mg.loadThis();
        mg.loadArgs();
        mg.valueOf(Type.getType(field.getType()));
        mg.invokeVirtual(Type.getObjectType(className),
                getMethod(void.class, "set", Object.class, Object.class));
    } else {
        // Simply access the field.
        mg.loadArg(0);
        mg.checkCast(Type.getType(field.getDeclaringClass()));
        mg.loadArg(1);
        mg.putField(Type.getType(field.getDeclaringClass()), field.getName(), Type.getType(field.getType()));
    }
    mg.returnValue();
    mg.endMethod();

}

From source file:io.datakernel.codegen.Utils.java

License:Apache License

public static void cast(Context ctx, Type type, Type targetType) {
    GeneratorAdapter g = ctx.getGeneratorAdapter();

    if (type.equals(targetType)) {
        return;//w ww .j a  va2  s  .  c  o  m
    }

    if (targetType == Type.VOID_TYPE) {
        if (type.getSize() == 1)
            g.pop();
        if (type.getSize() == 2)
            g.pop2();
        return;
    }

    if (type == Type.VOID_TYPE) {
        throw new RuntimeException(format("Can't cast VOID_TYPE to %s. %s", targetType.getClassName(),
                exceptionInGeneratedClass(ctx)));
    }

    if (type.equals(ctx.getThisType())) {
        final Class<?> javaType = getJavaType(ctx.getClassLoader(), targetType);
        for (Class<?> aClass : ctx.getOtherClasses()) {
            if (javaType.isAssignableFrom(aClass)) {
                return;
            }
        }
        throw new RuntimeException(format("Can't cast self %s to %s, %s", type.getClassName(),
                targetType.getClassName(), exceptionInGeneratedClass(ctx)));
    }

    if (!type.equals(ctx.getThisType()) && !targetType.equals(ctx.getThisType())
            && getJavaType(ctx.getClassLoader(), targetType)
                    .isAssignableFrom(getJavaType(ctx.getClassLoader(), type))) {
        return;
    }

    if (targetType.equals(getType(Object.class)) && isPrimitiveType(type)) {
        g.box(type);
        g.cast(wrap(type), getType(Object.class));
        return;
    }

    if ((isPrimitiveType(type) || isWrapperType(type))
            && (isPrimitiveType(targetType) || isWrapperType(targetType))) {

        Type targetTypePrimitive = isPrimitiveType(targetType) ? targetType : unwrap(targetType);

        if (isWrapperType(type)) {
            g.invokeVirtual(type, primitiveValueMethod(targetType));
            return;
        }

        assert isPrimitiveType(type);

        if (type != targetTypePrimitive) {
            g.cast(type, targetTypePrimitive);
        }

        if (isWrapperType(targetType)) {
            g.valueOf(targetTypePrimitive);
        }

        return;
    }

    g.checkCast(targetType);
}

From source file:org.apache.commons.weaver.privilizer.ActionGenerator.java

License:Apache License

/**
 * Generate impl method.//w  w  w .  j a va 2  s .  com
 */
private void impl() {
    final Method run = Method.getMethod("Object run()");

    final GeneratorAdapter mgen = new GeneratorAdapter(Opcodes.ACC_PUBLIC, run, null, exceptions, this);

    for (final Field field : fields) {
        mgen.loadThis();
        mgen.getField(action, field.name, field.type);
    }
    mgen.invokeStatic(owner.target, helper);

    if (methd.getReturnType().getSort() < Type.ARRAY) {
        mgen.valueOf(methd.getReturnType());
    }
    mgen.returnValue();
    mgen.endMethod();
}

From source file:org.apache.deltaspike.partialbean.impl.proxy.AsmProxyClassGenerator.java

License:Apache License

/**
 * Defines a new Object[] and push all method argmuments into the array.
 *
 * @param mg/*w ww. ja v  a 2 s. c  o m*/
 * @param method
 * @param methodType
 */
private static void loadArguments(GeneratorAdapter mg, java.lang.reflect.Method method, Type methodType) {
    // create the Object[]
    mg.push(methodType.getArgumentTypes().length);
    mg.newArray(TYPE_OBJECT);

    // push parameters into array
    for (int i = 0; i < methodType.getArgumentTypes().length; i++) {
        // keep copy of array on stack
        mg.dup();

        // push index onto stack
        mg.push(i);

        mg.loadArg(i);
        mg.valueOf(methodType.getArgumentTypes()[i]);
        mg.arrayStore(TYPE_OBJECT);
    }
}