/** * Transforms the given method type into a stack manipulation that loads its type onto the operand * stack. * * @param methodType The method type that should be loaded onto the operand stack. * @return A stack manipulation that loads the given method type. */ public static StackManipulation of(JavaInstance.MethodType methodType) { Type[] parameterType = new Type[methodType.getParameterTypes().size()]; int index = 0; for (TypeDescription typeDescription : methodType.getParameterTypes()) { parameterType[index++] = Type.getType(typeDescription.getDescriptor()); } return new MethodTypeConstant( Type.getMethodType( Type.getType(methodType.getReturnType().getDescriptor()), parameterType)); }
/** * Returns a fixed value from any intercepted method. The fixed value is stored in the constant * pool if this is possible. Java is capable of storing any primitive value, {@link String} values * and {@link Class} references in the constant pool. Since Java 7, {@code MethodHandle} as well * as {@code MethodType} references are also supported. Alternatively, the fixed value is stored * in a static field. * * <p>When a value is stored in the class's constant pool, its identity is lost. If an object's * identity is important, the {@link FixedValue#reference(Object)} method should be used instead. * * @param fixedValue The fixed value to return from the method. * @return An implementation for the given {@code fixedValue}. */ public static AssignerConfigurable value(Object fixedValue) { Class<?> type = fixedValue.getClass(); if (type == String.class) { return new ForPoolValue( new TextConstant((String) fixedValue), TypeDescription.STRING, Assigner.DEFAULT, Assigner.Typing.STATIC); } else if (type == Class.class) { return new ForPoolValue( ClassConstant.of(new TypeDescription.ForLoadedType((Class<?>) fixedValue)), TypeDescription.CLASS, Assigner.DEFAULT, Assigner.Typing.STATIC); } else if (type == Boolean.class) { return new ForPoolValue( IntegerConstant.forValue((Boolean) fixedValue), new TypeDescription.ForLoadedType(boolean.class), Assigner.DEFAULT, Assigner.Typing.STATIC); } else if (type == Byte.class) { return new ForPoolValue( IntegerConstant.forValue((Byte) fixedValue), new TypeDescription.ForLoadedType(byte.class), Assigner.DEFAULT, Assigner.Typing.STATIC); } else if (type == Short.class) { return new ForPoolValue( IntegerConstant.forValue((Short) fixedValue), new TypeDescription.ForLoadedType(short.class), Assigner.DEFAULT, Assigner.Typing.STATIC); } else if (type == Character.class) { return new ForPoolValue( IntegerConstant.forValue((Character) fixedValue), new TypeDescription.ForLoadedType(char.class), Assigner.DEFAULT, Assigner.Typing.STATIC); } else if (type == Integer.class) { return new ForPoolValue( IntegerConstant.forValue((Integer) fixedValue), new TypeDescription.ForLoadedType(int.class), Assigner.DEFAULT, Assigner.Typing.STATIC); } else if (type == Long.class) { return new ForPoolValue( LongConstant.forValue((Long) fixedValue), new TypeDescription.ForLoadedType(long.class), Assigner.DEFAULT, Assigner.Typing.STATIC); } else if (type == Float.class) { return new ForPoolValue( FloatConstant.forValue((Float) fixedValue), new TypeDescription.ForLoadedType(float.class), Assigner.DEFAULT, Assigner.Typing.STATIC); } else if (type == Double.class) { return new ForPoolValue( DoubleConstant.forValue((Double) fixedValue), new TypeDescription.ForLoadedType(double.class), Assigner.DEFAULT, Assigner.Typing.STATIC); } else if (JavaType.METHOD_HANDLE.getTypeStub().isAssignableFrom(type)) { return new ForPoolValue( MethodHandleConstant.of(JavaInstance.MethodHandle.of(fixedValue)), new TypeDescription.ForLoadedType(type), Assigner.DEFAULT, Assigner.Typing.STATIC); } else if (JavaType.METHOD_TYPE.getTypeStub().represents(type)) { return new ForPoolValue( MethodTypeConstant.of(JavaInstance.MethodType.of(fixedValue)), new TypeDescription.ForLoadedType(type), Assigner.DEFAULT, Assigner.Typing.STATIC); } else { return reference(fixedValue); } }