Java Code Examples for net.bytebuddy.implementation.bytecode.assign.Assigner#Typing

The following examples show how to use net.bytebuddy.implementation.bytecode.assign.Assigner#Typing . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TargetMethodAnnotationDrivenBinder.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public ParameterBinding<?> bind(AnnotationDescription.Loadable<S> annotation,
                                MethodDescription source,
                                ParameterDescription target,
                                Implementation.Target implementationTarget,
                                Assigner assigner,
                                Assigner.Typing typing) {
    if (!declaringType(annotation).represents(void.class)) {
        if (declaringType(annotation).isPrimitive() || declaringType(annotation).isArray()) {
            throw new IllegalStateException("A primitive type or array type cannot declare a field: " + source);
        } else if (!implementationTarget.getInstrumentedType().isAssignableTo(declaringType(annotation))) {
            return MethodDelegationBinder.ParameterBinding.Illegal.INSTANCE;
        }
    }
    FieldLocator fieldLocator = declaringType(annotation).represents(void.class)
            ? new FieldLocator.ForClassHierarchy(implementationTarget.getInstrumentedType())
            : new FieldLocator.ForExactType(declaringType(annotation), implementationTarget.getInstrumentedType());
    FieldLocator.Resolution resolution = fieldName(annotation).equals(BEAN_PROPERTY)
            ? resolveAccessor(fieldLocator, source)
            : fieldLocator.locate(fieldName(annotation));
    return resolution.isResolved() && !(source.isStatic() && !resolution.getField().isStatic())
            ? bind(resolution.getField(), annotation, source, target, implementationTarget, assigner)
            : ParameterBinding.Illegal.INSTANCE;
}
 
Example 2
Source File: MethodCall.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public StackManipulation toStackManipulation(ParameterDescription target, Assigner assigner, Assigner.Typing typing) {
    TypeDescription.Generic componentType;
    if (target.getType().represents(Object.class)) {
        componentType = TypeDescription.Generic.OBJECT;
    } else if (target.getType().isArray()) {
        componentType = target.getType().getComponentType();
    } else {
        throw new IllegalStateException("Cannot set method parameter array for non-array type: " + target);
    }
    List<StackManipulation> stackManipulations = new ArrayList<StackManipulation>(parameters.size());
    for (ParameterDescription parameter : parameters) {
        StackManipulation stackManipulation = new StackManipulation.Compound(
                MethodVariableAccess.load(parameter),
                assigner.assign(parameter.getType(), componentType, typing)
        );
        if (stackManipulation.isValid()) {
            stackManipulations.add(stackManipulation);
        } else {
            throw new IllegalStateException("Cannot assign " + parameter + " to " + componentType);
        }
    }
    return new StackManipulation.Compound(ArrayFactory.forType(componentType).withValues(stackManipulations));
}
 
Example 3
Source File: InvokeDynamic.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Instructs this implementation to use the provided assigner and decides if the assigner should apply
 * dynamic typing.
 *
 * @param assigner The assigner to use.
 * @param typing   {@code true} if the assigner should attempt dynamic typing.
 * @return The invoke dynamic instruction where the given assigner and dynamic-typing directive are applied.
 */
public Implementation.Composable withAssigner(Assigner assigner, Assigner.Typing typing) {
    return new InvokeDynamic(bootstrap,
            arguments,
            invocationProvider,
            terminationHandler,
            assigner,
            typing);
}
 
Example 4
Source File: MethodCall.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public StackManipulation toStackManipulation(MethodDescription invokedMethod, Assigner assigner, Assigner.Typing typing) {
    if (!invokedMethod.isInvokableOn(fieldDescription.getType().asErasure())) {
        throw new IllegalStateException("Cannot invoke " + invokedMethod + " on " + fieldDescription);
    }
    StackManipulation stackManipulation = assigner.assign(fieldDescription.getType(), invokedMethod.getDeclaringType().asGenericType(), typing);
    if (!stackManipulation.isValid()) {
        throw new IllegalStateException("Cannot invoke " + invokedMethod + " on " + fieldDescription);
    }
    return new StackManipulation.Compound(invokedMethod.isStatic() || fieldDescription.isStatic()
            ? StackManipulation.Trivial.INSTANCE
            : MethodVariableAccess.loadThis(),
            FieldAccess.forField(fieldDescription).read(), stackManipulation);
}
 
Example 5
Source File: InvokeDynamic.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new dynamic method invocation with implicit arguments.
 *
 * @param bootstrap          The bootstrap method or constructor.
 * @param arguments          The arguments that are provided to the bootstrap method or constructor.
 * @param invocationProvider The target provided that identifies the method to be bootstrapped.
 * @param terminationHandler A handler that handles the method return.
 * @param assigner           The assigner to be used.
 * @param typing             Indicates if dynamic type castings should be attempted for incompatible assignments.
 */
