com.facebook.litho.annotations.OnCreateMountContent Java Examples

The following examples show how to use com.facebook.litho.annotations.OnCreateMountContent. 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: DelegateMethodValidationTest.java    From litho with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() {
  when(mLayoutSpecModel.getRepresentedObject()).thenReturn(mModelRepresentedObject);
  when(mMountSpecModel.getRepresentedObject()).thenReturn(mMountSpecObject);

  mOnCreateMountContent =
      SpecMethodModel.<DelegateMethod, Void>builder()
          .annotations(ImmutableList.of((Annotation) () -> OnCreateMountContent.class))
          .modifiers(ImmutableList.of(Modifier.STATIC))
          .name("onCreateMountContent")
          .returnTypeSpec(new TypeSpec(ClassName.bestGuess("java.lang.MadeUpClass")))
          .typeVariables(ImmutableList.of())
          .methodParams(
              ImmutableList.of(
                  MockMethodParamModel.newBuilder()
                      .name("c")
                      .type(ClassNames.ANDROID_CONTEXT)
                      .representedObject(new Object())
                      .build()))
          .representedObject(mOnCreateMountContentObject)
          .typeModel(null)
          .build();
}
 
Example #2
Source File: SizeSpecMountWrapperComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnCreateMountContent
static FrameLayout onCreateMountContent(Context c) {
  // This LithoView will contain the new tree that's created from this point onwards
  // TODO: T59446191 Replace with proper solution. Remove the use of FrameLayout.
  FrameLayout wrapperView = new FrameLayout(c);
  wrapperView.addView(new LithoView(c));
  return wrapperView;
}
 
Example #3
Source File: RecordsShouldUpdateSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnCreateMountContent
static View onCreateMountContent(Context c) {
  return new View(c);
}
 
Example #4
Source File: ShouldUseGlobalPoolTrueMountSpecLifecycleTesterSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@UiThread
@OnCreateMountContent
static View onCreateMountContent(Context c) {
  return new View(c);
}
 
Example #5
Source File: SimpleMountSpecTesterSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@UiThread
@OnCreateMountContent
static ColorDrawable onCreateMountContent(Context c) {
  return new ColorDrawable();
}
 
Example #6
Source File: MountSpecLifecycleTesterSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@UiThread
@OnCreateMountContent
static View onCreateMountContent(Context c) {
  StaticContainer.sLastCreatedView = new View(c);
  return StaticContainer.sLastCreatedView;
}
 
Example #7
Source File: ShouldUseGlobalPoolFalseMountSpecLifecycleTesterSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@UiThread
@OnCreateMountContent
static View onCreateMountContent(Context c) {
  return new View(c);
}
 
Example #8
Source File: MountSpecTriggerTesterSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@UiThread
@OnCreateMountContent
static View onCreateMountContent(Context c) {
  return new View(c);
}
 
Example #9
Source File: TreePropTestMountSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnCreateMountContent
static Drawable onCreateMountContent(Context c) {
  return null;
}
 
Example #10
Source File: TestCrasherOnMountSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnCreateMountContent
static LithoView onCreateMountContent(Context c) {
  return new LithoView(c);
}
 
Example #11
Source File: DuplicatePropValidationTest.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnCreateMountContent
static Drawable onCreateMountContent(ComponentContext c) {
  return null;
}
 
Example #12
Source File: TestMountSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnCreateMountContent
static Drawable onCreateMountContent(Context c) {
  return new ColorDrawable(Color.RED);
}
 
Example #13
Source File: DynamicPropsValidation.java    From litho with Apache License 2.0 4 votes vote down vote up
static List<SpecModelValidationError> validate(SpecModel specModel) {
  if (!(specModel instanceof MountSpecModel)) {
    return validateHasNoDynamicProps(specModel);
  }

  final List<SpecModelValidationError> validationErrors = new ArrayList<>();

  final SpecMethodModel method =
      SpecModelUtils.getMethodModelWithAnnotation(specModel, OnCreateMountContent.class);
  if (method == null) {
    validationErrors.add(
        new SpecModelValidationError(
            specModel.getRepresentedObject(),
            specModel.getSpecName()
                + " does not define @OnCreateMountContent method which is required for all @MountSpecs."));
    return validationErrors;
  }

  final TypeName mountType = method.returnType;

  final Map<String, List<SpecMethodModel<BindDynamicValueMethod, Void>>> propToMethodMap =
      new HashMap<>();
  for (SpecMethodModel<BindDynamicValueMethod, Void> methodModel :
      ((MountSpecModel) specModel).getBindDynamicValueMethods()) {
    if (!validateBindDynamicValueMethod(methodModel, mountType, validationErrors)) {
      continue;
    }

    final String propName = methodModel.methodParams.get(1).getName();

    if (!propToMethodMap.containsKey(propName)) {
      propToMethodMap.put(propName, new ArrayList<>());
    }

    propToMethodMap.get(propName).add(methodModel);
  }

  for (PropModel dynamicProp : SpecModelUtils.getDynamicProps(specModel)) {
    final List<SpecMethodModel<BindDynamicValueMethod, Void>> methods =
        propToMethodMap.get(dynamicProp.getName());
    if (methods == null) {
      validationErrors.add(
          new SpecModelValidationError(
              specModel.getRepresentedObject(),
              specModel.getSpecName()
                  + " does not provide @OnBindDynamicValue method for dynamic prop "
                  + dynamicProp.getName()));
    } else if (methods.size() > 1) {
      validationErrors.add(
          new SpecModelValidationError(
              specModel.getRepresentedObject(),
              specModel.getSpecName()
                  + " provides more than one @OnBindDynamicValue method for dynamic prop "
                  + dynamicProp.getName()));
    }
  }

  return validationErrors;
}
 
