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

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

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

Field.getDeclaredAnnotation介绍

暂无

代码示例

代码示例来源:origin: kostaskougios/cloning

public Strategy strategyFor(Object toBeCloned, Field field) {
    if (toBeCloned == null) return Strategy.IGNORE;
    if (field.getDeclaredAnnotation(annotationClass) != null) return strategy;
    return Strategy.IGNORE;
  }
};

代码示例来源:origin: uk.com.robust-it/cloning

public Strategy strategyFor(Object toBeCloned, Field field) {
    if (toBeCloned == null) return Strategy.IGNORE;
    if (field.getDeclaredAnnotation(annotationClass) != null) return strategy;
    return Strategy.IGNORE;
  }
};

代码示例来源:origin: jerolba/jfleet

private boolean reviewSupportedRelations(Field field) {
  JoinTable joinTable = field.getDeclaredAnnotation(JoinTable.class);
  if (joinTable != null) {
    logger.warn("JoinTable mapping unsupported, has no effect on join table persistence if needed");
    return false;
  }
  JoinColumns joinColumns = field.getDeclaredAnnotation(JoinColumns.class);
  if (joinColumns != null) {
    throw new UnsupportedOperationException("@JoinColumns annotation not supported");
  }
  return true;
}

代码示例来源:origin: alibaba/sca-best-practice

private Field findIdField(Class<?> clazz) {
  Field[] fields = clazz.getDeclaredFields();
  if (fields == null) {
    return null;
  }
  for (Field field : fields) {
    if (field.getDeclaredAnnotation(Id.class) != null) {
      return field;
    }
  }
  return null;
}

代码示例来源:origin: spinnaker/halyard

public List<Field> localFiles() {
 Class<?> clazz = getClass();
 List<Field> res = new ArrayList<>();
 while (clazz != null) {
  res.addAll(Arrays.stream(clazz.getDeclaredFields())
    .filter(f -> f.getDeclaredAnnotation(LocalFile.class) != null)
    .collect(Collectors.toList()));
  clazz = clazz.getSuperclass();
 }
 return res;
}

代码示例来源:origin: com.sap.cloud.servicesdk.prov/api

public static <T> List<String> getKeyProperties(T p) {
  Field[] ss = p.getClass().getDeclaredFields();
  return Arrays
      .asList(ss)
      .stream()
      .filter(propertyElement -> propertyElement
          .getDeclaredAnnotation(Key.class) != null)
      .map(propertyElement -> propertyElement.getName())
      .collect(Collectors.toList());
}

代码示例来源:origin: com.netflix.spinnaker.halyard/halyard-config

public List<Field> localFiles() {
 Class<?> clazz = getClass();
 List<Field> res = new ArrayList<>();
 while (clazz != null) {
  res.addAll(Arrays.stream(clazz.getDeclaredFields())
    .filter(f -> f.getDeclaredAnnotation(LocalFile.class) != null)
    .collect(Collectors.toList()));
  clazz = clazz.getSuperclass();
 }
 return res;
}

代码示例来源:origin: alfa-laboratory/akita

/**
 * Поиск по аннотации "Name"
 */
private void checkNamedAnnotations() {
  List<String> list = Arrays.stream(getClass().getDeclaredFields())
      .filter(f -> f.getDeclaredAnnotation(Name.class) != null)
      .map(f -> f.getDeclaredAnnotation(Name.class).value())
      .collect(toList());
  if (list.size() != new HashSet<>(list).size()) {
    throw new IllegalStateException("Найдено несколько аннотаций @Name с одинаковым значением в классе " + this.getClass().getName());
  }
}

代码示例来源:origin: alfa-laboratory/akita

/**
 * Поиск и инициализации элементов страницы без аннотации Optional
 */
private List<SelenideElement> readWithWrappedElements() {
  return Arrays.stream(getClass().getDeclaredFields())
      .filter(f -> f.getDeclaredAnnotation(Optional.class) == null)
      .map(this::extractFieldValueViaReflection)
      .flatMap(v -> v instanceof List ? ((List<?>) v).stream() : Stream.of(v))
      .map(AkitaPage::castToSelenideElement)
      .filter(Objects::nonNull)
      .collect(toList());
}

