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 = Navigator.REFLECTION.getBaseClass(t, Collection.class); 123 if(base==null) 124 return this; // not a collection 125 126 return new TypeInfo(tagName, 127 Navigator.REFLECTION.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 // System.out.println("????? TypeInfo " + type); 177 if (type instanceof Class && ((Class)type).isArray() && !byte[].class.equals(type)) { 178 Type componentType = ((Class)type).getComponentType(); 179 Type genericComponentType = null; 180 if (genericType!= null && genericType instanceof GenericArrayType) { 181 GenericArrayType arrayType = (GenericArrayType) type; 182 genericComponentType = arrayType.getGenericComponentType(); 183 componentType = arrayType.getGenericComponentType(); 184 } 185 TypeInfo ti =new TypeInfo(tagName, componentType, annotations); 186 if (genericComponentType != null) ti.setGenericType(genericComponentType); 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 = Navigator.REFLECTION.getBaseClass(t, Collection.class); 192 if ( base != null) { 193 return new TypeInfo(tagName, Navigator.REFLECTION.getTypeArgument(base,0), annotations); 194 } 195 return null; 196 } 197 }