Example usage for java.lang.reflect Method getAnnotation

List of usage examples for java.lang.reflect Method getAnnotation

Introduction

In this page you can find the example usage for java.lang.reflect Method getAnnotation.

Prototype

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

Source Link

Usage

From source file:com.mmnaseri.dragonfly.metadata.impl.AnnotationTableMetadataResolver.java

private static CascadeMetadata getCascadeMetadata(Method method) {
    final List<CascadeType> cascadeTypes = new ArrayList<CascadeType>();
    if (method.isAnnotationPresent(OneToOne.class)) {
        cascadeTypes.addAll(Arrays.asList(method.getAnnotation(OneToOne.class).cascade()));
    } else if (method.isAnnotationPresent(OneToMany.class)) {
        cascadeTypes.addAll(Arrays.asList(method.getAnnotation(OneToMany.class).cascade()));
    } else if (method.isAnnotationPresent(ManyToOne.class)) {
        cascadeTypes.addAll(Arrays.asList(method.getAnnotation(ManyToOne.class).cascade()));
    } else if (method.isAnnotationPresent(ManyToMany.class)) {
        cascadeTypes.addAll(Arrays.asList(method.getAnnotation(ManyToMany.class).cascade()));
    }/*from   ww  w . j  a v  a 2  s. co  m*/
    final boolean cascadeAll = cascadeTypes.contains(CascadeType.ALL);
    return new ImmutableCascadeMetadata(cascadeAll || cascadeTypes.contains(CascadeType.PERSIST),
            cascadeAll || cascadeTypes.contains(CascadeType.MERGE),
            cascadeAll || cascadeTypes.contains(CascadeType.REMOVE),
            cascadeAll || cascadeTypes.contains(CascadeType.REFRESH));
}

From source file:com.github.dactiv.common.utils.ReflectionUtils.java

/**
 * ?methodannotationClass/*  w  w  w  . ja va  2s .co  m*/
 * 
 * @param method
 *            method
 * @param annotationClass
 *            annotationClass
 * 
 * @return {@link Annotation}
 */
public static <T extends Annotation> T getAnnotation(Method method, Class annotationClass) {

    Assert.notNull(method, "method?");
    Assert.notNull(annotationClass, "annotationClass?");

    method.setAccessible(true);
    if (method.isAnnotationPresent(annotationClass)) {
        return (T) method.getAnnotation(annotationClass);
    }
    return null;
}

From source file:com.someone.pizzaservice.infrastructure.BenchmarkProxyBeanPostprocessor.java

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    Set<String> setOfBenchmarkedMethods = new HashSet<>();
    Method[] methods = bean.getClass().getMethods();
    for (Method m : methods) {
        if (m.getAnnotation(Benchmark.class) != null && m.getAnnotation(Benchmark.class).active()) {
            System.out.println(beanName);
            System.out.println(bean.getClass().getName());
            setOfBenchmarkedMethods.add(m.getName());
        }//from  w ww . j  a  v a 2 s .co m
    }
    if (setOfBenchmarkedMethods.isEmpty()) {
        return bean;
    }
    // Here comes the tricky staff. If bean is enchanced by spring, getInterfaces no more provides actual interfaces
    // however superclass is an actual class, from which we still can get actual interfaces. Union of arrays provides us with actual 
    // interfaces in both cases.
    Class<?>[] interfaces = (Class<?>[]) ArrayUtils.addAll(bean.getClass().getInterfaces(),
            bean.getClass().getSuperclass().getInterfaces());
    InvocationHandler handler = new BenchmarkInvocationHandler(bean, setOfBenchmarkedMethods);
    bean = Proxy.newProxyInstance(bean.getClass().getClassLoader(), interfaces, handler);
    return bean;
}

From source file:com.capgemini.boot.core.factory.internal.DefaultAnnotationStrategy.java

@Override
public String getSettingName(Method factoryMethod) {
    return factoryMethod.getAnnotation(getFactoryMethodAnnotation()).setting();
}

From source file:com.teradata.tempto.internal.ReflectionInjectorHelper.java

