Java Code Examples for org.objectweb.asm.Type#SHORT

The following examples show how to use org.objectweb.asm.Type#SHORT . 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: GeneratorUtils.java    From GriefDefender with MIT License 6 votes vote down vote up
/**
 * Insert the necessary methods to box a primitive type (if the given type
 * is a primitive object).
 *
 * @param mv The method visitor
 * @param type The type to unbox
 */
public static void visitBoxingMethod(MethodVisitor mv, Type type) {
    if (type.getSort() == Type.BOOLEAN) {
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;", false);
    } else if (type.getSort() == Type.INT) {
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false);
    } else if (type.getSort() == Type.BYTE) {
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;", false);
    } else if (type.getSort() == Type.SHORT) {
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;", false);
    } else if (type.getSort() == Type.LONG) {
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;", false);
    } else if (type.getSort() == Type.FLOAT) {
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;", false);
    } else if (type.getSort() == Type.DOUBLE) {
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;", false);
    } else if (type.getSort() == Type.CHAR) {
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;", false);
    }
}
 
Example 2
Source File: TypeUtils.java    From cglib with Apache License 2.0 6 votes vote down vote up
public static Type getBoxedType(Type type) {
    switch (type.getSort()) {
    case Type.CHAR:
        return Constants.TYPE_CHARACTER;
    case Type.BOOLEAN:
        return Constants.TYPE_BOOLEAN;
    case Type.DOUBLE:
        return Constants.TYPE_DOUBLE;
    case Type.FLOAT:
        return Constants.TYPE_FLOAT;
    case Type.LONG:
        return Constants.TYPE_LONG;
    case Type.INT:
        return Constants.TYPE_INTEGER;
    case Type.SHORT:
        return Constants.TYPE_SHORT;
    case Type.BYTE:
        return Constants.TYPE_BYTE;
    default:
        return type;
    }
}
 
Example 3
Source File: ASMMethodVariables.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
Type getBoxedType(final Type type) {
    switch (type.getSort()) {
        case Type.BYTE:
            return BYTE_TYPE;
        case Type.BOOLEAN:
            return BOOLEAN_TYPE;
        case Type.SHORT:
            return SHORT_TYPE;
        case Type.CHAR:
            return CHARACTER_TYPE;
        case Type.INT:
            return INTEGER_TYPE;
        case Type.FLOAT:
            return FLOAT_TYPE;
        case Type.LONG:
            return LONG_TYPE;
        case Type.DOUBLE:
            return DOUBLE_TYPE;
    }
    return type;
}
 
Example 4
Source File: ArrayWrappingInterpreter.java    From AVM with MIT License 6 votes vote down vote up
@Override
// Override this method to get unmasked type from BasicInterpreter
public BasicValue newValue(final Type type) {
    if (type == null) {
        return BasicValue.UNINITIALIZED_VALUE;
    }
    switch (type.getSort()) {
        case Type.VOID:
            return null;
        case Type.BOOLEAN:
        case Type.CHAR:
        case Type.BYTE:
        case Type.SHORT:
        case Type.INT:
        case Type.FLOAT:
        case Type.LONG:
        case Type.DOUBLE:
        case Type.ARRAY:
        case Type.OBJECT:
            return new BasicValue(type);
        default:
            throw new AssertionError();
    }
}
 
Example 5
Source File: GeneratorAdapter.java    From JReFrameworker with MIT License 5 votes vote down vote up
/**
 * Generates the instruction to push the given value on the stack.
 *
 * @param value the value to be pushed on the stack.
 */
public void push(final Type value) {
  if (value == null) {
    mv.visitInsn(Opcodes.ACONST_NULL);
  } else {
    switch (value.getSort()) {
      case Type.BOOLEAN:
        mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Boolean", "TYPE", CLASS_DESCRIPTOR);
        break;
      case Type.CHAR:
        mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Character", "TYPE", CLASS_DESCRIPTOR);
        break;
      case Type.BYTE:
        mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Byte", "TYPE", CLASS_DESCRIPTOR);
        break;
      case Type.SHORT:
        mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Short", "TYPE", CLASS_DESCRIPTOR);
        break;
      case Type.INT:
        mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Integer", "TYPE", CLASS_DESCRIPTOR);
        break;
      case Type.FLOAT:
        mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Float", "TYPE", CLASS_DESCRIPTOR);
        break;
      case Type.LONG:
        mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Long", "TYPE", CLASS_DESCRIPTOR);
        break;
      case Type.DOUBLE:
        mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/Double", "TYPE", CLASS_DESCRIPTOR);
        break;
      default:
        mv.visitLdcInsn(value);
        break;
    }
  }
}
 
