@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return method.getDefaultValue(); } }));
public static Object callGetDefaultValue(Method thiz) { return thiz.getDefaultValue(); }
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; }
private void validateDefaultValueConfiguration(Method aliasedAttribute) { Object defaultValue = this.sourceAttribute.getDefaultValue(); Object aliasedDefaultValue = aliasedAttribute.getDefaultValue(); if (defaultValue == null || aliasedDefaultValue == null) { String msg = String.format("Misconfigured aliases: attribute '%s' in annotation [%s] " + "and attribute '%s' in annotation [%s] must declare default values.", this.sourceAttributeName, this.sourceAnnotationType.getName(), aliasedAttribute.getName(), aliasedAttribute.getDeclaringClass().getName()); throw new AnnotationConfigurationException(msg); } if (!ObjectUtils.nullSafeEquals(defaultValue, aliasedDefaultValue)) { String msg = String.format("Misconfigured aliases: attribute '%s' in annotation [%s] " + "and attribute '%s' in annotation [%s] must declare the same default value.", this.sourceAttributeName, this.sourceAnnotationType.getName(), aliasedAttribute.getName(), aliasedAttribute.getDeclaringClass().getName()); throw new AnnotationConfigurationException(msg); } }
Settings.class.getMethod("a").getDefaultValue() Settings.class.getMethod("b").getDefaultValue() Settings.class.getMethod("c").getDefaultValue()
/** * {@inheritDoc} */ public AnnotationValue<?, ?> getDefaultValue() { Object value = method.getDefaultValue(); return value == null ? AnnotationValue.UNDEFINED : AnnotationDescription.ForLoadedAnnotation.asValue(value, method.getReturnType()); }
/** * Retrieve the <em>default value</em> of a named 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(Annotation, String) */ @Nullable public static Object getDefaultValue( @Nullable Class<? extends Annotation> annotationType, @Nullable String attributeName) { if (annotationType == null || !StringUtils.hasText(attributeName)) { return null; } try { return annotationType.getDeclaredMethod(attributeName).getDefaultValue(); } catch (Throwable ex) { handleIntrospectionFailure(annotationType, ex); return null; } }
private static ImmutableMap<String, Object> resolveMembers( Class<? extends Annotation> annotationType) { ImmutableMap.Builder<String, Object> result = ImmutableMap.builder(); for (Method method : annotationType.getDeclaredMethods()) { result.put(method.getName(), method.getDefaultValue()); } return result.build(); }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { if (method.getName().equals("benchmarkRounds")) { log.trace("Intercepted benchmarkRounds() invocation: returning {}", rounds); return rounds; } if (method.getName().equals("warmupRounds")) { log.trace("Intercepted warmupRounds() invocation: returning {}", WARMUP_ROUNDS); return WARMUP_ROUNDS; } if (method.getName().equals("annotationType")) { return BenchmarkOptions.class; } log.trace("Returning default value for method intercepted invocation of method {}", method.getName()); return method.getDefaultValue(); } }
AnnotationBuilder() { for (Method method : annotationType().getDeclaredMethods()) { values.put(method.getName(), method.getDefaultValue()); } }
/** * Creates a default value for the given method. * * @param method The method from which to attempt the extraction of a default value. * @return A default value representation. */ private static AnnotationValue<?, ?> defaultValueOf(Method method) { Object defaultValue = method.getDefaultValue(); return defaultValue == null ? MissingValue.of(method) : AnnotationDescription.ForLoadedAnnotation.asValue(defaultValue, method.getReturnType()); }
if (value != null && !value.equals(method.getDefaultValue())) { Class<?> parameterType = ReflectUtils.getBoxedClass(method.getReturnType()); if ("filter".equals(property) || "listener".equals(property)) {
private void validateDefaultValueConfiguration(Method aliasedAttribute) { Object defaultValue = this.sourceAttribute.getDefaultValue(); Object aliasedDefaultValue = aliasedAttribute.getDefaultValue(); if (defaultValue == null || aliasedDefaultValue == null) { String msg = String.format("Misconfigured aliases: attribute '%s' in annotation [%s] " + "and attribute '%s' in annotation [%s] must declare default values.", this.sourceAttributeName, this.sourceAnnotationType.getName(), aliasedAttribute.getName(), aliasedAttribute.getDeclaringClass().getName()); throw new AnnotationConfigurationException(msg); } if (!ObjectUtils.nullSafeEquals(defaultValue, aliasedDefaultValue)) { String msg = String.format("Misconfigured aliases: attribute '%s' in annotation [%s] " + "and attribute '%s' in annotation [%s] must declare the same default value.", this.sourceAttributeName, this.sourceAnnotationType.getName(), aliasedAttribute.getName(), aliasedAttribute.getDeclaringClass().getName()); throw new AnnotationConfigurationException(msg); } }
if (value != null && !value.equals(method.getDefaultValue())) { Class<?> parameterType = ReflectUtils.getBoxedClass(method.getReturnType()); if ("filter".equals(property) || "listener".equals(property)) {
/** * Retrieve the <em>default value</em> of a named 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(Annotation, String) */ @Nullable public static Object getDefaultValue( @Nullable Class<? extends Annotation> annotationType, @Nullable String attributeName) { if (annotationType == null || !StringUtils.hasText(attributeName)) { return null; } try { return annotationType.getDeclaredMethod(attributeName).getDefaultValue(); } catch (Throwable ex) { handleIntrospectionFailure(annotationType, ex); return null; } }
class Defaults implements InvocationHandler { public static <A extends Annotation> A of(Class<A> annotation) { return (A) Proxy.newProxyInstance(annotation.getClassLoader(), new Class[] {annotation}, new Defaults()); } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return method.getDefaultValue(); } } Settings s = Defaults.of(Settings.class); System.out.printf("%s\n%s\n%s\n", s.a(), s.b(), s.c());
public static AnnotationSpec get(Annotation annotation, boolean includeDefaultValues) { Builder builder = builder(annotation.annotationType()); try { Method[] methods = annotation.annotationType().getDeclaredMethods(); Arrays.sort(methods, Comparator.comparing(Method::getName)); for (Method method : methods) { Object value = method.invoke(annotation); if (!includeDefaultValues) { if (Objects.deepEquals(value, method.getDefaultValue())) { continue; } } if (value.getClass().isArray()) { for (int i = 0; i < Array.getLength(value); i++) { builder.addMemberForValue(method.getName(), Array.get(value, i)); } continue; } if (value instanceof Annotation) { builder.addMember(method.getName(), "$L", get((Annotation) value)); continue; } builder.addMemberForValue(method.getName(), value); } } catch (Exception e) { throw new RuntimeException("Reflecting " + annotation + " failed!", e); } return builder.build(); }
try { Object attributeValue = method.invoke(annotation); Object defaultValue = method.getDefaultValue(); if (defaultValue != null && ObjectUtils.nullSafeEquals(attributeValue, defaultValue)) { attributeValue = new DefaultValueHolder(defaultValue);
Object defaultValue = annotationAttribute.getDefaultValue(); if (defaultValue != null && !attributes.containsKey(attributeName)) { if (defaultValue instanceof Annotation) {
private static void setMethodDefaultValue(MethodNode mn, Method m) { Object defaultValue = m.getDefaultValue(); ConstantExpression cExp = ConstantExpression.NULL; if (defaultValue!=null) cExp = new ConstantExpression(defaultValue); mn.setCode(new ReturnStatement(cExp)); mn.setAnnotationDefault(true); }