org.apache.juneau.annotation.Swap Maven / Gradle / Ivy
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance *
// * with the License. You may obtain a copy of the License at *
// * *
// * http://www.apache.org/licenses/LICENSE-2.0 *
// * *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the *
// * specific language governing permissions and limitations under the License. *
// ***************************************************************************************************************************
package org.apache.juneau.annotation;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
import java.lang.annotation.*;
import java.util.*;
import org.apache.juneau.transform.*;
/**
* Associates {@link PojoSwap} and {@link Surrogate} classes with POJOs and bean properties.
*
*
* A typical example is for rendering {@link Date Dates} and {@link Calendar Calendars} as a formatted string:
*
*
Example:
*
* public class MyClass {
*
* // During serialization, convert to ISO8601 date-time string.
* @Swap (CalendarSwap.ISO8601DT.class )
* public Calendar getTime();
* }
*
*
*
* This annotation can be used in the following locations:
*
* - Classes.
*
- Bean getters/setters/fields.
*
- Inside the {@link Swaps @Swaps} annotation.
*
*/
@Documented
@Target({TYPE,ANNOTATION_TYPE,FIELD,METHOD})
@Retention(RUNTIME)
@Inherited
public @interface Swap {
/**
* The {@link PojoSwap} and {@link Surrogate} class.
*
*
* A synonym for {@link #impl()}.
*/
Class> value() default Null.class;
/**
* The {@link PojoSwap} and {@link Surrogate} class.
*
*
* A synonym for {@link #value()}.
*/
Class> impl() default Null.class;
/**
* Identifies a template string along with this swap.
*
*
* Template strings are arbitrary strings associated with swaps that help provide additional context information
* for the swap class.
* They're called 'templates' because their primary purpose is for providing template names, such as Apache FreeMarker
* template names.
*
*
* The following is an example of a templated swap class used to serialize POJOs to HTML using FreeMarker:
*
*
* // Our abstracted templated swap class.
* public abstract class FreeMarkerSwap extends PojoSwap<Object,Reader> {
*
* public MediaType[] forMediaTypes() {
* return MediaType.forStrings ("*/html" );
* }
*
* public Reader swap(BeanSession session, Object o, String template) throws Exception {
* return getFreeMarkerReader(template, o); // Some method that creates raw HTML.
* }
* }
*
*
* @Swap (impl=FreeMarkerSwap.class , template="MyPojo.div.ftl" )
* public class MyPojo {}
*
*/
String template() default "";
/**
* Identifies the media types that this swap is applicable for.
*
*
* In the following example, the swap is only invoked by the JSON serializer:
*
*
* @Swap (impl=ToStringSwap.class , mediaTypes="*/json" )
* public class MyBean { ... }
*
* public class ToStringSwap extends PojoSwap<Object,String> {
* public String swap(BeanSession session, Object o) throws Exception {
* return o.toString();
* }
* }
*
*/
String[] mediaTypes() default {};
}