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

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

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

Field.getAnnotations介绍

暂无

代码示例

代码示例来源:origin: junit-team/junit4

public Annotation[] getAnnotations() {
  return field.getAnnotations();
}

代码示例来源:origin: google/j2objc

@Override
public Annotation[] getAnnotations() {
  return fField.getAnnotations();
}

代码示例来源:origin: JakeWharton/butterknife

private static boolean isRequired(Field field) {
 for (Annotation annotation : field.getAnnotations()) {
  if (annotation.annotationType().getSimpleName().equals("Nullable")) {
   return false;
  }
 }
 return true;
}

代码示例来源:origin: h2oai/h2o-2

private static boolean ignore(Field f) {
 for( Annotation a : f.getAnnotations() )
  if( a.annotationType() == Ignore.class )
   return true;
 return false;
}

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

public List<Annotation> callFieldGetAnnotations(Field m) {
  return Arrays.asList(m.getAnnotations());
}
public List<Annotation> callFieldGetDeclaredAnnotations(Field m) {

代码示例来源:origin: MovingBlocks/Terasology

private Annotation getFactory(Field field) {
  Annotation[] annotations = field.getAnnotations();
  for (Annotation annotation : annotations) {
    if (factories.containsKey(annotation.annotationType())) {
      return annotation;
    }
  }
  return null;
}

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

@Override
public List<Annotation> getAnnotations() {
  Annotation[] annotations = null;
  if (read != null) {
    annotations = read.getAnnotations();
  } else if (field != null) {
    annotations = field.getAnnotations();
  }
  return annotations != null ? Arrays.asList(annotations) : delegate.getAnnotations();
}

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

@Override
public List<Annotation> getAnnotations() {
  return ArrayUtils.toUnmodifiableList(field.getAnnotations());
}

代码示例来源:origin: iSoron/uhabits

private List<Pair<Field, Column>> getFieldColumnPairs()
{
  List<Pair<Field, Column>> fields = new ArrayList<>();
  for (Field field : klass.getDeclaredFields())
    for (Annotation annotation : field.getAnnotations())
    {
      if (!(annotation instanceof Column)) continue;
      Column column = (Column) annotation;
      fields.add(new ImmutablePair<>(field, column));
    }
  return fields;
}

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

try {
  Field field = getDeclaringClass().getDeclaredField(this.parameterName);
  Annotation[] fieldAnns = field.getAnnotations();
  if (fieldAnns.length > 0) {
    List<Annotation> merged = new ArrayList<>(anns.length + fieldAnns.length);

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

/**
 * Create a new type descriptor from a {@link Field}.
 * <p>Use this constructor when a source or target conversion point is a field.
 * @param field the field
 */
public TypeDescriptor(Field field) {
  this.resolvableType = ResolvableType.forField(field);
  this.type = this.resolvableType.resolve(field.getType());
  this.annotatedElement = new AnnotatedElementAdapter(field.getAnnotations());
}

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

/**
 * Obtain the annotations associated with the wrapped field or method/constructor parameter.
 */
public Annotation[] getAnnotations() {
  if (this.field != null) {
    Annotation[] fieldAnnotations = this.fieldAnnotations;
    if (fieldAnnotations == null) {
      fieldAnnotations = this.field.getAnnotations();
      this.fieldAnnotations = fieldAnnotations;
    }
    return fieldAnnotations;
  }
  else {
    return obtainMethodParameter().getParameterAnnotations();
  }
}

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

@Override public void attach(Linker linker) {
 bindings = new Binding<?>[fields.length];
 for (int i = 0; i < fields.length; i++) {
  Field field = fields[i];
  String key = Keys.get(field.getGenericType(), field.getAnnotations(), field);
  bindings[i] = linker.requestBinding(key, field, loader);
 }
}

代码示例来源:origin: swagger-api/swagger-core

/**
 * Collects field-level parameters from class.
 *
 * @param cls        is a class for collecting
 * @param components
 * @return the collection of supported parameters
 */
public static List<Parameter> collectFieldParameters(Class<?> cls, Components components, javax.ws.rs.Consumes classConsumes, JsonView jsonViewAnnotation) {
  final List<Parameter> parameters = new ArrayList<Parameter>();
  for (Field field : ReflectionUtils.getDeclaredFields(cls)) {
    final List<Annotation> annotations = Arrays.asList(field.getAnnotations());
    final Type genericType = field.getGenericType();
    parameters.addAll(collectParameters(genericType, annotations, components, classConsumes, jsonViewAnnotation));
  }
  return parameters;
}

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

/**
 * Create a new type descriptor from a {@link Field}.
 * <p>Use this constructor when a source or target conversion point is a field.
 * @param field the field
 */
public TypeDescriptor(Field field) {
  this.resolvableType = ResolvableType.forField(field);
  this.type = this.resolvableType.resolve(field.getType());
  this.annotatedElement = new AnnotatedElementAdapter(field.getAnnotations());
}

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

public static AnnotatedElement getAnnotatedElement(Class<?> beanClass, String propertyName, Class<?> propertyClass) {
  Field field = getFieldOrNull(beanClass, propertyName);
  Method method = getGetterOrNull(beanClass, propertyName, propertyClass);
  if (field == null || field.getAnnotations().length == 0) {
    return (method != null && method.getAnnotations().length > 0) ? method : EMPTY;
  } else if (method == null || method.getAnnotations().length == 0) {
    return field;
  } else {
    return new Annotations(field, method);
  }
}

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

/**
 * Obtain the annotations associated with the wrapped field or method/constructor parameter.
 */
public Annotation[] getAnnotations() {
  if (this.field != null) {
    Annotation[] fieldAnnotations = this.fieldAnnotations;
    if (fieldAnnotations == null) {
      fieldAnnotations = this.field.getAnnotations();
      this.fieldAnnotations = fieldAnnotations;
    }
    return fieldAnnotations;
  }
  else {
    return obtainMethodParameter().getParameterAnnotations();
  }
}

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

@Override
public void process(Class<?> clazz, Object testInstance) {
  Field[] fields = clazz.getDeclaredFields();
  for (Field field : fields) {
    boolean alreadyAssigned = false;
    for(Annotation annotation : field.getAnnotations()) {
      Object mock = createMockFor(annotation, field);
      if (mock != null) {
        throwIfAlreadyAssigned(field, alreadyAssigned);
        alreadyAssigned = true;
        try {
          setField(testInstance, field,mock);
        } catch (Exception e) {
          throw new MockitoException("Problems setting field " + field.getName() + " annotated with "
              + annotation, e);
        }
      }
    }
  }
}

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

private String fieldKey(String fieldName) throws NoSuchFieldException {
 Field field = KeysTest.class.getDeclaredField(fieldName);
 return Keys.get(field.getGenericType(), field.getAnnotations(), field);
}

代码示例来源:origin: google/j2objc

@SuppressWarnings("deprecation")
static void processAnnotationDeprecatedWay(AnnotationEngine annotationEngine, Object testClass, Field field) {
  boolean alreadyAssigned = false;
  for(Annotation annotation : field.getAnnotations()) {
    Object mock = annotationEngine.createMockFor(annotation, field);
    if (mock != null) {
      throwIfAlreadyAssigned(field, alreadyAssigned);
      alreadyAssigned = true;                
      try {
        new FieldSetter(testClass, field).set(mock);
      } catch (Exception e) {
        throw new MockitoException("Problems setting field " + field.getName() + " annotated with "
            + annotation, e);
      }
    }
  }
}

相关文章

微信公众号

最新文章

更多