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

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

Introduction

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

Prototype

@Override
    public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) 

Source Link

Usage

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

private static Collection<Parameter> parametersWithAnnotation(Invokable<?, ?> invokable,
        final Class<? extends Annotation> annotationType) {
    return filter(invokable.getParameters(), new Predicate<Parameter>() {
        public boolean apply(Parameter in) {
            return in.isAnnotationPresent(annotationType);
        }/* w w  w.  ja v a 2s  .  com*/
    });
}

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;/* ww  w . j av  a 2s  .c om*/
        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;
}