Example 6
Source File: WeavingClassVisitor.java    From glowroot with Apache License 2.0 5 votes vote down vote up
private static void loadType(MethodVisitor mv, Type type, Type ownerType) {
    switch (type.getSort()) {
        case Type.VOID:
            mv.visitFieldInsn(GETSTATIC, "java/lang/Void", "TYPE", "Ljava/lang/Class;");
            break;
        case Type.BOOLEAN:
            mv.visitFieldInsn(GETSTATIC, "java/lang/Boolean", "TYPE", "Ljava/lang/Class;");
            break;
        case Type.CHAR:
            mv.visitFieldInsn(GETSTATIC, "java/lang/Character", "TYPE", "Ljava/lang/Class;");
            break;
        case Type.BYTE:
            mv.visitFieldInsn(GETSTATIC, "java/lang/Byte", "TYPE", "Ljava/lang/Class;");
            break;
        case Type.SHORT:
            mv.visitFieldInsn(GETSTATIC, "java/lang/Short", "TYPE", "Ljava/lang/Class;");
            break;
        case Type.INT:
            mv.visitFieldInsn(GETSTATIC, "java/lang/Integer", "TYPE", "Ljava/lang/Class;");
            break;
        case Type.FLOAT:
            mv.visitFieldInsn(GETSTATIC, "java/lang/Float", "TYPE", "Ljava/lang/Class;");
            break;
        case Type.LONG:
            mv.visitFieldInsn(GETSTATIC, "java/lang/Long", "TYPE", "Ljava/lang/Class;");
            break;
        case Type.DOUBLE:
            mv.visitFieldInsn(GETSTATIC, "java/lang/Double", "TYPE", "Ljava/lang/Class;");
            break;
        case Type.ARRAY:
            loadArrayType(mv, type, ownerType);
            break;
        default:
            loadObjectType(mv, type, ownerType);
    }
}
 
Example 7
Source File: ABIUtils.java    From AVM with MIT License 5 votes vote down vote up
public static boolean isPrimitiveType(Type type) {
    switch (type.getSort()) {
        case Type.BYTE:
        case Type.BOOLEAN:
        case Type.CHAR:
        case Type.SHORT:
        case Type.INT:
        case Type.FLOAT:
        case Type.LONG:
        case Type.DOUBLE:
            return true;
        default:
            return false;
    }
}
 
Example 8
Source File: GeneratorAdapter.java    From Concurnas with MIT License 5 votes vote down vote up
/**
 * Generates the instructions to unbox the top stack value. This value is replaced by its unboxed
 * equivalent on top of the stack.
 *
 * @param type the type of the top stack value.
 */
public void unbox(final Type type) {
  Type boxedType = NUMBER_TYPE;
  Method unboxMethod;
  switch (type.getSort()) {
    case Type.VOID:
      return;
    case Type.CHAR:
      boxedType = CHARACTER_TYPE;
      unboxMethod = CHAR_VALUE;
      break;
    case Type.BOOLEAN:
      boxedType = BOOLEAN_TYPE;
      unboxMethod = BOOLEAN_VALUE;
      break;
    case Type.DOUBLE:
      unboxMethod = DOUBLE_VALUE;
      break;
    case Type.FLOAT:
      unboxMethod = FLOAT_VALUE;
      break;
    case Type.LONG:
      unboxMethod = LONG_VALUE;
      break;
    case Type.INT:
    case Type.SHORT:
    case Type.BYTE:
      unboxMethod = INT_VALUE;
      break;
    default:
      unboxMethod = null;
      break;
  }
  if (unboxMethod == null) {
    checkCast(type);
  } else {
    checkCast(boxedType);
    invokeVirtual(boxedType, unboxMethod);
  }
}
 
Example 9
Source File: AsmUtils.java    From grappa with Apache License 2.0 5 votes vote down vote up
/**
 * Get the class equivalent to an ASM {@link Type}
 *
 * @param type the type
 * @return the matching class
 */
