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