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

x33g5p2x  于2022-01-17 转载在 其他  
字(8.4k)|赞(0)|评价(0)|浏览(490)

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

Field.getAnnotation介绍

暂无

代码示例

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

/**
 * Returns the annotation instance for the given annotation type.
 *
 * @param annotationClass Tha annotation type to retrieve.
 * @param <A> Type of the annotation.
 * @return The annotation instance.
 */
public <A extends Annotation> A annotation(Class<A> annotationClass) {
  return field.getAnnotation(annotationClass);
}

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

private boolean isTransient(Class c, Field field) {
  if (Modifier.isTransient(field.getModifiers()))
    return true;
  while (c.getName().indexOf("$") >= 0) {
    c = c.getSuperclass(); // patch fuer reallive queries, kontraktor spore
  }
  if ( field.getName().startsWith("this$") && c.getAnnotation(AnonymousTransient.class) != null )
    return true;
  return (c.getAnnotation(Transient.class) != null && field.getAnnotation(Serialize.class) == null);
}

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

@Override
public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
  A annotation;
  if (read != null) {
    annotation = read.getAnnotation(annotationType);
  } else if (field != null) {
    annotation = field.getAnnotation(annotationType);
  } else {
    annotation = delegate.getAnnotation(annotationType);
  }
  return annotation;
}

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

public static <T extends Annotation> T getAnnotation(Class<?> clazz, String fieldName, Class<T> annotationClass) {
  try {
    Field field = getDeclaredField(clazz, fieldName);
    if (!field.isAccessible()) {
      field.setAccessible(true);
    }
    return field.getAnnotation(annotationClass);
  } catch (NoSuchFieldException e) {
    return null;
  }
}

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

public static <T extends Annotation> T getAnnotation(Class<?> clazz, String fieldName, Class<T> annotationClass) {
  try {
    Field field = getDeclaredField(clazz, fieldName);
    if (!field.isAccessible()) {
      field.setAccessible(true);
    }
    return field.getAnnotation(annotationClass);
  } catch (NoSuchFieldException e) {
    return null;
  }
}

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

/**
 * Retrieve a field/parameter annotation of the given type, if any.
 * @param annotationType the annotation type to retrieve
 * @return the annotation instance, or {@code null} if none found
 * @since 4.3.9
 */
@Nullable
public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
  return (this.field != null ? this.field.getAnnotation(annotationType) :
      obtainMethodParameter().getParameterAnnotation(annotationType));
}

代码示例来源:origin: apache/flink

private static Collection<OptionWithMetaInfo> findCommonOptions(String rootDir, String module, String packageName, String pathPrefix) throws IOException, ClassNotFoundException {
  Collection<OptionWithMetaInfo> commonOptions = new ArrayList<>(32);
  processConfigOptions(rootDir, module, packageName, pathPrefix, optionsClass -> extractConfigOptions(optionsClass).stream()
    .filter(optionWithMetaInfo -> optionWithMetaInfo.field.getAnnotation(Documentation.CommonOption.class) != null)
    .forEachOrdered(commonOptions::add));
  return commonOptions;
}

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

/**
 * Build a {@link org.springframework.aop.aspectj.DeclareParentsAdvisor}
 * for the given introduction field.
 * <p>Resulting Advisors will need to be evaluated for targets.
 * @param introductionField the field to introspect
 * @return the Advisor instance, or {@code null} if not an Advisor
 */
@Nullable
private Advisor getDeclareParentsAdvisor(Field introductionField) {
  DeclareParents declareParents = introductionField.getAnnotation(DeclareParents.class);
  if (declareParents == null) {
    // Not an introduction field
    return null;
  }
  if (DeclareParents.class == declareParents.defaultImpl()) {
    throw new IllegalStateException("'defaultImpl' attribute must be set on DeclareParents");
  }
  return new DeclareParentsAdvisor(
      introductionField.getType(), declareParents.value(), declareParents.defaultImpl());
}

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

protected void storeIndex(Field field, Object me, Object arg) {
  if (field.getAnnotation(RIndex.class) != null) {
    NamingScheme namingScheme = objectBuilder.getNamingScheme(me.getClass().getSuperclass());
    String indexName = namingScheme.getIndexName(me.getClass().getSuperclass(), field.getName());
    RSetMultimap<Object, Object> map = redisson.getSetMultimap(indexName, namingScheme.getCodec());
    map.put(arg, ((RLiveObject) me).getLiveObjectId());
  }
}

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