public static Class<?> getClassForType(final Type type)
{
    Objects.requireNonNull(type, "type");
    switch (type.getSort()) {
        case Type.BOOLEAN:
            return boolean.class;
        case Type.BYTE:
            return byte.class;
        case Type.CHAR:
            return char.class;
        case Type.DOUBLE:
            return double.class;
        case Type.FLOAT:
            return float.class;
        case Type.INT:
            return int.class;
        case Type.LONG:
            return long.class;
        case Type.SHORT:
            return short.class;
        case Type.VOID:
            return void.class;
        case Type.OBJECT:
        case Type.ARRAY:
            return CACHE.loadClass(type.getInternalName());
            //return getClassForInternalName(type.getInternalName());
    }
    throw new IllegalStateException(); // should be unreachable
}
 
Example 10
Source File: BasicInterpreter.java    From JByteMod-Beta with GNU General Public License v2.0 5 votes vote down vote up
@Override
public BasicValue newValue(final Type type) {
  if (type == null) {
    return BasicValue.UNINITIALIZED_VALUE;
  }
  switch (type.getSort()) {
  case Type.VOID:
    return null;
  case Type.BOOLEAN:
  case Type.CHAR:
  case Type.BYTE:
  case Type.SHORT:
  case Type.INT:
    return BasicValue.INT_VALUE;
  case Type.FLOAT:
    return BasicValue.FLOAT_VALUE;
  case Type.LONG:
    return BasicValue.LONG_VALUE;
  case Type.DOUBLE:
    return BasicValue.DOUBLE_VALUE;
  case Type.ARRAY:
  case Type.OBJECT:
    return BasicValue.REFERENCE_VALUE;
  default:
    throw new Error("Internal error");
  }
}
 
Example 11
Source File: ASMUtil.java    From json-smart-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Append the call of proper autoboxing method for the given primitif type.
 */
protected static void autoBoxing(MethodVisitor mv, Type fieldType) {
	switch (fieldType.getSort()) {
	case Type.BOOLEAN:
		mv.visitMethodInsn(INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;");
		break;
	case Type.BYTE:
		mv.visitMethodInsn(INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;");
		break;
	case Type.CHAR:
		mv.visitMethodInsn(INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;");
		break;
	case Type.SHORT:
		mv.visitMethodInsn(INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;");
		break;
	case Type.INT:
		mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
		break;
	case Type.FLOAT:
		mv.visitMethodInsn(INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;");
		break;
	case Type.LONG:
		mv.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;");
		break;
	case Type.DOUBLE:
		mv.visitMethodInsn(INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;");
		break;
	}
}
 
Example 12
Source File: PrimitiveTypeWidget.java    From yql-plus with Apache License 2.0 5 votes vote down vote up
@Override
public ComparisonAdapter getComparisionAdapter() {
    return new ComparisonAdapter() {
        @Override
        public void coerceBoolean(CodeEmitter scope, Label isTrue, Label isFalse, Label isNull) {
            MethodVisitor mv = scope.getMethodVisitor();
            switch (getJVMType().getSort()) {
                case Type.BOOLEAN:
                case Type.SHORT:
                case Type.INT:
                case Type.CHAR:
                    mv.visitJumpInsn(Opcodes.IFEQ, isFalse);
                    mv.visitJumpInsn(Opcodes.GOTO, isTrue);
                    break;
                case Type.FLOAT:
                    mv.visitInsn(Opcodes.FCONST_0);
                    mv.visitInsn(Opcodes.FCMPG);
                    mv.visitJumpInsn(Opcodes.IFEQ, isTrue);
                    mv.visitJumpInsn(Opcodes.GOTO, isFalse);
                    break;
                case Type.LONG:
                    mv.visitInsn(Opcodes.LCONST_0);
                    mv.visitInsn(Opcodes.LCMP);
                    mv.visitJumpInsn(Opcodes.IFEQ, isTrue);
                    mv.visitJumpInsn(Opcodes.GOTO, isFalse);
                    break;
                case Type.DOUBLE:
                    mv.visitInsn(Opcodes.DCONST_0);
                    mv.visitInsn(Opcodes.DCMPG);
                    mv.visitJumpInsn(Opcodes.IFEQ, isTrue);
                    mv.visitJumpInsn(Opcodes.GOTO, isFalse);
                    break;
                default:
                    throw new UnsupportedOperationException("Unsupported JVM type: " + getJVMType());

            }
        }
    };
}
 
Example 13
Source File: GenericGenerators.java    From coroutines with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 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}
 */
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;
}
 
Example 14
Source File: FieldAccess.java    From reflectasm with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
static private void insertSetObject (ClassWriter cw, String classNameInternal, ArrayList<Field> fields) {
	int maxStack = 6;
	MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "set", "(Ljava/lang/Object;ILjava/lang/Object;)V", null, null);
	mv.visitCode();
	mv.visitVarInsn(ILOAD, 2);

	if (!fields.isEmpty()) {
		maxStack--;
		Label[] labels = new Label[fields.size()];
		for (int i = 0, n = labels.length; i < n; i++)
			labels[i] = new Label();
		Label defaultLabel = new Label();
		mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

		for (int i = 0, n = labels.length; i < n; i++) {
			Field field = fields.get(i);
			Type fieldType = Type.getType(field.getType());

			mv.visitLabel(labels[i]);
			mv.visitFrame(F_SAME, 0, null, 0, null);
			mv.visitVarInsn(ALOAD, 1);
			mv.visitTypeInsn(CHECKCAST, classNameInternal);
			mv.visitVarInsn(ALOAD, 3);

			switch (fieldType.getSort()) {
			case Type.BOOLEAN:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Boolean");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z");
				break;
			case Type.BYTE:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Byte");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B");
				break;
			case Type.CHAR:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Character");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Character", "charValue", "()C");
				break;
			case Type.SHORT:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Short");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S");
				break;
			case Type.INT:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Integer");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I");
				break;
			case Type.FLOAT:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Float");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F");
				break;
			case Type.LONG:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Long");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J");
				break;
			case Type.DOUBLE:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Double");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D");
				break;
			case Type.ARRAY:
				mv.visitTypeInsn(CHECKCAST, fieldType.getDescriptor());
				break;
			case Type.OBJECT:
				mv.visitTypeInsn(CHECKCAST, fieldType.getInternalName());
				break;
			}

			mv.visitFieldInsn(PUTFIELD, field.getDeclaringClass().getName().replace('.', '/'), field.getName(),
				fieldType.getDescriptor());
			mv.visitInsn(RETURN);
		}

		mv.visitLabel(defaultLabel);
		mv.visitFrame(F_SAME, 0, null, 0, null);
	}
	mv = insertThrowExceptionForFieldNotFound(mv);
	mv.visitMaxs(maxStack, 4);
	mv.visitEnd();
}
 
