001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.annotation;
018
019import static java.lang.annotation.ElementType.*;
020import static java.lang.annotation.RetentionPolicy.*;
021
022import java.lang.annotation.*;
023
024import org.apache.juneau.*;
025
026/**
027 * Annotation that can be applied to classes to control how they are marshalled.
028 *
029 * <p>
030 * Can be used in the following locations:
031 * <ul>
032 *    <li>Marshalled classes.
033 *    <li><ja>@Rest</ja>-annotated classes and <ja>@RestOp</ja>-annotated methods when an {@link #on()} value is specified.
034 * </ul>
035 *
036 * <p>
037 * This annotation is typically only applied to non-bean classes.  The {@link Bean @Bean} annotation contains equivalent
038 * functionality for bean classes.
039 *
040 * <h5 class='section'>See Also:</h5><ul>
041 * </ul>
042 */
043@Documented
044@Target({METHOD,TYPE})
045@Retention(RUNTIME)
046@Inherited
047@Repeatable(MarshalledAnnotation.Array.class)
048@ContextApply(MarshalledAnnotation.Applier.class)
049public @interface Marshalled {
050
051    /**
052     * Optional description for the exposed API.
053     *
054     * @return The annotation value.
055     * @since 9.2.0
056     */
057    String[] description() default {};
058
059    /**
060    * POJO example.
061    *
062    * <p>
063    * Specifies an example of the specified class in Simplified JSON format.
064    *
065    * <p>
066    * Examples are used in cases such as POJO examples in Swagger documents.
067    *
068    * <h5 class='section'>Example:</h5>
069    * <p class='bjava'>
070    *    <ja>@Marshalled</ja>(example=<js>"{foo:'bar'}"</js>)
071    *    <jk>public class</jk> MyClass {...}
072    * </p>
073    *
074    * <h5 class='section'>Notes:</h5><ul>
075    *    <li class='note'>
076    *       Setting applies to specified class and all subclasses.
077    *    <li class='note'>
078    *       Keys are the class of the example.
079    *       <br>Values are JSON 5 representation of that class.
080    *    <li class='note'>
081    *       POJO examples can also be defined on classes via the following:
082    *       <ul class='spaced-list'>
083    *          <li>A static field annotated with {@link Example @Example}.
084    *          <li>A static method annotated with {@link Example @Example} with zero arguments or one {@link BeanSession} argument.
085    *          <li>A static method with name <c>example</c> with no arguments or one {@link BeanSession} argument.
086    *       </ul>
087    *    <li class='note'>
088    *       Supports <a class="doclink" href="https://juneau.apache.org/docs/topics/DefaultVarResolver">VarResolver.DEFAULT</a> (e.g. <js>"$C{myConfigVar}"</js>).
089    * </ul>
090    *
091    * <h5 class='section'>See Also:</h5><ul>
092    *    <li class='ja'>{@link Example}
093    * </ul>
094    *
095    * @return The annotation value.
096    */
097   String example() default "";
098
099   /**
100    * Implementation class.
101    *
102    * <p>
103    * For interfaces and abstract classes this method can be used to specify an implementation class for the
104    * interface/abstract class so that instances of the implementation class are used when instantiated (e.g. during a
105    * parse).
106    *
107    * <h5 class='section'>Example:</h5>
108    * <p class='bjava'>
109    *    <ja>@Marshalled</ja>(implClass=MyInterfaceImpl.<jk>class</jk>)
110    *    <jk>public class</jk> MyInterface {...}
111    * <p>
112    *
113    * @return The annotation value.
114    */
115   Class<?> implClass() default void.class;
116
117   /**
118    * Dynamically apply this annotation to the specified classes.
119    *
120    * <p>
121    * Used in conjunction with {@link org.apache.juneau.BeanContext.Builder#applyAnnotations(Class...)} to dynamically apply an annotation to an existing class.
122    * It is ignored when the annotation is applied directly to classes.
123    *
124    * <p>
125    * The following example shows the equivalent methods for applying the {@link Bean @Bean} annotation to REST methods:
126    * <p class='bjava'>
127    *    <jc>// Class with explicit annotation.</jc>
128    *    <ja>@Marshalled</ja>(example=<js>"{foo:'bar'}"</js>)
129    *    <jk>public class</jk> A {...}
130    *
131    *    <jc>// Class with annotation applied via @BeanConfig</jc>
132    *    <jk>public class</jk> B {...}
133    *
134    *    <jc>// Java REST method with @BeanConfig annotation.</jc>
135    *    <ja>@RestOp</ja>(...)
136    *    <ja>@Marshalled</ja>(on=<js>"B"</js>, example=<js>"{foo:'bar'}"</js>)
137    *    <jk>public void</jk> doFoo() {...}
138    * </p>
139    *
140    * <h5 class='section'>Valid patterns:</h5>
141    * <ul class='spaced-list'>
142    *  <li>Classes:
143    *       <ul>
144    *          <li>Fully qualified:
145    *             <ul>
146    *                <li><js>"com.foo.MyClass"</js>
147    *             </ul>
148    *          <li>Fully qualified inner class:
149    *             <ul>
150    *                <li><js>"com.foo.MyClass$Inner1$Inner2"</js>
151    *             </ul>
152    *          <li>Simple:
153    *             <ul>
154    *                <li><js>"MyClass"</js>
155    *             </ul>
156    *          <li>Simple inner:
157    *             <ul>
158    *                <li><js>"MyClass$Inner1$Inner2"</js>
159    *                <li><js>"Inner1$Inner2"</js>
160    *                <li><js>"Inner2"</js>
161    *             </ul>
162    *       </ul>
163    *    <li>A comma-delimited list of anything on this list.
164    * </ul>
165    *
166    * <h5 class='section'>See Also:</h5><ul>
167    *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a>
168    * </ul>
169    *
170    * @return The annotation value.
171    */
172   String[] on() default {};
173
174   /**
175    * Dynamically apply this annotation to the specified classes.
176    *
177    * <p>
178    * Identical to {@link #on()} except allows you to specify class objects instead of a strings.
179    *
180    * <h5 class='section'>See Also:</h5><ul>
181    *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a>
182    * </ul>
183    *
184    * @return The annotation value.
185    */
186   Class<?>[] onClass() default {};
187}