protected WithImplicitArguments(MethodDescription.InDefinedShape bootstrap,
                                List<?> arguments,
                                InvocationProvider invocationProvider,
                                TerminationHandler terminationHandler,
                                Assigner assigner,
                                Assigner.Typing typing) {
    super(bootstrap,
            arguments,
            invocationProvider,
            terminationHandler,
            assigner,
            typing);
}
 
Example 6
Source File: TargetMethodAnnotationDrivenBinder.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Locates a handler which is responsible for processing the given parameter. If no explicit handler can
 * be located, a fallback handler is provided.
 *
 * @param target The target parameter being handled.
 * @return A handler for processing the parameter with the given annotations.
 */
protected Handler prepare(ParameterDescription target) {
    Assigner.Typing typing = RuntimeType.Verifier.check(target);
    Handler handler = new Handler.Unbound(target, typing);
    for (AnnotationDescription annotation : target.getDeclaredAnnotations()) {
        ParameterBinder<?> parameterBinder = parameterBinders.get(annotation.getAnnotationType());
        if (parameterBinder != null && handler.isBound()) {
            throw new IllegalStateException("Ambiguous binding for parameter annotated with two handled annotation types");
        } else if (parameterBinder != null /* && !handler.isBound() */) {
            handler = Handler.Bound.of(target, parameterBinder, annotation, typing);
        }
    }
    return handler;
}
 
Example 7
Source File: InvokeDynamic.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new abstract delegator for a dynamic method invocation where the last argument is assigned an implicit type.
 *
 * @param bootstrap           The bootstrap method or constructor.
 * @param arguments           The arguments that are provided to the bootstrap method.
 * @param invocationProvider  The target provided that identifies the method to be bootstrapped.
 * @param terminationHandler  A handler that handles the method return.
 * @param assigner            The assigner to be used.
 * @param typing              Indicates if dynamic type castings should be attempted for incompatible assignments.
 * @param fieldName           The field name.
 * @param fieldLocatorFactory The field locator factory to use.
 */
protected OfField(MethodDescription.InDefinedShape bootstrap,
                  List<?> arguments,
                  InvocationProvider invocationProvider,
                  TerminationHandler terminationHandler,
                  Assigner assigner,
                  Assigner.Typing typing,
                  String fieldName,
                  FieldLocator.Factory fieldLocatorFactory) {
    super(bootstrap, arguments, invocationProvider, terminationHandler, assigner, typing);
    this.fieldName = fieldName;
    this.fieldLocatorFactory = fieldLocatorFactory;
}
 
Example 8
Source File: MethodCall.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public StackManipulation toStackManipulation(MethodDescription invokedMethod, MethodDescription instrumentedMethod, Assigner assigner, Assigner.Typing typing) {
    StackManipulation stackManipulation = assigner.assign(invokedMethod.getReturnType(), fieldDescription.getType(), typing);
    if (!stackManipulation.isValid()) {
        throw new IllegalStateException("Cannot assign result of " + invokedMethod + " to " + fieldDescription);
    }
    return new StackManipulation.Compound(stackManipulation, FieldAccess.forField(fieldDescription).write());
}
 
Example 9
Source File: MethodCall.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public StackManipulation toStackManipulation(ParameterDescription target, Assigner assigner, Assigner.Typing typing) {
    StackManipulation stackManipulation = new StackManipulation.Compound(
            MethodVariableAccess.loadThis(),
            assigner.assign(instrumentedType.asGenericType(), target.getType(), typing));
    if (!stackManipulation.isValid()) {
        throw new IllegalStateException("Cannot assign " + instrumentedType + " to " + target);
    }
    return stackManipulation;
}
 
Example 10
Source File: MethodCall.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public StackManipulation toStackManipulation(ParameterDescription target, Assigner assigner, Assigner.Typing typing) {
    ParameterDescription parameterDescription = instrumentedMethod.getParameters().get(index);
    StackManipulation stackManipulation = new StackManipulation.Compound(
            MethodVariableAccess.load(parameterDescription),
            assigner.assign(parameterDescription.getType(), target.getType(), typing));
    if (!stackManipulation.isValid()) {
        throw new IllegalStateException("Cannot assign " + parameterDescription + " to " + target + " for " + instrumentedMethod);
    }
    return stackManipulation;
}
 
Example 11
Source File: FixedValue.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Implementation withAssigner(Assigner assigner, Assigner.Typing typing) {
    return new ForArgument(assigner, typing, index);
}
 
