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

The following examples show how to use org.objectweb.asm.Type#CHAR . 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: 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 2
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 3
Source File: GeneratorAdapter.java    From JReFrameworker with MIT License 6 votes vote down vote up
private static 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;
    default:
      return type;
  }
}
 
Example 4
Source File: ASMUtils.java    From radon with GNU General Public License v3.0 6 votes vote down vote up
public static AbstractInsnNode getDefaultValue(Type type) {
    switch (type.getSort()) {
        case Type.BOOLEAN:
        case Type.CHAR:
        case Type.BYTE:
        case Type.SHORT:
        case Type.INT:
            return ASMUtils.getNumberInsn(0);
        case Type.FLOAT:
            return ASMUtils.getNumberInsn(0f);
        case Type.LONG:
            return ASMUtils.getNumberInsn(0L);
        case Type.DOUBLE:
            return ASMUtils.getNumberInsn(0d);
        case Type.OBJECT:
            return new InsnNode(Opcodes.ACONST_NULL);
        default:
            throw new AssertionError();
    }
}
 
Example 5
Source File: ASMUtils.java    From radon with GNU General Public License v3.0 6 votes vote down vote up
public static AbstractInsnNode getRandomValue(Type type) {
    switch (type.getSort()) {
        case Type.BOOLEAN:
            return ASMUtils.getNumberInsn(RandomUtils.getRandomInt(0, 2));
        case Type.CHAR:
            return ASMUtils.getNumberInsn(RandomUtils.getRandomInt(Character.MIN_VALUE, Character.MAX_VALUE));
        case Type.BYTE:
            return ASMUtils.getNumberInsn(RandomUtils.getRandomInt(Byte.MIN_VALUE, Byte.MAX_VALUE));
        case Type.SHORT:
            return ASMUtils.getNumberInsn(RandomUtils.getRandomInt(Short.MIN_VALUE, Short.MAX_VALUE));
        case Type.INT:
            return ASMUtils.getNumberInsn(RandomUtils.getRandomInt());
        case Type.FLOAT:
            return ASMUtils.getNumberInsn(RandomUtils.getRandomFloat());
        case Type.LONG:
            return ASMUtils.getNumberInsn(RandomUtils.getRandomLong());
        case Type.DOUBLE:
            return ASMUtils.getNumberInsn(RandomUtils.getRandomDouble());
        case Type.ARRAY:
        case Type.OBJECT:
            return new InsnNode(Opcodes.ACONST_NULL);
        default:
            throw new AssertionError();
    }
}
 
Example 6
Source File: CacheVariables.java    From coroutines with GNU Lesser General Public License v3.0 6 votes vote down vote up
public Variable getReturnCacheVar(Type type) {
    Validate.notNull(type);

    switch (type.getSort()) {
        case Type.BOOLEAN:
        case Type.BYTE:
        case Type.CHAR:
        case Type.SHORT:
        case Type.INT:
            return getIntReturnCacheVar();
        case Type.LONG:
            return getLongReturnCacheVar();
        case Type.FLOAT:
            return getFloatReturnCacheVar();
        case Type.DOUBLE:
            return getDoubleReturnCacheVar();
        case Type.ARRAY:
        case Type.OBJECT:
            return getObjectReturnCacheVar();
        case Type.VOID:
            return null;
        default:
            throw new IllegalArgumentException("Bad type");
    }
}
 
Example 7
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 8
Source File: GeneratorAdapter.java    From JByteMod-Beta with GNU General Public License v2.0 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;
  }
  if (unboxMethod == null) {
    checkCast(type);
  } else {
    checkCast(boxedType);
    invokeVirtual(boxedType, unboxMethod);
  }
}
 
