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

The following examples show how to use net.bytebuddy.implementation.bytecode.assign.Assigner#assign() . 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: 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) {
    if (!fieldDescription.isStatic() && instrumentedMethod.isStatic()) {
        throw new IllegalStateException("Cannot access non-static " + fieldDescription + " from " + instrumentedMethod);
    }
    StackManipulation stackManipulation = new StackManipulation.Compound(
            fieldDescription.isStatic()
                    ? StackManipulation.Trivial.INSTANCE
                    : MethodVariableAccess.loadThis(),
            FieldAccess.forField(fieldDescription).read(),
            assigner.assign(fieldDescription.getType(), target.getType(), typing)
    );
    if (!stackManipulation.isValid()) {
        throw new IllegalStateException("Cannot assign " + fieldDescription + " to " + target);
    }
    return stackManipulation;
}
 
Example 2
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 3
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 4
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 5
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 6
Source File: PrimitiveUnboxingDelegate.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public StackManipulation assignUnboxedTo(TypeDescription.Generic target, Assigner assigner, Assigner.Typing typing) {
    PrimitiveUnboxingDelegate primitiveUnboxingDelegate = PrimitiveUnboxingDelegate.forPrimitive(target);
    return new Compound(
            assigner.assign(originalType, primitiveUnboxingDelegate.getWrapperType(), typing),
            primitiveUnboxingDelegate);
}
 
Example 7
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 8
Source File: VoidAwareAssignerVoidToNonVoidTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testAssignDefaultValue() throws Exception {
    Assigner voidAwareAssigner = new VoidAwareAssigner(chainedAssigner);
    StackManipulation stackManipulation = voidAwareAssigner.assign(source, target, Assigner.Typing.DYNAMIC);
    assertThat(stackManipulation.isValid(), is(true));
    StackManipulation.Size size = stackManipulation.apply(methodVisitor, implementationContext);
    assertThat(size.getSizeImpact(), is(StackSize.of(targetType).getSize()));
    assertThat(size.getMaximalSize(), is(StackSize.of(targetType).getSize()));
    verify(methodVisitor).visitInsn(opcode);
    verifyNoMoreInteractions(methodVisitor);
}
 
Example 9
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) {
    StackManipulation stackManipulation = assigner.assign(methodDescription.getReturnType(), invokedMethod.getDeclaringType().asGenericType(), typing);
    if (!stackManipulation.isValid()) {
        throw new IllegalStateException("Cannot invoke " + invokedMethod + " on " + methodDescription.getReturnType());
    }
    return new StackManipulation.Compound(appender.toStackManipulation(instrumentedMethod,
            methodDescription,
            targetHandler), 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(MethodDescription invokedMethod, Assigner assigner, Assigner.Typing typing) {
    StackManipulation stackManipulation = assigner.assign(parameterDescription.getType(), invokedMethod.getDeclaringType().asGenericType(), typing);
    if (!stackManipulation.isValid()) {
        throw new IllegalStateException("Cannot invoke " + invokedMethod + " on " + parameterDescription.getType());
    }
    return new StackManipulation.Compound(MethodVariableAccess.load(parameterDescription), stackManipulation);
}
 
Example 11
Source File: VoidAwareAssignerTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testAssignNonVoidToNonVoid() throws Exception {
    Assigner voidAwareAssigner = new VoidAwareAssigner(chainedAssigner);
    StackManipulation chainedStackManipulation = mock(StackManipulation.class);
    when(chainedAssigner.assign(sourceTypeDescription, targetTypeDescription, Assigner.Typing.STATIC)).thenReturn(chainedStackManipulation);
    StackManipulation stackManipulation = voidAwareAssigner.assign(sourceTypeDescription, targetTypeDescription, Assigner.Typing.STATIC);
    assertThat(stackManipulation, is(chainedStackManipulation));
    verify(chainedAssigner).assign(sourceTypeDescription, targetTypeDescription, Assigner.Typing.STATIC);
    verifyNoMoreInteractions(chainedAssigner);
}
 
Example 12
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) {
    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(
            FieldAccess.forField(fieldDescription).read(),
            stackManipulation
    );
}
 
Example 13
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 assignment = assigner.assign(typeDefinition.asGenericType(), target.getType(), typing);
    if (!assignment.isValid()) {
        throw new IllegalStateException("Cannot assign " + target + " to " + typeDefinition);
    }
    return new StackManipulation.Compound(stackManipulation, assignment);
}
 
Example 14
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(
            appender.toStackManipulation(instrumentedMethod, methodDescription, targetHandler),
            assigner.assign(methodDescription.isConstructor()
                    ? methodDescription.getDeclaringType().asGenericType()
                    : methodDescription.getReturnType(), target.getType(), typing)
    );
    if (!stackManipulation.isValid()) {
        throw new IllegalStateException("Cannot assign return type of " + methodDescription + " to " + target);
    }
    return stackManipulation;
}
 
