1 /* 2 * Copyright (c) 1997, 2013, 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 26 package com.sun.xml.internal.ws.spi.db; 27 28 import java.lang.annotation.Annotation; 29 import java.lang.reflect.GenericArrayType; 30 import java.lang.reflect.Type; 31 import java.util.Collection; 32 import java.util.HashMap; 33 import java.util.Map; 34 35 import javax.xml.namespace.QName; 36 37 import com.sun.xml.internal.bind.v2.model.nav.Navigator; 38 39 /** 40 * A reference to a JAXB-bound type. 41 * 42 * <p> 43 * <b>Subject to change without notice</b>. 44 * 45 * @since 2.0 EA1 46 * @author Kohsuke Kawaguchi 47 * @author shih-chang.chen@oracle.com 48 */ 49 public final class TypeInfo { 50 51 /** 52 * The associated XML element name that the JAX-RPC uses with this type reference. 53 * 54 * Always non-null. Strings are interned. 55 */ 56 public final QName tagName; 57 58 /** 59 * The Java type that's being referenced. 60 * 61 * Always non-null. 62 */ 63 public Type type; 64 65 /** 66 * The annotations associated with the reference of this type. 67 * 68 * Always non-null. 69 */ 70 public final Annotation[] annotations; 71 72 private Map<String, Object> properties = new HashMap<String, Object>(); 73 74 private boolean isGlobalElement = true; 75 76 private TypeInfo parentCollectionType; 77 78 private Type genericType; 79 80 private boolean nillable = true; 81 82 public TypeInfo(QName tagName, Type type, Annotation... annotations) { 83 if(tagName==null || type==null || annotations==null) { 84 String nullArgs = ""; 85 86 if(tagName == null) nullArgs = "tagName"; 87 if(type == null) nullArgs += (nullArgs.length() > 0 ? ", type" : "type"); 88 if(annotations == null) nullArgs += (nullArgs.length() > 0 ? ", annotations" : "annotations"); 89 90 // Messages.ARGUMENT_CANT_BE_NULL.format(nullArgs); 91 92 throw new IllegalArgumentException( "Argument(s) \"" + nullArgs + "\" can''t be null.)"); 93 } 94 95 this.tagName = new QName(tagName.getNamespaceURI().intern(), tagName.getLocalPart().intern(), tagName.getPrefix()); 96 this.type = type; 97 if (type instanceof Class && ((Class<?>)type).isPrimitive()) nillable = false; 98 this.annotations = annotations; 99 } 100 101 /** 102 * Finds the specified annotation from the array and returns it. 103 * Null if not found. 104 */ 105 public <A extends Annotation> A get( Class<A> annotationType ) { 106 for (Annotation a : annotations) { 107 if(a.annotationType()==annotationType) 108 return annotationType.cast(a); 109 } 110 return null; 111 } 112 113 /** 114 * Creates a {@link TypeInfo} for the item type, 115 * if this {@link TypeInfo} represents a collection type. 116 * Otherwise returns an identical type. 117 */ 118 public TypeInfo toItemType() { 119 // if we are to reinstitute this check, check JAXB annotations only 120 // assert annotations.length==0; // not designed to work with adapters. 121 Type t = (genericType != null)? genericType : type; 122 Type base = Utils.REFLECTION_NAVIGATOR.getBaseClass(t, Collection.class); 123 if(base==null) 124 return this; // not a collection 125 126 return new TypeInfo(tagName, Utils.REFLECTION_NAVIGATOR.getTypeArgument(base,0)); 127 } 128 129 public Map<String, Object> properties() { 130 return properties; 131 } 132 133 public boolean isGlobalElement() { 134 return isGlobalElement; 135 } 136 137 public void setGlobalElement(boolean isGlobalElement) { 138 this.isGlobalElement = isGlobalElement; 139 } 140 141 public TypeInfo getParentCollectionType() { 142 return parentCollectionType; 143 } 144 145 public void setParentCollectionType(TypeInfo parentCollectionType) { 146 this.parentCollectionType = parentCollectionType; 147 } 148 149 public boolean isRepeatedElement() { 150 return (parentCollectionType != null); 151 } 152 153 public Type getGenericType() { 154 return genericType; 155 } 156 157 public void setGenericType(Type genericType) { 158 this.genericType = genericType; 159 } 160 161 public boolean isNillable() { 162 return nillable; 163 } 164 165 public void setNillable(boolean nillable) { 166 this.nillable = nillable; 167 } 168 169 public String toString() { 170 return new StringBuilder("TypeInfo: Type = ").append(type) 171 .append(", tag = ").append(tagName).toString(); 172 } 173 174 public TypeInfo getItemType() { 175 // System.out.println("????? TypeInfo " + type); 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 return ti; 187 } 188 // if (type instanceof Class && java.util.Collection.class.isAssignableFrom((Class)type)) { 189 Type t = (genericType != null)? genericType : type; 190 Type base = Utils.REFLECTION_NAVIGATOR.getBaseClass(t, Collection.class); 191 if ( base != null) { 192 return new TypeInfo(tagName, Utils.REFLECTION_NAVIGATOR.getTypeArgument(base,0), annotations); 193 } 194 return null; 195 } 196 }