Example 9
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 10
Source File: SimpleVerifier.java    From Concurnas with MIT License 5 votes vote down vote up
@Override
public BasicValue newValue(final Type type) {
  if (type == null) {
    return BasicValue.UNINITIALIZED_VALUE;
  }

  boolean isArray = type.getSort() == Type.ARRAY;
  if (isArray) {
    switch (type.getElementType().getSort()) {
      case Type.BOOLEAN:
      case Type.CHAR:
      case Type.BYTE:
      case Type.SHORT:
        return new BasicValue(type);
      default:
        break;
    }
  }

  BasicValue value = super.newValue(type);
  if (BasicValue.REFERENCE_VALUE.equals(value)) {
    if (isArray) {
      value = newValue(type.getElementType());
      StringBuilder descriptor = new StringBuilder();
      for (int i = 0; i < type.getDimensions(); ++i) {
        descriptor.append('[');
      }
      descriptor.append(value.getType().getDescriptor());
      value = new BasicValue(Type.getType(descriptor.toString()));
    } else {
      value = new BasicValue(type);
    }
  }
  return value;
}
 
Example 11
Source File: BasicInterpreter.java    From JReFrameworker with MIT License 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 AssertionError();
  }
}
 
Example 12
Source File: GeneratorAdapter.java    From JReFrameworker 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 13
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 14
Source File: LocalVariablesSorter.java    From JReFrameworker with MIT License 5 votes vote down vote up
/**
 * Constructs a new local variable of the given type.
 *
 * @param type the type of the local variable to be created.
 * @return the identifier of the newly created local variable.
 */
public int newLocal(final Type type) {
  Object localType;
  switch (type.getSort()) {
    case Type.BOOLEAN:
    case Type.CHAR:
    case Type.BYTE:
    case Type.SHORT:
    case Type.INT:
      localType = Opcodes.INTEGER;
      break;
    case Type.FLOAT:
      localType = Opcodes.FLOAT;
      break;
    case Type.LONG:
      localType = Opcodes.LONG;
      break;
    case Type.DOUBLE:
      localType = Opcodes.DOUBLE;
      break;
    case Type.ARRAY:
      localType = type.getDescriptor();
      break;
    case Type.OBJECT:
      localType = type.getInternalName();
      break;
    default:
      throw new AssertionError();
  }
  int local = newLocalMapping(type);
  setLocalType(local, type);
  setFrameLocal(local, localType);
  return local;
}
 
Example 15
Source File: JacksonSerializePrimitive.java    From yql-plus with Apache License 2.0 5 votes vote down vote up
@Override
public void generate(CodeEmitter code) {
    jsonGenerator.generate(code);
    source.generate(code);
    MethodVisitor mv = code.getMethodVisitor();
    String desc = Type.getMethodDescriptor(Type.VOID_TYPE, source.getType().getJVMType());
    String name;
    switch (source.getType().getJVMType().getSort()) {
        case Type.BOOLEAN:
            name = "writeBoolean";
            break;
        case Type.SHORT:
        case Type.INT:
        case Type.FLOAT:
        case Type.LONG:
        case Type.DOUBLE:
            name = "writeNumber";
            break;
        case Type.BYTE:
        case Type.CHAR:
            name = "writeObject";
            desc = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(Object.class));
            code.box(source.getType());
            break;
        default:
            throw new UnsupportedOperationException("Unknown primitive type: " + source.getType().getJVMType());
    }
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(JsonGenerator.class), name, desc, false);
}
 