Example #14
Source File: DelegateMethodValidation.java    From litho with Apache License 2.0 4 votes vote down vote up
static List<SpecModelValidationError> validateMountSpecModel(MountSpecModel specModel) {
  List<SpecModelValidationError> validationErrors = new ArrayList<>();
  validationErrors.addAll(
      validateMethods(
          specModel,
          DelegateMethodDescriptions.MOUNT_SPEC_DELEGATE_METHODS_MAP,
          DelegateMethodDescriptions.INTER_STAGE_INPUTS_MAP));

  final SpecMethodModel<DelegateMethod, Void> onCreateMountContentModel =
      SpecModelUtils.getMethodModelWithAnnotation(specModel, OnCreateMountContent.class);
  if (onCreateMountContentModel == null) {
    validationErrors.add(
        new SpecModelValidationError(
            specModel.getRepresentedObject(),
            "All MountSpecs need to have a method annotated with @OnCreateMountContent."));
  } else {
    final TypeName mountType = onCreateMountContentModel.returnType;
    ImmutableList<Class<? extends Annotation>> methodsAcceptingMountTypeAsSecondParam =
        ImmutableList.of(OnMount.class, OnBind.class, OnUnbind.class, OnUnmount.class);
    for (Class<? extends Annotation> annotation : methodsAcceptingMountTypeAsSecondParam) {
      final SpecMethodModel<DelegateMethod, Void> method =
          SpecModelUtils.getMethodModelWithAnnotation(specModel, annotation);
      if (method != null
          && (method.methodParams.size() < 2
              || !method.methodParams.get(1).getTypeName().equals(mountType))) {
        validationErrors.add(
            new SpecModelValidationError(
                method.representedObject,
                "The second parameter of a method annotated with "
                    + annotation
                    + " must "
                    + "have the same type as the return type of the method annotated with "
                    + "@OnCreateMountContent (i.e. "
                    + mountType
                    + ")."));
      }
    }
  }

  return validationErrors;
}
 
Example #15
Source File: MountSpecModelFactory.java    From litho with Apache License 2.0 4 votes vote down vote up
private static TypeName getMountType(
    Elements elements, TypeElement element, EnumSet<RunMode> runMode) {
  TypeElement viewType = elements.getTypeElement(ClassNames.VIEW_NAME);
  TypeElement drawableType = elements.getTypeElement(ClassNames.DRAWABLE_NAME);

  for (Element enclosedElement : element.getEnclosedElements()) {
    if (enclosedElement.getKind() != ElementKind.METHOD) {
      continue;
    }

    OnCreateMountContent annotation = enclosedElement.getAnnotation(OnCreateMountContent.class);
    if (annotation != null) {
      if (annotation.mountingType() == MountingType.VIEW) {
        return ClassNames.COMPONENT_LIFECYCLE_MOUNT_TYPE_VIEW;
      }
      if (annotation.mountingType() == MountingType.DRAWABLE) {
        return ClassNames.COMPONENT_LIFECYCLE_MOUNT_TYPE_DRAWABLE;
      }

      TypeMirror initialReturnType = ((ExecutableElement) enclosedElement).getReturnType();
      if (runMode.contains(RunMode.ABI)) {
        // We can't access the supertypes of the return type, so let's guess, and we'll verify
        // that our guess was correct when we do a full build later.
        if (initialReturnType.toString().contains("Drawable")) {
          return ClassNames.COMPONENT_LIFECYCLE_MOUNT_TYPE_DRAWABLE;
        } else {
          return ClassNames.COMPONENT_LIFECYCLE_MOUNT_TYPE_VIEW;
        }
      }

      TypeMirror returnType = initialReturnType;
      while (returnType.getKind() != TypeKind.NONE && returnType.getKind() != TypeKind.VOID) {
        final TypeElement returnElement = (TypeElement) ((DeclaredType) returnType).asElement();

        if (returnElement.equals(viewType)) {
          if (initialReturnType.toString().contains("Drawable")) {
            throw new ComponentsProcessingException(
                "Mount type cannot be correctly inferred from the name of "
                    + element
                    + ".  Please specify `@OnCreateMountContent(mountingType = MountingType.VIEW)`.");
          }

          return ClassNames.COMPONENT_LIFECYCLE_MOUNT_TYPE_VIEW;
        } else if (returnElement.equals(drawableType)) {
          if (!initialReturnType.toString().contains("Drawable")) {
            throw new ComponentsProcessingException(
                "Mount type cannot be correctly inferred from the name of "
                    + element
                    + ".  Please specify `@OnCreateMountContent(mountingType = MountingType.DRAWABLE)`.");
          }
          return ClassNames.COMPONENT_LIFECYCLE_MOUNT_TYPE_DRAWABLE;
        }

        try {
          returnType = returnElement.getSuperclass();
        } catch (RuntimeException e) {
          throw new ComponentsProcessingException(
              "Failed to get mount type for "
                  + element
                  + ".  Try specifying `@OnCreateMountContent(mountingType = MountingType.VIEW)` (or DRAWABLE).");
        }
      }
    }
  }

  return ClassNames.COMPONENT_LIFECYCLE_MOUNT_TYPE_NONE;
}
 
