com.facebook.litho.annotations.PropDefault Java Examples

The following examples show how to use com.facebook.litho.annotations.PropDefault. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PsiPropDefaultsExtractor.java    From litho with Apache License 2.0 6 votes vote down vote up
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));
}
 
Example #2
Source File: PropDefaultsExtractor.java    From litho with Apache License 2.0 6 votes vote down vote up
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));
}
 
Example #3
Source File: LithoPluginUtils.java    From litho with Apache License 2.0 4 votes vote down vote up
public static boolean isPropDefault(PsiField field) {
  return hasAnnotation(field, equals(PropDefault.class.getName()));
}
 
Example #4
Source File: PropDefaultsExtractorTest.java    From litho with Apache License 2.0 4 votes vote down vote up
@PropDefault
public static void title$annotations() {}
 
Example #5
Source File: PropDefaultsExtractorTest.java    From litho with Apache License 2.0 4 votes vote down vote up
@PropDefault
public final String getTitle() {
  return title;
}
 
Example #6
Source File: PropDefaultsExtractor.java    From litho with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
}