Example 15
Source File: FieldAccess.java    From reflectasm with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
static private void insertSetPrimitive (ClassWriter cw, String classNameInternal, ArrayList<Field> fields,
	Type primitiveType) {
	int maxStack = 6;
	int maxLocals = 4; // See correction below for LLOAD and DLOAD
	final String setterMethodName;
	final String typeNameInternal = primitiveType.getDescriptor();
	final int loadValueInstruction;
	switch (primitiveType.getSort()) {
	case Type.BOOLEAN:
		setterMethodName = "setBoolean";
		loadValueInstruction = ILOAD;
		break;
	case Type.BYTE:
		setterMethodName = "setByte";
		loadValueInstruction = ILOAD;
		break;
	case Type.CHAR:
		setterMethodName = "setChar";
		loadValueInstruction = ILOAD;
		break;
	case Type.SHORT:
		setterMethodName = "setShort";
		loadValueInstruction = ILOAD;
		break;
	case Type.INT:
		setterMethodName = "setInt";
		loadValueInstruction = ILOAD;
		break;
	case Type.FLOAT:
		setterMethodName = "setFloat";
		loadValueInstruction = FLOAD;
		break;
	case Type.LONG:
		setterMethodName = "setLong";
		loadValueInstruction = LLOAD;
		maxLocals++; // (LLOAD and DLOAD actually load two slots)
		break;
	case Type.DOUBLE:
		setterMethodName = "setDouble";
		loadValueInstruction = DLOAD;
		maxLocals++; // (LLOAD and DLOAD actually load two slots)
		break;
	default:
		setterMethodName = "set";
		loadValueInstruction = ALOAD;
		break;
	}
	MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, setterMethodName, "(Ljava/lang/Object;I" + typeNameInternal + ")V", null,
		null);
	mv.visitCode();
	mv.visitVarInsn(ILOAD, 2);

	if (!fields.isEmpty()) {
		maxStack--;
		Label[] labels = new Label[fields.size()];
		Label labelForInvalidTypes = new Label();
		boolean hasAnyBadTypeLabel = false;
		for (int i = 0, n = labels.length; i < n; i++) {
			if (Type.getType(fields.get(i).getType()).equals(primitiveType))
				labels[i] = new Label();
			else {
				labels[i] = labelForInvalidTypes;
				hasAnyBadTypeLabel = true;
			}
		}
		Label defaultLabel = new Label();
		mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

		for (int i = 0, n = labels.length; i < n; i++) {
			if (!labels[i].equals(labelForInvalidTypes)) {
				Field field = fields.get(i);
				mv.visitLabel(labels[i]);
				mv.visitFrame(F_SAME, 0, null, 0, null);
				mv.visitVarInsn(ALOAD, 1);
				mv.visitTypeInsn(CHECKCAST, classNameInternal);
				mv.visitVarInsn(loadValueInstruction, 3);
				mv.visitFieldInsn(PUTFIELD, field.getDeclaringClass().getName().replace('.', '/'), field.getName(),
					typeNameInternal);
				mv.visitInsn(RETURN);
			}
		}
		// Rest of fields: different type
		if (hasAnyBadTypeLabel) {
			mv.visitLabel(labelForInvalidTypes);
			mv.visitFrame(F_SAME, 0, null, 0, null);
			insertThrowExceptionForFieldType(mv, primitiveType.getClassName());
		}
		// Default: field not found
		mv.visitLabel(defaultLabel);
		mv.visitFrame(F_SAME, 0, null, 0, null);
	}
	mv = insertThrowExceptionForFieldNotFound(mv);
	mv.visitMaxs(maxStack, maxLocals);
	mv.visitEnd();
}
 
