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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

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

mercurial