Skip to content

Commit

Permalink
Refactored implementations to new type system.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Dec 20, 2015
1 parent fab3e4a commit 8fb9282
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 36 deletions.
Expand Up @@ -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;
Expand All @@ -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.*;

Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -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));
}

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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;
Expand All @@ -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.*;

Expand All @@ -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.
Expand All @@ -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;
Expand All @@ -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));
}

Expand All @@ -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)));
}

/**
Expand All @@ -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);
}

Expand All @@ -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)));
}

/**
Expand All @@ -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);
}

Expand Down Expand Up @@ -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.
Expand All @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -333,15 +335,15 @@ 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);
}
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());
Expand Down

0 comments on commit 8fb9282

Please sign in to comment.