List of usage examples for java.lang.reflect Constructor getParameterAnnotations
@Override
public Annotation[][] getParameterAnnotations()
From source file:Main.java
public static void print_method_or_constructor(Member member) { Constructor c = (Constructor) member; Annotation[][] annos = c.getParameterAnnotations(); }
From source file:ch.digitalfondue.npjt.ConstructorAnnotationRowMapper.java
/** * Check if the given class has the correct form. * /*from w w w .jav a2 s .co m*/ * <ul> * <li>must have exactly one public constructor.</li> * <li>must at least have one parameter.</li> * <li>all the parameters must be annotated with @Column annotation.</li> * </ul> * * @param clazz * @return */ public static boolean hasConstructorInTheCorrectForm(Class<?> clazz) { if (clazz.getConstructors().length != 1) { return false; } Constructor<?> con = clazz.getConstructors()[0]; if (con.getParameterTypes().length == 0) { return false; } Annotation[][] parameterAnnotations = con.getParameterAnnotations(); for (Annotation[] as : parameterAnnotations) { if (!hasColumnAnnotation(as)) { return false; } } return true; }
From source file:edu.uchicago.lowasser.flaginjection.Flags.java
public static void addFlagBindings(Binder binder, TypeLiteral<?> literal) { for (Field field : literal.getRawType().getDeclaredFields()) { if (field.isAnnotationPresent(Flag.class)) { Flag annot = field.getAnnotation(Flag.class); addFlagBinding(binder, annot, literal.getFieldType(field)); }/* w ww . j av a2 s . c o m*/ } for (Constructor<?> constructor : literal.getRawType().getDeclaredConstructors()) { List<TypeLiteral<?>> parameterTypes = literal.getParameterTypes(constructor); Annotation[][] parameterAnnotations = constructor.getParameterAnnotations(); for (int i = 0; i < parameterTypes.size(); i++) { Annotation[] annotations = parameterAnnotations[i]; TypeLiteral<?> typ = parameterTypes.get(i); for (Annotation annot : annotations) { if (annot instanceof Flag) { addFlagBinding(binder, (Flag) annot, typ); } } } } }
From source file:co.jirm.mapper.definition.SqlParameterDefinition.java
private static Map<String, SqlParameterDefinition> getSqlBeanParametersFromJsonCreatorConstructor( Constructor<?> c, SqlObjectConfig config) { Map<String, SqlParameterDefinition> parameters = new LinkedHashMap<String, SqlParameterDefinition>(); Annotation[][] aas = c.getParameterAnnotations(); Class<?>[] pts = c.getParameterTypes(); if (aas == null || aas.length == 0) { return parameters; }//from www. j a va 2 s. co m for (int i = 0; i < aas.length; i++) { Annotation[] as = aas[i]; Class<?> parameterType = pts[i]; for (int j = 0; j < as.length; j++) { Annotation a = as[j]; if (JsonProperty.class.equals(a.annotationType())) { JsonProperty p = (JsonProperty) a; String value = p.value(); final SqlParameterDefinition definition = parameterDef(config, c.getDeclaringClass(), value, parameterType, i); parameters.put(value, definition); } } } return parameters; }
From source file:at.ac.tuwien.infosys.jcloudscale.utility.ReflectionUtil.java
public static boolean[] findByRefParams(Constructor<?> method) { Annotation[][] paramAnnotations = method.getParameterAnnotations(); Class<?>[] paramTypes = method.getParameterTypes(); return findByRefParams(paramAnnotations, paramTypes); }
From source file:de.escalon.hypermedia.spring.uber.UberUtils.java
/** * Renders input fields for bean properties of bean to add or update or patch. * * @param uberFields//w ww .j a v a 2s . c o m * to add to * @param beanType * to render * @param annotatedParameters * which describes the method * @param annotatedParameter * which requires the bean * @param currentCallValue * sample call value */ private static void recurseBeanCreationParams(List<UberField> uberFields, Class<?> beanType, ActionDescriptor annotatedParameters, ActionInputParameter annotatedParameter, Object currentCallValue, String parentParamName, Set<String> knownFields) { // TODO collection, map and object node creation are only describable by an annotation, not via type reflection if (ObjectNode.class.isAssignableFrom(beanType) || Map.class.isAssignableFrom(beanType) || Collection.class.isAssignableFrom(beanType) || beanType.isArray()) { return; // use @Input(include) to list parameter names, at least? Or mix with hdiv's form builder? } try { Constructor[] constructors = beanType.getConstructors(); // find default ctor Constructor constructor = PropertyUtils.findDefaultCtor(constructors); // find ctor with JsonCreator ann if (constructor == null) { constructor = PropertyUtils.findJsonCreator(constructors, JsonCreator.class); } Assert.notNull(constructor, "no default constructor or JsonCreator found for type " + beanType.getName()); int parameterCount = constructor.getParameterTypes().length; if (parameterCount > 0) { Annotation[][] annotationsOnParameters = constructor.getParameterAnnotations(); Class[] parameters = constructor.getParameterTypes(); int paramIndex = 0; for (Annotation[] annotationsOnParameter : annotationsOnParameters) { for (Annotation annotation : annotationsOnParameter) { if (JsonProperty.class == annotation.annotationType()) { JsonProperty jsonProperty = (JsonProperty) annotation; // TODO use required attribute of JsonProperty for required fields String paramName = jsonProperty.value(); Class parameterType = parameters[paramIndex]; Object propertyValue = PropertyUtils.getPropertyOrFieldValue(currentCallValue, paramName); MethodParameter methodParameter = new MethodParameter(constructor, paramIndex); addUberFieldsForMethodParameter(uberFields, methodParameter, annotatedParameter, annotatedParameters, parentParamName, paramName, parameterType, propertyValue, knownFields); paramIndex++; // increase for each @JsonProperty } } } Assert.isTrue(parameters.length == paramIndex, "not all constructor arguments of @JsonCreator " + constructor.getName() + " are annotated with @JsonProperty"); } Set<String> knownConstructorFields = new HashSet<String>(uberFields.size()); for (UberField sirenField : uberFields) { knownConstructorFields.add(sirenField.getName()); } // TODO support Option provider by other method args? Map<String, PropertyDescriptor> propertyDescriptors = PropertyUtils.getPropertyDescriptors(beanType); // add input field for every setter for (PropertyDescriptor propertyDescriptor : propertyDescriptors.values()) { final Method writeMethod = propertyDescriptor.getWriteMethod(); String propertyName = propertyDescriptor.getName(); if (writeMethod == null || knownFields.contains(parentParamName + propertyName)) { continue; } final Class<?> propertyType = propertyDescriptor.getPropertyType(); Object propertyValue = PropertyUtils.getPropertyOrFieldValue(currentCallValue, propertyName); MethodParameter methodParameter = new MethodParameter(propertyDescriptor.getWriteMethod(), 0); addUberFieldsForMethodParameter(uberFields, methodParameter, annotatedParameter, annotatedParameters, parentParamName, propertyName, propertyType, propertyValue, knownConstructorFields); } } catch (Exception e) { throw new RuntimeException("Failed to write input fields for constructor", e); } }
From source file:com.github.tomakehurst.wiremock.matching.StringValuePattern.java
public final String getName() { Constructor<?> constructor = FluentIterable.of(this.getClass().getDeclaredConstructors()) .firstMatch(new Predicate<Constructor<?>>() { @Override/* w ww . j a va2s . c o m*/ public boolean apply(Constructor<?> input) { return (input.getParameterAnnotations().length > 0 && input.getParameterAnnotations()[0].length > 0 && input.getParameterAnnotations()[0][0] instanceof JsonProperty); } }).orNull(); if (constructor == null) { throw new IllegalStateException( "Constructor must have a first parameter annotatated with JsonProperty(\"<operator name>\")"); } JsonProperty jsonPropertyAnnotation = (JsonProperty) constructor.getParameterAnnotations()[0][0]; return jsonPropertyAnnotation.value(); }
From source file:ch.algotrader.config.ConfigBeanFactory.java
@SuppressWarnings("unchecked") public <T> T create(final ConfigParams configParams, final Class<T> clazz) { Validate.notNull(configParams, "ConfigParams is null"); Validate.notNull(clazz, "Target class is null"); Constructor<?>[] constructors = clazz.getConstructors(); if (constructors.length != 1) { throw new ConfigBeanCreationException( clazz.getName() + " config bean class is expected to declare one constructor only"); }//from w w w . ja va2s . co m Constructor<?> constructor = constructors[0]; Annotation[][] parameterAnnotations = constructor.getParameterAnnotations(); Class<?>[] parameterTypes = constructor.getParameterTypes(); if (parameterTypes.length != parameterAnnotations.length) { throw new ConfigBeanCreationException(clazz.getName() + " config bean metadata is inconsistent"); } Object[] paramValues = new Object[parameterTypes.length]; for (int i = 0; i < parameterTypes.length; i++) { Class<?> paramType = parameterTypes[i]; Annotation[] annotations = parameterAnnotations[i]; ConfigName configName = null; for (Annotation annotation : annotations) { if (annotation.annotationType().equals(ConfigName.class)) { configName = (ConfigName) annotation; break; } } if (configName == null) { throw new ConfigBeanCreationException( clazz.getName() + " config bean parameter does not have mandatory metadata"); } Object paramValue = configParams.getParameter(configName.value(), paramType); if (paramValue == null && !configName.optional()) { throw new ConfigBeanCreationException("Config parameter '" + configName.value() + "' is undefined"); } paramValues[i] = paramValue; } try { return (T) constructor.newInstance(paramValues); } catch (InstantiationException | InvocationTargetException | IllegalAccessException ex) { throw new ConfigBeanCreationException(ex); } }
From source file:com.github.hateoas.forms.spring.SpringActionDescriptor.java
static List<ActionParameterType> findConstructorInfo(final Class<?> beanType) { List<ActionParameterType> parametersInfo = new ArrayList<ActionParameterType>(); Constructor<?>[] constructors = beanType.getConstructors(); // find default ctor Constructor<?> constructor = PropertyUtils.findDefaultCtor(constructors); // find ctor with JsonCreator ann if (constructor == null) { constructor = PropertyUtils.findJsonCreator(constructors, JsonCreator.class); }//from w w w . ja va 2s. co m if (constructor != null) { int parameterCount = constructor.getParameterTypes().length; if (parameterCount > 0) { Annotation[][] annotationsOnParameters = constructor.getParameterAnnotations(); Class<?>[] parameters = constructor.getParameterTypes(); int paramIndex = 0; for (Annotation[] annotationsOnParameter : annotationsOnParameters) { for (Annotation annotation : annotationsOnParameter) { if (JsonProperty.class == annotation.annotationType()) { JsonProperty jsonProperty = (JsonProperty) annotation; // TODO use required attribute of JsonProperty for required fields -> String paramName = jsonProperty.value(); MethodParameter methodParameter = new MethodParameter(constructor, paramIndex); parametersInfo.add(new MethodParameterType(paramName, methodParameter)); paramIndex++; // increase for each @JsonProperty } } } Assert.isTrue(parameters.length == paramIndex, "not all constructor arguments of @JsonCreator " + constructor.getName() + " are annotated with @JsonProperty"); } } return parametersInfo; }
From source file:io.neba.core.resourcemodels.factory.ModelInstantiator.java
/** * @return either the default or an @Inject constructor, if present. Fails if neither a public default constructor nor a public @Inject constructor is * present, of if multiple @Inject constructors exist. */// ww w. jav a 2s .c o m @SuppressWarnings("unchecked") @Nonnull private static <T> ModelConstructor<T> resolveConstructor(@Nonnull Class<T> modelType) { ModelConstructor<T> constructor; Constructor<T> injectionConstructor = null, defaultConstructor = null; for (Constructor c : modelType.getConstructors()) { if (c.getParameterCount() == 0) { defaultConstructor = c; } if (!annotations(c).containsName(INJECT_ANNOTATION_NAME)) { continue; } if (injectionConstructor != null) { throw new InvalidModelException("Unable to instantiate model " + modelType + ". " + "Found more than one constructor annotated with @Inject: " + injectionConstructor + ", " + c); } injectionConstructor = c; } if (injectionConstructor != null) { Type[] parameters = injectionConstructor.getGenericParameterTypes(); ServiceDependency[] serviceDependencies = new ServiceDependency[parameters.length]; Annotation[][] parameterAnnotations = injectionConstructor.getParameterAnnotations(); for (int i = 0; i < parameters.length; ++i) { Filter filter = findFilterAnnotation(parameterAnnotations[i]); serviceDependencies[i] = new ServiceDependency(parameters[i], modelType, filter); } constructor = new ModelConstructor<>(injectionConstructor, serviceDependencies); } else if (defaultConstructor != null) { constructor = new ModelConstructor<>(defaultConstructor); } else { throw new InvalidModelException("The model " + modelType + " has neither a public default constructor nor a public constructor annotated with @Inject."); } return constructor; }