Example 12
Source File: InvokeDynamic.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
@Override
protected StackManipulation resolve(MethodDescription interceptedMethod, TypeDescription returnType, Assigner assigner, Assigner.Typing typing) {
    StackManipulation stackManipulation = assigner.assign(returnType.asGenericType(), interceptedMethod.getReturnType(), typing);
    if (!stackManipulation.isValid()) {
        throw new IllegalStateException("Cannot return " + returnType + " from " + interceptedMethod);
    }
    return new StackManipulation.Compound(stackManipulation, MethodReturn.of(interceptedMethod.getReturnType()));
}
 
Example 13
Source File: FixedValue.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Implementation withAssigner(Assigner assigner, Assigner.Typing typing) {
    return new ForThisValue(assigner, typing);
}
 
Example 14
Source File: TargetMethodAnnotationDrivenBinder.java    From byte-buddy with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a parameter binding for the given target parameter.
 *
 * @param annotation           The annotation that was cause for the delegation to this argument binder.
 * @param source               The intercepted source method.
 * @param target               Tge target parameter that is subject to be bound to
 *                             intercepting the {@code source} method.
 * @param implementationTarget The target of the current implementation that is subject to this binding.
 * @param assigner             An assigner that can be used for applying the binding.
 * @param typing               The typing to apply.
 * @return A parameter binding for the requested target method parameter.
 */
ParameterBinding<?> bind(AnnotationDescription.Loadable<T> annotation,
                         MethodDescription source,
                         ParameterDescription target,
                         Implementation.Target implementationTarget,
                         Assigner assigner,
                         Assigner.Typing typing);
 
Example 15
Source File: FieldAccessor.java    From byte-buddy with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new setter instrumentation for a parameter value.
 *
 * @param fieldLocation      The field's location.
 * @param assigner           The assigner to use.
 * @param typing             Indicates if dynamic type castings should be attempted for incompatible assignments.
 * @param terminationHandler The termination handler to apply.
 * @param index              The parameter's index.
 */
protected OfParameterValue(FieldLocation fieldLocation,
                           Assigner assigner,
                           Assigner.Typing typing,
                           TerminationHandler terminationHandler,
                           int index) {
    super(fieldLocation, assigner, typing, terminationHandler);
    this.index = index;
}
 
Example 16
Source File: FixedValue.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new constant pool fixed value implementation.
 *
 * @param valueLoadInstruction The instruction that is responsible for loading the constant pool value onto the
 *                             operand stack.
 * @param loadedType           A type description representing the loaded type.
 * @param assigner             The assigner to use for assigning the fixed value to the return type of the
 *                             instrumented value.
 * @param typing               Indicates if dynamic type castings should be attempted for incompatible assignments.
 */
private ForPoolValue(Assigner assigner, Assigner.Typing typing, StackManipulation valueLoadInstruction, TypeDescription loadedType) {
    super(assigner, typing);
    this.valueLoadInstruction = valueLoadInstruction;
    this.loadedType = loadedType;
}
 
Example 17
Source File: PrimitiveUnboxingDelegate.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
/**
 * Attempts to unbox the represented type in order to assign the unboxed value to the given target type
 * while using the assigner that is provided by the method call.
 *
 * @param target   The type that is the desired outcome of the assignment.
 * @param assigner The assigner used to assign the unboxed type to the target type.
 * @param typing   Determines if a type-casting should be attempted for incompatible types.
 * @return A stack manipulation representing this assignment if such an assignment is possible. An illegal
 * assignment otherwise.
 */
StackManipulation assignUnboxedTo(TypeDescription.Generic target, Assigner assigner, Assigner.Typing typing);
 
Example 18
Source File: FixedValue.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new fixed value implementation.
 *
 * @param assigner The assigner to use for assigning the fixed value to the return type of the instrumented value.
 * @param typing   Indicates if dynamic type castings should be attempted for incompatible assignments.
 */
protected FixedValue(Assigner assigner, Assigner.Typing typing) {
    this.assigner = assigner;
    this.typing = typing;
}
 
Example 19
Source File: MethodCall.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
/**
 * Defines an assigner to be used for assigning values to the parameters of the invoked method. This assigner
 * is also used for assigning the invoked method's return value to the field being set.
 *
 * @param assigner The assigner to use.
 * @param typing   Indicates if dynamic type castings should be attempted for incompatible assignments.
 * @return This field-setting method call using the provided assigner.
 */
public Composable withAssigner(Assigner assigner, Assigner.Typing typing) {
    return new FieldSetting((MethodCall) methodCall.withAssigner(assigner, typing));
}
 
Example 20
Source File: InvokeDynamic.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a stack manipulation that handles the method return.
 *
 * @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);