protected void storeIndex(Field field, Object me, Object arg) {
  if (field.getAnnotation(RIndex.class) != null) {
    NamingScheme namingScheme = objectBuilder.getNamingScheme(me.getClass().getSuperclass());
    String indexName = namingScheme.getIndexName(me.getClass().getSuperclass(), field.getName());
    RSetMultimap<Object, Object> map = redisson.getSetMultimap(indexName, namingScheme.getCodec());
    map.put(arg, ((RLiveObject) me).getLiveObjectId());
  }
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testEquivalence() {
  assertTrue(AnnotationUtils.equals(field1.getAnnotation(TestAnnotation.class), field2.getAnnotation(TestAnnotation.class)));
  assertTrue(AnnotationUtils.equals(field2.getAnnotation(TestAnnotation.class), field1.getAnnotation(TestAnnotation.class)));
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testNonEquivalentAnnotationsOfSameType() {
  assertFalse(AnnotationUtils.equals(field1.getAnnotation(TestAnnotation.class), field3.getAnnotation(TestAnnotation.class)));
  assertFalse(AnnotationUtils.equals(field3.getAnnotation(TestAnnotation.class), field1.getAnnotation(TestAnnotation.class)));
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testAnnotationsOfDifferingTypes() {
  assertFalse(AnnotationUtils.equals(field1.getAnnotation(TestAnnotation.class), field4.getAnnotation(NestAnnotation.class)));
  assertFalse(AnnotationUtils.equals(field4.getAnnotation(NestAnnotation.class), field1.getAnnotation(TestAnnotation.class)));
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testSameInstance() {
  assertTrue(AnnotationUtils.equals(field1.getAnnotation(TestAnnotation.class), field1.getAnnotation(TestAnnotation.class)));
}

代码示例来源:origin: code4craft/webmagic

public ObjectFormatter build() {
    Formatter formatter = field.getAnnotation(Formatter.class);
    if (formatter != null && !formatter.formatter().equals(Formatter.DEFAULT_FORMATTER)) {
      return initFormatter(formatter.formatter(), formatter.value());
    }
    if (formatter == null || formatter.subClazz().equals(Void.class)) {
      return initFormatterForType(field.getType(), formatter != null ? formatter.value() : null);
    } else {
      return initFormatterForType(formatter.subClazz(), formatter.value());
    }
  }
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test(timeout = 666000)
public void testHashCode() throws Exception {
  final Test test = getClass().getDeclaredMethod("testHashCode").getAnnotation(Test.class);
  assertEquals(test.hashCode(), AnnotationUtils.hashCode(test));
  final TestAnnotation testAnnotation1 = field1.getAnnotation(TestAnnotation.class);
  assertEquals(testAnnotation1.hashCode(), AnnotationUtils.hashCode(testAnnotation1));
  final TestAnnotation testAnnotation3 = field3.getAnnotation(TestAnnotation.class);
  assertEquals(testAnnotation3.hashCode(), AnnotationUtils.hashCode(testAnnotation3));
}

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

private <T> Object generateId(Class<T> entityClass) throws NoSuchFieldException {
  String idFieldName = getRIdFieldName(entityClass);
  RId annotation = ClassUtils.getDeclaredField(entityClass, idFieldName)
      .getAnnotation(RId.class);
  Resolver resolver = getResolver(entityClass, annotation.generator(), annotation);
  Object id = resolver.resolve(entityClass, annotation, idFieldName, redisson);
  return id;
}

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

private <T> Object generateId(Class<T> entityClass) throws NoSuchFieldException {
  String idFieldName = getRIdFieldName(entityClass);
  RId annotation = ClassUtils.getDeclaredField(entityClass, idFieldName)
      .getAnnotation(RId.class);
  Resolver resolver = getResolver(entityClass, annotation.generator(), annotation);
  Object id = resolver.resolve(entityClass, annotation, idFieldName, redisson);
  return id;
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testOneArgNull() {
  assertFalse(AnnotationUtils.equals(field1.getAnnotation(TestAnnotation.class), null));
  assertFalse(AnnotationUtils.equals(null, field1.getAnnotation(TestAnnotation.class)));
}

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

protected static String getPropertyKey(Field field)
{
 JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class);
 if (null != jsonProperty) {
  return getPropertyKey(
    (jsonProperty.value() == null || jsonProperty.value().isEmpty())
    ? field.getName()
    : jsonProperty.value()
  );
 }
 return null;
}

相关文章

微信公众号

最新文章

更多