Java Code Examples for net.bytebuddy.implementation.bytecode.member.MethodReturn#of()

The following examples show how to use net.bytebuddy.implementation.bytecode.member.MethodReturn#of() . 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
/**
 * {@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 2
Source File: ByteBuddy.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public ByteCodeAppender appender(Target implementationTarget) {
    StringBuilder stringBuilder = new StringBuilder();
    List<Object> methodHandles = new ArrayList<Object>(implementationTarget.getInstrumentedType().getRecordComponents().size());
    for (RecordComponentDescription.InDefinedShape recordComponent : implementationTarget.getInstrumentedType().getRecordComponents()) {
        if (stringBuilder.length() > 0) {
            stringBuilder.append(";");
        }
        stringBuilder.append(recordComponent.getActualName());
        methodHandles.add(JavaConstant.MethodHandle.ofGetter(implementationTarget.getInstrumentedType().getDeclaredFields()
                .filter(named(recordComponent.getActualName()))
                .getOnly()).asConstantPoolValue());
    }
    return new ByteCodeAppender.Simple(MethodVariableAccess.loadThis(),
            stackManipulation,
            MethodInvocation.invoke(new MethodDescription.Latent(JavaType.OBJECT_METHODS.getTypeStub(), new MethodDescription.Token("bootstrap",
                    Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
                    TypeDescription.Generic.OBJECT,
                    Arrays.asList(JavaType.METHOD_HANDLES_LOOKUP.getTypeStub().asGenericType(),
                            TypeDescription.STRING.asGenericType(),
                            JavaType.TYPE_DESCRIPTOR.getTypeStub().asGenericType(),
                            TypeDescription.CLASS.asGenericType(),
                            TypeDescription.STRING.asGenericType(),
                            TypeDescription.ArrayProjection.of(JavaType.METHOD_HANDLE.getTypeStub()).asGenericType())))).dynamic(name,
                    returnType,
                    CompoundList.of(implementationTarget.getInstrumentedType(), arguments),
                    CompoundList.of(Arrays.asList(org.objectweb.asm.Type.getType(implementationTarget.getInstrumentedType().getDescriptor()), stringBuilder.toString()), methodHandles)),
            MethodReturn.of(returnType));
}
 
Example 3
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.isConstructor()
            ? invokedMethod.getDeclaringType().asGenericType()
            : invokedMethod.getReturnType(), instrumentedMethod.getReturnType(), typing);
    if (!stackManipulation.isValid()) {
        throw new IllegalStateException("Cannot return " + invokedMethod.getReturnType() + " from " + instrumentedMethod);
    }
    return new StackManipulation.Compound(stackManipulation, MethodReturn.of(instrumentedMethod.getReturnType()));
}
 
Example 4
Source File: FieldAccessor.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) {
    if (!instrumentedMethod.isMethod()) {
        throw new IllegalArgumentException(instrumentedMethod + " does not describe a field getter or setter");
    }
    FieldDescription fieldDescription = fieldLocation.resolve(instrumentedMethod);
    if (!fieldDescription.isStatic() && instrumentedMethod.isStatic()) {
        throw new IllegalStateException("Cannot set instance field " + fieldDescription + " from " + instrumentedMethod);
    }
    StackManipulation implementation, initialization = fieldDescription.isStatic()
            ? StackManipulation.Trivial.INSTANCE
            : MethodVariableAccess.loadThis();
    if (!instrumentedMethod.getReturnType().represents(void.class)) {
        implementation = new StackManipulation.Compound(
                initialization,
                FieldAccess.forField(fieldDescription).read(),
                assigner.assign(fieldDescription.getType(), instrumentedMethod.getReturnType(), typing),
                MethodReturn.of(instrumentedMethod.getReturnType())
        );
    } else if (instrumentedMethod.getReturnType().represents(void.class) && instrumentedMethod.getParameters().size() == 1) {
        if (fieldDescription.isFinal() && instrumentedMethod.isMethod()) {
            throw new IllegalStateException("Cannot set final field " + fieldDescription + " from " + instrumentedMethod);
        }
        implementation = new StackManipulation.Compound(
                initialization,
                MethodVariableAccess.load(instrumentedMethod.getParameters().get(0)),
                assigner.assign(instrumentedMethod.getParameters().get(0).getType(), fieldDescription.getType(), typing),
                FieldAccess.forField(fieldDescription).write(),
                MethodReturn.VOID
        );
    } else {
        throw new IllegalArgumentException("Method " + instrumentedMethod + " is no bean accessor");
    }
    if (!implementation.isValid()) {
        throw new IllegalStateException("Cannot set or get value of " + instrumentedMethod + " using " + fieldDescription);
    }
    return new Size(implementation.apply(methodVisitor, implementationContext).getMaximalSize(), instrumentedMethod.getStackSize());
}
 
Example 5
Source File: MethodDelegationBinder.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
public StackManipulation resolve(Assigner assigner, Assigner.Typing typing, MethodDescription source, MethodDescription target) {
    return new StackManipulation.Compound(assigner.assign(target.isConstructor()
                    ? target.getDeclaringType().asGenericType()
                    : target.getReturnType(),
            source.getReturnType(),
            typing), MethodReturn.of(source.getReturnType()));
}
 
Example 6
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 7
Source File: SuperMethodCall.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
@Override
protected StackManipulation of(MethodDescription methodDescription) {
    return MethodReturn.of(methodDescription.getReturnType());
}