Example 16
Source File: InputCoverageFactory.java    From botsing with Apache License 2.0 4 votes vote down vote up
protected void detectGoals(String className, String methodName, Class<?>[] argumentClasses ,Type[] argumentTypes, List<InputCoverageTestFitness> goals){
    for (int i=0; i<argumentTypes.length;i++){
        Type argType = argumentTypes[i];

        int typeSort = argType.getSort();
        if(typeSort == Type.OBJECT) {
            Class<?> typeClass = argumentClasses[i];
            if(ClassUtils.isPrimitiveWrapper(typeClass)) {
                typeSort = Type.getType(ClassUtils.wrapperToPrimitive(typeClass)).getSort();
                goals.add(createGoal(className, methodName, i, argType, REF_NULL));
            }
        }

        switch (typeSort) {
            case Type.BOOLEAN:
                goals.add(createGoal(className, methodName, i, argType, BOOL_TRUE));
                goals.add(createGoal(className, methodName, i, argType, BOOL_FALSE));
                break;
            case Type.CHAR:
                goals.add(createGoal(className, methodName, i, argType, CHAR_ALPHA));
                goals.add(createGoal(className, methodName, i, argType, CHAR_DIGIT));
                goals.add(createGoal(className, methodName, i, argType, CHAR_OTHER));
                break;
            case Type.BYTE:
            case Type.SHORT:
            case Type.INT:
            case Type.FLOAT:
            case Type.LONG:
            case Type.DOUBLE:
                goals.add(createGoal(className, methodName, i, argType, NUM_NEGATIVE));
                goals.add(createGoal(className, methodName, i, argType, NUM_ZERO));
                goals.add(createGoal(className, methodName, i, argType, NUM_POSITIVE));
                break;
            case Type.ARRAY:
                goals.add(createGoal(className, methodName, i, argType, REF_NULL));
                goals.add(createGoal(className, methodName, i, argType, ARRAY_EMPTY));
                goals.add(createGoal(className, methodName, i, argType, ARRAY_NONEMPTY));
                break;
            case Type.OBJECT:
                goals.add(createGoal(className, methodName, i, argType, REF_NULL));
                if (argType.getClassName().equals("java.lang.String")) {
                    goals.add(createGoal(className, methodName, i, argType, STRING_EMPTY));
                    goals.add(createGoal(className, methodName, i, argType, STRING_NONEMPTY));
                } else if(List.class.isAssignableFrom(argumentClasses[i])) {
                    goals.add(createGoal(className, methodName, i, argType, LIST_EMPTY));
                    goals.add(createGoal(className, methodName, i, argType, LIST_NONEMPTY));

                } else if(Set.class.isAssignableFrom(argumentClasses[i])) {
                    goals.add(createGoal(className, methodName, i, argType, SET_EMPTY));
                    goals.add(createGoal(className, methodName, i, argType, SET_NONEMPTY));

                } else if(Map.class.isAssignableFrom(argumentClasses[i])) {
                    goals.add(createGoal(className, methodName, i, argType, MAP_EMPTY));
                    goals.add(createGoal(className, methodName, i, argType, MAP_NONEMPTY));
                    // TODO: Collection.class?
                } else {
                    boolean observerGoalsAdded = false;
                    Class<?> paramClazz = argumentClasses[i];
                    for(Inspector inspector : InspectorManager.getInstance().getInspectors(paramClazz)) {
                        String insp = inspector.getMethodCall() + Type.getMethodDescriptor(inspector.getMethod());
                        Type t = Type.getReturnType(inspector.getMethod());
                        if (t.getSort() == Type.BOOLEAN) {
                            goals.add(createGoal(className, methodName, i, argType, REF_NONNULL + ":" + argType.getClassName() + ":" + insp + ":" + BOOL_TRUE));
                            goals.add(createGoal(className, methodName, i, argType, REF_NONNULL + ":" + argType.getClassName() + ":" + insp + ":" + BOOL_FALSE));
                            observerGoalsAdded = true;
                        } else if (Arrays.asList(new Integer[]{Type.BYTE, Type.SHORT, Type.INT, Type.FLOAT, Type.LONG, Type.DOUBLE}).contains(t.getSort())) {
                            goals.add(createGoal(className, methodName, i, argType, REF_NONNULL + ":" + argType.getClassName() + ":" + insp + ":" + NUM_NEGATIVE));
                            goals.add(createGoal(className, methodName, i, argType, REF_NONNULL + ":" + argType.getClassName() + ":" + insp + ":" + NUM_ZERO));
                            goals.add(createGoal(className, methodName, i, argType, REF_NONNULL + ":" + argType.getClassName() + ":" + insp + ":" + NUM_POSITIVE));
                            observerGoalsAdded = true;
                        }
                    }
                    if (!observerGoalsAdded){
                        goals.add(createGoal(className, methodName, i, argType, REF_NONNULL));                                goals.add(createGoal(className, methodName, i, argType, REF_NONNULL));
                    }
                }
                break;
            default:
                break;
        }
    }
}
 
