001/* 002 * ElementUnion.java March 2011 003 * 004 * Copyright (C) 2011, Niall Gallagher <niallg@users.sf.net> 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 015 * implied. See the License for the specific language governing 016 * permissions and limitations under the License. 017 */ 018 019package org.simpleframework.xml; 020 021import java.lang.annotation.Retention; 022import java.lang.annotation.RetentionPolicy; 023 024/** 025 * The <code>ElementUnion</code> annotation is used to describe fields 026 * and methods that can dynamically match a schema class. Each union 027 * can have a number of different XML class schemas matched based on 028 * an XML element name or the instance type. This provides a means 029 * of expressing a logical OR. By annotating a field or method as a 030 * union it can take multiple forms. For example. 031 * <pre> 032 * 033 * @ElementUnion({ 034 * @Element(name="circle", type=Circle.class), 035 * @Element(name="square", type=Square.class) 036 * }) 037 * private Shape shape; 038 * 039 * </pre> 040 * For the above definition the <code>Shape</code> field can take 041 * be any of the declared types. On deserialization the name of the 042 * element will determine the type that is instantiated and the XML 043 * structure to be consumed. For serialization the instance type will 044 * determine the name of the element the object will serialized as. 045 * This provides a useful means of consume more complicated sources. 046 * 047 * @author Niall Gallagher 048 * 049 * @see org.simpleframework.xml.Element 050 */ 051@Retention(RetentionPolicy.RUNTIME) 052public @interface ElementUnion { 053 054 /** 055 * This provides the <code>Element</code> annotations that have 056 * been defined for this union. Each element describes the XML 057 * class schema to use and the name of the XML element. This 058 * allows the serialization process to determine which elements 059 * map to the defined types. Also, the types define how the XML 060 * is generated for a given instance. 061 * 062 * @return the elements defined for the union declaration 063 */ 064 Element[] value(); 065}