Example #16
Source File: GlideImageSpec.java    From litho-glide with MIT License 4 votes vote down vote up
@OnCreateMountContent
static ImageView onCreateMountContent(Context c) {
  return new ImageView(c);
}
 
Example #17
Source File: PicassoImageSpec.java    From litho-picasso with MIT License 4 votes vote down vote up
@OnCreateMountContent
static ImageView onCreateMountContent(Context c) {
  ImageView imageView = new ImageView(c);
  return imageView;
}
 
Example #18
Source File: FrescoVitoImage2Spec.java    From fresco with MIT License 4 votes vote down vote up
@OnCreateMountContent(mountingType = MountingType.DRAWABLE)
static FrescoDrawable2 onCreateMountContent(Context c) {
  return FrescoVitoProvider.getController().createDrawable();
}
 
Example #19
Source File: EditTextSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnCreateMountContent
protected static EditTextWithEventHandlers onCreateMountContent(Context c) {
  return new EditTextWithEventHandlers(c);
}
 
Example #20
Source File: HorizontalScrollSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnCreateMountContent
static HorizontalScrollLithoView onCreateMountContent(Context c) {
  return new HorizontalScrollLithoView(c);
}
 
Example #21
Source File: ImageSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnCreateMountContent
static MatrixDrawable onCreateMountContent(Context c) {
  return new MatrixDrawable();
}
 
Example #22
Source File: CardClipSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnCreateMountContent
static CardClipDrawable onCreateMountContent(Context c) {
  return new CardClipDrawable();
}
 
Example #23
Source File: RecyclerSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnCreateMountContent
static SectionsRecyclerView onCreateMountContent(Context c) {
  return new SectionsRecyclerView(c, new LithoRecylerView(c));
}
 
Example #24
Source File: TransparencyEnabledCardClipSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnCreateMountContent
static TransparencyEnabledCardClipDrawable onCreateMountContent(Context c) {
  return new TransparencyEnabledCardClipDrawable();
}
 
Example #25
Source File: ProgressSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnCreateMountContent
static ProgressBar onCreateMountContent(Context c) {
  return new ProgressView(c);
}
 
Example #26
Source File: TextSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnCreateMountContent
static TextDrawable onCreateMountContent(Context c) {
  return new TextDrawable();
}
 
Example #27
Source File: TextInputSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnCreateMountContent
protected static EditTextWithEventHandlers onCreateMountContent(Context c) {
  return new EditTextWithEventHandlers(c);
}
 
Example #28
Source File: PreallocatedMountSpecLifecycleTesterSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@UiThread
@OnCreateMountContent
static View onCreateMountContent(Context c) {
  StaticContainer.sLastCreatedView = new View(c);
  return StaticContainer.sLastCreatedView;
}
 
Example #29
Source File: CardShadowSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnCreateMountContent
static CardShadowDrawable onCreateMountContent(Context c) {
  return new CardShadowDrawable();
}
 
Example #30
Source File: VerticalScrollSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnCreateMountContent
static LithoScrollView onCreateMountContent(Context context) {
  return (LithoScrollView)
      LayoutInflater.from(context).inflate(R.layout.litho_scroll_view, null, false);
}