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 */
041@Documented
042@Target({ METHOD, TYPE })
043@Retention(RUNTIME)
044@Inherited
045@Repeatable(MarshalledAnnotation.Array.class)
046@ContextApply(MarshalledAnnotation.Applier.class)
047public @interface Marshalled {
048
049   /**
050    * Optional description for the exposed API.
051    *
052    * @return The annotation value.
053    * @since 9.2.0
054    */
055   String[] description() default {};
056
057   /**
058    * POJO example.
059    *
060    * <p>
061    * Specifies an example of the specified class in Simplified JSON format.
062    *
063    * <p>
064    * Examples are used in cases such as POJO examples in Swagger documents.
065    *
066    * <h5 class='section'>Example:</h5>
067    * <p class='bjava'>
068    *    <ja>@Marshalled</ja>(example=<js>"{foo:'bar'}"</js>)
069    *    <jk>public class</jk> MyClass {...}
070    * </p>
071    *
072    * <h5 class='section'>Notes:</h5><ul>
073    *    <li class='note'>
074    *       Setting applies to specified class and all subclasses.
075    *    <li class='note'>
076    *       Keys are the class of the example.
077    *       <br>Values are JSON 5 representation of that class.
078    *    <li class='note'>
079    *       POJO examples can also be defined on classes via the following:
080    *       <ul class='spaced-list'>
081    *          <li>A static field annotated with {@link Example @Example}.
082    *          <li>A static method annotated with {@link Example @Example} with zero arguments or one {@link BeanSession} argument.
083    *          <li>A static method with name <c>example</c> with no arguments or one {@link BeanSession} argument.
084    *       </ul>
085    *    <li class='note'>
086    *       Supports <a class="doclink" href="https://juneau.apache.org/docs/topics/DefaultVarResolver">VarResolver.DEFAULT</a> (e.g. <js>"$C{myConfigVar}"</js>).
087    * </ul>
088    *
089    * <h5 class='section'>See Also:</h5><ul>
090    *    <li class='ja'>{@link Example}
091    * </ul>
092    *
093    * @return The annotation value.
094    */
095   String example() default "";
096
097   /**
098    * Implementation class.
099    *
100    * <p>
101    * For interfaces and abstract classes this method can be used to specify an implementation class for the
102    * interface/abstract class so that instances of the implementation class are used when instantiated (e.g. during a
103    * parse).
104    *
105    * <h5 class='section'>Example:</h5>
106    * <p class='bjava'>
107    *    <ja>@Marshalled</ja>(implClass=MyInterfaceImpl.<jk>class</jk>)
108    *    <jk>public class</jk> MyInterface {...}
109    * <p>
110    *
111    * @return The annotation value.
112    */
113   Class<?> implClass() default void.class;
114
115   /**
116    * Dynamically apply this annotation to the specified classes.
117    *
118    * <p>
119    * Used in conjunction with {@link org.apache.juneau.BeanContext.Builder#applyAnnotations(Class...)} to dynamically apply an annotation to an existing class.
120    * It is ignored when the annotation is applied directly to classes.
121    *
122    * <p>
123    * The following example shows the equivalent methods for applying the {@link Bean @Bean} annotation to REST methods:
124    * <p class='bjava'>
125    *    <jc>// Class with explicit annotation.</jc>
126    *    <ja>@Marshalled</ja>(example=<js>"{foo:'bar'}"</js>)
127    *    <jk>public class</jk> A {...}
128    *
129    *    <jc>// Class with annotation applied via @BeanConfig</jc>
130    *    <jk>public class</jk> B {...}
131    *
132    *    <jc>// Java REST method with @BeanConfig annotation.</jc>
133    *    <ja>@RestOp</ja>(...)
134    *    <ja>@Marshalled</ja>(on=<js>"B"</js>, example=<js>"{foo:'bar'}"</js>)
135    *    <jk>public void</jk> doFoo() {...}
136    * </p>
137    *
138    * <h5 class='section'>Valid patterns:</h5>
139    * <ul class='spaced-list'>
140    *  <li>Classes:
141    *       <ul>
142    *          <li>Fully qualified:
143    *             <ul>
144    *                <li><js>"com.foo.MyClass"</js>
145    *             </ul>
146    *          <li>Fully qualified inner class:
147    *             <ul>
148    *                <li><js>"com.foo.MyClass$Inner1$Inner2"</js>
149    *             </ul>
150    *          <li>Simple:
151    *             <ul>
152    *                <li><js>"MyClass"</js>
153    *             </ul>
154    *          <li>Simple inner:
155    *             <ul>
156    *                <li><js>"MyClass$Inner1$Inner2"</js>
157    *                <li><js>"Inner1$Inner2"</js>
158    *                <li><js>"Inner2"</js>
159    *             </ul>
160    *       </ul>
161    *    <li>A comma-delimited list of anything on this list.
162    * </ul>
163    *
164    * <h5 class='section'>See Also:</h5><ul>
165    *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a>
166    * </ul>
167    *
168    * @return The annotation value.
169    */
170   String[] on() default {};
171
172   /**
173    * Dynamically apply this annotation to the specified classes.
174    *
175    * <p>
176    * Identical to {@link #on()} except allows you to specify class objects instead of a strings.
177    *
178    * <h5 class='section'>See Also:</h5><ul>
179    *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/DynamicallyAppliedAnnotations">Dynamically Applied Annotations</a>
180    * </ul>
181    *
182    * @return The annotation value.
183    */
184   Class<?>[] onClass() default {};
185}