src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/TypeUtil.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.tools.internal.xjc.reader;
ohair@286 27
ohair@286 28 import java.util.ArrayList;
ohair@286 29 import java.util.Collection;
ohair@286 30 import java.util.Comparator;
ohair@286 31 import java.util.Iterator;
ohair@286 32 import java.util.List;
ohair@286 33 import java.util.Set;
ohair@286 34 import java.util.TreeSet;
ohair@286 35
ohair@286 36 import com.sun.codemodel.internal.JClass;
ohair@286 37 import com.sun.codemodel.internal.JCodeModel;
ohair@286 38 import com.sun.codemodel.internal.JDefinedClass;
ohair@286 39 import com.sun.codemodel.internal.JType;
ohair@286 40 import com.sun.tools.internal.xjc.ErrorReceiver;
ohair@286 41
ohair@286 42 import org.xml.sax.Locator;
ohair@286 43 import org.xml.sax.SAXParseException;
ohair@286 44
ohair@286 45 /**
ohair@286 46 * Type-related utility methods.
ohair@286 47 *
ohair@286 48 * @author
ohair@286 49 * <a href="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a>
ohair@286 50 */
ohair@286 51 public class TypeUtil {
ohair@286 52
ohair@286 53
ohair@286 54 /**
ohair@286 55 * Computes the common base type of two types.
ohair@286 56 *
ohair@286 57 * @param types
ohair@286 58 * set of {@link JType} objects.
ohair@286 59 */
ohair@286 60 public static JType getCommonBaseType( JCodeModel codeModel, Collection<? extends JType> types ) {
ohair@286 61 return getCommonBaseType( codeModel, types.toArray(new JType[types.size()]) );
ohair@286 62 }
ohair@286 63
ohair@286 64 /**
ohair@286 65 * Computes the common base type of types.
ohair@286 66 *
ohair@286 67 * TODO: this is a very interesting problem. Since one type has possibly
ohair@286 68 * multiple base types, it's not an easy problem.
ohair@286 69 * The current implementation is very naive.
ohair@286 70 *
ohair@286 71 * To make the result deterministic across differente JVMs, we have to
ohair@286 72 * use a Set whose ordering is deterministic.
ohair@286 73 */
ohair@286 74 public static JType getCommonBaseType(JCodeModel codeModel, JType... t) {
ohair@286 75 // first, eliminate duplicates.
ohair@286 76 Set<JType> uniqueTypes = new TreeSet<JType>(typeComparator);
ohair@286 77 for (JType type : t)
ohair@286 78 uniqueTypes.add(type);
ohair@286 79
ohair@286 80 // if this yields only one type. return now.
ohair@286 81 // this is the only case where we can return a primitive type
ohair@286 82 // from this method
ohair@286 83 if (uniqueTypes.size() == 1)
ohair@286 84 return uniqueTypes.iterator().next();
ohair@286 85
ohair@286 86 // assertion failed. nullType can be used only under a very special circumstance
ohair@286 87 assert !uniqueTypes.isEmpty();
ohair@286 88
ohair@286 89 // the null type doesn't need to be taken into account.
ohair@286 90 uniqueTypes.remove(codeModel.NULL);
ohair@286 91
ohair@286 92 // box all the types and compute the intersection of all types
ohair@286 93 Set<JClass> s = null;
ohair@286 94
ohair@286 95 for (JType type : uniqueTypes) {
ohair@286 96 JClass cls = type.boxify();
ohair@286 97
ohair@286 98 if (s == null)
ohair@286 99 s = getAssignableTypes(cls);
ohair@286 100 else
ohair@286 101 s.retainAll(getAssignableTypes(cls));
ohair@286 102 }
ohair@286 103
ohair@286 104 // any JClass can be casted to Object, so make sure it's always there
ohair@286 105 s.add( codeModel.ref(Object.class));
ohair@286 106
ohair@286 107 // refine 's' by removing "lower" types.
ohair@286 108 // for example, if we have both java.lang.Object and
ohair@286 109 // java.io.InputStream, then we don't want to use java.lang.Object.
ohair@286 110
ohair@286 111 JClass[] raw = s.toArray(new JClass[s.size()]);
ohair@286 112 s.clear();
ohair@286 113
ohair@286 114 for (int i = 0; i < raw.length; i++) { // for each raw[i]
ohair@286 115 int j;
ohair@286 116 for (j = 0; j < raw.length; j++) { // see if raw[j] "includes" raw[i]
ohair@286 117 if (i == j)
ohair@286 118 continue;
ohair@286 119
ohair@286 120 if (raw[i].isAssignableFrom(raw[j]))
ohair@286 121 break; // raw[j] is derived from raw[i], hence j includes i.
ohair@286 122 }
ohair@286 123
ohair@286 124 if (j == raw.length)
ohair@286 125 // no other type inclueds raw[i]. remember this value.
ohair@286 126 s.add(raw[i]);
ohair@286 127 }
ohair@286 128
ohair@286 129 assert !s.isEmpty(); // since at least java.lang.Object has to be there
ohair@286 130
ohair@286 131 // we now pick the candidate for the return type
ohair@286 132 JClass result = pickOne(s);
ohair@286 133
ohair@286 134 // finally, sometimes this method is used to compute the base type of types like
ohair@286 135 // JAXBElement<A>, JAXBElement<B>, and JAXBElement<C>.
ohair@286 136 // for those inputs, at this point result=JAXBElement.
ohair@286 137 //
ohair@286 138 // here, we'll try to figure out the parameterization
ohair@286 139 // so that we can return JAXBElement<? extends D> instead of just "JAXBElement".
ohair@286 140 if(result.isParameterized())
ohair@286 141 return result;
ohair@286 142
ohair@286 143 // for each uniqueType we store the list of base type parameterization
ohair@286 144 List<List<JClass>> parameters = new ArrayList<List<JClass>>(uniqueTypes.size());
ohair@286 145 int paramLen = -1;
ohair@286 146
ohair@286 147 for (JType type : uniqueTypes) {
ohair@286 148 JClass cls = type.boxify();
ohair@286 149 JClass bp = cls.getBaseClass(result);
ohair@286 150 // if there's no parameterization in the base type,
ohair@286 151 // we won't do any better than <?>. Thus no point in trying to figure out the parameterization.
ohair@286 152 // just return the base type.
ohair@286 153 if(bp.equals(result))
ohair@286 154 return result;
ohair@286 155
ohair@286 156 assert bp.isParameterized();
ohair@286 157 List<JClass> tp = bp.getTypeParameters();
ohair@286 158 parameters.add(tp);
ohair@286 159
ohair@286 160 assert paramLen==-1 || paramLen==tp.size();
ohair@286 161 // since 'bp' always is a parameterized version of 'result', it should always
ohair@286 162 // have the same number of parameters.
ohair@286 163 paramLen = tp.size();
ohair@286 164 }
ohair@286 165
ohair@286 166 List<JClass> paramResult = new ArrayList<JClass>();
ohair@286 167 List<JClass> argList = new ArrayList<JClass>(parameters.size());
ohair@286 168 // for each type parameter compute the common base type
ohair@286 169 for( int i=0; i<paramLen; i++ ) {
ohair@286 170 argList.clear();
ohair@286 171 for (List<JClass> list : parameters)
ohair@286 172 argList.add(list.get(i));
ohair@286 173
ohair@286 174 // compute the lower bound.
ohair@286 175 JClass bound = (JClass)getCommonBaseType(codeModel,argList);
ohair@286 176 boolean allSame = true;
ohair@286 177 for (JClass a : argList)
ohair@286 178 allSame &= a.equals(bound);
ohair@286 179 if(!allSame)
ohair@286 180 bound = bound.wildcard();
ohair@286 181
ohair@286 182 paramResult.add(bound);
ohair@286 183 }
ohair@286 184
ohair@286 185 return result.narrow(paramResult);
ohair@286 186 }
ohair@286 187
ohair@286 188 private static JClass pickOne(Set<JClass> s) {
ohair@286 189 // we may have more than one candidates at this point.
ohair@286 190 // any user-defined generated types should have
ohair@286 191 // precedence over system-defined existing types.
ohair@286 192 //
ohair@286 193 // so try to return such a type if any.
ohair@286 194 for (JClass c : s)
ohair@286 195 if (c instanceof JDefinedClass)
ohair@286 196 return c;
ohair@286 197
ohair@286 198 // we can do more if we like. for example,
ohair@286 199 // we can avoid types in the RI runtime.
ohair@286 200 // but for now, just return the first one.
ohair@286 201 return s.iterator().next();
ohair@286 202 }
ohair@286 203
ohair@286 204 private static Set<JClass> getAssignableTypes( JClass t ) {
ohair@286 205 Set<JClass> r = new TreeSet<JClass>(typeComparator);
ohair@286 206 getAssignableTypes(t,r);
ohair@286 207 return r;
ohair@286 208 }
ohair@286 209
ohair@286 210 /**
ohair@286 211 * Returns the set of all classes/interfaces that a given type
ohair@286 212 * implements/extends, including itself.
ohair@286 213 *
ohair@286 214 * For example, if you pass java.io.FilterInputStream, then the returned
ohair@286 215 * set will contain java.lang.Object, java.lang.InputStream, and
ohair@286 216 * java.lang.FilterInputStream.
ohair@286 217 */
ohair@286 218 private static void getAssignableTypes( JClass t, Set<JClass> s ) {
ohair@286 219 if(!s.add(t))
ohair@286 220 return;
ohair@286 221
ohair@286 222 // add its raw type
ohair@286 223 s.add(t.erasure());
ohair@286 224
ohair@286 225 // if this type is added for the first time,
ohair@286 226 // recursively process the super class.
ohair@286 227 JClass _super = t._extends();
ohair@286 228 if(_super!=null)
ohair@286 229 getAssignableTypes(_super,s);
ohair@286 230
ohair@286 231 // recursively process all implemented interfaces
ohair@286 232 Iterator<JClass> itr = t._implements();
ohair@286 233 while(itr.hasNext())
ohair@286 234 getAssignableTypes(itr.next(),s);
ohair@286 235 }
ohair@286 236
ohair@286 237 /**
ohair@286 238 * Obtains a {@link JType} object for the string representation
ohair@286 239 * of a type.
ohair@286 240 */
ohair@286 241 public static JType getType( JCodeModel codeModel,
ohair@286 242 String typeName, ErrorReceiver errorHandler, Locator errorSource ) {
ohair@286 243
ohair@286 244 try {
ohair@286 245 return codeModel.parseType(typeName);
ohair@286 246 } catch( ClassNotFoundException ee ) {
ohair@286 247
ohair@286 248 // make it a warning
ohair@286 249 errorHandler.warning( new SAXParseException(
ohair@286 250 Messages.ERR_CLASS_NOT_FOUND.format(typeName)
ohair@286 251 ,errorSource));
ohair@286 252
ohair@286 253 // recover by assuming that it's a class that derives from Object
ohair@286 254 return codeModel.directClass(typeName);
ohair@286 255 }
ohair@286 256 }
ohair@286 257
ohair@286 258 /**
ohair@286 259 * Compares {@link JType} objects by their names.
ohair@286 260 */
ohair@286 261 private static final Comparator<JType> typeComparator = new Comparator<JType>() {
ohair@286 262 public int compare(JType t1, JType t2) {
ohair@286 263 return t1.fullName().compareTo(t2.fullName());
ohair@286 264 }
ohair@286 265 };
ohair@286 266 }

mercurial