Java源码示例:com.facebook.litho.annotations.PropDefault

示例1
private static ImmutableList<PropDefaultModel> extractFromField(PsiField psiField) {
  final Annotation propDefaultAnnotation =
      PsiAnnotationProxyUtils.findAnnotationInHierarchy(psiField, PropDefault.class);
  if (propDefaultAnnotation == null) {
    return ImmutableList.of();
  }

  final ResType propDefaultResType = ((PropDefault) propDefaultAnnotation).resType();
  final int propDefaultResId = ((PropDefault) propDefaultAnnotation).resId();

  return ImmutableList.of(
      new PropDefaultModel(
          PsiTypeUtils.getTypeName(psiField.getType()),
          psiField.getName(),
          PsiModifierExtractor.extractModifiers(psiField.getModifierList()),
          psiField,
          propDefaultResType,
          propDefaultResId));
}
 
示例2
private static ImmutableList<PropDefaultModel> extractFromField(Element enclosedElement) {
  if (enclosedElement.getKind() != ElementKind.FIELD) {
    return ImmutableList.of();
  }

  final VariableElement variableElement = (VariableElement) enclosedElement;
  final Annotation propDefaultAnnotation = variableElement.getAnnotation(PropDefault.class);
  if (propDefaultAnnotation == null) {
    return ImmutableList.of();
  }

  final ResType propDefaultResType = ((PropDefault) propDefaultAnnotation).resType();
  final int propDefaultResId = ((PropDefault) propDefaultAnnotation).resId();

  return ImmutableList.of(
      new PropDefaultModel(
          TypeName.get(variableElement.asType()),
          variableElement.getSimpleName().toString(),
          ImmutableList.copyOf(new ArrayList<>(variableElement.getModifiers())),
          variableElement,
          propDefaultResType,
          propDefaultResId));
}
 
示例3
public static boolean isPropDefault(PsiField field) {
  return hasAnnotation(field, equals(PropDefault.class.getName()));
}
 
示例4
@PropDefault
public static void title$annotations() {}
 
示例5
@PropDefault
public final String getTitle() {
  return title;
}
 
示例6
/**
 * This attempts to extract a prop-default from a <em>method</em>. This is only necessary for
 * Kotlin KAPT generated code, which currently does a rather strange thing where it generates a
 * method <code>void field_name$annotations()</code> for every <code>field_name</code> that has
 * all annotations for said field.
 *
 * <p>So, if we find a method that looks like this, and it contains a <code>PropDefault</code>
 * annotation, we will try to find a matching field of this name and add use it as basis for our
 * prop-default.
 */
private static ImmutableList<PropDefaultModel> extractFromMethod(Element enclosedElement) {
  if (enclosedElement.getKind() != ElementKind.METHOD) {
    return ImmutableList.of();
  }

  final ExecutableElement methodElement = (ExecutableElement) enclosedElement;

  final Annotation propDefaultAnnotation = methodElement.getAnnotation(PropDefault.class);
  if (propDefaultAnnotation == null) {
    return ImmutableList.of();
  }

  final String methodName = methodElement.getSimpleName().toString();

  boolean isPropDefaultWithoutGet =
      methodName.endsWith("$annotations")
          && methodElement.getReturnType().getKind() == TypeKind.VOID;

  final String baseName;

  /*
   * In case an [@PropDefault] annotated variable does not include `get` on the Kotlin
   * annotation, we fallback to the previous method of identifying `PropDefault` values.
   * Note here that this method is deprecated and might be removed from KAPT some time in
   * future.
   *
   * If a user annotates that variable with `@get:PropDefault` we identify the
   * `PropDefault` values through the accompanying `get` method of that variable.
   * */
  if (isPropDefaultWithoutGet) {
    baseName = methodName.subSequence(0, methodName.indexOf('$')).toString();
  } else {
    baseName =
        methodName.replaceFirst("get", "").substring(0, 1).toLowerCase()
            + methodName.replaceFirst("get", "").substring(1);
  }

  final Optional<? extends Element> element =
      enclosedElement.getEnclosingElement().getEnclosedElements().stream()
          .filter(e -> e.getSimpleName().toString().equals(baseName))
          .findFirst();

  final ResType propDefaultResType = ((PropDefault) propDefaultAnnotation).resType();
  final int propDefaultResId = ((PropDefault) propDefaultAnnotation).resId();

  return element
      .map(
          e ->
              ImmutableList.of(
                  new PropDefaultModel(
                      TypeName.get(e.asType()),
                      baseName,
                      ImmutableList.copyOf(new ArrayList<>(methodElement.getModifiers())),
                      methodElement,
                      propDefaultResType,
                      propDefaultResId)))
      .orElseGet(ImmutableList::of);
}