Java Code Examples for net.bytebuddy.implementation.bytecode.member.MethodInvocation#invoke()

The following examples show how to use net.bytebuddy.implementation.bytecode.member.MethodInvocation#invoke() . 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: PropertyAccessorCollector.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
@Override
protected StackManipulation invocationOperation(
        AnnotatedMember annotatedMember, TypeDefinition beanClassDescription) {

    final String methodName = annotatedMember.getName();
    @SuppressWarnings("unchecked")
    final MethodList<MethodDescription> matchingMethods =
            (MethodList<MethodDescription>) beanClassDescription.getDeclaredMethods().filter(named(methodName));

    if (matchingMethods.size() == 1) { //method was declared on class
        return MethodInvocation.invoke(matchingMethods.getOnly());
    }
    if (matchingMethods.isEmpty()) { //method was not found on class, try super class
        return invocationOperation(annotatedMember, beanClassDescription.getSuperClass());
    }
    else { //should never happen
        throw new IllegalStateException("Could not find definition of method: " + methodName);
    }
}
 
Example 2
Source File: PropertyMutatorCollector.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
@Override
protected StackManipulation invocationOperation(AnnotatedMember annotatedMember,
        TypeDefinition beanClassDescription) {

    final String methodName = annotatedMember.getName();
    @SuppressWarnings("unchecked")
    final MethodList<MethodDescription> matchingMethods =
            (MethodList<MethodDescription>) beanClassDescription.getDeclaredMethods().filter(named(methodName));

    if (matchingMethods.size() == 1) { //method was declared on class
        return MethodInvocation.invoke(matchingMethods.getOnly());
    }
    if (matchingMethods.isEmpty()) { //method was not found on class, try super class
        return invocationOperation(annotatedMember, beanClassDescription.getSuperClass());
    }
    else { //should never happen
        throw new IllegalStateException("Could not find definition of method: " + methodName);
    }

}
 
Example 3
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 4
Source File: CodeGenUtil.java    From curiostack with MIT License 5 votes vote down vote up
/**
 * Returns a {@link StackManipulation} that returns the {@link
 * com.google.protobuf.Descriptors.EnumDescriptor} for the given enum field.
 */
static StackManipulation getEnumDescriptor(ProtoFieldInfo info) {
  Class<?> clz = info.enumClass();
  final MethodDescription.ForLoadedMethod getDescriptor;
  try {
    getDescriptor = new MethodDescription.ForLoadedMethod(clz.getDeclaredMethod("getDescriptor"));
  } catch (NoSuchMethodException e) {
    throw new IllegalStateException("Not an enum class: " + clz, e);
  }
  return MethodInvocation.invoke(getDescriptor);
}
 
Example 5
Source File: MemberSubstitution.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public StackManipulation resolve(TypeDescription targetType,
                                 ByteCodeElement target,
                                 TypeList.Generic parameters,
                                 TypeDescription.Generic result,
                                 int freeOffset) {
    MethodDescription methodDescription = methodResolver.resolve(targetType, target, parameters, result);
    if (!methodDescription.isAccessibleTo(instrumentedType)) {
        throw new IllegalStateException(instrumentedType + " cannot access " + methodDescription);
    }
    TypeList.Generic mapped = methodDescription.isStatic()
            ? methodDescription.getParameters().asTypeList()
            : new TypeList.Generic.Explicit(CompoundList.of(methodDescription.getDeclaringType(), methodDescription.getParameters().asTypeList()));
    if (!methodDescription.getReturnType().asErasure().isAssignableTo(result.asErasure())) {
        throw new IllegalStateException("Cannot assign return value of " + methodDescription + " to " + result);
    } else if (mapped.size() != parameters.size()) {
        throw new IllegalStateException("Cannot invoke " + methodDescription + " on " + parameters.size() + " parameters");
    }
    for (int index = 0; index < mapped.size(); index++) {
        if (!parameters.get(index).asErasure().isAssignableTo(mapped.get(index).asErasure())) {
            throw new IllegalStateException("Cannot invoke " + methodDescription + " on parameter " + index + " of type " + parameters.get(index));
        }
    }
    return methodDescription.isVirtual()
            ? MethodInvocation.invoke(methodDescription).virtual(mapped.get(THIS_REFERENCE).asErasure())
            : MethodInvocation.invoke(methodDescription);
}
 
