Search in sources :

Example 26 with Annotation

use of java.lang.annotation.Annotation in project jersey by jersey.

the class ParamInjectionResolver method resolve.

@Override
@SuppressWarnings("unchecked")
public Object resolve(Injectee injectee) {
    AnnotatedElement annotated = injectee.getParent();
    Annotation[] annotations;
    if (annotated.getClass().equals(Constructor.class)) {
        annotations = ((Constructor) annotated).getParameterAnnotations()[injectee.getPosition()];
    } else {
        annotations = annotated.getDeclaredAnnotations();
    }
    Class componentClass = injectee.getInjecteeClass();
    Type genericType = injectee.getRequiredType();
    final Type targetGenericType;
    if (injectee.isFactory()) {
        targetGenericType = ReflectionHelper.getTypeArgument(genericType, 0);
    } else {
        targetGenericType = genericType;
    }
    final Class<?> targetType = ReflectionHelper.erasure(targetGenericType);
    final Parameter parameter = Parameter.create(componentClass, componentClass, hasEncodedAnnotation(injectee), targetType, targetGenericType, annotations);
    final Supplier<?> paramValueSupplier = valueSupplierProvider.getValueSupplier(parameter);
    if (paramValueSupplier != null) {
        if (injectee.isFactory()) {
            return paramValueSupplier;
        } else {
            return paramValueSupplier.get();
        }
    }
    return null;
}
Also used : Type(java.lang.reflect.Type) Constructor(java.lang.reflect.Constructor) AnnotatedElement(java.lang.reflect.AnnotatedElement) Parameter(org.glassfish.jersey.server.model.Parameter) Annotation(java.lang.annotation.Annotation)

Example 27 with Annotation

use of java.lang.annotation.Annotation in project jersey by jersey.

the class EntityFilteringClientTest method testEntityAnnotationsOverConfiguration.

@Test
public void testEntityAnnotationsOverConfiguration() throws Exception {
    final ClientConfig config = new ClientConfig().property(EntityFilteringFeature.ENTITY_FILTERING_SCOPE, SecondaryDetailedView.Factory.get());
    configureClient(config);
    final String fields = ClientBuilder.newClient(config).target(getBaseUri()).request().post(Entity.entity(new ManyFilteringsOnClassEntity(), ENTITY_FILTERING, new Annotation[] { PrimaryDetailedView.Factory.get() }), String.class);
    assertSameFields(fields, "field,accessor,property,manyEntities.property1,manyEntities.field1,oneEntities.field2," + "oneEntities.property2,oneEntities.property1,oneEntities.field1,defaultEntities.field,defaultEntities" + ".property");
}
Also used : ManyFilteringsOnClassEntity(org.glassfish.jersey.tests.e2e.entity.filtering.domain.ManyFilteringsOnClassEntity) ClientConfig(org.glassfish.jersey.client.ClientConfig) Annotation(java.lang.annotation.Annotation) Test(org.junit.Test)

Example 28 with Annotation

use of java.lang.annotation.Annotation in project che by eclipse.

the class Service method generateLinkForMethod.

