001package react4j.annotations;
002
003import arez.annotations.ActAsComponent;
004import arez.annotations.ArezComponent;
005import arez.annotations.Memoize;
006import arez.annotations.Observable;
007import arez.annotations.Observe;
008import arez.component.Identifiable;
009import java.lang.annotation.Documented;
010import java.lang.annotation.ElementType;
011import java.lang.annotation.Target;
012import javax.annotation.Nonnull;
013import react4j.Keyed;
014
015/**
016 * Annotation used to specify an abstract method that returns a prop.
017 * The property is extracted from Reacts underlying props object. By default the prop is passed as
018 * a value in when creating the view but it can also be retrieved from the react context.
019 *
020 * <p>The method that is annotated with this annotation must also comply with the following constraints:</p>
021 * <ul>
022 * <li>Must not be annotated with any other react annotation</li>
023 * <li>Must have 0 parameters</li>
024 * <li>Must return a value</li>
025 * <li>Must be an abstract instance method</li>
026 * <li>Must not throw exceptions</li>
027 * <li>Must be accessible from the same package as the class annotated by {@link View}</li>
028 * <li>
029 *   Should not be public as not expected to be invoked outside the view. A warning will be generated but can
030 *   be suppressed by the {@link SuppressWarnings} or {@link SuppressReact4jWarnings} annotations with a key
031 *   "React4j:PublicMethod". This warning is also suppressed by the annotation processor if it is implementing
032 *   an interface method.
033 * </li>
034 * <li>
035 *   Should not be protected if in the class annotated with the {@link View} annotation as the method is not
036 *   expected to be invoked outside the view. A warning will be generated but can be suppressed by the
037 *   {@link SuppressWarnings} or {@link SuppressReact4jWarnings} annotations with a key "React4j:ProtectedMethod".
038 * </li>
039 * </ul>
040 */
041@Documented
042@Target( ElementType.METHOD )
043public @interface Input
044{
045  /**
046   * Return the name of the input.
047   * The name is the key used when accessing the input from the inputs object. It is also used when creating
048   * the builder steps associated with the inputs that set {@link #source()} to {@link Source#DEFAULT}.
049   *
050   * @return the name of the input.
051   */
052  @Nonnull
053  String name() default "<default>";
054
055  /**
056   * Return the qualifier used to access value from context.
057   * It must only be specified if {@link #source()} is set to {@link Source#CONTEXT}.
058   *
059   * @return the qualifier used to access value from context.
060   */
061  @Nonnull
062  String qualifier() default "";
063
064  /**
065   * The setting controlling where the input value is source from.
066   * If the source is set to {@link Source#CONTEXT} then the input is sometimes described as a "TreeInput"
067   * as it is transparently passed from a parent view to all child views. A "TreeInput" does not
068   * have to be specified by the user when creating the view.
069   *
070   * @return the setting controlling where the input value is source from.
071   */
072  Source source() default Source.DEFAULT;
073
074  /**
075   * Setting indicating whether the input should be supplied when the view is constructed.
076   * This influences validation when enabled and how the Builder class is created.
077   * If set to {@link Feature#ENABLE} then the user MUST supply the input and the builder will require the user
078   * to specify the value. If set to {@link Feature#DISABLE} then the user can optionally supply the input.
079   * If set to {@link Feature#AUTODETECT} then the annotation processor will treat it as {@link Feature#DISABLE}
080   * if there is a corresponding {@link InputDefault} for the input or the {@link #source()} parameter is set to
081   * {@link Source#CONTEXT}, otherwise it will be treated as {@link Feature#ENABLE}. The value of this setting
082   * must not be {@link Feature#ENABLE} when {@link #source()} is set to {@link Source#CONTEXT}.
083   *
084   * @return the flag indicating whether the input needs to be supplied.
085   */
086  Feature require() default Feature.AUTODETECT;
087
088  /**
089   * Indicate whether the input should be annotated by {@link Observable}.
090   *
091   * <p>If set to {@link Feature#AUTODETECT} then the input will be observable if and only if:</p>
092   * <ul>
093   * <li>{@link #immutable()} ()} is not set to {@code true}.</li>
094   * <li>the view has at least one method annotated with {@link Memoize} or {@link Observe}.</li>
095   * </ul>
096   *
097   * @return the enum indicating whether input is observable.
098   */
099  Feature observable() default Feature.AUTODETECT;
100
101  /**
102   * Return an enum indicating whether the view should check whether the value of the input is disposed
103   * prior to rendering. If the value is disposed then the render method will exit early and return null.
104   * If this parameter is set to {@link Feature#AUTODETECT} then the annotation processor will inspect the
105   * type of the input and treat it as {@link Feature#ENABLE} if the type is annotated with the {@link ArezComponent}
106   * annotation or the {@link ActAsComponent} annotation.
107   *
108   * @return an enum indicating whether the view should check whether the value of the input is disposed prior to rendering.
109   */
110  Feature disposable() default Feature.AUTODETECT;
111
112  /**
113   * Return an enum indicating whether the view should be disposed if the input is disposed. To enable this feature,
114   * the input MUST set {@link #immutable()} to <code>true</code>, {@link #disposable()} MUST resolve to
115   * {@link Feature#ENABLE}. The type of the input is expected to implement the {@link arez.component.DisposeNotifier}
116   * interface either directly or indirectly. If this parameter is set to {@link Feature#AUTODETECT} then the
117   * annotation processor will treat it as {@link Feature#ENABLE} if {@link #immutable()} is <code>true</code> and
118   * {@link #disposable()} resolves to {@link Feature#ENABLE}.
119   *
120   * @return an enum indicating whether the view should be disposed if the input is disposed.
121   */
122  Feature dependency() default Feature.AUTODETECT;
123
124  /**
125   * True if the input is not expected to change after initial value is set. If the value of the input does change
126   * then it is expected that the view will be unmounted and a new view created. This is implemented
127   * by synthesizing a key for the view every time the view that is derived from this input. To enable this
128   * the annotation processor must be able to identify the type of the input so that a key can be synthesized. The
129   * following types are supported by the annotation processor;
130   *
131   * <ul>
132   * <li>primitive types (i.e. boolean, short etc) and their corresponding boxed types (i.e. {@link java.lang.Boolean}, {@link java.lang.Short} etc).</li>
133   * <li>the {@link java.lang.String} type</li>
134   * <li>any class that implements {@link Keyed}</li>
135   * <li>any class that is annotated with {@link ArezComponent} where the {@link ArezComponent#requireId()} parameter does not resolve to {@link arez.annotations.Feature#DISABLE}</li>
136   * <li>any class or interface that is annotated with {@link ActAsComponent}. It is assumed that every implementation is an Arez component where the {@link ArezComponent#requireId()} parameter does not resolve to {@link arez.annotations.Feature#DISABLE}</li>
137   * <li>any class or interface that is compatible with {@link Identifiable}</li>
138   * <li>if none of the above scenarios is valid then the code will attempt to derive the key at runtime. First via the {@link Keyed} interface, then the {@link Identifiable} interface and if these strategies are not possible then toString() will be invoked on the input.</li>
139   * </ul>
140   *
141   * <p>It should be noted that if a type implements {@link Keyed} and is annotated with either {@link ArezComponent}
142   * or {@link ActAsComponent} then the annotation processor will assume the {@link Keyed} interface is to used in
143   * preference to other alternative strategies.</p>
144   *
145   * @return true if changing the input recreates the view.
146   */
147  boolean immutable() default false;
148
149  /**
150   * Enum where the input is sourced from.
151   */
152  enum Source
153  {
154    /**
155     * The input value is passed to the view during construction.
156     */
157    DEFAULT,
158    /**
159     * The input value is retrieved from the react context.
160     */
161    CONTEXT
162  }
163}