Java Code Examples for net.bytebuddy.implementation.bytecode.member.MethodVariableAccess#loadThis()

The following examples show how to use net.bytebuddy.implementation.bytecode.member.MethodVariableAccess#loadThis() . 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 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public StackManipulation toStackManipulation(MethodDescription invokedMethod, Assigner assigner, Assigner.Typing typing) {
    if (instrumentedMethod.isStatic() && !invokedMethod.isStatic() && !invokedMethod.isConstructor()) {
        throw new IllegalStateException("Cannot invoke " + invokedMethod + " from " + instrumentedMethod);
    } else if (invokedMethod.isConstructor() && (!instrumentedMethod.isConstructor()
            || !instrumentedType.equals(invokedMethod.getDeclaringType().asErasure())
            && !instrumentedType.getSuperClass().asErasure().equals(invokedMethod.getDeclaringType().asErasure()))) {
        throw new IllegalStateException("Cannot invoke " + invokedMethod + " from " + instrumentedMethod + " in " + instrumentedType);
    }
    return new StackManipulation.Compound(
            invokedMethod.isStatic()
                    ? StackManipulation.Trivial.INSTANCE
                    : MethodVariableAccess.loadThis(),
            invokedMethod.isConstructor()
                    ? Duplication.SINGLE
                    : StackManipulation.Trivial.INSTANCE
    );
}
 
Example 3
Source File: MethodCallProxy.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) {
    FieldList<?> fieldList = instrumentedType.getDeclaredFields();
    StackManipulation[] fieldLoading = new StackManipulation[fieldList.size()];
    int index = 0;
    for (FieldDescription fieldDescription : fieldList) {
        fieldLoading[index] = new StackManipulation.Compound(
                MethodVariableAccess.loadThis(),
                MethodVariableAccess.load(instrumentedMethod.getParameters().get(index)),
                FieldAccess.forField(fieldDescription).write()
        );
        index++;
    }
    StackManipulation.Size stackSize = new StackManipulation.Compound(
            MethodVariableAccess.loadThis(),
            MethodInvocation.invoke(ConstructorCall.INSTANCE.objectTypeDefaultConstructor),
            new StackManipulation.Compound(fieldLoading),
            MethodReturn.VOID
    ).apply(methodVisitor, implementationContext);
    return new Size(stackSize.getMaximalSize(), instrumentedMethod.getStackSize());
}
 
Example 4
Source File: EqualsMethod.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public ByteCodeAppender appender(Target implementationTarget) {
    if (implementationTarget.getInstrumentedType().isInterface()) {
        throw new IllegalStateException("Cannot implement meaningful equals method for " + implementationTarget.getInstrumentedType());
    }
    List<FieldDescription.InDefinedShape> fields = new ArrayList<FieldDescription.InDefinedShape>(implementationTarget.getInstrumentedType()
            .getDeclaredFields()
            .filter(not(isStatic().or(ignored))));
    Collections.sort(fields, comparator);
    return new Appender(implementationTarget.getInstrumentedType(), new StackManipulation.Compound(
            superClassCheck.resolve(implementationTarget.getInstrumentedType()),
            MethodVariableAccess.loadThis(),
            MethodVariableAccess.REFERENCE.loadFrom(1),
            ConditionalReturn.onIdentity().returningTrue(),
            typeCompatibilityCheck.resolve(implementationTarget.getInstrumentedType())
    ), fields, nonNullable);
}
 
Example 5
Source File: FieldAccessor.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
protected StackManipulation resolve(FieldLocation.Prepared target,
                                    FieldDescription fieldDescription,
                                    TypeDescription instrumentedType,
                                    MethodDescription instrumentedMethod) {
    FieldDescription resolved = target.resolve(instrumentedMethod);
    if (!resolved.isStatic() && instrumentedMethod.isStatic()) {
        throw new IllegalStateException("Cannot set instance field " + fieldDescription + " from " + instrumentedMethod);
    }
    return new StackManipulation.Compound(
            resolved.isStatic()
                    ? StackManipulation.Trivial.INSTANCE
                    : MethodVariableAccess.loadThis(),
            FieldAccess.forField(resolved).read(),
            assigner.assign(resolved.getType(), fieldDescription.getType(), typing)
    );
}
 
Example 6
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 7
Source File: MethodDelegation.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public StackManipulation prepare(MethodDescription instrumentedMethod) {
    if (instrumentedMethod.isStatic() && !fieldDescription.isStatic()) {
        throw new IllegalStateException("Cannot read " + fieldDescription + " from " + instrumentedMethod);
    }
    return new StackManipulation.Compound(fieldDescription.isStatic()
            ? StackManipulation.Trivial.INSTANCE
            : MethodVariableAccess.loadThis(), FieldAccess.forField(fieldDescription).read());
}
 
Example 8
Source File: MethodDelegation.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public StackManipulation prepare(MethodDescription instrumentedMethod) {
    if (instrumentedMethod.isStatic() && !methodDescription.isStatic()) {
        throw new IllegalStateException("Cannot invoke " + methodDescription + " from " + instrumentedMethod);
    }
    return new StackManipulation.Compound(methodDescription.isStatic()
            ? StackManipulation.Trivial.INSTANCE
            : MethodVariableAccess.loadThis(), MethodInvocation.invoke(methodDescription));
}
 
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(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 11
Source File: HashCodeMethod.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public StackManipulation resolve(TypeDescription instrumentedType) {
    TypeDefinition superClass = instrumentedType.getSuperClass();
    if (superClass == null) {
        throw new IllegalStateException(instrumentedType + " does not declare a super class");
    }
    return new StackManipulation.Compound(MethodVariableAccess.loadThis(), MethodInvocation.invoke(HASH_CODE).special(superClass.asErasure()));
}
 
Example 12
Source File: EqualsMethod.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Override
protected StackManipulation resolve(TypeDescription instrumentedType) {
    TypeDefinition superClass = instrumentedType.getSuperClass();
    if (superClass == null) {
        throw new IllegalStateException(instrumentedType + " does not declare a super class");
    }
    return new StackManipulation.Compound(MethodVariableAccess.loadThis(),
            MethodVariableAccess.REFERENCE.loadFrom(1),
            MethodInvocation.invoke(EQUALS).special(superClass.asErasure()),
            ConditionalReturn.onZeroInteger());
}
 
Example 13
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 14
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) {
    if (instrumentedMethod.isStatic()) {
        throw new IllegalStateException("Cannot get this instance from static method: " + instrumentedMethod);
    } else if (!instrumentedType.isAssignableTo(typeDescription)) {
        throw new IllegalStateException(instrumentedType + " is not assignable to " + instrumentedType);
    }
    return new Resolved.Simple(MethodVariableAccess.loadThis(), typeDescription);
}
 
Example 15
Source File: MethodCall.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public StackManipulation prepare() {
    return fieldDescription.isStatic()
            ? StackManipulation.Trivial.INSTANCE
            : MethodVariableAccess.loadThis();
}