Skip to content

Commit

Permalink
Merge pull request #962 from ljnelson/issue-961
Browse files Browse the repository at this point in the history
Fixes #961 by handing JavaConstant case in FixedValue.value() method
  • Loading branch information
raphw authored Oct 31, 2020
2 parents 7a693d2 + e0a690e commit 7138b6d
Showing 1 changed file with 39 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ protected FixedValue(Assigner assigner, Assigner.Typing typing) {
/**
* <p>
* 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.
* Specifically, an argument that is a {@link JavaConstant}, {@link TypeDescription}, primitive, {@link String} or
* {@link Class} value is stored 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>
* <p>
* When a value is stored in the class's constant pool, its identity is lost. If an object's identity is important, the
Expand All @@ -85,35 +85,45 @@ protected FixedValue(Assigner assigner, Assigner.Typing typing) {
*
* @param fixedValue The fixed value to return from the method.
* @return An implementation for the given {@code value}.
*
* @see #value(JavaConstant)
* @see #value(TypeDescription)
* @see #nullValue()
*/
public static AssignerConfigurable value(Object fixedValue) {
Class<?> type = fixedValue.getClass();
if (type == String.class) {
return new ForPoolValue(new TextConstant((String) fixedValue), TypeDescription.STRING);
} else if (type == Class.class) {
return new ForPoolValue(ClassConstant.of(TypeDescription.ForLoadedType.of((Class<?>) fixedValue)), TypeDescription.CLASS);
} else if (type == Boolean.class) {
return new ForPoolValue(IntegerConstant.forValue((Boolean) fixedValue), boolean.class);
} else if (type == Byte.class) {
return new ForPoolValue(IntegerConstant.forValue((Byte) fixedValue), byte.class);
} else if (type == Short.class) {
return new ForPoolValue(IntegerConstant.forValue((Short) fixedValue), short.class);
} else if (type == Character.class) {
return new ForPoolValue(IntegerConstant.forValue((Character) fixedValue), char.class);
} else if (type == Integer.class) {
return new ForPoolValue(IntegerConstant.forValue((Integer) fixedValue), int.class);
} else if (type == Long.class) {
return new ForPoolValue(LongConstant.forValue((Long) fixedValue), long.class);
} else if (type == Float.class) {
return new ForPoolValue(FloatConstant.forValue((Float) fixedValue), float.class);
} else if (type == Double.class) {
return new ForPoolValue(DoubleConstant.forValue((Double) fixedValue), double.class);
} else if (JavaType.METHOD_HANDLE.getTypeStub().isAssignableFrom(type)) {
return new ForPoolValue(new JavaConstantValue(JavaConstant.MethodHandle.ofLoaded(fixedValue)), type);
} else if (JavaType.METHOD_TYPE.getTypeStub().represents(type)) {
return new ForPoolValue(new JavaConstantValue(JavaConstant.MethodType.ofLoaded(fixedValue)), type);
if (fixedValue instanceof JavaConstant) {
return value((JavaConstant) fixedValue);
} else if (fixedValue instanceof TypeDescription) {
return value((TypeDescription) fixedValue);
} else {
return reference(fixedValue);
Class<?> type = fixedValue.getClass();
if (type == String.class) {
return new ForPoolValue(new TextConstant((String) fixedValue), TypeDescription.STRING);
} else if (type == Class.class) {
return new ForPoolValue(ClassConstant.of(TypeDescription.ForLoadedType.of((Class<?>) fixedValue)), TypeDescription.CLASS);
} else if (type == Boolean.class) {
return new ForPoolValue(IntegerConstant.forValue((Boolean) fixedValue), boolean.class);
} else if (type == Byte.class) {
return new ForPoolValue(IntegerConstant.forValue((Byte) fixedValue), byte.class);
} else if (type == Short.class) {
return new ForPoolValue(IntegerConstant.forValue((Short) fixedValue), short.class);
} else if (type == Character.class) {
return new ForPoolValue(IntegerConstant.forValue((Character) fixedValue), char.class);
} else if (type == Integer.class) {
return new ForPoolValue(IntegerConstant.forValue((Integer) fixedValue), int.class);
} else if (type == Long.class) {
return new ForPoolValue(LongConstant.forValue((Long) fixedValue), long.class);
} else if (type == Float.class) {
return new ForPoolValue(FloatConstant.forValue((Float) fixedValue), float.class);
} else if (type == Double.class) {
return new ForPoolValue(DoubleConstant.forValue((Double) fixedValue), double.class);
} else if (JavaType.METHOD_HANDLE.getTypeStub().isAssignableFrom(type)) {
return new ForPoolValue(new JavaConstantValue(JavaConstant.MethodHandle.ofLoaded(fixedValue)), type);
} else if (JavaType.METHOD_TYPE.getTypeStub().represents(type)) {
return new ForPoolValue(new JavaConstantValue(JavaConstant.MethodType.ofLoaded(fixedValue)), type);
} else {
return reference(fixedValue);
}
}
}

Expand Down

0 comments on commit 7138b6d

Please sign in to comment.