Java Code Examples for java.lang.reflect.Method#getDefaultValue()
The following examples show how to use
java.lang.reflect.Method#getDefaultValue() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnnotationHandler.java From simplexml with Apache License 2.0 | 6 votes |
/** * This is used to handle all invocations on the wrapped annotation. * Typically the response to an invocation will result in the * default value of the annotation attribute being returned. If the * method is an <code>equals</code> or <code>toString</code> then * this will be handled by an internal implementation. * * @param proxy this is the proxy object the invocation was made on * @param method this is the method that was invoked on the proxy * @param list this is the list of parameters to be used * * @return this is used to return the result of the invocation */ public Object invoke(Object proxy, Method method, Object[] list) throws Throwable { String name = method.getName(); if(name.equals(STRING)) { return toString(); } if(name.equals(EQUAL)) { return equals(proxy, list); } if(name.equals(CLASS)) { return type; } if(name.equals(REQUIRED)) { return required; } if(name.equals(ATTRIBUTE)) { return attribute; } return method.getDefaultValue(); }
Example 2
Source File: PsiAnnotationProxyUtils.java From litho with Apache License 2.0 | 6 votes |
private Object invoke(Method method, Object[] args) throws IllegalAccessException { Class<?> returnType = method.getReturnType(); if (returnType.isEnum()) { PsiAnnotation currentAnnotation = AnnotationUtil.findAnnotationInHierarchy( mListOwner, Collections.singleton(mAnnotationClass.getCanonicalName())); PsiReferenceExpression declaredValue = (PsiReferenceExpression) currentAnnotation.findAttributeValue(method.getName()); if (declaredValue == null) { return method.getDefaultValue(); } PsiIdentifier identifier = PsiTreeUtil.getChildOfType(declaredValue, PsiIdentifier.class); return Enum.valueOf((Class<Enum>) returnType, identifier.getText()); } try { if (args == null) { return method.invoke(mStubbed); } return method.invoke(mStubbed, args); } catch (InvocationTargetException e) { return method.getDefaultValue(); } }
Example 3
Source File: Qualifiers.java From Diorite with MIT License | 6 votes |
/** * Create annotation instance with given values. * * @param type * type of annotation, must be annotated with {@link Qualifier} or {@link Scope} * @param value * value of annotation. * @param <T> * type of annotation. * * @return annotation instance with given values. */ public static <T extends Annotation> T of(Class<T> type, Object value) { Method best = null; for (Method method : type.getDeclaredMethods()) { String methodName = method.getName(); Object def = method.getDefaultValue(); if (methodName.equals("value") && (def == null)) { return of(type, Map.of("value", value)); } if (def == null) { best = method; } } if (best != null) { return of(type, Map.of(best.getName(), value)); } return of(type, Map.of("value", value)); }
Example 4
Source File: AnnotationTypeMapping.java From spring-analysis-note with MIT License | 6 votes |
private void validateMirrorSet(MirrorSet mirrorSet) { Method firstAttribute = mirrorSet.get(0); Object firstDefaultValue = firstAttribute.getDefaultValue(); for (int i = 1; i <= mirrorSet.size() - 1; i++) { Method mirrorAttribute = mirrorSet.get(i); Object mirrorDefaultValue = mirrorAttribute.getDefaultValue(); if (firstDefaultValue == null || mirrorDefaultValue == null) { throw new AnnotationConfigurationException(String.format( "Misconfigured aliases: %s and %s must declare default values.", AttributeMethods.describe(firstAttribute), AttributeMethods.describe(mirrorAttribute))); } if (!ObjectUtils.nullSafeEquals(firstDefaultValue, mirrorDefaultValue)) { throw new AnnotationConfigurationException(String.format( "Misconfigured aliases: %s and %s must declare the same default value.", AttributeMethods.describe(firstAttribute), AttributeMethods.describe(mirrorAttribute))); } } }
Example 5
Source File: HDAnnotation.java From httpdoc with Apache License 2.0 | 6 votes |
public HDAnnotation(Annotation annotation) { try { if (annotation == null) throw new NullPointerException(); this.type = new HDClass(annotation.annotationType()); Map<CharSequence, HDAnnotationConstant[]> properties = new LinkedHashMap<>(); Method[] methods = annotation.annotationType().getMethods(); for (Method method : methods) { if (method.getDeclaringClass() != annotation.annotationType()) continue; if (method.getParameterTypes().length > 0) continue; Object value = method.invoke(annotation); String name = method.getName(); HDAnnotationConstant[] constants = value.getClass().isArray() ? valuesOf((Object[]) value) : new HDAnnotationConstant[]{valueOf(value)}; Object defaultValue = method.getDefaultValue(); boolean original = value.equals(defaultValue); properties.put(new HDAnnotationKey(name, original), constants); } this.properties = properties; } catch (Exception e) { throw new RuntimeException(e); } }
Example 6
Source File: AnnotationFactory.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName().equals("annotationType")) { return annotationClass; } if (method.getName().equals("toString")) { return toString(); } if (method.getName().equals("hashCode")) { return hashCode(); } if (method.getName().equals("equals")) { return annotationDescr.equals(args[0]); } try { annotationClass.getMethod(method.getName()); } catch (NoSuchMethodException e) { throw new RuntimeException("Invoked not existing method " + method.getName() + " on annotation " + annotationClass.getName()); } Object value = annotationDescr.getValue(method.getName()); return value == null ? method.getDefaultValue() : normalizeResult(method.getReturnType(), value); }
Example 7
Source File: AnnotationInstanceProvider.java From deltaspike with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Exception { if ("hashCode".equals(method.getName())) { return hashCode(); } else if ("equals".equals(method.getName())) { if (Proxy.isProxyClass(args[0].getClass())) { if (Proxy.getInvocationHandler(args[0]) instanceof AnnotationInstanceProvider) { return equals(Proxy.getInvocationHandler(args[0])); } } return equals(args[0]); } else if ("annotationType".equals(method.getName())) { return annotationType(); } else if ("toString".equals(method.getName())) { return toString(); } else { if (memberValues.containsKey(method.getName())) { return memberValues.get(method.getName()); } else // Default cause, probably won't ever happen, unless annotations get actual methods { return method.getDefaultValue(); } } }
Example 8
Source File: AnnotationUtils.java From conf4j with MIT License | 5 votes |
/** * Retrieve the <em>default value</em> of a named Annotation attribute, given the {@link Class annotation type}. * * @param annotationType the <em>annotation type</em> for which the default value should be retrieved * @param attributeName the name of the attribute value to retrieve. * @return the default value of the named attribute, or {@code null} if not found. */ public static Object getDefaultValue(Class<? extends Annotation> annotationType, String attributeName) { try { Method method = annotationType.getDeclaredMethod(attributeName); return method.getDefaultValue(); } catch (Exception ignore) { return null; } }
Example 9
Source File: AnnotationUtils.java From valdr-bean-validation with MIT License | 5 votes |
/** * Retrieve the <em>default value</em> of a named Annotation attribute, given the {@link Class annotation type}. * @param annotationType the <em>annotation type</em> for which the default value should be retrieved * @param attributeName the name of the attribute value to retrieve. * @return the default value of the named attribute, or {@code null} if not found * @see #getDefaultValue(java.lang.annotation.Annotation, String) */ public static Object getDefaultValue(Class<? extends Annotation> annotationType, String attributeName) { try { Method method = annotationType.getDeclaredMethod(attributeName, new Class[0]); return method.getDefaultValue(); } catch (Exception ex) { return null; } }
Example 10
Source File: AnnotationMetadataReadingVisitor.java From thinking-in-spring-boot-samples with Apache License 2.0 | 5 votes |
public AnnotationVisitor visitAnnotation(final String desc, boolean visible) { final String className = Type.getType(desc).getClassName(); final Map<String, Object> attributes = new LinkedHashMap<String, Object>(); return new EmptyVisitor() { public void visit(String name, Object value) { // Explicitly defined annotation attribute value. attributes.put(name, value); } public void visitEnd() { try { Class annotationClass = classLoader.loadClass(className); // Check declared default values of attributes in the annotation type. Method[] annotationAttributes = annotationClass.getMethods(); for (int i = 0; i < annotationAttributes.length; i++) { Method annotationAttribute = annotationAttributes[i]; String attributeName = annotationAttribute.getName(); Object defaultValue = annotationAttribute.getDefaultValue(); if (defaultValue != null && !attributes.containsKey(attributeName)) { attributes.put(attributeName, defaultValue); } } // Register annotations that the annotation type is annotated with. Annotation[] metaAnnotations = annotationClass.getAnnotations(); Set<String> metaAnnotationTypeNames = new HashSet<String>(); for (Annotation metaAnnotation : metaAnnotations) { metaAnnotationTypeNames.add(metaAnnotation.annotationType().getName()); } metaAnnotationMap.put(className, metaAnnotationTypeNames); } catch (ClassNotFoundException ex) { // Class not found - can't determine meta-annotations. } attributesMap.put(className, attributes); } }; }
Example 11
Source File: AnnotationHandler.java From simplexml with Apache License 2.0 | 5 votes |
/** * This is used to extract the default value used for the provided * annotation attribute. This will return the default value for * all attributes except that it makes the requirement optional. * Making the requirement optional provides better functionality. * * @param method this is the annotation representing the attribute * * @return this returns the default value for the attribute */ private Object value(Method method) { String name = method.getName(); if(name.equals(REQUIRED)) { return required; } if(name.equals(ATTRIBUTE)) { return attribute; } return method.getDefaultValue(); }
Example 12
Source File: RecursiveAnnotationAttributesVisitor.java From dolphin with Apache License 2.0 | 5 votes |
private void registerDefaultValues(Class<?> annotationClass) { // Only do further scanning for public annotations; we'd run into // IllegalAccessExceptions otherwise, and we don't want to mess with // accessibility in a SecurityManager environment. if (Modifier.isPublic(annotationClass.getModifiers())) { // Check declared default values of attributes in the annotation type. Method[] annotationAttributes = annotationClass.getMethods(); for (Method annotationAttribute : annotationAttributes) { String attributeName = annotationAttribute.getName(); Object defaultValue = annotationAttribute.getDefaultValue(); if (defaultValue != null && !this.attributes.containsKey(attributeName)) { if (defaultValue instanceof Annotation) { defaultValue = AnnotationAttributes.fromMap(AnnotationUtils.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] = AnnotationAttributes.fromMap( AnnotationUtils.getAnnotationAttributes(realAnnotations[i], false, true)); } defaultValue = mappedAnnotations; } this.attributes.put(attributeName, defaultValue); } } } }
Example 13
Source File: XMLConfigurer.java From oval with Eclipse Public License 2.0 | 5 votes |
@SuppressWarnings("unchecked") @Override public Object newInstance(final Class type) { final Object instance = super.newInstance(type); // test if a AnnotationCheck instance is requested if (instance instanceof AbstractAnnotationCheck) { // determine the constraint annotation Class<Annotation> constraintAnnotation = null; final ParameterizedType genericSuperclass = (ParameterizedType) type.getGenericSuperclass(); for (final Type genericType : genericSuperclass.getActualTypeArguments()) { final Class<?> genericClass = (Class<?>) genericType; if (genericClass.isAnnotation() && genericClass.isAnnotationPresent(Constraint.class)) { constraintAnnotation = (Class<Annotation>) genericClass; break; } } // in case we could determine the constraint annotation, read the attributes and // apply the declared default values to the check instance if (constraintAnnotation != null) { for (final Method m : constraintAnnotation.getMethods()) { final Object defaultValue = m.getDefaultValue(); if (defaultValue != null) { ReflectionUtils.setViaSetter(instance, m.getName(), defaultValue); } } } } return instance; }
Example 14
Source File: TypeMappedAnnotation.java From spring-analysis-note with MIT License | 5 votes |
@Nullable private <T> T getValue(int attributeIndex, Class<T> type) { Method attribute = this.mapping.getAttributes().get(attributeIndex); Object value = getValue(attributeIndex, true, false); if (value == null) { value = attribute.getDefaultValue(); } return adapt(attribute, value, type); }
Example 15
Source File: Annotations.java From businessworks with Apache License 2.0 | 5 votes |
public static boolean isAllDefaultMethods(Class<? extends Annotation> annotationType) { boolean hasMethods = false; for (Method m : annotationType.getDeclaredMethods()) { hasMethods = true; if (m.getDefaultValue() == null) { return false; } } return hasMethods; }
Example 16
Source File: ProxyAnnotation.java From jackson-lombok with MIT License | 5 votes |
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName().equals("annotationType")) { return annotationType; } Object value = properties.get(method.getName()); if (value != null) { return value; } return method.getDefaultValue(); }
Example 17
Source File: FeignClientBuilderTests.java From spring-cloud-openfeign with Apache License 2.0 | 4 votes |
private static Object getDefaultValueFromFeignClientAnnotation( final String methodName) { final Method method = ReflectionUtils.findMethod(FeignClient.class, methodName); return method.getDefaultValue(); }
Example 18
Source File: TypedAnnotationWriter.java From openjdk-8 with GNU General Public License v2.0 | 4 votes |
@SuppressWarnings("unchecked") public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if(method.getDeclaringClass()==JAnnotationWriter.class) { try { return method.invoke(this,args); } catch (InvocationTargetException e) { throw e.getTargetException(); } } String name = method.getName(); Object arg=null; if(args!=null && args.length>0) arg = args[0]; // check how it's defined on the annotation Method m = annotation.getDeclaredMethod(name); Class<?> rt = m.getReturnType(); // array value if(rt.isArray()) { return addArrayValue(proxy,name,rt.getComponentType(),method.getReturnType(),arg); } // sub annotation if(Annotation.class.isAssignableFrom(rt)) { Class<? extends Annotation> r = (Class<? extends Annotation>)rt; return new TypedAnnotationWriter( r,method.getReturnType(),use.annotationParam(name,r)).createProxy(); } // scalar value if(arg instanceof JType) { JType targ = (JType) arg; checkType(Class.class,rt); if(m.getDefaultValue()!=null) { // check the default if(targ.equals(targ.owner().ref((Class)m.getDefaultValue()))) return proxy; // defaulted } use.param(name,targ); return proxy; } // other Java built-in types checkType(arg.getClass(),rt); if(m.getDefaultValue()!=null && m.getDefaultValue().equals(arg)) // defaulted. no need to write out. return proxy; if(arg instanceof String) { use.param(name,(String)arg); return proxy; } if(arg instanceof Boolean) { use.param(name,(Boolean)arg); return proxy; } if(arg instanceof Integer) { use.param(name,(Integer)arg); return proxy; } if(arg instanceof Class) { use.param(name,(Class)arg); return proxy; } if(arg instanceof Enum) { use.param(name,(Enum)arg); return proxy; } throw new IllegalArgumentException("Unable to handle this method call "+method.toString()); }
Example 19
Source File: TypedAnnotationWriter.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
@SuppressWarnings("unchecked") public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if(method.getDeclaringClass()==JAnnotationWriter.class) { try { return method.invoke(this,args); } catch (InvocationTargetException e) { throw e.getTargetException(); } } String name = method.getName(); Object arg=null; if(args!=null && args.length>0) arg = args[0]; // check how it's defined on the annotation Method m = annotation.getDeclaredMethod(name); Class<?> rt = m.getReturnType(); // array value if(rt.isArray()) { return addArrayValue(proxy,name,rt.getComponentType(),method.getReturnType(),arg); } // sub annotation if(Annotation.class.isAssignableFrom(rt)) { Class<? extends Annotation> r = (Class<? extends Annotation>)rt; return new TypedAnnotationWriter( r,method.getReturnType(),use.annotationParam(name,r)).createProxy(); } // scalar value if(arg instanceof JType) { JType targ = (JType) arg; checkType(Class.class,rt); if(m.getDefaultValue()!=null) { // check the default if(targ.equals(targ.owner().ref((Class)m.getDefaultValue()))) return proxy; // defaulted } use.param(name,targ); return proxy; } // other Java built-in types checkType(arg.getClass(),rt); if(m.getDefaultValue()!=null && m.getDefaultValue().equals(arg)) // defaulted. no need to write out. return proxy; if(arg instanceof String) { use.param(name,(String)arg); return proxy; } if(arg instanceof Boolean) { use.param(name,(Boolean)arg); return proxy; } if(arg instanceof Integer) { use.param(name,(Integer)arg); return proxy; } if(arg instanceof Class) { use.param(name,(Class)arg); return proxy; } if(arg instanceof Enum) { use.param(name,(Enum)arg); return proxy; } throw new IllegalArgumentException("Unable to handle this method call "+method.toString()); }
Example 20
Source File: AnnotationUtils.java From lams with GNU General Public License v2.0 | 3 votes |
/** * 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> * <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(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; }