Example usage for com.google.common.reflect Parameter getAnnotation

List of usage examples for com.google.common.reflect Parameter getAnnotation

Introduction

In this page you can find the example usage for com.google.common.reflect Parameter getAnnotation.

Prototype

@Override
    @Nullable
    public <A extends Annotation> A getAnnotation(Class<A> annotationType) 

Source Link

Usage

From source file:org.jclouds.rest.internal.RestAnnotationProcessor.java

private static List<Part> getParts(Invocation invocation, Multimap<String, ?> tokenValues) {
    ImmutableList.Builder<Part> parts = ImmutableList.<Part>builder();
    for (Parameter param : parametersWithAnnotation(invocation.getInvokable(), PartParam.class)) {
        PartParam partParam = param.getAnnotation(PartParam.class);
        PartOptions options = new PartOptions();
        if (!PartParam.NO_CONTENT_TYPE.equals(partParam.contentType()))
            options.contentType(partParam.contentType());
        if (!PartParam.NO_FILENAME.equals(partParam.filename()))
            options.filename(replaceTokens(partParam.filename(), tokenValues));
        Object arg = invocation.getArgs().get(param.hashCode());
        checkNotNull(arg, partParam.name());
        Part part = Part.create(partParam.name(), newPayload(arg), options);
        parts.add(part);//from   w ww.  j av  a2s  .  c  om
    }
    return parts.build();
}

From source file:org.jclouds.rest.internal.RestAnnotationProcessor.java

@VisibleForTesting
static URI getEndpointInParametersOrNull(Invocation invocation, Injector injector) {
    Collection<Parameter> endpointParams = parametersWithAnnotation(invocation.getInvokable(),
            EndpointParam.class);
    if (endpointParams.isEmpty())
        return null;
    checkState(endpointParams.size() == 1, "invocation.getInvoked() %s has too many EndpointParam annotations",
            invocation.getInvokable());/*from  w w w.ja v a2s . c o  m*/
    Parameter endpointParam = get(endpointParams, 0);
    Function<Object, URI> parser = injector
            .getInstance(endpointParam.getAnnotation(EndpointParam.class).parser());
    int position = endpointParam.hashCode();// guava issue 1243
    try {
        URI returnVal = parser.apply(invocation.getArgs().get(position));
        checkArgument(returnVal != null,
                format("endpoint for [%s] not configured for %s", position, invocation.getInvokable()));
        return returnVal;
    } catch (NullPointerException e) {
        throw new IllegalArgumentException(format("argument at index %d on invocation.getInvoked() %s was null",
                position, invocation.getInvokable()), e);
    }
}

From source file:org.jclouds.rest.InputParamValidator.java

/**
 * Returns if all the method parameters passed all of their corresponding validators or throws an
 * {@link IllegalArgumentException}./*from  w w  w . j av a 2 s  . c  om*/
 * 
 * @param parameters
 *           annotations for method's arguments
 * @param args
 *           arguments that correspond to the array of annotations
 */
private void performParameterValidation(Invocation invocation) {
    for (Parameter param : invocation.getInvokable().getParameters()) {
        ParamValidators annotation = param.getAnnotation(ParamValidators.class);
        if (annotation == null)
            continue;
        List<Validator<?>> parameterValidators = getValidatorsFromAnnotation(annotation);
        // TODO position guava issue 1243
        runPredicatesAgainstArg(parameterValidators, invocation.getArgs().get(param.hashCode()));
    }
}

From source file:org.jclouds.rest.internal.RestAnnotationProcessor.java

private Multimap<String, Object> getPathParamKeyValues(Invocation invocation) {
    Multimap<String, Object> pathParamValues = LinkedHashMultimap.create();
    for (Parameter param : parametersWithAnnotation(invocation.getInvokable(), PathParam.class)) {
        PathParam pathParam = param.getAnnotation(PathParam.class);
        String paramKey = pathParam.value();
        Optional<?> paramValue = getParamValue(invocation, param.getAnnotation(ParamParser.class),
                param.hashCode(), paramKey);
        if (paramValue.isPresent())
            pathParamValues.put(paramKey, paramValue.get().toString());
    }//from  ww  w. j av a2s .c o  m
    return pathParamValues;
}

From source file:org.jclouds.rest.internal.RestAnnotationProcessor.java

private Multimap<String, Object> getFormParamKeyValues(Invocation invocation) {
    Multimap<String, Object> formParamValues = LinkedHashMultimap.create();
    for (Parameter param : parametersWithAnnotation(invocation.getInvokable(), FormParam.class)) {
        FormParam formParam = param.getAnnotation(FormParam.class);
        String paramKey = formParam.value();
        Optional<?> paramValue = getParamValue(invocation, param.getAnnotation(ParamParser.class),
                param.hashCode(), paramKey);
        if (paramValue.isPresent())
            formParamValues.put(paramKey, paramValue.get().toString());
    }/*from   w  w w.  j a  va2  s .  c  o  m*/
    return formParamValues;
}

From source file:org.jclouds.rest.internal.RestAnnotationProcessor.java