Example 16
Source File: BogusJumpInserter.java    From radon with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Generates a generic "escape" pattern to avoid inserting multiple copies of the same bytecode instructions.
 *
 * @param methodNode the {@link MethodNode} we are inserting into.
 * @return a {@link LabelNode} which "escapes" all other flow.
 */
private static LabelNode exitLabel(MethodNode methodNode) {
    LabelNode lb = new LabelNode();
    LabelNode escapeNode = new LabelNode();

    InsnList insns = methodNode.instructions;
    AbstractInsnNode target = insns.getFirst();

    insns.insertBefore(target, new JumpInsnNode(GOTO, escapeNode));
    insns.insertBefore(target, lb);

    switch (Type.getReturnType(methodNode.desc).getSort()) {
        case Type.VOID:
            insns.insertBefore(target, new InsnNode(RETURN));
            break;
        case Type.BOOLEAN:
            insns.insertBefore(target, ASMUtils.getNumberInsn(RandomUtils.getRandomInt(2)));
            insns.insertBefore(target, new InsnNode(IRETURN));
            break;
        case Type.CHAR:
            insns.insertBefore(target, ASMUtils.getNumberInsn(RandomUtils
                    .getRandomInt(Character.MAX_VALUE + 1)));
            insns.insertBefore(target, new InsnNode(IRETURN));
            break;
        case Type.BYTE:
            insns.insertBefore(target, ASMUtils.getNumberInsn(RandomUtils.getRandomInt(Byte.MAX_VALUE + 1)));
            insns.insertBefore(target, new InsnNode(IRETURN));
            break;
        case Type.SHORT:
            insns.insertBefore(target, ASMUtils.getNumberInsn(RandomUtils.getRandomInt(Short.MAX_VALUE + 1)));
            insns.insertBefore(target, new InsnNode(IRETURN));
            break;
        case Type.INT:
            insns.insertBefore(target, ASMUtils.getNumberInsn(RandomUtils.getRandomInt()));
            insns.insertBefore(target, new InsnNode(IRETURN));
            break;
        case Type.LONG:
            insns.insertBefore(target, ASMUtils.getNumberInsn(RandomUtils.getRandomLong()));
            insns.insertBefore(target, new InsnNode(LRETURN));
            break;
        case Type.FLOAT:
            insns.insertBefore(target, ASMUtils.getNumberInsn(RandomUtils.getRandomFloat()));
            insns.insertBefore(target, new InsnNode(FRETURN));
            break;
        case Type.DOUBLE:
            insns.insertBefore(target, ASMUtils.getNumberInsn(RandomUtils.getRandomDouble()));
            insns.insertBefore(target, new InsnNode(DRETURN));
            break;
        default:
            insns.insertBefore(target, new InsnNode(ACONST_NULL));
            insns.insertBefore(target, new InsnNode(ARETURN));
            break;
    }
    insns.insertBefore(target, escapeNode);

    return lb;
}
 
