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

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

Introduction

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

Prototype

<T extends Annotation> T getAnnotation(Class<T> annotationClass);

Source Link

Document

Returns this element's annotation for the specified type if such an annotation is present, else null.

Usage

From source file:org.jclouds.aws.filters.FormSignerUtils.java

private static Optional<String> getAnnotatedApiVersion(Invocation invocation) {
    final Invokable<?, ?> invokable = invocation.getInvokable();
    if (invokable.isAnnotationPresent(ApiVersionOverride.class)) {
        return Optional.fromNullable(invokable.getAnnotation(ApiVersionOverride.class).value());
    } else {//w  w w .j  a v  a2  s.  c  o  m
        final Class<?> owner = invokable.getOwnerType().getRawType();
        if (owner.isAnnotationPresent(ApiVersionOverride.class)) {
            return Optional.fromNullable(owner.getAnnotation(ApiVersionOverride.class).value());
        }
    }
    return Optional.absent();
}

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

static Class<? extends HandlerWithResult<?>> getSaxResponseParserClassOrNull(Invokable<?, ?> invoked) {
    XMLResponseParser annotation = invoked.getAnnotation(XMLResponseParser.class);
    if (annotation != null) {
        return annotation.value();
    }//from  w w w .  ja  v a  2s. com
    return null;
}

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

@SuppressWarnings("unchecked")
private static Key<? extends Function<HttpResponse, ?>> getJAXBParserKeyForMethod(Invokable<?, ?> invoked) {
    Optional<Type> configuredReturnVal = Optional.absent();
    if (invoked.isAnnotationPresent(JAXBResponseParser.class)) {
        Type configuredClass = invoked.getAnnotation(JAXBResponseParser.class).value();
        configuredReturnVal = configuredClass.equals(NullType.class) ? Optional.<Type>absent()
                : Optional.<Type>of(configuredClass);
    }//from  w ww. j a  v a  2  s  .c  o m
    Type returnVal = configuredReturnVal.or(getReturnTypeFor(invoked.getReturnType()));
    Type parserType = newParameterizedType(ParseXMLWithJAXB.class, returnVal);
    return (Key<? extends Function<HttpResponse, ?>>) Key.get(parserType);
}

From source file:org.jclouds.rest.config.ReadAnnotationsAndProperties.java

@Override
public String getCommandName(Invocation invocation) {
    Invokable<?, ?> invoked = invocation.getInvokable();
    if (invoked.isAnnotationPresent(Named.class)) {
        return invoked.getAnnotation(Named.class).value();
    } else {//from  ww  w  . j ava 2 s.com
        // TODO: remove old logic once Named annotations are on all methods
        String className = invoked.getOwnerType().getRawType().getSimpleName().replace("AsyncClient", "Client")
                .replace("AsyncApi", "Api");
        return className + "." + invoked.getName();
    }
}

From source file:org.jclouds.rest.binders.BindMapToStringPayload.java

@SuppressWarnings("unchecked")
@Override/*from ww w . j  a  v  a 2  s.c  o m*/
public <R extends HttpRequest> R bindToRequest(R request, Map<String, Object> postParams) {
    checkNotNull(postParams, "postParams");
    GeneratedHttpRequest r = GeneratedHttpRequest.class.cast(checkNotNull(request, "request"));
    Invokable<?, ?> invoked = r.getInvocation().getInvokable();
    checkArgument(invoked.isAnnotationPresent(Payload.class),
            "method %s must have @Payload annotation to use this binder", invoked);
    String payload = invoked.getAnnotation(Payload.class).value();
    if (postParams.size() > 0) {
        payload = urlDecode(expand(payload, postParams));
    }
    return (R) request.toBuilder().payload(payload).build();
}

From source file:org.jclouds.oauth.v2.functions.BuildTokenRequest.java

protected String getOAuthScopes(GeneratedHttpRequest request) {
    Invokable<?, ?> invokable = request.getInvocation().getInvokable();

    OAuthScopes classScopes = invokable.getOwnerType().getRawType().getAnnotation(OAuthScopes.class);
    OAuthScopes methodScopes = invokable.getAnnotation(OAuthScopes.class);

    // if no annotations are present the rely on globally set scopes
    if (classScopes == null && methodScopes == null) {
        checkState(globalScopes != null,
                String.format("REST class or method should be annotated "
                        + "with OAuthScopes specifying required permissions. Alternatively a global property "
                        + "\"oauth.scopes\" may be set to define scopes globally. REST Class: %s, Method: %s",
                        invokable.getOwnerType(), invokable.getName()));
        return globalScopes;
    }//from   www . j  av  a  2s .  c om

    OAuthScopes scopes = methodScopes != null ? methodScopes : classScopes;
    return Joiner.on(",").join(scopes.value());
}

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