private Link generateLinkForMethod(UriInfo uriInfo, String linkRel, Method method, Object... pathParameters) {
    String httpMethod = null;
    final HttpMethod httpMethodAnnotation = getMetaAnnotation(method, HttpMethod.class);
    if (httpMethodAnnotation != null) {
        httpMethod = httpMethodAnnotation.value();
    }
    if (httpMethod == null) {
        throw new IllegalArgumentException(format("Method '%s' has not any HTTP method annotation and may not be used to produce link.", method.getName()));
    }
    final Consumes consumes = getAnnotation(method, Consumes.class);
    final Produces produces = getAnnotation(method, Produces.class);
    final UriBuilder baseUriBuilder = uriInfo.getBaseUriBuilder();
    final LinkedList<String> matchedURIs = new LinkedList<>(uriInfo.getMatchedURIs());
    // Get path to the root resource.
    if (uriInfo.getMatchedResources().size() < matchedURIs.size()) {
        matchedURIs.remove();
    }
    while (!matchedURIs.isEmpty()) {
        baseUriBuilder.path(matchedURIs.pollLast());
    }
    final Path path = method.getAnnotation(Path.class);
    if (path != null) {
        baseUriBuilder.path(path.value());
    }
    final Link link = DtoFactory.getInstance().createDto(Link.class).withRel(linkRel).withHref(baseUriBuilder.build(pathParameters).toString()).withMethod(httpMethod);
    if (consumes != null) {
        link.setConsumes(consumes.value()[0]);
    }
    if (produces != null) {
        link.setProduces(produces.value()[0]);
    }
    Class<?>[] parameterClasses = method.getParameterTypes();
    if (parameterClasses.length > 0) {
        Annotation[][] annotations = method.getParameterAnnotations();
        for (int i = 0; i < parameterClasses.length; i++) {
            if (annotations[i].length > 0) {
                boolean isBodyParameter = false;
                QueryParam queryParam = null;
                Description description = null;
                Required required = null;
                Valid valid = null;
                DefaultValue defaultValue = null;
                for (int j = 0; j < annotations[i].length; j++) {
                    Annotation annotation = annotations[i][j];
                    isBodyParameter |= !JAX_RS_ANNOTATIONS.contains(annotation.annotationType().getName());
                    Class<?> annotationType = annotation.annotationType();
                    if (annotationType == QueryParam.class) {
                        queryParam = (QueryParam) annotation;
                    } else if (annotationType == Description.class) {
                        description = (Description) annotation;
                    } else if (annotationType == Required.class) {
                        required = (Required) annotation;
                    } else if (annotationType == Valid.class) {
                        valid = (Valid) annotation;
                    } else if (annotationType == DefaultValue.class) {
                        defaultValue = (DefaultValue) annotation;
                    }
                }
                if (queryParam != null) {
                    LinkParameter parameter = DtoFactory.getInstance().createDto(LinkParameter.class).withName(queryParam.value()).withRequired(required != null).withType(getParameterType(parameterClasses[i]));
                    if (defaultValue != null) {
                        parameter.setDefaultValue(defaultValue.value());
                    }
                    if (description != null) {
                        parameter.setDescription(description.value());
                    }
                    if (valid != null) {
                        parameter.setValid(Arrays.asList(valid.value()));
                    }
                    link.getParameters().add(parameter);
                } else if (isBodyParameter) {
                    if (description != null) {
                        link.setRequestBody(DtoFactory.getInstance().createDto(RequestBodyDescriptor.class).withDescription(description.value()));
                    }
                }
            }
        }
    }
    return link;
}
Also used : Path(javax.ws.rs.Path) RequestBodyDescriptor(org.eclipse.che.api.core.rest.shared.dto.RequestBodyDescriptor) Description(org.eclipse.che.api.core.rest.annotations.Description) LinkParameter(org.eclipse.che.api.core.rest.shared.dto.LinkParameter) LinkedList(java.util.LinkedList) Annotation(java.lang.annotation.Annotation) DefaultValue(javax.ws.rs.DefaultValue) Required(org.eclipse.che.api.core.rest.annotations.Required) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) QueryParam(javax.ws.rs.QueryParam) Valid(org.eclipse.che.api.core.rest.annotations.Valid) UriBuilder(javax.ws.rs.core.UriBuilder) HttpMethod(javax.ws.rs.HttpMethod) Link(org.eclipse.che.api.core.rest.shared.dto.Link) GenerateLink(org.eclipse.che.api.core.rest.annotations.GenerateLink)

Example 29 with Annotation

use of java.lang.annotation.Annotation in project cucumber-jvm by cucumber.

the class CucumberTestContextManager method checkAnnotationsEqual.