Example 17
Source File: LambdaDesugaring.java    From bazel with Apache License 2.0 4 votes vote down vote up
/**
 * Returns whether a given instruction can be used to push argument of {@code type} on stack.
 */
private /* static */ boolean isPushForType(AbstractInsnNode insn, Type type) {
  int opcode = insn.getOpcode();
  if (opcode == type.getOpcode(Opcodes.ILOAD)) {
    return true;
  }
  // b/62060793: AsyncAwait rewrites bytecode to convert java methods into state machine with
  // support of lambdas. Constant zero values are pushed on stack for all yet uninitialized
  // local variables. And SIPUSH instruction is used to advance an internal state of a state
  // machine.
  switch (type.getSort()) {
    case Type.BOOLEAN:
      return opcode == Opcodes.ICONST_0 || opcode == Opcodes.ICONST_1;

    case Type.BYTE:
    case Type.CHAR:
    case Type.SHORT:
    case Type.INT:
      return opcode == Opcodes.SIPUSH
          || opcode == Opcodes.ICONST_0
          || opcode == Opcodes.ICONST_1
          || opcode == Opcodes.ICONST_2
          || opcode == Opcodes.ICONST_3
          || opcode == Opcodes.ICONST_4
          || opcode == Opcodes.ICONST_5
          || opcode == Opcodes.ICONST_M1;

    case Type.LONG:
      return opcode == Opcodes.LCONST_0 || opcode == Opcodes.LCONST_1;

    case Type.FLOAT:
      return opcode == Opcodes.FCONST_0
          || opcode == Opcodes.FCONST_1
          || opcode == Opcodes.FCONST_2;

    case Type.DOUBLE:
      return opcode == Opcodes.DCONST_0 || opcode == Opcodes.DCONST_1;

    case Type.OBJECT:
    case Type.ARRAY:
      return opcode == Opcodes.ACONST_NULL;

    default:
      // Support for BIPUSH and LDC* opcodes is not implemented as there is no known use case.
      return false;
  }
}
 
Example 18
Source File: EmitUtils.java    From cglib with Apache License 2.0 4 votes vote down vote up
private static void append_string_helper(CodeEmitter e,
                                         Type type,
                                         ArrayDelimiters delims,
                                         CustomizerRegistry registry,
                                         ProcessArrayCallback callback) {
    Label skip = e.make_label();
    Label end = e.make_label();
    if (TypeUtils.isPrimitive(type)) {
        switch (type.getSort()) {
        case Type.INT:
        case Type.SHORT:
        case Type.BYTE:
            e.invoke_virtual(Constants.TYPE_STRING_BUFFER, APPEND_INT);
            break;
        case Type.DOUBLE:
            e.invoke_virtual(Constants.TYPE_STRING_BUFFER, APPEND_DOUBLE);
            break;
        case Type.FLOAT:
            e.invoke_virtual(Constants.TYPE_STRING_BUFFER, APPEND_FLOAT);
            break;
        case Type.LONG:
            e.invoke_virtual(Constants.TYPE_STRING_BUFFER, APPEND_LONG);
            break;
        case Type.BOOLEAN:
            e.invoke_virtual(Constants.TYPE_STRING_BUFFER, APPEND_BOOLEAN);
            break;
        case Type.CHAR:
            e.invoke_virtual(Constants.TYPE_STRING_BUFFER, APPEND_CHAR);
            break;
        }
    } else if (TypeUtils.isArray(type)) {
        e.dup();
        e.ifnull(skip);
        e.swap();
        if (delims != null && delims.before != null && !"".equals(delims.before)) {
            e.push(delims.before);
            e.invoke_virtual(Constants.TYPE_STRING_BUFFER, APPEND_STRING);
            e.swap();
        }
        EmitUtils.process_array(e, type, callback);
        shrinkStringBuffer(e, 2);
        if (delims != null && delims.after != null && !"".equals(delims.after)) {
            e.push(delims.after);
            e.invoke_virtual(Constants.TYPE_STRING_BUFFER, APPEND_STRING);
        }
    } else {
        e.dup();
        e.ifnull(skip);
        for (Customizer customizer : registry.get(Customizer.class)) {
            customizer.customize(e, type);
        }
        e.invoke_virtual(Constants.TYPE_OBJECT, TO_STRING);
        e.invoke_virtual(Constants.TYPE_STRING_BUFFER, APPEND_STRING);
    }
    e.goTo(end);
    e.mark(skip);
    e.pop();
    e.push("null");
    e.invoke_virtual(Constants.TYPE_STRING_BUFFER, APPEND_STRING);
    e.mark(end);
}
 
