List of usage examples for java.lang.reflect Method getDefaultValue
public Object getDefaultValue()
From source file:org.jboss.seam.spring.utils.AnnotationInvocationHandler.java
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // delegate methods to if (method.getDeclaringClass().equals(AnnotationInvocationHandler.class) || method.getDeclaringClass().equals(Object.class) || method.getDeclaringClass().equals(Annotation.class)) { return method.invoke(this, args); }/*from ww w .j ava 2 s. c o m*/ if (registeredValues != null && registeredValues.containsKey(method.getName())) { if (conversionService != null) { return conversionService.convert(registeredValues.get(method.getName()), method.getReturnType()); } else { return registeredValues.get(method.getName()); } } else { return method.getDefaultValue(); } }
From source file:be.idamediafoundry.sofa.livecycle.dsc.util.AnnotationDrivenQDoxComponentInfoExtractor.java
private <T extends java.lang.annotation.Annotation> T convertToJavaLang(final Annotation annotation, final Class<T> expectedType) { try {/*from w ww .java 2 s . c om*/ final Class<?> annotationClass = Class.forName(annotation.getType().getFullyQualifiedName()); @SuppressWarnings("unchecked") T proxy = (T) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[] { annotationClass }, new InvocationHandler() { public Object invoke(Object instance, Method method, Object[] args) throws Throwable { if (method.getName().equals("toString")) { return "Proxied annotation of type " + annotationClass; } else if (method.getName().equals("getClass")) { return annotationClass; } Object value = annotation.getProperty(method.getName()); if (value == null) { return method.getDefaultValue(); } if (value instanceof Annotation) { java.lang.annotation.Annotation sub = convertToJavaLang((Annotation) value, java.lang.annotation.Annotation.class); return sub; } else { AnnotationConstant constant = (AnnotationConstant) value; value = constant.getValue(); return value; } } }); return proxy; } catch (ClassNotFoundException e) { throw new IllegalArgumentException( "The source code is annotated with a class that could not be found on your project's classpath, please fix this!", e); } }
From source file:org.lunarray.model.descriptor.scanner.AnnotationMetaModelProcessor.java
/** * Copy a value.//from ww w .j a va 2 s . c o m * * @param values * The values to copy to. * @param annotation * The annotation to copy for. * @param method * The method of the annotation to copy. * @throws MappingException * Thrown if the value could not be related. */ private void copyValue(final AnnotationMetaValues values, final Annotation annotation, final Method method) throws MappingException { final String name = method.getName(); final Object defaultValue = method.getDefaultValue(); try { final Object obj = method.invoke(annotation); if (!CheckUtil.isNull(obj) && !obj.equals(defaultValue)) { values.getMetaAnnotationList(name); final Class<?> type = obj.getClass(); this.processType(values, name, obj, type); } } catch (final IllegalArgumentException e) { throw new MappingException(e); } catch (final IllegalAccessException e) { throw new MappingException(e); } catch (final InvocationTargetException e) { throw new MappingException(e); } }
From source file:org.apache.bval.jsr.AnnotationConstraintBuilder.java
private void buildGroups(final Method method) throws IllegalAccessException, InvocationTargetException { if (!TypeUtils.isAssignable(method.getReturnType(), ConstraintAnnotationAttributes.GROUPS.getType())) { throw new ConstraintDefinitionException( "Return type for groups() must be of type " + ConstraintAnnotationAttributes.GROUPS.getType()); }/*w ww. j a va 2 s . c o m*/ final Object raw = method.invoke(constraintValidation.getAnnotation()); Class<?>[] garr; if (raw instanceof Class<?>) { garr = new Class[] { (Class<?>) raw }; } else if (raw instanceof Class<?>[]) { garr = (Class<?>[]) raw; if (Object[].class.cast(method.getDefaultValue()).length > 0) { throw new ConstraintDefinitionException("Default value for groups() must be an empty array"); } } else { garr = null; } if (ArrayUtils.isEmpty(garr)) { garr = GroupsComputer.DEFAULT_GROUP; } constraintValidation.setGroups(garr); }
From source file:org.springframework.core.annotation.AnnotationUtils.java
/** * Register the annotation-declared default values for the given attributes, * if available./*w w w . j a va2s.c o m*/ * @param attributes the annotation attributes to process * @since 4.3.2 */ public static void registerDefaultValues(AnnotationAttributes attributes) { // Only do defaults scanning for public annotations; we'd run into // IllegalAccessExceptions otherwise, and we don't want to mess with // accessibility in a SecurityManager environment. Class<? extends Annotation> annotationType = attributes.annotationType(); if (annotationType != null && Modifier.isPublic(annotationType.getModifiers())) { // Check declared default values of attributes in the annotation type. for (Method annotationAttribute : getAttributeMethods(annotationType)) { String attributeName = annotationAttribute.getName(); Object defaultValue = annotationAttribute.getDefaultValue(); if (defaultValue != null && !attributes.containsKey(attributeName)) { if (defaultValue instanceof Annotation) { defaultValue = getAnnotationAttributes((Annotation) defaultValue, false, true); } else if (defaultValue instanceof Annotation[]) { Annotation[] realAnnotations = (Annotation[]) defaultValue; AnnotationAttributes[] mappedAnnotations = new AnnotationAttributes[realAnnotations.length]; for (int i = 0; i < realAnnotations.length; i++) { mappedAnnotations[i] = getAnnotationAttributes(realAnnotations[i], false, true); } defaultValue = mappedAnnotations; } attributes.put(attributeName, new DefaultValueHolder(defaultValue)); } } } }
From source file:org.springframework.core.annotation.AnnotationUtils.java
/** * Retrieve the given annotation's attributes as an {@link AnnotationAttributes} map. * <p>This method provides fully recursive annotation reading capabilities on par with * the reflection-based {@link org.springframework.core.type.StandardAnnotationMetadata}. * <p><strong>NOTE</strong>: This variant of {@code getAnnotationAttributes()} is * only intended for use within the framework. The following special rules apply: * <ol>/* www .java 2 s . c o m*/ * <li>Default values will be replaced with default value placeholders.</li> * <li>The resulting, merged annotation attributes should eventually be * {@linkplain #postProcessAnnotationAttributes post-processed} in order to * ensure that placeholders have been replaced by actual default values and * in order to enforce {@code @AliasFor} semantics.</li> * </ol> * @param annotatedElement the element that is annotated with the supplied annotation; * may be {@code null} if unknown * @param annotation the annotation to retrieve the attributes for * @param classValuesAsString whether to convert Class references into Strings (for * compatibility with {@link org.springframework.core.type.AnnotationMetadata}) * or to preserve them as Class references * @param nestedAnnotationsAsMap whether to convert nested annotations into * {@link AnnotationAttributes} maps (for compatibility with * {@link org.springframework.core.type.AnnotationMetadata}) or to preserve them as * {@code Annotation} instances * @return the annotation attributes (a specialized Map) with attribute names as keys * and corresponding attribute values as values (never {@code null}) * @since 4.2 * @see #postProcessAnnotationAttributes */ static AnnotationAttributes retrieveAnnotationAttributes(@Nullable Object annotatedElement, Annotation annotation, boolean classValuesAsString, boolean nestedAnnotationsAsMap) { Class<? extends Annotation> annotationType = annotation.annotationType(); AnnotationAttributes attributes = new AnnotationAttributes(annotationType); for (Method method : getAttributeMethods(annotationType)) { try { Object attributeValue = method.invoke(annotation); Object defaultValue = method.getDefaultValue(); if (defaultValue != null && ObjectUtils.nullSafeEquals(attributeValue, defaultValue)) { attributeValue = new DefaultValueHolder(defaultValue); } attributes.put(method.getName(), adaptValue(annotatedElement, attributeValue, classValuesAsString, nestedAnnotationsAsMap)); } catch (Throwable ex) { if (ex instanceof InvocationTargetException) { Throwable targetException = ((InvocationTargetException) ex).getTargetException(); rethrowAnnotationConfigurationException(targetException); } throw new IllegalStateException("Could not obtain annotation attribute value for " + method, ex); } } return attributes; }
From source file:org.jgentleframework.core.interceptor.AnnotationAroundAdviceMethodInterceptor.java
@Override public Object invoke(MethodInvocation invocation) throws Throwable { // checking runtime loading. if (runtimeLoading) init();/*from ww w . j a va 2s . com*/ Object result = null; Method invoMethod = invocation.getMethod(); Object proxy = invocation.getThis(); try { if (invoMethod.getName().equals("hashCode")) { return this.hashCode(proxy); } else if (invoMethod.getName().equals("equals")) { return equals(invocation.getArguments()[0], proxy); } else if (invoMethod.getName().equals("toString")) { return toString(proxy); } else if (invoMethod.getName().equals("annotationType")) { return annotationType(); } } catch (Exception e) { if (log.isFatalEnabled()) { log.fatal("Could not invoke core method [" + invoMethod + "] of this annotation proxy !!", e); } } result = this.attributesMapping.get(invoMethod); // executes wrapping try { Definition defMethod = this.definition.getMemberDefinition(invoMethod); if (defMethod != null && defMethod.isAnnotationPresent(Inject.class)) { Inject inject = defMethod.getAnnotation(Inject.class); if (inject.invocation()) { result = InOutExecutor.getInjectedDependency(inject, invoMethod.getReturnType(), this.provider); } } if (result == null) result = invoMethod.getDefaultValue(); // proceed the invocation Object anotherResult = null; try { anotherResult = invocation.proceed(); } catch (MethodInterceptorCallbackException e) { } result = result != anotherResult && anotherResult != null ? anotherResult : result; } finally { if (this.outjectMethodMap.containsKey(invoMethod)) { Outject outject = this.outjectMethodMap.get(invoMethod); if (outject.invocation()) InOutExecutor.setOutjectedDependency(outject, result, provider, invoMethod.getReturnType()); } } return result; }
From source file:org.apache.bval.jsr.AnnotationConstraintBuilder.java
/** build attributes, payload, groups from 'annotation' */ @Privileged/*from w w w . j a v a 2 s . com*/ private void buildFromAnnotation() { if (constraintValidation.getAnnotation() == null) { return; } final Class<? extends Annotation> annotationType = constraintValidation.getAnnotation().annotationType(); boolean foundPayload = false; boolean foundGroups = false; Method validationAppliesTo = null; boolean foundMessage = false; for (final Method method : AnnotationProxyBuilder.findMethods(annotationType)) { // groups + payload must also appear in attributes (also // checked by TCK-Tests) if (method.getParameterTypes().length == 0) { try { final String name = method.getName(); if (ConstraintAnnotationAttributes.PAYLOAD.getAttributeName().equals(name)) { buildPayload(method); foundPayload = true; } else if (ConstraintAnnotationAttributes.GROUPS.getAttributeName().equals(name)) { buildGroups(method); foundGroups = true; } else if (ConstraintAnnotationAttributes.VALIDATION_APPLIES_TO.getAttributeName() .equals(name)) { buildValidationAppliesTo(method); validationAppliesTo = method; } else if (name.startsWith("valid")) { throw new ConstraintDefinitionException( "constraints parameters can't start with valid: " + name); } else { if (ConstraintAnnotationAttributes.MESSAGE.getAttributeName().equals(name)) { foundMessage = true; if (!TypeUtils.isAssignable(method.getReturnType(), ConstraintAnnotationAttributes.MESSAGE.getType())) { throw new ConstraintDefinitionException("Return type for message() must be of type " + ConstraintAnnotationAttributes.MESSAGE.getType()); } } constraintValidation.getAttributes().put(name, method.invoke(constraintValidation.getAnnotation())); } } catch (final ConstraintDefinitionException cde) { throw cde; } catch (final Exception e) { // do nothing log.log(Level.WARNING, String.format("Error processing annotation: %s ", constraintValidation.getAnnotation()), e); } } } if (!foundMessage) { throw new ConstraintDefinitionException( "Annotation " + annotationType.getName() + " has no message method"); } if (!foundPayload) { throw new ConstraintDefinitionException( "Annotation " + annotationType.getName() + " has no payload method"); } if (!foundGroups) { throw new ConstraintDefinitionException( "Annotation " + annotationType.getName() + " has no groups method"); } if (validationAppliesTo != null && !ConstraintTarget.IMPLICIT.equals(validationAppliesTo.getDefaultValue())) { throw new ConstraintDefinitionException("validationAppliesTo default value should be IMPLICIT"); } // valid validationAppliesTo final Constraint annotation = annotationType.getAnnotation(Constraint.class); if (annotation == null) { return; } final Pair validationTarget = computeValidationTarget(annotation.validatedBy()); for (final Annotation a : annotationType.getAnnotations()) { final Class<? extends Annotation> aClass = a.annotationType(); if (aClass.getName().startsWith("java.lang.annotation.")) { continue; } final Constraint inheritedConstraint = aClass.getAnnotation(Constraint.class); if (inheritedConstraint != null && !aClass.getName().startsWith("javax.validation.constraints.")) { final Pair validationTargetInherited = computeValidationTarget(inheritedConstraint.validatedBy()); if ((validationTarget.a > 0 && validationTargetInherited.b > 0 && validationTarget.b == 0) || (validationTarget.b > 0 && validationTargetInherited.a > 0 && validationTarget.a == 0)) { throw new ConstraintDefinitionException("Parent and child constraint have different targets"); } } } }