private boolean isAnnotatedWithInject(Method method) {
    return method.getAnnotation(Inject.class) != null
            || method.getAnnotation(javax.inject.Inject.class) != null;
}

From source file:org.zkybase.kite.circuitbreaker.interceptor.AnnotationCircuitBreakerSource.java

private CircuitBreakerTemplate parseAnnotation(Method method) {
    GuardedByCircuitBreaker ann = method.getAnnotation(ANN_CLASS);
    return (ann == null ? null : beanFactory.getBean(ann.value(), CircuitBreakerTemplate.class));
}

From source file:edu.usu.sdl.openstorefront.doc.JaxrsProcessor.java

public static APIResourceModel processRestClass(Class resource, String rootPath) {
    APIResourceModel resourceModel = new APIResourceModel();

    resourceModel.setClassName(resource.getName());
    resourceModel.setResourceName(/*from   w  w  w  .  jav  a  2s . c o m*/
            String.join(" ", StringUtils.splitByCharacterTypeCamelCase(resource.getSimpleName())));

    APIDescription aPIDescription = (APIDescription) resource.getAnnotation(APIDescription.class);
    if (aPIDescription != null) {
        resourceModel.setResourceDescription(aPIDescription.value());
    }

    Path path = (Path) resource.getAnnotation(Path.class);
    if (path != null) {
        resourceModel.setResourcePath(rootPath + "/" + path.value());
    }

    RequireAdmin requireAdmin = (RequireAdmin) resource.getAnnotation(RequireAdmin.class);
    if (requireAdmin != null) {
        resourceModel.setRequireAdmin(true);
    }

    //class parameters
    mapParameters(resourceModel.getResourceParams(), resource.getDeclaredFields());

    //methods
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    int methodId = 0;
    for (Method method : resource.getDeclaredMethods()) {

        APIMethodModel methodModel = new APIMethodModel();
        methodModel.setId(methodId++);

        //rest method
        List<String> restMethods = new ArrayList<>();
        GET getMethod = (GET) method.getAnnotation(GET.class);
        POST postMethod = (POST) method.getAnnotation(POST.class);
        PUT putMethod = (PUT) method.getAnnotation(PUT.class);
        DELETE deleteMethod = (DELETE) method.getAnnotation(DELETE.class);
        if (getMethod != null) {
            restMethods.add("GET");
        }
        if (postMethod != null) {
            restMethods.add("POST");
        }
        if (putMethod != null) {
            restMethods.add("PUT");
        }
        if (deleteMethod != null) {
            restMethods.add("DELETE");
        }
        methodModel.setRestMethod(String.join(",", restMethods));

        if (restMethods.isEmpty()) {
            //skip non-rest methods
            continue;
        }

        //produces
        Produces produces = (Produces) method.getAnnotation(Produces.class);
        if (produces != null) {
            methodModel.setProducesTypes(String.join(",", produces.value()));
        }

        //consumes
        Consumes consumes = (Consumes) method.getAnnotation(Consumes.class);
        if (consumes != null) {
            methodModel.setConsumesTypes(String.join(",", consumes.value()));
        }

        aPIDescription = (APIDescription) method.getAnnotation(APIDescription.class);
        if (aPIDescription != null) {
            methodModel.setDescription(aPIDescription.value());
        }

        path = (Path) method.getAnnotation(Path.class);
        if (path != null) {
            methodModel.setMethodPath(path.value());
        }

        requireAdmin = (RequireAdmin) method.getAnnotation(RequireAdmin.class);
        if (requireAdmin != null) {
            methodModel.setRequireAdmin(true);
        }

        try {
            if (!(method.getReturnType().getSimpleName().equalsIgnoreCase(Void.class.getSimpleName()))) {
                APIValueModel valueModel = new APIValueModel();
                DataType dataType = (DataType) method.getAnnotation(DataType.class);

                boolean addResponseObject = true;
                if ("javax.ws.rs.core.Response".equals(method.getReturnType().getName()) && dataType == null) {
                    addResponseObject = false;
                }

                if (addResponseObject) {
                    valueModel.setValueObjectName(method.getReturnType().getSimpleName());
                    ReturnType returnType = (ReturnType) method.getAnnotation(ReturnType.class);
                    Class returnTypeClass;
                    if (returnType != null) {
                        returnTypeClass = returnType.value();
                    } else {
                        returnTypeClass = method.getReturnType();
                    }

                    if (!"javax.ws.rs.core.Response".equals(method.getReturnType().getName())) {
                        if (ReflectionUtil.isCollectionClass(method.getReturnType()) == false) {
                            try {
                                valueModel.setValueObject(
                                        objectMapper.writeValueAsString(returnTypeClass.newInstance()));
                                mapValueField(valueModel.getValueFields(),
                                        ReflectionUtil.getAllFields(returnTypeClass).toArray(new Field[0]));
                                mapComplexTypes(valueModel.getAllComplexTypes(),
                                        ReflectionUtil.getAllFields(returnTypeClass).toArray(new Field[0]),
                                        false);

                                aPIDescription = (APIDescription) returnTypeClass
                                        .getAnnotation(APIDescription.class);
                                if (aPIDescription != null) {
                                    valueModel.setValueDescription(aPIDescription.value());
                                }
                            } catch (InstantiationException iex) {
                                log.log(Level.WARNING, MessageFormat.format(
                                        "Unable to instantiated type: {0} make sure the type is not abstract.",
                                        returnTypeClass));
                            }
                        }
                    }

                    if (dataType != null) {
                        String typeName = dataType.value().getSimpleName();
                        if (StringUtils.isNotBlank(dataType.actualClassName())) {
                            typeName = dataType.actualClassName();
                        }
                        valueModel.setTypeObjectName(typeName);
                        try {
                            valueModel.setTypeObject(
                                    objectMapper.writeValueAsString(dataType.value().newInstance()));
                            mapValueField(valueModel.getTypeFields(),
                                    ReflectionUtil.getAllFields(dataType.value()).toArray(new Field[0]));
                            mapComplexTypes(valueModel.getAllComplexTypes(),
                                    ReflectionUtil.getAllFields(dataType.value()).toArray(new Field[0]), false);

                            aPIDescription = (APIDescription) dataType.value()
                                    .getAnnotation(APIDescription.class);
                            if (aPIDescription != null) {
                                valueModel.setTypeDescription(aPIDescription.value());
                            }

                        } catch (InstantiationException iex) {
                            log.log(Level.WARNING, MessageFormat.format(
                                    "Unable to instantiated type: {0} make sure the type is not abstract.",
                                    dataType.value()));
                        }
                    }

                    methodModel.setResponseObject(valueModel);
                }
            }
        } catch (IllegalAccessException | JsonProcessingException ex) {
            log.log(Level.WARNING, null, ex);
        }

        //method parameters
        mapMethodParameters(methodModel.getMethodParams(), method.getParameters());

        //Handle Consumed Objects
        mapConsumedObjects(methodModel, method.getParameters());

        resourceModel.getMethods().add(methodModel);
    }
    Collections.sort(resourceModel.getMethods(), new ApiMethodComparator<>());
    return resourceModel;
}

From source file:mercury.core.AngularMethodDescription.java

public AngularMethodDescription(Method m) {
    name = m.getName();//w  w w .j  a va  2s  . c om
    annotation = m.getAnnotation(AngularMethod.class);
    if (annotation != null) {
    }
}

From source file:com.capgemini.boot.core.factory.internal.DefaultAnnotationStrategy.java

@Override
public String getBeanNameSuffix(Method factoryMethod) {
    final SettingBackedBean annotation = factoryMethod.getAnnotation(getFactoryMethodAnnotation());
    return annotation.beanNameSuffix();
}

From source file:com.twitter.common.inject.TimedInterceptor.java

private SlidingStats createStats(Method method) {
    Timed timed = method.getAnnotation(Timed.class);
    Preconditions.checkArgument(timed != null, "TimedInterceptor can only be applied to @Timed methods");

    String name = timed.value();//from w w  w. j  a  v  a 2s. c o m
    String statName = !StringUtils.isEmpty(name) ? name : method.getName();
    return new SlidingStats(statName, "nanos");
}