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.bind.api; 27 28 import java.lang.annotation.Annotation; 29 import java.lang.reflect.Type; 30 import java.util.Arrays; 31 import java.util.Collection; 32 33 import javax.xml.namespace.QName; 34 35 import com.sun.xml.internal.bind.v2.model.nav.Navigator; 36 37 /** 38 * A reference to a JAXB-bound type. 39 * 40 * <p> 41 * <b>Subject to change without notice</b>. 42 * 43 * @since 2.0 EA1 44 * @author Kohsuke Kawaguchi 45 */ 46 public final class TypeReference { 47 48 /** 49 * The associated XML element name that the JAX-RPC uses with this type reference. 50 * 51 * Always non-null. Strings are interned. 52 */ 53 public final QName tagName; 54 55 /** 56 * The Java type that's being referenced. 57 * 58 * Always non-null. 59 */ 60 public final Type type; 61 62 /** 63 * The annotations associated with the reference of this type. 64 * 65 * Always non-null. 66 */ 67 public final Annotation[] annotations; 68 69 public TypeReference(QName tagName, Type type, Annotation... annotations) { 70 if(tagName==null || type==null || annotations==null) { 71 String nullArgs = ""; 72 73 if(tagName == null) nullArgs = "tagName"; 74 if(type == null) nullArgs += (nullArgs.length() > 0 ? ", type" : "type"); 75 if(annotations == null) nullArgs += (nullArgs.length() > 0 ? ", annotations" : "annotations"); 76 77 Messages.ARGUMENT_CANT_BE_NULL.format(nullArgs); 78 79 throw new IllegalArgumentException(Messages.ARGUMENT_CANT_BE_NULL.format(nullArgs)); 80 } 81 82 this.tagName = new QName(tagName.getNamespaceURI().intern(), tagName.getLocalPart().intern(), tagName.getPrefix()); 83 this.type = type; 84 this.annotations = annotations; 85 } 86 87 /** 88 * Finds the specified annotation from the array and returns it. 89 * Null if not found. 90 */ 91 public <A extends Annotation> A get( Class<A> annotationType ) { 92 for (Annotation a : annotations) { 93 if(a.annotationType()==annotationType) 94 return annotationType.cast(a); 95 } 96 return null; 97 } 98 99 /** 100 * Creates a {@link TypeReference} for the item type, 101 * if this {@link TypeReference} represents a collection type. 102 * Otherwise returns an identical type. 103 */ 104 public TypeReference toItemType() { 105 // if we are to reinstitute this check, check JAXB annotations only 106 // assert annotations.length==0; // not designed to work with adapters. 107 108 Type base = Navigator.REFLECTION.getBaseClass(type, Collection.class); 109 if(base==null) 110 return this; // not a collection 111 112 return new TypeReference(tagName, 113 Navigator.REFLECTION.getTypeArgument(base,0)); 114 } 115 116 @Override 117 public boolean equals(Object o) { 118 if (this == o) return true; 119 if (o == null || getClass() != o.getClass()) return false; 120 121 TypeReference that = (TypeReference) o; 122 123 if (!Arrays.equals(annotations, that.annotations)) return false; 124 if (!tagName.equals(that.tagName)) return false; 125 if (!type.equals(that.type)) return false; 126 127 return true; 128 } 129 130 @Override 131 public int hashCode() { 132 int result = tagName.hashCode(); 133 result = 31 * result + type.hashCode(); 134 result = 31 * result + Arrays.hashCode(annotations); 135 return result; 136 } 137 }