Java Code Examples for net.bytebuddy.implementation.bytecode.StackManipulation#isValid()

The following examples show how to use net.bytebuddy.implementation.bytecode.StackManipulation#isValid() . 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: FixedValue.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * Blueprint method that for applying the actual implementation.
 *
 * @param methodVisitor           The method visitor to which the implementation is applied to.
 * @param implementationContext   The implementation context for the given implementation.
 * @param instrumentedMethod      The instrumented method that is target of the implementation.
 * @param fixedValueType          A description of the type of the fixed value that is loaded by the
 *                                {@code valueLoadingInstruction}.
 * @param valueLoadingInstruction A stack manipulation that represents the loading of the fixed value onto the
 *                                operand stack.
 * @return A representation of the stack and variable array sized that are required for this implementation.
 */
protected ByteCodeAppender.Size apply(MethodVisitor methodVisitor,
                                      Context implementationContext,
                                      MethodDescription instrumentedMethod,
                                      TypeDescription.Generic fixedValueType,
                                      StackManipulation valueLoadingInstruction) {
    StackManipulation assignment = assigner.assign(fixedValueType, instrumentedMethod.getReturnType(), typing);
    if (!assignment.isValid()) {
        throw new IllegalArgumentException("Cannot return value of type " + fixedValueType + " for " + instrumentedMethod);
    }
    StackManipulation.Size stackSize = new StackManipulation.Compound(
            valueLoadingInstruction,
            assignment,
            MethodReturn.of(instrumentedMethod.getReturnType())
    ).apply(methodVisitor, implementationContext);
    return new ByteCodeAppender.Size(stackSize.getMaximalSize(), instrumentedMethod.getStackSize());
}
 
Example 2
Source File: FixedValue.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) {
    if (instrumentedMethod.getParameters().size() <= index) {
        throw new IllegalStateException(instrumentedMethod + " does not define a parameter with index " + index);
    }
    ParameterDescription parameterDescription = instrumentedMethod.getParameters().get(index);
    StackManipulation stackManipulation = new StackManipulation.Compound(
            MethodVariableAccess.load(parameterDescription),
            assigner.assign(parameterDescription.getType(), instrumentedMethod.getReturnType(), typing),
            MethodReturn.of(instrumentedMethod.getReturnType())
    );
    if (!stackManipulation.isValid()) {
        throw new IllegalStateException("Cannot assign " + instrumentedMethod.getReturnType() + " to " + parameterDescription);
    }
    return new Size(stackManipulation.apply(methodVisitor, implementationContext).getMaximalSize(), instrumentedMethod.getStackSize());
}
 
Example 3
Source File: FieldAccessor.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) {
    FieldDescription fieldDescription = fieldLocation.resolve(instrumentedMethod);
    if (!fieldDescription.isStatic() && instrumentedMethod.isStatic()) {
        throw new IllegalStateException("Cannot set instance field " + fieldDescription + " from " + instrumentedMethod);
    } else if (fieldDescription.isFinal() && instrumentedMethod.isMethod()) {
        throw new IllegalStateException("Cannot set final field " + fieldDescription + " from " + instrumentedMethod);
    }
    StackManipulation stackManipulation = resolve(initialized, fieldDescription, instrumentedType, instrumentedMethod);
    if (!stackManipulation.isValid()) {
        throw new IllegalStateException("Set value cannot be assigned to " + fieldDescription);
    }
    return new Size(new StackManipulation.Compound(
            instrumentedMethod.isStatic()
                    ? StackManipulation.Trivial.INSTANCE
                    : MethodVariableAccess.loadThis(),
            stackManipulation,
            FieldAccess.forField(fieldDescription).write(),
            terminationHandler.resolve(instrumentedMethod)
    ).apply(methodVisitor, implementationContext).getMaximalSize(), instrumentedMethod.getStackSize());
}
 
Example 4
Source File: TargetMethodAnnotationDrivenBinder.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public MethodBinding bind(Implementation.Target implementationTarget,
                          MethodDescription source,
                          MethodDelegationBinder.TerminationHandler terminationHandler,
                          MethodInvoker methodInvoker,
                          Assigner assigner) {
    if (!candidate.isAccessibleTo(implementationTarget.getInstrumentedType())) {
        return MethodBinding.Illegal.INSTANCE;
    }
    StackManipulation methodTermination = terminationHandler.resolve(assigner, typing, source, candidate);
    if (!methodTermination.isValid()) {
        return MethodBinding.Illegal.INSTANCE;
    }
    MethodBinding.Builder methodDelegationBindingBuilder = new MethodBinding.Builder(methodInvoker, candidate);
    for (DelegationProcessor.Handler handler : handlers) {
        ParameterBinding<?> parameterBinding = handler.bind(source, implementationTarget, assigner);
        if (!parameterBinding.isValid() || !methodDelegationBindingBuilder.append(parameterBinding)) {
            return MethodBinding.Illegal.INSTANCE;
        }
    }
    return methodDelegationBindingBuilder.build(methodTermination);
}
 
Example 5
Source File: RebaseImplementationTarget.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a special method invocation for the given method.
 *
 * @param resolvedMethod      The rebased method to be invoked.
 * @param instrumentedType    The instrumented type on which the method is to be invoked if it is non-static.
 * @param prependedParameters Any additional arguments that are to be provided to the rebased method.
 * @return A special method invocation of the rebased method.
 */