Example 17
Source File: AsmClassAccess.java    From jadira with Apache License 2.0 4 votes vote down vote up
private static void enhanceForGetValueObject(ClassVisitor cw, String accessClassNm, String clazzNm, Field[] fields) {

		MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "getValue", "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;", null, null);

		mv.visitCode();
		mv.visitVarInsn(ALOAD, 0);
		mv.visitFieldInsn(GETFIELD, accessClassNm, "fieldNames", "[Ljava/lang/String;");
		mv.visitVarInsn(ALOAD, 2);
		mv.visitMethodInsn(INVOKESTATIC, "java/util/Arrays", "binarySearch", "([Ljava/lang/Object;Ljava/lang/Object;)I");
		mv.visitVarInsn(ISTORE, 3);
		mv.visitVarInsn(ILOAD, 3);

		final int maxStack;

		if (fields.length > 0) {
			maxStack = 5;
			Label[] labels = constructLabels(fields);

			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[i];
				mv.visitLabel(labels[i]);
				mv.visitFrame(F_SAME, 0, null, 0, null);
				mv.visitVarInsn(ALOAD, 1);
				mv.visitTypeInsn(CHECKCAST, clazzNm);
				mv.visitFieldInsn(GETFIELD, clazzNm, field.getName(), Type.getDescriptor(field.getType()));

				Type fieldType = Type.getType(field.getType());
				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;
				}

				mv.visitInsn(ARETURN);
			}

			mv.visitLabel(defaultLabel);
			mv.visitFrame(F_SAME, 0, null, 0, null);
		} else {
			maxStack = 6;
		}
		enhanceForThrowingException(mv, IllegalArgumentException.class, "Field was not found", "Ljava/lang/Object;", ALOAD, 2);
		mv.visitMaxs(maxStack, 4);
		mv.visitEnd();
	}
 
Example 18
Source File: AsmClassAccess.java    From jadira with Apache License 2.0 4 votes vote down vote up
private static void enhanceForGetValuePrimitive(ClassVisitor cw, String accessClassNm, String clazzNm, Field[] fields, Type type) {

		String methodName;
		final String typeNm = type.getDescriptor();
		final int instruction;

		switch (type.getSort()) {
		case Type.BOOLEAN:
			methodName = "getBooleanValue";
			instruction = IRETURN;
			break;
		case Type.BYTE:
			methodName = "getByteValue";
			instruction = IRETURN;
			break;
		case Type.CHAR:
			methodName = "getCharValue";
			instruction = IRETURN;
			break;
		case Type.SHORT:
			methodName = "getShortValue";
			instruction = IRETURN;
			break;
		case Type.INT:
			methodName = "getIntValue";
			instruction = IRETURN;
			break;
		case Type.FLOAT:
			methodName = "getFloatValue";
			instruction = FRETURN;
			break;
		case Type.LONG:
			methodName = "getLongValue";
			instruction = LRETURN;
			break;
		case Type.DOUBLE:
			methodName = "getDoubleValue";
			instruction = DRETURN;
			break;
		default:
			methodName = "getValue";
			instruction = ARETURN;
			break;
		}

		MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, methodName, "(Ljava/lang/Object;Ljava/lang/String;)" + typeNm, null, null);

		mv.visitCode();
		mv.visitVarInsn(ALOAD, 0);
		mv.visitFieldInsn(GETFIELD, accessClassNm, "fieldNames", "[Ljava/lang/String;");
		mv.visitVarInsn(ALOAD, 2);
		mv.visitMethodInsn(INVOKESTATIC, "java/util/Arrays", "binarySearch", "([Ljava/lang/Object;Ljava/lang/Object;)I");
		mv.visitVarInsn(ISTORE, 3);
		mv.visitVarInsn(ILOAD, 3);

		final int maxStack;

		if (fields.length > 0) {
			maxStack = 5;
			Label[] labels = constructLabels(fields);

			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[i];
				mv.visitLabel(labels[i]);
				mv.visitFrame(F_SAME, 0, null, 0, null);
				mv.visitVarInsn(ALOAD, 1);
				mv.visitTypeInsn(CHECKCAST, clazzNm);
				mv.visitFieldInsn(GETFIELD, clazzNm, field.getName(), typeNm);
				mv.visitInsn(instruction);
			}

			mv.visitLabel(defaultLabel);
			mv.visitFrame(F_SAME, 0, null, 0, null);
		} else {
			maxStack = 6;
		}
		enhanceForThrowingException(mv, IllegalArgumentException.class, "Field was not found", "Ljava/lang/Object;", ALOAD, 2);
		mv.visitMaxs(maxStack, 4);
		mv.visitEnd();
	}
 