Example 6
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 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, Implementation.Target implementationTarget) {
    if (invokedMethod.isVirtual() && !invokedMethod.isInvokableOn(instrumentedType)) {
        throw new IllegalStateException("Cannot invoke " + invokedMethod + " on " + instrumentedType);
    }
    return invokedMethod.isVirtual()
            ? MethodInvocation.invoke(invokedMethod).virtual(instrumentedType)
            : MethodInvocation.invoke(invokedMethod);
}
 
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, Implementation.Target implementationTarget) {
    if (!invokedMethod.isAccessibleTo(implementationTarget.getInstrumentedType()) || !invokedMethod.isVirtual()) {
        throw new IllegalStateException("Cannot invoke " + invokedMethod + " virtually");
    }
    return MethodInvocation.invoke(invokedMethod);
}
 
Example 9
Source File: TypeProxy.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the singleton instance.
 */
@SuppressFBWarnings(value = "SE_BAD_FIELD_STORE", justification = "Fields of enumerations are never serialized")
AbstractMethodErrorThrow() {
    TypeDescription abstractMethodError = TypeDescription.ForLoadedType.of(AbstractMethodError.class);
    MethodDescription constructor = abstractMethodError.getDeclaredMethods()
            .filter(isConstructor().and(takesArguments(0))).getOnly();
    implementation = new Compound(TypeCreation.of(abstractMethodError),
            Duplication.SINGLE,
            MethodInvocation.invoke(constructor),
            Throw.INSTANCE);
}
 
Example 10
Source File: EqualsMethod.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Override
public StackManipulation resolve(TypeDescription instrumentedType) {
    return new StackManipulation.Compound(
            MethodVariableAccess.REFERENCE.loadFrom(1),
            ConditionalReturn.onNullValue(),
            MethodVariableAccess.REFERENCE.loadFrom(0),
            MethodInvocation.invoke(GET_CLASS),
            MethodVariableAccess.REFERENCE.loadFrom(1),
            MethodInvocation.invoke(GET_CLASS),
            ConditionalReturn.onNonIdentity()
    );
}
 
Example 11
Source File: ExceptionMethod.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public StackManipulation make() {
    return new StackManipulation.Compound(
            TypeCreation.of(throwableType),
            Duplication.SINGLE,
            MethodInvocation.invoke(targetConstructor));
}
 
Example 12
Source File: ExceptionMethod.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public StackManipulation make() {
    return new StackManipulation.Compound(
            TypeCreation.of(throwableType),
            Duplication.SINGLE,
            new TextConstant(message),
            MethodInvocation.invoke(targetConstructor));
}
 
Example 13
Source File: CodeGenUtil.java    From curiostack with MIT License 4 votes vote down vote up
/** Returns a {@link StackManipulation} that invokes the given {@link Method}. */
static StackManipulation invoke(Method method) {
  return MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(method));
}
 
Example 14
Source File: CopierImplementation.java    From unsafe with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void buildCopyStack(List<StackManipulation> stack, int iterations, Method getMethod, Method putMethod, long stride) throws
    NoSuchFieldException, NoSuchMethodException {

  final Field unsafeField = UnsafeCopier.class.getDeclaredField("unsafe");

  final StackManipulation copyStack = new StackManipulation.Compound(
      // unsafe.putLong(dest, destOffset, unsafe.getLong(src));
      MethodVariableAccess.REFERENCE.loadOffset(0), // ALOAD 0 this

      FieldAccess.forField(new FieldDescription.ForLoadedField(unsafeField)).getter(), // GETFIELD

      MethodVariableAccess.REFERENCE.loadOffset(1), // ALOAD 1 dest
      MethodVariableAccess.LONG.loadOffset(4),      // LLOAD 4 destOffset

      MethodVariableAccess.REFERENCE.loadOffset(0), // ALOAD 0 this
      FieldAccess.forField(new FieldDescription.ForLoadedField(unsafeField)).getter(), // GETFIELD

      MethodVariableAccess.LONG.loadOffset(2),      // LLOAD 2 src

      MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(getMethod)),
      MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(putMethod))
  );

  final StackManipulation incrementStack = new StackManipulation.Compound(
      // destOffset += 8; src += 8;
      MethodVariableAccess.LONG.loadOffset(4), // LLOAD 4 destOffset
      LongConstant.forValue(stride),      // LDC 8 strideWidth
      LongAdd.INSTANCE,                        // LADD
      MethodVariableStore.LONG.storeOffset(4), // LSTORE 4

      MethodVariableAccess.LONG.loadOffset(2), // LLOAD 2 src
      LongConstant.forValue(stride),      // LDC 8 strideWidth
      LongAdd.INSTANCE,                        // LADD
      MethodVariableStore.LONG.storeOffset(2)  // LSTORE 2
  );

  for (int i = 0; i < iterations; i++) {
    stack.add(copyStack);
    stack.add(incrementStack);
  }
}
 
Example 15
Source File: MethodDelegationBinder.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public StackManipulation invoke(MethodDescription methodDescription) {
    return MethodInvocation.invoke(methodDescription);
}