代码示例来源:origin: org.testifyproject/api

/**
 * Get {@link Real} annotation.
 *
 * @return optional with real annotation, empty optional otherwise
 */
default Optional<Real> getReal() {
  return ofNullable(getMember().getDeclaredAnnotation(Real.class));
}

代码示例来源:origin: org.testifyproject/api

/**
 * Get {@link Sut} annotation.
 *
 * @return sut annotation
 */
default Sut getSut() {
  return getMember().getDeclaredAnnotation(Sut.class);
}

代码示例来源:origin: org.testifyproject/api

/**
 * Get {@link Fake} annotation.
 *
 * @return optional with fake annotation, empty optional otherwise
 */
default Optional<Fake> getFake() {
  return ofNullable(getMember().getDeclaredAnnotation(Fake.class));
}

代码示例来源:origin: org.testifyproject/api

/**
 * Get {@link Fixture} annotation.
 *
 * @return optional with fixture annotation, empty optional otherwise
 */
default Optional<Fixture> getFixture() {
  return ofNullable(getMember().getDeclaredAnnotation(Fixture.class));
}

代码示例来源:origin: org.testifyproject/api

/**
 * Get {@link Property} annotation.
 *
 * @return optional with property annotation, empty optional otherwise
 */
default Optional<Property> getProperty() {
  return ofNullable(getMember().getDeclaredAnnotation(Property.class));
}

代码示例来源:origin: org.testifyproject/api

/**
 * Get {@link Virtual} annotation.
 *
 * @return optional with virtual annotation, empty optional otherwise
 */
default Optional<Virtual> getVirtual() {
  return ofNullable(getMember().getDeclaredAnnotation(Virtual.class));
}

代码示例来源:origin: alfa-laboratory/akita

private void eachForm(Consumer<AkitaPage> func) {
  Arrays.stream(getClass().getDeclaredFields())
      .filter(f -> f.getDeclaredAnnotation(Optional.class) == null)
      .forEach(f -> {
        if (AkitaPage.class.isAssignableFrom(f.getType())){
          AkitaPage akitaPage = AkitaScenario.getInstance().getPage((Class<? extends AkitaPage>)f.getType()).initialize();
          func.accept(akitaPage);
        }
      });
}

代码示例来源:origin: gitchennan/elasticsearch-query-toolkit

public static void mapDataType(XContentBuilder mappingBuilder, Field field) throws IOException {
    if (!isValidBinaryType(field)) {
      throw new IllegalArgumentException(
          String.format("field type[%s] is invalid type of binary.", field.getType()));
    }

    BinaryField binaryField = field.getDeclaredAnnotation(BinaryField.class);
    mapDataType(mappingBuilder, binaryField);
  }
}

代码示例来源:origin: gitchennan/elasticsearch-query-toolkit

public static void mapDataType(XContentBuilder mappingBuilder, Field field) throws IOException {
    if (!isValidIPFieldType(field)) {
      throw new IllegalArgumentException(
          String.format("field type[%s] is invalid type of ip.", field.getType()));
    }

    IPField ipField = field.getDeclaredAnnotation(IPField.class);
    mapDataType(mappingBuilder, ipField);
  }
}

代码示例来源:origin: gitchennan/elasticsearch-query-toolkit

public static void mapDataType(XContentBuilder mappingBuilder, Field field) throws IOException {
    if (!isValidCompletionFieldType(field)) {
      throw new IllegalArgumentException(
          String.format("field type[%s] is invalid type of string.", field.getType()));
    }

    CompletionField completionField = field.getDeclaredAnnotation(CompletionField.class);
    mapDataType(mappingBuilder, completionField);
  }
}

代码示例来源:origin: com.github.almasb/fxgl-core

public static <A extends java.lang.annotation.Annotation> Array<Field>
  findFieldsByAnnotation(Object instance, Class<A> annotationClass) {
  Array<Field> fields = new Array<>();
  for (java.lang.reflect.Field field : instance.getClass().getDeclaredFields()) {
    if (field.getDeclaredAnnotation(annotationClass) != null) {
      fields.add(field);
    }
  }
  return fields;
}

相关文章

微信公众号

最新文章

更多