protected static Implementation.SpecialMethodInvocation of(MethodDescription resolvedMethod, TypeDescription instrumentedType, TypeList prependedParameters) {
    StackManipulation stackManipulation = resolvedMethod.isStatic()
            ? MethodInvocation.invoke(resolvedMethod)
            : MethodInvocation.invoke(resolvedMethod).special(instrumentedType);
    if (stackManipulation.isValid()) {
        List<StackManipulation> stackManipulations = new ArrayList<StackManipulation>(prependedParameters.size() + 1);
        for (TypeDescription prependedParameter : prependedParameters) {
            stackManipulations.add(DefaultValue.of(prependedParameter));
        }
        stackManipulations.add(stackManipulation);
        return new RebasedMethodInvocation(resolvedMethod, instrumentedType, new Compound(stackManipulations), prependedParameters);
    } else {
        return Illegal.INSTANCE;
    }
}
 
Example 6
Source File: DefaultMethodCall.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) {
    StackManipulation defaultMethodInvocation = locateDefault(instrumentedMethod);
    if (!defaultMethodInvocation.isValid()) {
        throw new IllegalStateException("Cannot invoke default method on " + instrumentedMethod);
    }
    StackManipulation.Size stackSize = new StackManipulation.Compound(
            MethodVariableAccess.allArgumentsOf(instrumentedMethod).prependThisReference(),
            defaultMethodInvocation,
            MethodReturn.of(instrumentedMethod.getReturnType())
    ).apply(methodVisitor, implementationContext);
    return new Size(stackSize.getMaximalSize(), instrumentedMethod.getStackSize());
}
 
Example 7
Source File: ArrayFactory.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public boolean isValid() {
    for (StackManipulation stackManipulation : stackManipulations) {
        if (!stackManipulation.isValid()) {
            return false;
        }
    }
    return arrayCreator.isValid();
}
 
Example 8
Source File: InvokeDynamic.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Resolved resolve(TypeDescription instrumentedType, MethodDescription instrumentedMethod, Assigner assigner, Assigner.Typing typing) {
    FieldDescription fieldDescription = instrumentedType.getDeclaredFields().filter(named(name)).getOnly();
    StackManipulation stackManipulation = assigner.assign(fieldDescription.getType(), fieldType.asGenericType(), typing);
    if (!stackManipulation.isValid()) {
        throw new IllegalStateException("Cannot assign " + fieldDescription + " to " + fieldType);
    }
    return new Resolved.Simple(new StackManipulation.Compound(FieldAccess.forField(fieldDescription).read(),
            stackManipulation), fieldDescription.getType().asErasure());
}
 
Example 9
Source File: InvokeDynamic.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected Resolved doResolve(StackManipulation access, TypeDescription.Generic typeDescription, Assigner assigner, Assigner.Typing typing) {
    StackManipulation stackManipulation = assigner.assign(typeDescription, this.typeDescription.asGenericType(), typing);
    if (!stackManipulation.isValid()) {
        throw new IllegalStateException("Cannot assign " + typeDescription + " to " + this.typeDescription);
    }
    return new Resolved.Simple(new StackManipulation.Compound(access, stackManipulation), this.typeDescription);
}
 
Example 10
Source File: InvokeDynamic.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected Resolved doResolve(StackManipulation access, TypeDescription.Generic type, Assigner assigner, Assigner.Typing typing) {
    StackManipulation stackManipulation = assigner.assign(type, typeDescription.asGenericType(), typing);
    if (!stackManipulation.isValid()) {
        throw new IllegalStateException("Cannot assign " + type + " to " + typeDescription);
    }
    return new Resolved.Simple(new StackManipulation.Compound(access, stackManipulation), typeDescription);
}
 
Example 11
Source File: SuperMethodCall.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext, MethodDescription instrumentedMethod) {
    StackManipulation superMethodCall = implementationTarget
            .invokeDominant(instrumentedMethod.asSignatureToken())
            .withCheckedCompatibilityTo(instrumentedMethod.asTypeToken());
    if (!superMethodCall.isValid()) {
        throw new IllegalStateException("Cannot call super (or default) method for " + instrumentedMethod);
    }
    StackManipulation.Size size = new StackManipulation.Compound(
            MethodVariableAccess.allArgumentsOf(instrumentedMethod).prependThisReference(),
            superMethodCall,
            terminationHandler.of(instrumentedMethod)
    ).apply(methodVisitor, implementationContext);
    return new Size(size.getMaximalSize(), instrumentedMethod.getStackSize());
}
 
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: Implementation.java    From byte-buddy with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a special method invocation for a given invocation target.
 *
 * @param methodDescription The method that represents the special method invocation.
 * @param typeDescription   The type on which the method should be invoked on by an {@code INVOKESPECIAL}
 *                          invocation.
 * @return A special method invocation representing a legal invocation if the method can be invoked
 * specially on the target type or an illegal invocation if this is not possible.
 */
public static SpecialMethodInvocation of(MethodDescription methodDescription, TypeDescription typeDescription) {
    StackManipulation stackManipulation = MethodInvocation.invoke(methodDescription).special(typeDescription);
    return stackManipulation.isValid()
            ? new Simple(methodDescription, typeDescription, stackManipulation)
            : SpecialMethodInvocation.Illegal.INSTANCE;
}