diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/implementation/FieldAccessor.java b/byte-buddy-dep/src/main/java/net/bytebuddy/implementation/FieldAccessor.java index e870f2a3e7a..ab78984abc4 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/implementation/FieldAccessor.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/implementation/FieldAccessor.java @@ -6,6 +6,7 @@ import net.bytebuddy.description.modifier.ModifierContributor; import net.bytebuddy.description.type.TypeDefinition; import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.description.type.generic.GenericTypeDescription; import net.bytebuddy.dynamic.TargetType; import net.bytebuddy.dynamic.scaffold.InstrumentedType; import net.bytebuddy.implementation.bytecode.ByteCodeAppender; @@ -18,6 +19,8 @@ import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; +import java.lang.reflect.Type; + import static net.bytebuddy.matcher.ElementMatchers.*; import static net.bytebuddy.utility.ByteBuddyCommons.*; @@ -526,16 +529,16 @@ public interface FieldDefinable extends OwnerTypeLocatable { * @param modifier The modifiers for the field. * @return A field accessor that defines a field of the given type. */ - AssignerConfigurable defineAs(Class type, ModifierContributor.ForField... modifier); + AssignerConfigurable defineAs(Type type, ModifierContributor.ForField... modifier); /** * Defines a field with the given name in the instrumented type. * - * @param typeDescription The type of the field. - * @param modifier The modifiers for the field. + * @param typeDefinition The type of the field. + * @param modifier The modifiers for the field. * @return A field accessor that defines a field of the given type. */ - AssignerConfigurable defineAs(TypeDescription typeDescription, ModifierContributor.ForField... modifier); + AssignerConfigurable defineAs(TypeDefinition typeDefinition, ModifierContributor.ForField... modifier); } /** @@ -700,16 +703,16 @@ private ForNamedField(Assigner assigner, } @Override - public AssignerConfigurable defineAs(Class type, ModifierContributor.ForField... modifier) { - return defineAs(new TypeDescription.ForLoadedType(nonNull(type)), modifier); + public AssignerConfigurable defineAs(Type type, ModifierContributor.ForField... modifier) { + return defineAs(TypeDefinition.Sort.describe(type), modifier); } @Override - public AssignerConfigurable defineAs(TypeDescription typeDescription, ModifierContributor.ForField... modifier) { + public AssignerConfigurable defineAs(TypeDefinition typeDefinition, ModifierContributor.ForField... modifier) { return new ForNamedField(assigner, typing, fieldName, - PreparationHandler.FieldDefiner.of(fieldName, isActualType(typeDescription), nonNull(modifier)), + PreparationHandler.FieldDefiner.of(fieldName, isActualType(typeDefinition).asGenericType(), nonNull(modifier)), FieldLocator.ForInstrumentedType.INSTANCE); } @@ -836,7 +839,7 @@ class FieldDefiner implements PreparationHandler { /** * The type of the field that is to be defined. */ - private final TypeDescription typeDescription; + private final GenericTypeDescription typeDescription; /** * The modifier of the field that is to be defined. @@ -850,7 +853,7 @@ class FieldDefiner implements PreparationHandler { * @param typeDescription The type of the field that is to be defined. * @param modifiers The modifiers of the field that is to be defined. */ - protected FieldDefiner(String name, TypeDescription typeDescription, int modifiers) { + protected FieldDefiner(String name, GenericTypeDescription typeDescription, int modifiers) { this.name = name; this.typeDescription = typeDescription; this.modifiers = modifiers; @@ -864,7 +867,7 @@ protected FieldDefiner(String name, TypeDescription typeDescription, int modifie * @param contributor The modifiers of the field that is to be defined. * @return A corresponding preparation handler. */ - public static PreparationHandler of(String name, TypeDescription typeDescription, ModifierContributor.ForField... contributor) { + public static PreparationHandler of(String name, GenericTypeDescription typeDescription, ModifierContributor.ForField... contributor) { return new FieldDefiner(name, typeDescription, resolveModifierContributors(ByteBuddyCommons.FIELD_MODIFIER_MASK, contributor)); } diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/implementation/FixedValue.java b/byte-buddy-dep/src/main/java/net/bytebuddy/implementation/FixedValue.java index ef588c9e758..789d00a5efe 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/implementation/FixedValue.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/implementation/FixedValue.java @@ -3,6 +3,7 @@ import net.bytebuddy.description.field.FieldDescription; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.description.type.generic.GenericTypeDescription; import net.bytebuddy.dynamic.scaffold.InstrumentedType; import net.bytebuddy.implementation.bytecode.ByteCodeAppender; import net.bytebuddy.implementation.bytecode.StackManipulation; @@ -234,9 +235,9 @@ public static AssignerConfigurable value(JavaInstance fixedValue) { protected ByteCodeAppender.Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod, - TypeDescription fixedValueType, + GenericTypeDescription fixedValueType, StackManipulation valueLoadingInstruction) { - StackManipulation assignment = assigner.assign(fixedValueType, instrumentedMethod.getReturnType().asErasure(), typing); + StackManipulation assignment = assigner.assign(fixedValueType.asErasure(), instrumentedMethod.getReturnType().asErasure(), typing); if (!assignment.isValid()) { throw new IllegalArgumentException("Cannot return value of type " + fixedValueType + " for " + instrumentedMethod); } @@ -326,7 +327,7 @@ public ByteCodeAppender appender(Target implementationTarget) { @Override public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) { - return apply(methodVisitor, implementationContext, instrumentedMethod, loadedType, valueLoadInstruction); + return apply(methodVisitor, implementationContext, instrumentedMethod, loadedType.asGenericType(), valueLoadInstruction); } @Override @@ -379,7 +380,7 @@ protected static class ForStaticField extends FixedValue implements AssignerConf /** * The type if the field for storing the fixed value. */ - private final TypeDescription fieldType; + private final GenericTypeDescription fieldType; /** * Creates a new static field fixed value implementation with a random name for the field containing the fixed @@ -407,7 +408,7 @@ protected ForStaticField(String fieldName, Object fixedValue, Assigner assigner, super(assigner, typing); this.fieldName = fieldName; this.fixedValue = fixedValue; - fieldType = new TypeDescription.ForLoadedType(fixedValue.getClass()); + fieldType = new GenericTypeDescription.ForNonGenericType.OfLoadedType(fixedValue.getClass()); } @Override diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/implementation/Forwarding.java b/byte-buddy-dep/src/main/java/net/bytebuddy/implementation/Forwarding.java index d980b60fb9f..82e12bd2510 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/implementation/Forwarding.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/implementation/Forwarding.java @@ -2,7 +2,9 @@ import net.bytebuddy.description.field.FieldDescription; import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.description.type.TypeDefinition; import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.description.type.generic.GenericTypeDescription; import net.bytebuddy.dynamic.scaffold.InstrumentedType; import net.bytebuddy.implementation.bytecode.ByteCodeAppender; import net.bytebuddy.implementation.bytecode.StackManipulation; @@ -13,6 +15,8 @@ import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; +import java.lang.reflect.Type; + import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.utility.ByteBuddyCommons.*; @@ -38,7 +42,7 @@ public class Forwarding implements Implementation { /** * The type of the field. */ - protected final TypeDescription fieldType; + protected final GenericTypeDescription fieldType; /** * A handler for preparing the instrumented type and the field invocation operation. @@ -52,7 +56,7 @@ public class Forwarding implements Implementation { * @param fieldType The type of the field. * @param preparationHandler A handler for preparing the instrumented type and the field invocation operation. */ - protected Forwarding(String fieldName, TypeDescription fieldType, PreparationHandler preparationHandler) { + protected Forwarding(String fieldName, GenericTypeDescription fieldType, PreparationHandler preparationHandler) { this.fieldName = fieldName; this.fieldType = fieldType; this.preparationHandler = preparationHandler; @@ -79,7 +83,7 @@ public static Implementation to(Object delegate) { */ public static Implementation to(Object delegate, String fieldName) { return new Forwarding(isValidIdentifier(fieldName), - new TypeDescription.ForLoadedType(nonNull(delegate).getClass()), + new GenericTypeDescription.ForNonGenericType.OfLoadedType(nonNull(delegate).getClass()), new PreparationHandler.ForStaticInstance(delegate)); } @@ -91,8 +95,8 @@ public static Implementation to(Object delegate, String fieldName) { * @param fieldType The type of the field and thus the type of which the delegate is assumed to be of. * @return A corresponding implementation. */ - public static Implementation toStaticField(String fieldName, Class fieldType) { - return toStaticField(fieldName, new TypeDescription.ForLoadedType(nonNull(fieldType))); + public static Implementation toStaticField(String fieldName, Type fieldType) { + return toStaticField(fieldName, TypeDefinition.Sort.describe(nonNull(fieldType))); } /** @@ -103,9 +107,9 @@ public static Implementation toStaticField(String fieldName, Class fieldType) * @param fieldType The type of the field and thus the type of which the delegate is assumed to be of. * @return A corresponding implementation. */ - public static Implementation toStaticField(String fieldName, TypeDescription fieldType) { + public static Implementation toStaticField(String fieldName, TypeDefinition fieldType) { return new Forwarding(isValidIdentifier(fieldName), - isActualType(fieldType), + isActualType(fieldType).asGenericType(), PreparationHandler.ForStaticField.INSTANCE); } @@ -117,8 +121,8 @@ public static Implementation toStaticField(String fieldName, TypeDescription fie * @param fieldType The type of the field and thus the type of which the delegate is assumed to be of. * @return A corresponding implementation. */ - public static Implementation toInstanceField(String fieldName, Class fieldType) { - return toInstanceField(fieldName, new TypeDescription.ForLoadedType(nonNull(fieldType))); + public static Implementation toInstanceField(String fieldName, Type fieldType) { + return toInstanceField(fieldName, TypeDefinition.Sort.describe(nonNull(fieldType))); } /** @@ -129,9 +133,9 @@ public static Implementation toInstanceField(String fieldName, Class fieldTyp * @param fieldType The type of the field and thus the type of which the delegate is assumed to be of. * @return A corresponding implementation. */ - public static Implementation toInstanceField(String fieldName, TypeDescription fieldType) { + public static Implementation toInstanceField(String fieldName, TypeDefinition fieldType) { return new Forwarding(isValidIdentifier(fieldName), - nonNull(fieldType), + isActualType(fieldType).asGenericType(), PreparationHandler.ForInstanceField.INSTANCE); } @@ -196,7 +200,7 @@ protected interface PreparationHandler { * @param fieldType The type of the field. * @return The prepared instrumented type. */ - InstrumentedType prepare(InstrumentedType instrumentedType, String fieldName, TypeDescription fieldType); + InstrumentedType prepare(InstrumentedType instrumentedType, String fieldName, GenericTypeDescription fieldType); /** * Creates a stack manipulation for loading the field owner onto the operand stack. @@ -216,7 +220,7 @@ enum ForInstanceField implements PreparationHandler { INSTANCE; @Override - public InstrumentedType prepare(InstrumentedType instrumentedType, String fieldName, TypeDescription fieldType) { + public InstrumentedType prepare(InstrumentedType instrumentedType, String fieldName, GenericTypeDescription fieldType) { if (instrumentedType.isInterface()) { throw new IllegalStateException("Cannot define instance field '" + fieldName + "' for " + instrumentedType); } @@ -245,10 +249,8 @@ enum ForStaticField implements PreparationHandler { INSTANCE; @Override - public InstrumentedType prepare(InstrumentedType instrumentedType, String fieldName, TypeDescription fieldType) { - return instrumentedType.withField(new FieldDescription.Token(fieldName, - Opcodes.ACC_SYNTHETIC | Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, - fieldType)); + public InstrumentedType prepare(InstrumentedType instrumentedType, String fieldName, GenericTypeDescription fieldType) { + return instrumentedType.withField(new FieldDescription.Token(fieldName, Opcodes.ACC_SYNTHETIC | Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, fieldType)); } @Override @@ -282,7 +284,7 @@ public ForStaticInstance(Object target) { } @Override - public InstrumentedType prepare(InstrumentedType instrumentedType, String fieldName, TypeDescription fieldType) { + public InstrumentedType prepare(InstrumentedType instrumentedType, String fieldName, GenericTypeDescription fieldType) { return instrumentedType .withField(new FieldDescription.Token(fieldName, Opcodes.ACC_SYNTHETIC | Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, fieldType)) .withInitializer(new LoadedTypeInitializer.ForStaticField(fieldName, target)); @@ -333,7 +335,7 @@ private Appender(StackManipulation delegateLoadingInstruction) { @Override public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) { - if (!instrumentedMethod.isInvokableOn(fieldType)) { + if (!instrumentedMethod.isInvokableOn(fieldType.asErasure())) { throw new IllegalArgumentException("Cannot forward " + instrumentedMethod + " to " + fieldType); } else if (instrumentedMethod.isStatic()) { throw new IllegalArgumentException("Cannot forward the static method " + instrumentedMethod); @@ -341,7 +343,7 @@ public Size apply(MethodVisitor methodVisitor, Context implementationContext, Me StackManipulation.Size stackSize = new StackManipulation.Compound( delegateLoadingInstruction, MethodVariableAccess.allArgumentsOf(instrumentedMethod), - MethodInvocation.invoke(instrumentedMethod).virtual(fieldType), + MethodInvocation.invoke(instrumentedMethod).virtual(fieldType.asErasure()), MethodReturn.returning(instrumentedMethod.getReturnType().asErasure()) ).apply(methodVisitor, implementationContext); return new Size(stackSize.getMaximalSize(), instrumentedMethod.getStackSize());