Example 19
Source File: AsmClassAccess.java    From jadira with Apache License 2.0 4 votes vote down vote up
private static void enhanceForPutValueObject(ClassVisitor cw, String accessClassNm, String clazzNm, Field[] fields) {

		int maxStack = 6;
		MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "putValue", "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V", null, null);

		mv.visitCode();

		mv.visitVarInsn(ALOAD, 0);
		mv.visitFieldInsn(GETFIELD, accessClassNm, "fieldNames", "[Ljava/lang/String;");
		mv.visitVarInsn(ALOAD, 2);
		mv.visitMethodInsn(INVOKESTATIC, "java/util/Arrays", "binarySearch", "([Ljava/lang/Object;Ljava/lang/Object;)I");
		mv.visitVarInsn(ISTORE, 4);
		mv.visitVarInsn(ILOAD, 4);

		if (fields.length > 0) {
			maxStack = 5;
			Label[] labels = constructLabels(fields);

			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[i];

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

				mv.visitVarInsn(ALOAD, 3);

				Type fieldType = Type.getType(field.getType());
				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, clazzNm, field.getName(), fieldType.getDescriptor());
				mv.visitInsn(RETURN);
			}

			mv.visitLabel(defaultLabel);
			mv.visitFrame(F_SAME, 0, null, 0, null);
		} else {
			maxStack = 6;
		}
		enhanceForThrowingException(mv, IllegalArgumentException.class, "Field was not found", "Ljava/lang/Object;", ALOAD, 2);
		mv.visitMaxs(maxStack, 5);
		mv.visitEnd();
	}
 
Example 20
Source File: JSondeMethodTransformerOld.java    From jsonde with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private int generateArgumentsArray(int argIndex) {

        Type[] argumentTypes = Type.getArgumentTypes(methodDesc);

        super.visitIntInsn(BIPUSH, argumentTypes.length);
        super.visitTypeInsn(ANEWARRAY, "java/lang/Object");

        for (int i = 0; i < argumentTypes.length; i++) {
            Type argumentType = argumentTypes[i];

            super.visitInsn(DUP);
            super.visitIntInsn(BIPUSH, i);
            super.visitVarInsn(argumentType.getOpcode(ILOAD), argIndex);

            // boxing start

            switch (argumentType.getSort()) {
                case Type.BYTE:
                    super.visitMethodInsn(
                            INVOKESTATIC,
                            "java/lang/Byte",
                            "valueOf",
                            "(B)Ljava/lang/Byte;"
                    );
                    break;
                case Type.BOOLEAN:
                    super.visitMethodInsn(
                            INVOKESTATIC,
                            "java/lang/Boolean",
                            "valueOf",
                            "(Z)Ljava/lang/Boolean;"
                    );
                    break;
                case Type.SHORT:
                    super.visitMethodInsn(
                            INVOKESTATIC,
                            "java/lang/Short",
                            "valueOf",
                            "(S)Ljava/lang/Short;"
                    );
                    break;
                case Type.CHAR:
                    super.visitMethodInsn(
                            INVOKESTATIC,
                            "java/lang/Character",
                            "valueOf",
                            "(C)Ljava/lang/Character;"
                    );
                    break;
                case Type.INT:
                    super.visitMethodInsn(
                            INVOKESTATIC,
                            "java/lang/Integer",
                            "valueOf",
                            "(I)Ljava/lang/Integer;"
                    );
                    break;
                case Type.FLOAT:
                    super.visitMethodInsn(
                            INVOKESTATIC,
                            "java/lang/Float",
                            "valueOf",
                            "(F)Ljava/lang/Float;"
                    );
                    break;
                case Type.LONG:
                    super.visitMethodInsn(
                            INVOKESTATIC,
                            "java/lang/Long",
                            "valueOf",
                            "(J)Ljava/lang/Long;"
                    );
                    break;
                case Type.DOUBLE:
                    super.visitMethodInsn(
                            INVOKESTATIC,
                            "java/lang/Double",
                            "valueOf",
                            "(D)Ljava/lang/Double;"
                    );
                    break;
            }

            // boxing end

            super.visitInsn(AASTORE);

            argIndex += argumentType.getSize();
        }

        super.visitVarInsn(ASTORE, argIndex);

        return argIndex;
    }