private Multimap<String, String> buildHeaders(Multimap<String, ?> tokenValues, Invocation invocation) {
    Multimap<String, String> headers = LinkedHashMultimap.create();
    addHeaderIfAnnotationPresentOnMethod(headers, invocation, tokenValues);
    for (Parameter headerParam : parametersWithAnnotation(invocation.getInvokable(), HeaderParam.class)) {
        Annotation key = headerParam.getAnnotation(HeaderParam.class);
        String value = invocation.getArgs().get(headerParam.hashCode()).toString();
        value = replaceTokens(value, tokenValues);
        headers.put(((HeaderParam) key).value(), value);
    }/* ww  w  . j av a 2  s  .  c om*/
    addProducesIfPresentOnTypeOrMethod(headers, invocation);
    addConsumesIfPresentOnTypeOrMethod(headers, invocation);
    return headers;
}

From source file:org.jclouds.rest.internal.RestAnnotationProcessor.java

private Map<String, Object> buildPayloadParams(Invocation invocation) {
    Map<String, Object> payloadParamValues = Maps.newLinkedHashMap();
    for (Parameter param : parametersWithAnnotation(invocation.getInvokable(), PayloadParam.class)) {
        PayloadParam payloadParam = param.getAnnotation(PayloadParam.class);
        String paramKey = payloadParam.value();
        Optional<?> paramValue = getParamValue(invocation, param.getAnnotation(ParamParser.class),
                param.hashCode(), paramKey);
        if (paramValue.isPresent())
            payloadParamValues.put(paramKey, paramValue.get());
    }/*w ww.j a va 2  s . co  m*/
    return payloadParamValues;
}

From source file:org.jclouds.rest.internal.RestAnnotationProcessor.java

private Multimap<String, Object> getQueryParamKeyValues(Invocation invocation) {
    Multimap<String, Object> queryParamValues = LinkedHashMultimap.create();
    for (Parameter param : parametersWithAnnotation(invocation.getInvokable(), QueryParam.class)) {
        QueryParam queryParam = param.getAnnotation(QueryParam.class);
        String paramKey = queryParam.value();
        Optional<?> paramValue = getParamValue(invocation, param.getAnnotation(ParamParser.class),
                param.hashCode(), paramKey);
        if (paramValue.isPresent())
            if (paramValue.get() instanceof Iterable) {
                @SuppressWarnings("unchecked")
                Iterable<String> iterableStrings = transform(Iterable.class.cast(paramValue.get()),
                        toStringFunction());
                queryParamValues.putAll(paramKey, iterableStrings);
            } else {
                queryParamValues.put(paramKey, paramValue.get().toString());
            }// w w w . j a  va 2  s. c o m
    }
    return queryParamValues;
}

From source file:org.jclouds.rest.internal.RestAnnotationProcessor.java

private GeneratedHttpRequest decorateRequest(GeneratedHttpRequest request) throws NegativeArraySizeException {
    Invocation invocation = request.getInvocation();
    List<Object> args = request.getInvocation().getArgs();
    Set<Parameter> binderOrWrapWith = ImmutableSet
            .copyOf(concat(parametersWithAnnotation(invocation.getInvokable(), BinderParam.class),
                    parametersWithAnnotation(invocation.getInvokable(), WrapWith.class)));
    OUTER: for (Parameter entry : binderOrWrapWith) {
        int position = entry.hashCode();
        boolean shouldBreak = false;
        Binder binder;/*  w w w  .  j ava  2 s .c  o  m*/
        if (entry.isAnnotationPresent(BinderParam.class))
            binder = injector.getInstance(entry.getAnnotation(BinderParam.class).value());
        else
            binder = injector.getInstance(BindToJsonPayloadWrappedWith.Factory.class)
                    .create(entry.getAnnotation(WrapWith.class).value());
        Object arg = args.size() >= position + 1 ? args.get(position) : null;
        if (args.size() >= position + 1 && arg != null) {
            Class<?> parameterType = entry.getType().getRawType();
            Class<? extends Object> argType = arg.getClass();
            if (!argType.isArray() && parameterType.isArray()) {// TODO: &&
                                                                // invocation.getInvokable().isVarArgs())
                                                                // {
                int arrayLength = args.size() - invocation.getInvokable().getParameters().size() + 1;
                if (arrayLength == 0)
                    break OUTER;
                arg = (Object[]) Array.newInstance(arg.getClass(), arrayLength);
                System.arraycopy(args.toArray(), position, arg, 0, arrayLength);
                shouldBreak = true;
            } else if (argType.isArray() && parameterType.isArray()) {// TODO:
                                                                      // &&
                                                                      // invocation.getInvokable().isVarArgs())
                                                                      // {
            } else {
                if (arg.getClass().isArray()) {
                    Object[] payloadArray = (Object[]) arg;
                    arg = payloadArray.length > 0 ? payloadArray[0] : null;
                }
            }
            if (arg != null) {
                request = binder.bindToRequest(request, arg);
            }
            if (shouldBreak)
                break OUTER;
        } else {
            if (position + 1 == invocation.getInvokable().getParameters().size() && entry.getType().isArray())// TODO:
                                                                                                              // &&
                                                                                                              // invocation.getInvokable().isVarArgs())
                continue OUTER;

            if (entry.isAnnotationPresent(Nullable.class)) {
                continue OUTER;
            }
            checkNotNull(arg, invocation.getInvokable().getName() + " parameter " + (position + 1));
        }
    }
    return request;
}