001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.annotation;
014
015import static java.lang.annotation.ElementType.*;
016import static java.lang.annotation.RetentionPolicy.*;
017
018import java.lang.annotation.*;
019
020import org.apache.juneau.transform.*;
021
022/**
023 * Associates {@link PojoSwap} and {@link Surrogate} classes with POJOs and bean properties.
024 *
025 * <p>
026 * This annotation can be used in the following locations:
027 * <ul>
028 *    <li>Classes.
029 *    <li>Bean getters/setters/fields.
030 *    <li>Inside the {@link Swaps @Swaps} annotation.
031 * </ul>
032 *
033 * <h5 class='section'>See Also:</h5>
034 * <ul>
035 *    <li class='link'>{@doc juneau-marshall.Transforms.SwapAnnotation}
036 * </ul>
037 */
038@Documented
039@Target({TYPE,ANNOTATION_TYPE,FIELD,METHOD})
040@Retention(RUNTIME)
041@Inherited
042public @interface Swap {
043
044   /**
045    * The {@link PojoSwap} and {@link Surrogate} class.
046    *
047    * <p>
048    * A synonym for {@link #value()}.
049    */
050   Class<?> impl() default Null.class;
051
052   /**
053    * Identifies the media types that this swap is applicable for.
054    *
055    * <p>
056    * In the following example, the swap is only invoked by the JSON serializer:
057    *
058    * <p class='bcode w800'>
059    *    <ja>@Swap</ja>(impl=ToStringSwap.<jk>class</jk>, mediaTypes=<js>"&#42;/json"</js>)
060    *    <jk>public class</jk> MyBean { ... }
061    *
062    *    <jk>public class</jk> ToStringSwap <jk>extends</jk> PojoSwap&lt;Object,String&gt; {
063    *          <jk>public</jk> String swap(BeanSession session, Object o) <jk>throws</jk> Exception {
064    *             <jk>return</jk> o.toString();
065    *          }
066    *       }
067    * </p>
068    *
069    * <h5 class='section'>See Also:</h5>
070    * <ul>
071    *    <li class='link'>{@doc juneau-marshall.Transforms.PerMediaTypePojoSwaps}
072    * </ul>
073    */
074   String[] mediaTypes() default {};
075
076   /**
077    * Identifies a template string along with this swap.
078    *
079    * <p>
080    * Template strings are arbitrary strings associated with swaps that help provide additional context information
081    * for the swap class.
082    * They're called 'templates' because their primary purpose is for providing template names, such as Apache FreeMarker
083    * template names.
084    *
085    * <p>
086    * The following is an example of a templated swap class used to serialize POJOs to HTML using FreeMarker:
087    *
088    * <p class='bcode w800'>
089    *    <jc>// Our templated swap class.</jc>
090    *    <jk>public class</jk> FreeMarkerSwap <jk>extends</jk> PojoSwap&lt;Object,Reader&gt; {
091    *
092    *       <jk>public</jk> MediaType[] forMediaTypes() {
093    *          <jk>return</jk> MediaType.<jsm>forStrings</jsm>(<js>"&#42;/html"</js>);
094    *       }
095    *
096    *       <jk>public</jk> Reader swap(BeanSession session, Object o, String template) <jk>throws</jk> Exception {
097    *          <jk>return</jk> getFreeMarkerReader(template, o);  <jc>// Some method that creates raw HTML.</jc>
098    *       }
099    *    }
100    * </p>
101    * <p class='bcode w800'>
102    *    <ja>@Swap</ja>(impl=FreeMarkerSwap.<jk>class</jk>, template=<js>"MyPojo.div.ftl"</js>)
103    *    <jk>public class</jk> MyPojo {}
104    * </p>
105    *
106    * <h5 class='section'>See Also:</h5>
107    * <ul>
108    *    <li class='link'>{@doc juneau-marshall.Transforms.TemplatedSwaps}
109    * </ul>
110    */
111   String template() default "";
112
113   /**
114    * The {@link PojoSwap} and {@link Surrogate} class.
115    *
116    * <p>
117    * A synonym for {@link #impl()}.
118    */
119   Class<?> value() default Null.class;
120}