java.lang.reflect.Method.getDefaultValue()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(244)

本文整理了Java中java.lang.reflect.Method.getDefaultValue()方法的一些代码示例,展示了Method.getDefaultValue()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Method.getDefaultValue()方法的具体详情如下:
包路径:java.lang.reflect.Method
类名称:Method
方法名:getDefaultValue

Method.getDefaultValue介绍

[英]Returns the default value for the annotation member represented by this Method instance. If the member is of a primitive type, an instance of the corresponding wrapper type is returned. Returns null if no default is associated with the member, or if the method instance does not represent a declared member of an annotation type.
[中]返回此方法实例表示的注释成员的默认值。如果成员是基元类型,则返回相应包装器类型的实例。如果没有与成员关联的默认值,或者如果方法实例不表示注释类型的声明成员,则返回null。

代码示例

代码示例来源:origin: robolectric/robolectric

@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  return method.getDefaultValue();
 }
}));

代码示例来源:origin: spring-projects/spring-loaded

public static Object callGetDefaultValue(Method thiz) {
  return thiz.getDefaultValue();
}

代码示例来源:origin: com.google.inject/guice

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;
}

代码示例来源:origin: spring-projects/spring-framework

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);
  }
}

代码示例来源:origin: stackoverflow.com

Settings.class.getMethod("a").getDefaultValue()
Settings.class.getMethod("b").getDefaultValue()
Settings.class.getMethod("c").getDefaultValue()

代码示例来源:origin: redisson/redisson

/**
 * {@inheritDoc}
 */
public AnnotationValue<?, ?> getDefaultValue() {
  Object value = method.getDefaultValue();
  return value == null
      ? AnnotationValue.UNDEFINED
      : AnnotationDescription.ForLoadedAnnotation.asValue(value, method.getReturnType());
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * 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;
  }
}

代码示例来源:origin: com.google.inject/guice

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();
}

代码示例来源:origin: thinkaurelius/titan

@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();
  }
}

代码示例来源:origin: MorphiaOrg/morphia

AnnotationBuilder() {
  for (Method method : annotationType().getDeclaredMethods()) {
    values.put(method.getName(), method.getDefaultValue());
  }
}

代码示例来源:origin: redisson/redisson

/**
 * 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());
}

代码示例来源:origin: apache/incubator-dubbo

if (value != null && !value.equals(method.getDefaultValue())) {
  Class<?> parameterType = ReflectUtils.getBoxedClass(method.getReturnType());
  if ("filter".equals(property) || "listener".equals(property)) {

代码示例来源:origin: org.springframework/spring-core

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);
  }
}

代码示例来源:origin: apache/incubator-dubbo

if (value != null && !value.equals(method.getDefaultValue())) {
  Class<?> parameterType = ReflectUtils.getBoxedClass(method.getReturnType());
  if ("filter".equals(property) || "listener".equals(property)) {

代码示例来源:origin: org.springframework/spring-core

/**
 * 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;
  }
}

代码示例来源:origin: stackoverflow.com

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());

代码示例来源:origin: square/javapoet

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();
}

代码示例来源:origin: spring-projects/spring-framework

try {
  Object attributeValue = method.invoke(annotation);
  Object defaultValue = method.getDefaultValue();
  if (defaultValue != null && ObjectUtils.nullSafeEquals(attributeValue, defaultValue)) {
    attributeValue = new DefaultValueHolder(defaultValue);

代码示例来源:origin: spring-projects/spring-framework

Object defaultValue = annotationAttribute.getDefaultValue();
if (defaultValue != null && !attributes.containsKey(attributeName)) {
  if (defaultValue instanceof Annotation) {

代码示例来源:origin: org.codehaus.groovy/groovy

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);
}

相关文章

微信公众号

最新文章

更多