Java Code Examples for org.objectweb.asm.commons.GeneratorAdapter#unbox()

The following examples show how to use org.objectweb.asm.commons.GeneratorAdapter#unbox() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ByteCodeUtils.java    From Stark with Apache License 2.0 6 votes vote down vote up
/**
 * Given an array with values at the top of the stack, the values are unboxed and stored
 * on the given variables. The array is popped from the stack.
 */
static void restoreVariables(
        @NonNull GeneratorAdapter mv,
        @NonNull List<LocalVariable> variables) {
    for (int i = 0; i < variables.size(); i++) {
        LocalVariable variable = variables.get(i);
        // Duplicates the array on the stack;
        mv.dup();
        // Sets up the index
        mv.push(i);
        // Gets the Object value
        mv.arrayLoad(Type.getType(Object.class));
        // Unboxes to the type of the local variable
        mv.unbox(variable.type);
        // Restores the local variable
        mv.visitVarInsn(variable.type.getOpcode(Opcodes.ISTORE), variable.var);
    }
    // Pops the array from the stack.
    mv.pop();
}
 
Example 2
Source File: MeasureHyperLogLog.java    From datakernel with Apache License 2.0 6 votes vote down vote up
@Override
public Type load(Context ctx) {
	GeneratorAdapter g = ctx.getGeneratorAdapter();
	Type accumulatorType = accumulator.load(ctx);
	Type valueType = value.load(ctx);
	String methodName;
	Type methodParameterType;
	if (valueType == LONG_TYPE || valueType.getClassName().equals(Long.class.getName())) {
		methodName = "addLong";
		methodParameterType = LONG_TYPE;
	} else if (valueType == INT_TYPE || valueType.getClassName().equals(Integer.class.getName())) {
		methodName = "addInt";
		methodParameterType = INT_TYPE;
	} else {
		methodName = "addObject";
		methodParameterType = getType(Object.class);
	}

	if (isWrapperType(valueType)) {
		g.unbox(methodParameterType);
	}

	ctx.invoke(accumulatorType, methodName, methodParameterType);

	return VOID_TYPE;
}
 
Example 3
Source File: ByteCodeUtils.java    From Stark with Apache License 2.0 5 votes vote down vote up
/**
 * Generates unboxing bytecode for the passed type. An {@link Object} is expected to be on the
 * stack when these bytecodes are inserted.
 *
 * ASM takes a short cut when dealing with short/byte types and convert them into int rather
 * than short/byte types. This is not an issue on the jvm nor Android's ART but it is an issue
 * on Dalvik.
 *
 * @param mv the {@link GeneratorAdapter} generating a method implementation.
 * @param type the expected un-boxed type.
 */
public static void unbox(GeneratorAdapter mv, Type type) {
    if (type.equals(Type.SHORT_TYPE)) {
        mv.checkCast(NUMBER_TYPE);
        mv.invokeVirtual(NUMBER_TYPE, SHORT_VALUE);
    } else if (type.equals(Type.BYTE_TYPE)) {
        mv.checkCast(NUMBER_TYPE);
        mv.invokeVirtual(NUMBER_TYPE, BYTE_VALUE);
    } else {
        mv.unbox(type);
    }
}
 
Example 4
Source File: ByteCodeUtils.java    From Aceso with Apache License 2.0 5 votes vote down vote up
/**
 * Generates unboxing bytecode for the passed type. An {@link Object} is expected to be on the
 * stack when these bytecodes are inserted.
 *
 * ASM takes a short cut when dealing with short/byte types and convert them into int rather
 * than short/byte types. This is not an issue on the jvm nor Android's ART but it is an issue
 * on Dalvik.
 *
 * @param mv the {@link GeneratorAdapter} generating a method implementation.
 * @param type the expected un-boxed type.
 */
public static void unbox(GeneratorAdapter mv, Type type) {
    if (type.equals(Type.SHORT_TYPE)) {
        mv.checkCast(NUMBER_TYPE);
        mv.invokeVirtual(NUMBER_TYPE, SHORT_VALUE);
    } else if (type.equals(Type.BYTE_TYPE)) {
        mv.checkCast(NUMBER_TYPE);
        mv.invokeVirtual(NUMBER_TYPE, BYTE_VALUE);
    } else {
        mv.unbox(type);
    }
}
 
Example 5
Source File: ByteCodeUtils.java    From AnoleFix with MIT License 5 votes vote down vote up
/**
 * Generates unboxing bytecode for the passed type. An {@link Object} is expected to be on the
 * stack when these bytecodes are inserted.
 *
 * ASM takes a short cut when dealing with short/byte types and convert them into int rather
 * than short/byte types. This is not an issue on the jvm nor Android's ART but it is an issue
 * on Dalvik.
 *
 * @param mv the {@link GeneratorAdapter} generating a method implementation.
 * @param type the expected un-boxed type.
 */
