Java Code Examples for net.bytebuddy.description.method.MethodDescription#getParameters()

The following examples show how to use net.bytebuddy.description.method.MethodDescription#getParameters() . 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
/**
 * Resolves this appender to a stack manipulation.
 *
 * @param instrumentedMethod The instrumented method.
 * @param invokedMethod      The invoked method.
 * @param targetHandler      The resolved target handler to base the stack manipulation upon.
 * @return A stack manipulation that represents this method call.
 */
protected StackManipulation toStackManipulation(MethodDescription instrumentedMethod, MethodDescription invokedMethod, TargetHandler.Resolved targetHandler) {
    List<ArgumentLoader> argumentLoaders = new ArrayList<ArgumentLoader>();
    for (ArgumentLoader.ArgumentProvider argumentProvider : argumentProviders) {
        argumentLoaders.addAll(argumentProvider.resolve(instrumentedMethod, invokedMethod));
    }
    ParameterList<?> parameters = invokedMethod.getParameters();
    if (parameters.size() != argumentLoaders.size()) {
        throw new IllegalStateException(invokedMethod + " does not accept " + argumentLoaders.size() + " arguments");
    }
    Iterator<? extends ParameterDescription> parameterIterator = parameters.iterator();
    List<StackManipulation> argumentInstructions = new ArrayList<StackManipulation>(argumentLoaders.size());
    for (ArgumentLoader argumentLoader : argumentLoaders) {
        argumentInstructions.add(argumentLoader.toStackManipulation(parameterIterator.next(), assigner, typing));
    }
    return new StackManipulation.Compound(
            targetHandler.toStackManipulation(invokedMethod, assigner, typing),
            new StackManipulation.Compound(argumentInstructions),
            methodInvoker.toStackManipulation(invokedMethod, implementationTarget),
            terminationHandler.toStackManipulation(invokedMethod, instrumentedMethod, assigner, typing)
    );
}
 
Example 2
Source File: TemplateModelProxyHandler.java    From flow with Apache License 2.0 5 votes vote down vote up
private static boolean isAccessor(MethodDescription method) {
    if (method.getDeclaringType().represents(Object.class)) {
        return false;
    }
    String methodName = method.getName();
    Generic returnType = method.getReturnType();
    ParameterList<?> args = method.getParameters();

    boolean isSetter = Generic.VOID.equals(returnType) && args.size() == 1
            && ReflectTools.isSetterName(methodName);
    boolean isGetter = !Generic.VOID.equals(returnType) && args.isEmpty()
            && ReflectTools.isGetterName(methodName,
                    returnType.represents(boolean.class));
    return isSetter || isGetter;
}
 
Example 3
Source File: CopierImplementation.java    From unsafe with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void checkMethodSignature(MethodDescription instrumentedMethod) {
  final String errMessage = "%s must have signature `void copy(java.lang.Object, long)`";
  Preconditions.checkArgument(instrumentedMethod.getReturnType().represents(void.class),
      errMessage, instrumentedMethod);

  ParameterList parameters = instrumentedMethod.getParameters();
  Preconditions.checkArgument(parameters.size() == 2, errMessage, instrumentedMethod);

  Preconditions.checkArgument(parameters.get(0).getTypeDescription().represents(Object.class),
      errMessage, instrumentedMethod);

  Preconditions.checkArgument(parameters.get(1).getTypeDescription().represents(long.class),
      errMessage, instrumentedMethod);
}
 
Example 4
Source File: ArgumentTypeNameMatch.java    From skywalking with Apache License 2.0 5 votes vote down vote up
/**
 * Match the target method.
 *
 * @param target target method description.
 * @return true if matched. or false.
 */
@Override
public boolean matches(MethodDescription target) {
    ParameterList<?> parameters = target.getParameters();
    if (parameters.size() > index) {
        return parameters.get(index).getType().asErasure().getName().equals(argumentTypeName);
    }

    return false;
}
 
Example 5
Source File: MethodAttributeAppender.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a method attribute appender factory that writes all annotations that are defined for every parameter
 * of the given method.
 *
 * @param methodDescription The method from which to extract the parameter annotations.
 * @return A method attribute appender factory for an appender that writes all parameter annotations of the supplied method.
 */
public static Factory ofParameterAnnotations(MethodDescription methodDescription) {
    ParameterList<?> parameters = methodDescription.getParameters();
    List<MethodAttributeAppender.Factory> factories = new ArrayList<MethodAttributeAppender.Factory>(parameters.size());
    for (ParameterDescription parameter : parameters) {
        factories.add(new Explicit(parameter.getIndex(), parameter.getDeclaredAnnotations()));
    }
    return new Factory.Compound(factories);
}
 
Example 6
Source File: MethodCall.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public List<ArgumentLoader> resolve(MethodDescription instrumentedMethod, MethodDescription invokedMethod) {
    List<ArgumentLoader> argumentLoaders = new ArrayList<ArgumentLoader>(instrumentedMethod.getParameters().size());
    for (ParameterDescription parameterDescription : instrumentedMethod.getParameters()) {
        argumentLoaders.add(new ForMethodParameter(parameterDescription.getIndex(), instrumentedMethod));
    }
    return argumentLoaders;
}
 
Example 7
Source File: MethodCallProxy.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a linked hash map of field names to their types where each field represents a parameter of the method.
 *
 * @param methodDescription The method to extract into fields.
 * @return A map of fields in the order they need to be loaded onto the operand stack for invoking the original
 * method, including a reference to the instance of the instrumented type that is invoked if applicable.
 */
private static LinkedHashMap<String, TypeDescription> extractFields(MethodDescription methodDescription) {
    LinkedHashMap<String, TypeDescription> typeDescriptions = new LinkedHashMap<String, TypeDescription>();
    int currentIndex = 0;
    if (!methodDescription.isStatic()) {
        typeDescriptions.put(fieldName(currentIndex++), methodDescription.getDeclaringType().asErasure());
    }
    for (ParameterDescription parameterDescription : methodDescription.getParameters()) {
        typeDescriptions.put(fieldName(currentIndex++), parameterDescription.getType().asErasure());
    }
    return typeDescriptions;
}
 
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) {
    ParameterList<?> parameters = instrumentedMethod.getParameters();
    if (index >= parameters.size()) {
        throw new IllegalStateException("No parameter " + index + " for " + instrumentedMethod);
    }
    return doResolve(MethodVariableAccess.load(parameters.get(index)), parameters.get(index).getType(), assigner, typing);
}
 
Example 9
Source File: TargetMethodAnnotationDrivenBinder.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public MethodDelegationBinder.Record compile(MethodDescription candidate) {
    if (IgnoreForBinding.Verifier.check(candidate)) {
        return MethodDelegationBinder.Record.Illegal.INSTANCE;
    }
    List<DelegationProcessor.Handler> handlers = new ArrayList<DelegationProcessor.Handler>(candidate.getParameters().size());
    for (ParameterDescription parameterDescription : candidate.getParameters()) {
        handlers.add(delegationProcessor.prepare(parameterDescription));
    }
    return new Record(candidate, handlers, RuntimeType.Verifier.check(candidate));
}