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