public static void unbox(GeneratorAdapter mv, Type type) {
    if (type.equals(Type.SHORT_TYPE)) {
        mv.checkCast(NUMBER_TYPE);
        mv.invokeVirtual(NUMBER_TYPE, SHORT_VALUE);
    } else if (type.equals(Type.BYTE_TYPE)) {
        mv.checkCast(NUMBER_TYPE);
        mv.invokeVirtual(NUMBER_TYPE, BYTE_VALUE);
    } else {
        mv.unbox(type);
    }
}
 
Example 6
Source File: ExpressionArithmeticOp.java    From datakernel with Apache License 2.0 5 votes vote down vote up
@Override
public Type load(Context ctx) {
	GeneratorAdapter g = ctx.getGeneratorAdapter();
	Type leftType = left.load(ctx);
	if (isWrapperType(leftType)) {
		leftType = unwrap(leftType);
		g.unbox(leftType);
	}
	Type rightType = right.load(ctx);
	if (isWrapperType(rightType)) {
		rightType = unwrap(rightType);
		g.unbox(rightType);
	}
	if (op != SHL && op != SHR && op != USHR) {
		Type resultType = getType(unifyArithmeticTypes(ctx.toJavaType(leftType), ctx.toJavaType(rightType)));
		if (leftType != resultType) {
			int rightLocal = g.newLocal(rightType);
			g.storeLocal(rightLocal);
			g.cast(leftType, resultType);
			g.loadLocal(rightLocal);
		}
		if (rightType != resultType) {
			g.cast(rightType, resultType);
		}
		g.visitInsn(resultType.getOpcode(op.opCode));
		return resultType;
	} else {
		if (rightType != Type.getType(int.class)) {
			g.cast(rightType, Type.getType(int.class));
		}
		g.visitInsn(leftType.getOpcode(op.opCode));
		return leftType;
	}
}
 
Example 7
Source File: IncrementalSupportVisitor.java    From AnoleFix with MIT License 4 votes vote down vote up
/***
 * Inserts a trampoline to this class so that the updated methods can make calls to
 * constructors.
 * <p>
 * <p/>
 * Pseudo code for this trampoline:
 * <code>
 * ClassName(Object[] args, Marker unused) {
 * String name = (String) args[0];
 * if (name.equals(
 * "java/lang/ClassName.(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;")) {
 * this((String)arg[1], arg[2]);
 * return
 * }
 * if (name.equals("SuperClassName.(Ljava/lang/String;I)V")) {
 * super((String)arg[1], (int)arg[2]);
 * return;
 * }
 * ...
 * StringBuilder $local1 = new StringBuilder();
 * $local1.append("Method not found ");
 * $local1.append(name);
 * $local1.append(" in " $classType $super implementation");
 * throw new $package/InstantReloadException($local1.toString());
 * }
 * </code>
 */
private void createDispatchingThis() {
    // Gather all methods from itself and its superclasses to generate a giant constructor
    // implementation.
    // This will work fine as long as we don't support adding constructors to classes.
    final Map<String, MethodNode> uniqueMethods = new HashMap<String, MethodNode>();

    addAllNewConstructors(uniqueMethods, classNode, true /*keepPrivateConstructors*/);
    for (ClassNode parentNode : parentNodes) {
        addAllNewConstructors(uniqueMethods, parentNode, false /*keepPrivateConstructors*/);
    }

    int access = Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC;

    Method m = new Method(AsmUtils.CONSTRUCTOR,
            ConstructorArgsRedirection.DISPATCHING_THIS_SIGNATURE);
    MethodVisitor visitor = super.visitMethod(0, m.getName(), m.getDescriptor(), null, null);
    final GeneratorAdapter mv = new GeneratorAdapter(access, m, visitor);

    mv.visitCode();
    // Mark this code as redirection code
    Label label = new Label();
    mv.visitLineNumber(0, label);

    // Get and store the constructor canonical name.
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.push(0);
    mv.visitInsn(Opcodes.AALOAD);
    mv.unbox(Type.getType("Ljava/lang/String;"));
    final int constructorCanonicalName = mv.newLocal(Type.getType("Ljava/lang/String;"));
    mv.storeLocal(constructorCanonicalName);

    new StringSwitch() {

        @Override
        void visitString() {
            mv.loadLocal(constructorCanonicalName);
        }

        @Override
        void visitCase(String canonicalName) {
            MethodNode methodNode = uniqueMethods.get(canonicalName);
            String owner = canonicalName.split("\\.")[0];

            // Parse method arguments and
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            Type[] args = Type.getArgumentTypes(methodNode.desc);
            int argc = 0;
            for (Type t : args) {
                mv.visitVarInsn(Opcodes.ALOAD, 1);
                mv.push(argc + 1);
                mv.visitInsn(Opcodes.AALOAD);
                ByteCodeUtils.unbox(mv, t);
                argc++;
            }

            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, owner, AsmUtils.CONSTRUCTOR,
                    methodNode.desc, false);

            mv.visitInsn(Opcodes.RETURN);
        }

        @Override
        void visitDefault() {
            writeMissingMessageWithHash(mv, visitedClassName);
        }
    }.visit(mv, uniqueMethods.keySet());

    mv.visitMaxs(1, 3);
    mv.visitEnd();
}
 