Example 15
Source File: VoidAwareAssignerVoidToNonVoidTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testAssignNoDefaultValue() throws Exception {
    Assigner voidAwareAssigner = new VoidAwareAssigner(chainedAssigner);
    StackManipulation stackManipulation = voidAwareAssigner.assign(source, target, Assigner.Typing.STATIC);
    assertThat(stackManipulation.isValid(), is(false));
    stackManipulation.apply(methodVisitor, implementationContext);
}
 
Example 16
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.load(parameterDescription),
            IntegerConstant.forValue(index),
            ArrayAccess.of(parameterDescription.getType().getComponentType()).load(),
            assigner.assign(parameterDescription.getType().getComponentType(), target.getType(), typing)
    );
    if (!stackManipulation.isValid()) {
        throw new IllegalStateException("Cannot assign " + parameterDescription.getType().getComponentType() + " to " + target);
    }
    return stackManipulation;
}
 
Example 17
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 18
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 19
Source File: InvokeDynamic.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Resolved resolve(TypeDescription instrumentedType, MethodDescription instrumentedMethod, Assigner assigner, Assigner.Typing typing) {
    return new Resolved.Simple(new StackManipulation.Compound(stackManipulation,
            assigner.assign(primitiveType.asGenericType(), wrapperType.asGenericType(), typing)), wrapperType);
}
 
Example 20
Source File: TargetMethodAnnotationDrivenBinder.java    From byte-buddy with Apache License 2.0 4 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) {
    Object value = bind(annotation, source, target);
    if (value == null) {
        return new ParameterBinding.Anonymous(DefaultValue.of(target.getType()));
    }
    StackManipulation stackManipulation;
    TypeDescription suppliedType;
    if (value instanceof Boolean) {
        stackManipulation = IntegerConstant.forValue((Boolean) value);
        suppliedType = TypeDescription.ForLoadedType.of(boolean.class);
    } else if (value instanceof Byte) {
        stackManipulation = IntegerConstant.forValue((Byte) value);
        suppliedType = TypeDescription.ForLoadedType.of(byte.class);
    } else if (value instanceof Short) {
        stackManipulation = IntegerConstant.forValue((Short) value);
        suppliedType = TypeDescription.ForLoadedType.of(short.class);
    } else if (value instanceof Character) {
        stackManipulation = IntegerConstant.forValue((Character) value);
        suppliedType = TypeDescription.ForLoadedType.of(char.class);
    } else if (value instanceof Integer) {
        stackManipulation = IntegerConstant.forValue((Integer) value);
        suppliedType = TypeDescription.ForLoadedType.of(int.class);
    } else if (value instanceof Long) {
        stackManipulation = LongConstant.forValue((Long) value);
        suppliedType = TypeDescription.ForLoadedType.of(long.class);
    } else if (value instanceof Float) {
        stackManipulation = FloatConstant.forValue((Float) value);
        suppliedType = TypeDescription.ForLoadedType.of(float.class);
    } else if (value instanceof Double) {
        stackManipulation = DoubleConstant.forValue((Double) value);
        suppliedType = TypeDescription.ForLoadedType.of(double.class);
    } else if (value instanceof String) {
        stackManipulation = new TextConstant((String) value);
        suppliedType = TypeDescription.STRING;
    } else if (value instanceof Class) {
        stackManipulation = ClassConstant.of(TypeDescription.ForLoadedType.of((Class<?>) value));
        suppliedType = TypeDescription.CLASS;
    } else if (value instanceof TypeDescription) {
        stackManipulation = ClassConstant.of((TypeDescription) value);
        suppliedType = TypeDescription.CLASS;
    } else if (JavaType.METHOD_HANDLE.isInstance(value)) {
        stackManipulation = new JavaConstantValue(JavaConstant.MethodHandle.ofLoaded(value));
        suppliedType = JavaType.METHOD_HANDLE.getTypeStub();
    } else if (value instanceof JavaConstant.MethodHandle) {
        stackManipulation = new JavaConstantValue((JavaConstant.MethodHandle) value);
        suppliedType = JavaType.METHOD_HANDLE.getTypeStub();
    } else if (JavaType.METHOD_TYPE.isInstance(value)) {
        stackManipulation = new JavaConstantValue(JavaConstant.MethodType.ofLoaded(value));
        suppliedType = JavaType.METHOD_HANDLE.getTypeStub();
    } else if (value instanceof JavaConstant.MethodType) {
        stackManipulation = new JavaConstantValue((JavaConstant.MethodType) value);
        suppliedType = JavaType.METHOD_HANDLE.getTypeStub();
    } else {
        throw new IllegalStateException("Not able to save in class's constant pool: " + value);
    }
    return new ParameterBinding.Anonymous(new StackManipulation.Compound(
            stackManipulation,
            assigner.assign(suppliedType.asGenericType(), target.getType(), typing)
    ));
}