1/*
2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.xml.internal.ws.spi.db;
27
28import java.lang.annotation.Annotation;
29import java.lang.reflect.GenericArrayType;
30import java.lang.reflect.Type;
31import java.util.Collection;
32import java.util.HashMap;
33import java.util.Map;
34
35import javax.xml.namespace.QName;
36import javax.xml.bind.annotation.XmlElementWrapper;
37
38/**
39 * A reference to a JAXB-bound type.
40 *
41 * <p>
42 * <b>Subject to change without notice</b>.
43 *
44 * @since 2.0 EA1
45 * @author Kohsuke Kawaguchi
46 * @author shih-chang.chen@oracle.com
47 */
48public final class TypeInfo {
49
50    /**
51     * The associated XML element name that the JAX-RPC uses with this type reference.
52     *
53     * Always non-null. Strings are interned.
54     */
55    public final QName tagName;
56
57    /**
58     * The Java type that's being referenced.
59     *
60     * Always non-null.
61     */
62    public Type type;
63
64    /**
65     * The annotations associated with the reference of this type.
66     *
67     * Always non-null.
68     */
69    public final Annotation[] annotations;
70
71    private Map<String, Object> properties = new HashMap<String, Object>();
72
73    private boolean isGlobalElement = true;
74
75    private TypeInfo parentCollectionType;
76
77    private TypeInfo wrapperType;
78
79    private Type genericType;
80
81    private boolean nillable = true;
82
83    public TypeInfo(QName tagName, Type type, Annotation... annotations) {
84        if(tagName==null || type==null || annotations==null) {
85            String nullArgs = "";
86
87            if(tagName == null)     nullArgs = "tagName";
88            if(type == null)        nullArgs += (nullArgs.length() > 0 ? ", type" : "type");
89            if(annotations == null) nullArgs += (nullArgs.length() > 0 ? ", annotations" : "annotations");
90
91//            Messages.ARGUMENT_CANT_BE_NULL.format(nullArgs);
92
93            throw new IllegalArgumentException( "Argument(s) \"" + nullArgs + "\" can''t be null.)");
94        }
95
96        this.tagName = new QName(tagName.getNamespaceURI().intern(), tagName.getLocalPart().intern(), tagName.getPrefix());
97        this.type = type;
98        if (type instanceof Class && ((Class<?>)type).isPrimitive()) nillable = false;
99        this.annotations = annotations;
100    }
101
102    /**
103     * Finds the specified annotation from the array and returns it.
104     * Null if not found.
105     */
106    public <A extends Annotation> A get( Class<A> annotationType ) {
107        for (Annotation a : annotations) {
108            if(a.annotationType()==annotationType)
109                return annotationType.cast(a);
110        }
111        return null;
112    }
113
114    /**
115     * Creates a {@link TypeInfo} for the item type,
116     * if this {@link TypeInfo} represents a collection type.
117     * Otherwise returns an identical type.
118     */
119    public TypeInfo toItemType() {
120        // if we are to reinstitute this check, check JAXB annotations only
121        // assert annotations.length==0;   // not designed to work with adapters.
122        Type t = (genericType != null)? genericType : type;
123        Type base = Utils.REFLECTION_NAVIGATOR.getBaseClass(t, Collection.class);
124        if(base==null)
125            return this;    // not a collection
126
127        return new TypeInfo(tagName, Utils.REFLECTION_NAVIGATOR.getTypeArgument(base,0));
128    }
129
130    public Map<String, Object> properties() {
131                return properties;
132        }
133
134        public boolean isGlobalElement() {
135                return isGlobalElement;
136        }
137
138        public void setGlobalElement(boolean isGlobalElement) {
139                this.isGlobalElement = isGlobalElement;
140        }
141
142        public TypeInfo getParentCollectionType() {
143                return parentCollectionType;
144        }
145
146        public void setParentCollectionType(TypeInfo parentCollectionType) {
147                this.parentCollectionType = parentCollectionType;
148        }
149
150        public boolean isRepeatedElement() {
151                return (parentCollectionType != null);
152        }
153
154        public Type getGenericType() {
155                return genericType;
156        }
157
158        public void setGenericType(Type genericType) {
159                this.genericType = genericType;
160        }
161
162    public boolean isNillable() {
163        return nillable;
164    }
165
166    public void setNillable(boolean nillable) {
167        this.nillable = nillable;
168    }
169
170    public String toString() {
171        return new StringBuilder("TypeInfo: Type = ").append(type)
172                .append(", tag = ").append(tagName).toString();
173    }
174
175    public TypeInfo getItemType() {
176        if (type instanceof Class && ((Class)type).isArray() && !byte[].class.equals(type)) {
177            Type componentType = ((Class)type).getComponentType();
178            Type genericComponentType = null;
179            if (genericType!= null && genericType instanceof GenericArrayType) {
180                GenericArrayType arrayType = (GenericArrayType) type;
181                genericComponentType = arrayType.getGenericComponentType();
182                componentType = arrayType.getGenericComponentType();
183            }
184            TypeInfo ti =new TypeInfo(tagName, componentType, annotations);
185            if (genericComponentType != null) ti.setGenericType(genericComponentType);
186            for(Annotation anno : annotations) if (anno instanceof XmlElementWrapper) ti.wrapperType = this;
187            return ti;
188        }
189//        if (type instanceof Class && java.util.Collection.class.isAssignableFrom((Class)type)) {
190        Type t = (genericType != null)? genericType : type;
191        Type base = Utils.REFLECTION_NAVIGATOR.getBaseClass(t, Collection.class);
192        if ( base != null)  {
193            return new TypeInfo(tagName, Utils.REFLECTION_NAVIGATOR.getTypeArgument(base,0), annotations);
194        }
195        return null;
196    }
197
198    public TypeInfo getWrapperType() {
199        return wrapperType;
200    }
201}
202