Example 8
Source File: AsmDeltaSpikeProxyClassGenerator.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
private static void defineMethod(ClassWriter cw, java.lang.reflect.Method method, Type proxyType)
{
    Type methodType = Type.getType(method);
    
    ArrayList<Type> exceptionsToCatch = new ArrayList<Type>();
    for (Class<?> exception : method.getExceptionTypes())
    {
        if (!RuntimeException.class.isAssignableFrom(exception))
        {
            exceptionsToCatch.add(Type.getType(exception));
        }
    }
    
    // push the method definition
    int modifiers = (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED) & method.getModifiers();
    Method asmMethod = Method.getMethod(method);
    GeneratorAdapter mg = new GeneratorAdapter(modifiers,
            asmMethod,
            null,
            getTypes(method.getExceptionTypes()),
            cw);

    // copy annotations
    for (Annotation annotation : method.getDeclaredAnnotations())
    {
        mg.visitAnnotation(Type.getDescriptor(annotation.annotationType()), true).visitEnd();
    }

    mg.visitCode();

    Label tryBlockStart = mg.mark();

    mg.loadThis();
    mg.getField(proxyType, FIELDNAME_INVOCATION_HANDLER, TYPE_DELTA_SPIKE_PROXY_INVOCATION_HANDLER);
    mg.loadThis();
    loadCurrentMethod(mg, method, methodType);
    loadArguments(mg, method, methodType);
    
    mg.invokeVirtual(TYPE_DELTA_SPIKE_PROXY_INVOCATION_HANDLER,
            Method.getMethod("Object invoke(Object, java.lang.reflect.Method, Object[])"));

    // cast the result
    mg.unbox(methodType.getReturnType());

    // build try catch
    Label tryBlockEnd = mg.mark();
    
    // push return
    mg.returnValue();

    // catch runtime exceptions and rethrow it
    Label rethrow = mg.mark();
    mg.visitVarInsn(Opcodes.ASTORE, 1);
    mg.visitVarInsn(Opcodes.ALOAD, 1);
    mg.throwException();
    mg.visitTryCatchBlock(tryBlockStart, tryBlockEnd, rethrow, Type.getInternalName(RuntimeException.class));

    // catch checked exceptions and rethrow it
    boolean throwableCatched = false;
    if (!exceptionsToCatch.isEmpty())
    {
        rethrow = mg.mark();
        mg.visitVarInsn(Opcodes.ASTORE, 1);
        mg.visitVarInsn(Opcodes.ALOAD, 1);
        mg.throwException();

        // catch declared exceptions and rethrow it...
        for (Type exceptionType : exceptionsToCatch)
        {
            if (exceptionType.getClassName().equals(Throwable.class.getName()))
            {
                throwableCatched = true;
            }
            mg.visitTryCatchBlock(tryBlockStart, tryBlockEnd, rethrow, exceptionType.getInternalName());
        }
    }

    // if throwable isn't alreached cachted, catch it and wrap it with an UndeclaredThrowableException and throw it
    if (!throwableCatched)
    {
        Type uteType = Type.getType(UndeclaredThrowableException.class);
        Label wrapAndRethrow = mg.mark();

        mg.visitVarInsn(Opcodes.ASTORE, 1);
        mg.newInstance(uteType);
        mg.dup();
        mg.visitVarInsn(Opcodes.ALOAD, 1);
        mg.invokeConstructor(uteType,
                Method.getMethod("void <init>(java.lang.Throwable)"));
        mg.throwException();

        mg.visitTryCatchBlock(tryBlockStart, tryBlockEnd, wrapAndRethrow, Type.getInternalName(Throwable.class));
    }

    // finish the method
    mg.endMethod();
    mg.visitMaxs(12, 12);
    mg.visitEnd();
}