private void checkAnnotationsEqual(Class<?> stepClassWithSpringContext, Class<?> stepClass) {
    Annotation[] annotations1 = stepClassWithSpringContext.getAnnotations();
    Annotation[] annotations2 = stepClass.getAnnotations();
    if (annotations1.length != annotations2.length) {
        throw new CucumberException("Annotations differs on glue classes found: " + stepClassWithSpringContext.getName() + ", " + stepClass.getName());
    }
    for (Annotation annotation : annotations1) {
        if (!isAnnotationInArray(annotation, annotations2)) {
            throw new CucumberException("Annotations differs on glue classes found: " + stepClassWithSpringContext.getName() + ", " + stepClass.getName());
        }
    }
}
Also used : CucumberException(cucumber.runtime.CucumberException) Annotation(java.lang.annotation.Annotation)

Example 30 with Annotation

use of java.lang.annotation.Annotation in project openhab1-addons by openhab.

the class MetadataHandler method generate.

/**
     * Scans the class and generates metadata.
     */
public void generate(Class<?> clazz) throws IllegalAccessException {
    if (clazz == null) {
        return;
    }
    for (Field field : clazz.getDeclaredFields()) {
        if (field.getType().getName().startsWith(PACKAGE_TO_SCAN) && !field.isEnumConstant()) {
            generate(field.getType());
        } else {
            for (Annotation annotation : field.getAnnotations()) {
                if (annotation.annotationType().equals(ProviderMappings.class)) {
                    ProviderMappings providerAnnotations = (ProviderMappings) annotation;
                    for (Provider provider : providerAnnotations.value()) {
                        Map<String, ProviderMappingInfo> mappings = providerMappings.get(provider.name());
                        if (mappings == null) {
                            mappings = new HashMap<String, ProviderMappingInfo>();
                            providerMappings.put(provider.name(), mappings);
                        }
                        Converter<?> converter = getConverter(field, provider.converter());
                        String target = clazz.getSimpleName().toLowerCase() + "." + field.getName();
                        ProviderMappingInfo pm = new ProviderMappingInfo(provider.property(), target, converter);
                        mappings.put(pm.getSource(), pm);
                        logger.trace("Added provider mapping {}: {}", provider.name(), pm);
                    }
                } else if (annotation.annotationType().equals(ForecastMappings.class)) {
                    ForecastMappings forecastsAnnotations = (ForecastMappings) annotation;
                    for (Forecast forecast : forecastsAnnotations.value()) {
                        List<String> forecastProperties = forecastMappings.get(forecast.provider());
                        if (forecastProperties == null) {
                            forecastProperties = new ArrayList<String>();
                            forecastMappings.put(forecast.provider(), forecastProperties);
                        }
                        forecastProperties.add(forecast.property());
                        logger.trace("Added forecast mapping {}: {}", forecast.provider(), forecast.property());
                    }
                }
            }
        }
    }
}
Also used : ProviderMappings(org.openhab.binding.weather.internal.annotation.ProviderMappings) ForecastMappings(org.openhab.binding.weather.internal.annotation.ForecastMappings) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) Provider(org.openhab.binding.weather.internal.annotation.Provider) Field(java.lang.reflect.Field) Forecast(org.openhab.binding.weather.internal.annotation.Forecast) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Annotation (java.lang.annotation.Annotation)1784 Method (java.lang.reflect.Method)412 ArrayList (java.util.ArrayList)245 Field (java.lang.reflect.Field)172 Test (org.junit.Test)164 Type (java.lang.reflect.Type)149 HashMap (java.util.HashMap)146 HashSet (java.util.HashSet)129 Map (java.util.Map)95 List (java.util.List)92 IOException (java.io.IOException)71 Set (java.util.Set)71 ParameterizedType (java.lang.reflect.ParameterizedType)62 InvocationTargetException (java.lang.reflect.InvocationTargetException)53 Collection (java.util.Collection)52 LinkedHashMap (java.util.LinkedHashMap)36 Collectors (java.util.stream.Collectors)36 Constructor (java.lang.reflect.Constructor)33 LinkedHashSet (java.util.LinkedHashSet)32 Optional (java.util.Optional)32