private Optional<Long> timeoutInNanos(Invokable<?, ?> invoked, Map<String, Long> timeouts) {
    Optional<Long> defaultMillis = fromNullable(timeouts.get("default"));
    Optional<Long> timeoutMillis;
    if (invoked.isAnnotationPresent(Named.class)) {
        String commandName = invoked.getAnnotation(Named.class).value();
        timeoutMillis = fromNullable(timeouts.get(commandName)).or(defaultMillis);
    } else {/*from ww  w .  j  av a 2 s . c om*/
        // TODO: remove old logic, once Named annotations are present on all methods
        String className = invoked.getOwnerType().getRawType().getSimpleName().replace("AsyncClient", "Client")
                .replace("AsyncApi", "Api");
        timeoutMillis = fromNullable(timeouts.get(className + "." + invoked.getName()))
                .or(fromNullable(timeouts.get(className))).or(defaultMillis);
    }
    if (timeoutMillis.isPresent())
        return Optional.of(TimeUnit.MILLISECONDS.toNanos(timeoutMillis.get()));
    return Optional.absent();
}

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

@SuppressWarnings("unchecked")
@VisibleForTesting/*from  w w w.j ava 2 s. co m*/
protected Key<? extends Function<HttpResponse, ?>> getParserOrThrowException(Invocation invocation) {
    Invokable<?, ?> invoked = invocation.getInvokable();
    Set<String> acceptHeaders = getAcceptHeaders.apply(invocation);
    ResponseParser annotation = invoked.getAnnotation(ResponseParser.class);
    Class<?> rawReturnType = invoked.getReturnType().getRawType();
    if (annotation == null) {
        if (rawReturnType.equals(void.class) || invoked.getReturnType().equals(futureVoidToken)) {
            return Key.get(ReleasePayloadAndReturn.class);
        } else if (rawReturnType.equals(boolean.class) || rawReturnType.equals(Boolean.class)
                || invoked.getReturnType().equals(futureBooleanToken)) {
            return Key.get(ReturnTrueIf2xx.class);
        } else if (rawReturnType.equals(InputStream.class)
                || invoked.getReturnType().equals(futureInputStreamToken)) {
            return Key.get(ReturnInputStream.class);
        } else if (rawReturnType.equals(HttpResponse.class)
                || invoked.getReturnType().equals(futureHttpResponseToken)) {
            return Key.get(Class.class.cast(IdentityFunction.class));
        } else if (acceptHeaders.contains(APPLICATION_JSON)) {
            return getJsonParserKeyForMethod(invoked);
        } else if (acceptHeaders.contains(APPLICATION_XML)
                || invoked.isAnnotationPresent(JAXBResponseParser.class)) {
            return getJAXBParserKeyForMethod(invoked);
        } else if (rawReturnType.equals(String.class) || invoked.getReturnType().equals(futureStringToken)) {
            return Key.get(ReturnStringIf2xx.class);
        } else if (rawReturnType.equals(URI.class) || invoked.getReturnType().equals(futureURIToken)) {
            return Key.get(ParseURIFromListOrLocationHeaderIf20x.class);
        } else {
            throw new IllegalStateException(
                    "You must specify a ResponseParser annotation on: " + invoked.toString());
        }
    }
    return Key.get(annotation.value());
}

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

@VisibleForTesting
@SuppressWarnings({ "rawtypes", "unchecked" })
public Function<HttpResponse, ?> getTransformerForMethod(Invocation invocation, Injector injector) {
    Invokable<?, ?> invoked = invocation.getInvokable();
    Function<HttpResponse, ?> transformer;
    if (invoked.isAnnotationPresent(SelectJson.class)) {
        Type returnVal = getReturnTypeFor(invoked.getReturnType());
        if (invoked.isAnnotationPresent(OnlyElement.class))
            returnVal = newParameterizedType(Set.class, returnVal);
        transformer = new ParseFirstJsonValueNamed(injector.getInstance(GsonWrapper.class),
                TypeLiteral.get(returnVal), invoked.getAnnotation(SelectJson.class).value());
        if (invoked.isAnnotationPresent(OnlyElement.class))
            transformer = compose(new OnlyElementOrNull(), transformer);
    } else {//w ww  . j a  va  2s  .  com
        transformer = injector.getInstance(getParserOrThrowException(invocation));
    }
    return transformer;
}