Skip to content

Commit

Permalink
Allow defining custom setters using field accessor.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Oct 30, 2016
1 parent 5d9f2b9 commit a43e5bd
Show file tree
Hide file tree
Showing 17 changed files with 760 additions and 612 deletions.

Large diffs are not rendered by default.

Expand Up @@ -234,7 +234,7 @@ public static WithImplicitTarget bootstrap(MethodDescription.InDefinedShape boot
return new WithImplicitTarget(bootstrapMethod, return new WithImplicitTarget(bootstrapMethod,
serializedArguments, serializedArguments,
new InvocationProvider.Default(), new InvocationProvider.Default(),
TerminationHandler.ForMethodReturn.INSTANCE, TerminationHandler.RETURNING,
Assigner.DEFAULT, Assigner.DEFAULT,
Assigner.Typing.STATIC); Assigner.Typing.STATIC);
} }
Expand Down Expand Up @@ -744,7 +744,7 @@ public Implementation andThen(Implementation implementation) {
return new Implementation.Compound(new InvokeDynamic(bootstrapMethod, return new Implementation.Compound(new InvokeDynamic(bootstrapMethod,
handleArguments, handleArguments,
invocationProvider, invocationProvider,
TerminationHandler.ForChainedInvocation.INSTANCE, TerminationHandler.DROPPING,
assigner, assigner,
typing), typing),
implementation); implementation);
Expand Down Expand Up @@ -2896,69 +2896,51 @@ public String toString() {
* A termination handler is responsible to handle the return value of a method that is invoked via a * A termination handler is responsible to handle the return value of a method that is invoked via a
* {@link net.bytebuddy.implementation.InvokeDynamic}. * {@link net.bytebuddy.implementation.InvokeDynamic}.
*/ */
protected interface TerminationHandler { protected enum TerminationHandler {


/** /**
* Returns a stack manipulation that handles the method return. * A termination handler that returns the bound method's return value.
*
* @param interceptedMethod The method being intercepted.
* @param returnType The return type of the instrumented method.
* @param assigner The assigner to use.
* @param typing Indicates if dynamic type castings should be attempted for incompatible assignments.
* @return A stack manipulation that handles the method return.
*/
StackManipulation resolve(MethodDescription interceptedMethod,
TypeDescription returnType,
Assigner assigner,
Assigner.Typing typing);

/**
* Returns the return value of the dynamic invocation from the intercepted method.
*/ */
enum ForMethodReturn implements TerminationHandler { RETURNING {

/**
* The singleton instance.
*/
INSTANCE;

@Override @Override
public StackManipulation resolve(MethodDescription interceptedMethod, TypeDescription returnType, Assigner assigner, Assigner.Typing typing) { protected StackManipulation resolve(MethodDescription interceptedMethod, TypeDescription returnType, Assigner assigner, Assigner.Typing typing) {
StackManipulation stackManipulation = assigner.assign(returnType.asGenericType(), interceptedMethod.getReturnType(), typing); StackManipulation stackManipulation = assigner.assign(returnType.asGenericType(), interceptedMethod.getReturnType(), typing);
if (!stackManipulation.isValid()) { if (!stackManipulation.isValid()) {
throw new IllegalStateException("Cannot return " + returnType + " from " + interceptedMethod); throw new IllegalStateException("Cannot return " + returnType + " from " + interceptedMethod);
} }
return new StackManipulation.Compound(stackManipulation, MethodReturn.of(interceptedMethod.getReturnType().asErasure())); return new StackManipulation.Compound(stackManipulation, MethodReturn.of(interceptedMethod.getReturnType().asErasure()));
} }

},
@Override
public String toString() {
return "InvokeDynamic.TerminationHandler.ForMethodReturn." + name();
}
}


/** /**
* Drops the return value of the dynamic invocation from the operand stack without returning from the * A termination handler that drops the bound method's return value.
* intercepted method.
*/ */
enum ForChainedInvocation implements TerminationHandler { DROPPING {

/**
* The singleton instance.
*/
INSTANCE;

@Override @Override
public StackManipulation resolve(MethodDescription interceptedMethod, TypeDescription returnType, Assigner assigner, Assigner.Typing typing) { protected StackManipulation resolve(MethodDescription interceptedMethod, TypeDescription returnType, Assigner assigner, Assigner.Typing typing) {
return Removal.pop(interceptedMethod.isConstructor() return Removal.pop(interceptedMethod.isConstructor()
? interceptedMethod.getDeclaringType().asErasure() ? interceptedMethod.getDeclaringType().asErasure()
: interceptedMethod.getReturnType().asErasure()); : interceptedMethod.getReturnType().asErasure());
} }
};


@Override /**
public String toString() { * Returns a stack manipulation that handles the method return.
return "InvokeDynamic.TerminationHandler.ForChainedInvocation." + name(); *
} * @param interceptedMethod The method being intercepted.
* @param returnType The return type of the instrumented method.
* @param assigner The assigner to use.
* @param typing Indicates if dynamic type castings should be attempted for incompatible assignments.
* @return A stack manipulation that handles the method return.
*/
protected abstract StackManipulation resolve(MethodDescription interceptedMethod,
TypeDescription returnType,
Assigner assigner,
Assigner.Typing typing);

@Override
public String toString() {
return "InvokeDynamic.TerminationHandler." + name();
} }
} }


Expand Down
Expand Up @@ -215,7 +215,7 @@ public static MethodCall construct(MethodDescription methodDescription) {
TargetHandler.ForConstructingInvocation.INSTANCE, TargetHandler.ForConstructingInvocation.INSTANCE,
Collections.<ArgumentLoader.Factory>emptyList(), Collections.<ArgumentLoader.Factory>emptyList(),
MethodInvoker.ForContextualInvocation.INSTANCE, MethodInvoker.ForContextualInvocation.INSTANCE,
TerminationHandler.ForMethodReturn.INSTANCE, TerminationHandler.RETURNING,
Assigner.DEFAULT, Assigner.DEFAULT,
Assigner.Typing.STATIC); Assigner.Typing.STATIC);
} }
Expand Down Expand Up @@ -469,7 +469,7 @@ public Implementation andThen(Implementation implementation) {
targetHandler, targetHandler,
argumentLoaders, argumentLoaders,
methodInvoker, methodInvoker,
TerminationHandler.ForChainedInvocation.INSTANCE, TerminationHandler.DROPPING,
assigner, assigner,
typing), implementation); typing), implementation);
} }
Expand Down Expand Up @@ -2451,32 +2451,12 @@ public String toString() {
* A termination handler is responsible to handle the return value of a method that is invoked via a * A termination handler is responsible to handle the return value of a method that is invoked via a
* {@link net.bytebuddy.implementation.MethodCall}. * {@link net.bytebuddy.implementation.MethodCall}.
*/ */
protected interface TerminationHandler { protected enum TerminationHandler {


/** /**
* Returns a stack manipulation that handles the method return. * A termination handler that returns the invoked method's return value.
*
* @param invokedMethod The method that was invoked by the method call.
* @param instrumentedMethod The method being intercepted.
* @param assigner The assigner to be used.
* @param typing Indicates if dynamic type castings should be attempted for incompatible assignments.
* @return A stack manipulation that handles the method return.
*/
StackManipulation resolve(MethodDescription invokedMethod,
MethodDescription instrumentedMethod,
Assigner assigner,
Assigner.Typing typing);

/**
* Returns the return value of the method call from the intercepted method.
*/ */
enum ForMethodReturn implements TerminationHandler { RETURNING {

/**
* The singleton instance.
*/
INSTANCE;

@Override @Override
public StackManipulation resolve(MethodDescription invokedMethod, MethodDescription instrumentedMethod, Assigner assigner, Assigner.Typing typing) { public StackManipulation resolve(MethodDescription invokedMethod, MethodDescription instrumentedMethod, Assigner assigner, Assigner.Typing typing) {
StackManipulation stackManipulation = assigner.assign(invokedMethod.isConstructor() StackManipulation stackManipulation = assigner.assign(invokedMethod.isConstructor()
Expand All @@ -2488,35 +2468,37 @@ public StackManipulation resolve(MethodDescription invokedMethod, MethodDescript
return new StackManipulation.Compound(stackManipulation, return new StackManipulation.Compound(stackManipulation,
MethodReturn.of(instrumentedMethod.getReturnType().asErasure())); MethodReturn.of(instrumentedMethod.getReturnType().asErasure()));
} }

},
@Override
public String toString() {
return "MethodCall.TerminationHandler.ForMethodReturn." + name();
}
}


/** /**
* Drops the return value of the called method from the operand stack without returning from the intercepted * A termination handler that drops the invoked method's return value.
* method.
*/ */
enum ForChainedInvocation implements TerminationHandler { DROPPING {

/**
* The singleton instance.
*/
INSTANCE;

@Override @Override
public StackManipulation resolve(MethodDescription invokedMethod, MethodDescription instrumentedMethod, Assigner assigner, Assigner.Typing typing) { protected StackManipulation resolve(MethodDescription invokedMethod, MethodDescription instrumentedMethod, Assigner assigner, Assigner.Typing typing) {
return Removal.pop(invokedMethod.isConstructor() return Removal.pop(invokedMethod.isConstructor()
? invokedMethod.getDeclaringType().asErasure() ? invokedMethod.getDeclaringType().asErasure()
: invokedMethod.getReturnType().asErasure()); : invokedMethod.getReturnType().asErasure());
} }
};


@Override /**
public String toString() { * Returns a stack manipulation that handles the method return.
return "MethodCall.TerminationHandler.ForChainedInvocation." + name(); *
} * @param invokedMethod The method that was invoked by the method call.
* @param instrumentedMethod The method being intercepted.
* @param assigner The assigner to be used.
* @param typing Indicates if dynamic type castings should be attempted for incompatible assignments.
* @return A stack manipulation that handles the method return.
*/
protected abstract StackManipulation resolve(MethodDescription invokedMethod,
MethodDescription instrumentedMethod,
Assigner assigner,
Assigner.Typing typing);

@Override
public String toString() {
return "MethodCall.TerminationHandler." + name();
} }
} }


Expand All @@ -2539,7 +2521,7 @@ protected WithoutSpecifiedTarget(MethodLocator methodLocator) {
TargetHandler.ForSelfOrStaticInvocation.INSTANCE, TargetHandler.ForSelfOrStaticInvocation.INSTANCE,
Collections.<ArgumentLoader.Factory>emptyList(), Collections.<ArgumentLoader.Factory>emptyList(),
MethodInvoker.ForContextualInvocation.INSTANCE, MethodInvoker.ForContextualInvocation.INSTANCE,
TerminationHandler.ForMethodReturn.INSTANCE, TerminationHandler.RETURNING,
Assigner.DEFAULT, Assigner.DEFAULT,
Assigner.Typing.STATIC); Assigner.Typing.STATIC);
} }
Expand Down Expand Up @@ -2568,7 +2550,7 @@ public <T> MethodCall on(T target, Class<? super T> type) {
new TargetHandler.ForStaticField(target, new TypeDescription.Generic.OfNonGenericType.ForLoadedType(type)), new TargetHandler.ForStaticField(target, new TypeDescription.Generic.OfNonGenericType.ForLoadedType(type)),
argumentLoaders, argumentLoaders,
new MethodInvoker.ForVirtualInvocation(type), new MethodInvoker.ForVirtualInvocation(type),
TerminationHandler.ForMethodReturn.INSTANCE, terminationHandler,
assigner, assigner,
typing); typing);
} }
Expand All @@ -2587,7 +2569,7 @@ public MethodCall onArgument(int index) {
new TargetHandler.ForMethodParameter(index), new TargetHandler.ForMethodParameter(index),
argumentLoaders, argumentLoaders,
MethodInvoker.ForVirtualInvocation.WithImplicitType.INSTANCE, MethodInvoker.ForVirtualInvocation.WithImplicitType.INSTANCE,
TerminationHandler.ForMethodReturn.INSTANCE, terminationHandler,
assigner, assigner,
typing); typing);
} }
Expand Down Expand Up @@ -2617,7 +2599,7 @@ public MethodCall onInstanceField(TypeDescription.Generic typeDescription, Strin
new TargetHandler.ForInstanceField(fieldName, typeDescription), new TargetHandler.ForInstanceField(fieldName, typeDescription),
argumentLoaders, argumentLoaders,
new MethodInvoker.ForVirtualInvocation(typeDescription.asErasure()), new MethodInvoker.ForVirtualInvocation(typeDescription.asErasure()),
TerminationHandler.ForMethodReturn.INSTANCE, terminationHandler,
assigner, assigner,
typing); typing);
} }
Expand All @@ -2635,7 +2617,7 @@ public MethodCall onSuper() {
TargetHandler.ForSelfOrStaticInvocation.INSTANCE, TargetHandler.ForSelfOrStaticInvocation.INSTANCE,
argumentLoaders, argumentLoaders,
MethodInvoker.ForSuperMethodInvocation.INSTANCE, MethodInvoker.ForSuperMethodInvocation.INSTANCE,
TerminationHandler.ForMethodReturn.INSTANCE, terminationHandler,
assigner, assigner,
typing); typing);
} }
Expand All @@ -2650,7 +2632,7 @@ public MethodCall onDefault() {
TargetHandler.ForSelfOrStaticInvocation.INSTANCE, TargetHandler.ForSelfOrStaticInvocation.INSTANCE,
argumentLoaders, argumentLoaders,
MethodInvoker.ForDefaultMethodInvocation.INSTANCE, MethodInvoker.ForDefaultMethodInvocation.INSTANCE,
TerminationHandler.ForMethodReturn.INSTANCE, terminationHandler,
assigner, assigner,
typing); typing);
} }
Expand Down
Expand Up @@ -237,7 +237,7 @@ public static MethodDelegation to(TypeDescription typeDescription) {
return new MethodDelegation(ImplementationDelegate.ForStaticMethod.of(typeDescription), return new MethodDelegation(ImplementationDelegate.ForStaticMethod.of(typeDescription),
TargetMethodAnnotationDrivenBinder.ParameterBinder.DEFAULTS, TargetMethodAnnotationDrivenBinder.ParameterBinder.DEFAULTS,
Argument.NextUnboundAsDefaultsProvider.INSTANCE, Argument.NextUnboundAsDefaultsProvider.INSTANCE,
TargetMethodAnnotationDrivenBinder.TerminationHandler.Returning.INSTANCE, TargetMethodAnnotationDrivenBinder.TerminationHandler.RETURNING,
MethodDelegationBinder.AmbiguityResolver.DEFAULT, MethodDelegationBinder.AmbiguityResolver.DEFAULT,
Assigner.DEFAULT); Assigner.DEFAULT);
} }
Expand Down Expand Up @@ -421,7 +421,7 @@ public static MethodDelegation to(Object delegate, Type type, String fieldName,
return new MethodDelegation(new ImplementationDelegate.ForInstance(delegate, fieldName, typeDescription, methodGraphCompiler), return new MethodDelegation(new ImplementationDelegate.ForInstance(delegate, fieldName, typeDescription, methodGraphCompiler),
TargetMethodAnnotationDrivenBinder.ParameterBinder.DEFAULTS, TargetMethodAnnotationDrivenBinder.ParameterBinder.DEFAULTS,
Argument.NextUnboundAsDefaultsProvider.INSTANCE, Argument.NextUnboundAsDefaultsProvider.INSTANCE,
TargetMethodAnnotationDrivenBinder.TerminationHandler.Returning.INSTANCE, TargetMethodAnnotationDrivenBinder.TerminationHandler.RETURNING,
MethodDelegationBinder.AmbiguityResolver.DEFAULT, MethodDelegationBinder.AmbiguityResolver.DEFAULT,
Assigner.DEFAULT); Assigner.DEFAULT);
} }
Expand All @@ -448,7 +448,7 @@ public static MethodDelegation toConstructor(TypeDescription typeDescription) {
return new MethodDelegation(ImplementationDelegate.ForConstruction.of(typeDescription), return new MethodDelegation(ImplementationDelegate.ForConstruction.of(typeDescription),
TargetMethodAnnotationDrivenBinder.ParameterBinder.DEFAULTS, TargetMethodAnnotationDrivenBinder.ParameterBinder.DEFAULTS,
Argument.NextUnboundAsDefaultsProvider.INSTANCE, Argument.NextUnboundAsDefaultsProvider.INSTANCE,
TargetMethodAnnotationDrivenBinder.TerminationHandler.Returning.INSTANCE, TargetMethodAnnotationDrivenBinder.TerminationHandler.RETURNING,
MethodDelegationBinder.AmbiguityResolver.DEFAULT, MethodDelegationBinder.AmbiguityResolver.DEFAULT,
Assigner.DEFAULT); Assigner.DEFAULT);
} }
Expand Down Expand Up @@ -501,7 +501,7 @@ public static MethodDelegation toField(String name, FieldLocator.Factory fieldLo
return new MethodDelegation(new ImplementationDelegate.ForField(name, fieldLocatorFactory, methodGraphCompiler), return new MethodDelegation(new ImplementationDelegate.ForField(name, fieldLocatorFactory, methodGraphCompiler),
TargetMethodAnnotationDrivenBinder.ParameterBinder.DEFAULTS, TargetMethodAnnotationDrivenBinder.ParameterBinder.DEFAULTS,
Argument.NextUnboundAsDefaultsProvider.INSTANCE, Argument.NextUnboundAsDefaultsProvider.INSTANCE,
TargetMethodAnnotationDrivenBinder.TerminationHandler.Returning.INSTANCE, TargetMethodAnnotationDrivenBinder.TerminationHandler.RETURNING,
MethodDelegationBinder.AmbiguityResolver.DEFAULT, MethodDelegationBinder.AmbiguityResolver.DEFAULT,
Assigner.DEFAULT); Assigner.DEFAULT);
} }
Expand Down Expand Up @@ -611,7 +611,7 @@ public Implementation andThen(Implementation implementation) {
return new Compound(new MethodDelegation(implementationDelegate, return new Compound(new MethodDelegation(implementationDelegate,
parameterBinders, parameterBinders,
defaultsProvider, defaultsProvider,
TargetMethodAnnotationDrivenBinder.TerminationHandler.Dropping.INSTANCE, TargetMethodAnnotationDrivenBinder.TerminationHandler.DROPPING,
ambiguityResolver, ambiguityResolver,
assigner), implementation); assigner), implementation);
} }
Expand Down

0 comments on commit a43e5bd

Please sign in to comment.