Example 19
Source File: TrashClasses.java    From radon with GNU General Public License v3.0 4 votes vote down vote up
private MethodNode methodGen() {
    String randDesc = descGen();
    MethodNode method = new MethodNode(ACC_STATIC + ACC_PRIVATE, randomString(), randDesc, null, null);
    int instructions = RandomUtils.getRandomInt(30) + 30;

    InsnList insns = new InsnList();

    for (int i = 0; i < instructions; ++i)
        insns.add(junkInstructions());

    Type returnType = Type.getReturnType(randDesc);
    switch (returnType.getSort()) {
        case Type.VOID:
            insns.add(new InsnNode(RETURN));
            break;
        case Type.BOOLEAN:
        case Type.CHAR:
        case Type.BYTE:
        case Type.SHORT:
        case Type.INT:
            if (RandomUtils.getRandomInt(10) % 2 == 1)
                insns.add(new InsnNode(ICONST_0));
            else
                insns.add(new InsnNode(ICONST_1));

            insns.add(new InsnNode(IRETURN));
            break;
        case Type.FLOAT:
            insns.add(ASMUtils.getNumberInsn(RandomUtils.getRandomFloat()));
            insns.add(new InsnNode(FRETURN));
            break;
        case Type.LONG:
            insns.add(ASMUtils.getNumberInsn(RandomUtils.getRandomLong()));
            insns.add(new InsnNode(LRETURN));
            break;
        case Type.DOUBLE:
            insns.add(ASMUtils.getNumberInsn(RandomUtils.getRandomDouble()));
            insns.add(new InsnNode(DRETURN));
            break;
        default:
            insns.add(new VarInsnNode(ALOAD, RandomUtils.getRandomInt(30)));
            insns.add(new InsnNode(ARETURN));
            break;
    }

    method.instructions = insns;
    return method;
}
 
Example 20
Source File: OperandStackStateGenerators.java    From coroutines with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Compute sizes required for the storage arrays that will contain the operand stack at this frame.
 * @param frame frame to compute for
 * @param offset the position within the operand stack to start calculating
 * @param length the number of stack items to include in calculation
 * @return size required by each storage array
 * @throws NullPointerException if any argument is {@code null}
 * @throws IllegalArgumentException if any numeric argument is negative, or if {@code offset + length} is larger than the size of the
 * operand stack
 */
public static StorageSizes computeSizes(Frame<BasicValue> frame, int offset, int length) {
    Validate.notNull(frame);
    Validate.isTrue(offset >= 0);
    Validate.isTrue(length >= 0);
    Validate.isTrue(offset < frame.getStackSize());
    Validate.isTrue(offset + length <= frame.getStackSize());
    
    // Count size required for each storage array
    int intsSize = 0;
    int longsSize = 0;
    int floatsSize = 0;
    int doublesSize = 0;
    int objectsSize = 0;
    for (int i = offset + length - 1; i >= offset; i--) {
        BasicValue basicValue = frame.getStack(i);
        Type type = basicValue.getType();
        
        // If type is 'Lnull;', this means that the slot has been assigned null and that "there has been no merge yet that would 'raise'
        // the type toward some class or interface type" (from ASM mailing list). We know this slot will always contain null at this
        // point in the code so we can avoid saving it. When we load it back up, we can simply push a null in to that slot, thereby
        // keeping the same 'Lnull;' type.
        if ("Lnull;".equals(type.getDescriptor())) {
            continue;
        }
        
        switch (type.getSort()) {
            case Type.BOOLEAN:
            case Type.BYTE:
            case Type.SHORT:
            case Type.CHAR:
            case Type.INT:
                intsSize++;
                break;
            case Type.FLOAT:
                floatsSize++;
                break;
            case Type.LONG:
                longsSize++;
                break;
            case Type.DOUBLE:
                doublesSize++;
                break;
            case Type.ARRAY:
            case Type.OBJECT:
                objectsSize++;
                break;
            case Type.METHOD:
            case Type.VOID:
            default:
                throw new IllegalStateException();
        }
    }
    
    return new StorageSizes(intsSize, longsSize, floatsSize, doublesSize, objectsSize);
}