1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/model/nav/ReflectionNavigator.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,633 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.bind.v2.model.nav; 1.30 + 1.31 +import java.lang.reflect.Array; 1.32 +import java.lang.reflect.Field; 1.33 +import java.lang.reflect.GenericArrayType; 1.34 +import java.lang.reflect.GenericDeclaration; 1.35 +import java.lang.reflect.Method; 1.36 +import java.lang.reflect.Modifier; 1.37 +import java.lang.reflect.ParameterizedType; 1.38 +import java.lang.reflect.Type; 1.39 +import java.lang.reflect.TypeVariable; 1.40 +import java.lang.reflect.WildcardType; 1.41 +import java.util.Arrays; 1.42 +import java.util.Collection; 1.43 + 1.44 +import com.sun.xml.internal.bind.v2.runtime.Location; 1.45 + 1.46 +/** 1.47 + * {@link Navigator} implementation for {@code java.lang.reflect}. 1.48 + * 1.49 + */ 1.50 +/*package*/final class ReflectionNavigator implements Navigator<Type, Class, Field, Method> { 1.51 + 1.52 +// ---------- Singleton ----------------- 1.53 + private static final ReflectionNavigator INSTANCE = new ReflectionNavigator(); 1.54 + 1.55 + /*package*/static ReflectionNavigator getInstance() { 1.56 + return INSTANCE; 1.57 + } 1.58 + 1.59 + private ReflectionNavigator() { 1.60 + } 1.61 +// --------------------------------------- 1.62 + 1.63 + public Class getSuperClass(Class clazz) { 1.64 + if (clazz == Object.class) { 1.65 + return null; 1.66 + } 1.67 + Class sc = clazz.getSuperclass(); 1.68 + if (sc == null) { 1.69 + sc = Object.class; // error recovery 1.70 + } 1.71 + return sc; 1.72 + } 1.73 + 1.74 + private static final TypeVisitor<Type, Class> baseClassFinder = new TypeVisitor<Type, Class>() { 1.75 + 1.76 + public Type onClass(Class c, Class sup) { 1.77 + // t is a raw type 1.78 + if (sup == c) { 1.79 + return sup; 1.80 + } 1.81 + 1.82 + Type r; 1.83 + 1.84 + Type sc = c.getGenericSuperclass(); 1.85 + if (sc != null) { 1.86 + r = visit(sc, sup); 1.87 + if (r != null) { 1.88 + return r; 1.89 + } 1.90 + } 1.91 + 1.92 + for (Type i : c.getGenericInterfaces()) { 1.93 + r = visit(i, sup); 1.94 + if (r != null) { 1.95 + return r; 1.96 + } 1.97 + } 1.98 + 1.99 + return null; 1.100 + } 1.101 + 1.102 + public Type onParameterizdType(ParameterizedType p, Class sup) { 1.103 + Class raw = (Class) p.getRawType(); 1.104 + if (raw == sup) { 1.105 + // p is of the form sup<...> 1.106 + return p; 1.107 + } else { 1.108 + // recursively visit super class/interfaces 1.109 + Type r = raw.getGenericSuperclass(); 1.110 + if (r != null) { 1.111 + r = visit(bind(r, raw, p), sup); 1.112 + } 1.113 + if (r != null) { 1.114 + return r; 1.115 + } 1.116 + for (Type i : raw.getGenericInterfaces()) { 1.117 + r = visit(bind(i, raw, p), sup); 1.118 + if (r != null) { 1.119 + return r; 1.120 + } 1.121 + } 1.122 + return null; 1.123 + } 1.124 + } 1.125 + 1.126 + public Type onGenericArray(GenericArrayType g, Class sup) { 1.127 + // not clear what I should do here 1.128 + return null; 1.129 + } 1.130 + 1.131 + public Type onVariable(TypeVariable v, Class sup) { 1.132 + return visit(v.getBounds()[0], sup); 1.133 + } 1.134 + 1.135 + public Type onWildcard(WildcardType w, Class sup) { 1.136 + // not clear what I should do here 1.137 + return null; 1.138 + } 1.139 + 1.140 + /** 1.141 + * Replaces the type variables in {@code t} by its actual arguments. 1.142 + * 1.143 + * @param decl 1.144 + * provides a list of type variables. See {@link GenericDeclaration#getTypeParameters()} 1.145 + * @param args 1.146 + * actual arguments. See {@link ParameterizedType#getActualTypeArguments()} 1.147 + */ 1.148 + private Type bind(Type t, GenericDeclaration decl, ParameterizedType args) { 1.149 + return binder.visit(t, new BinderArg(decl, args.getActualTypeArguments())); 1.150 + } 1.151 + }; 1.152 + 1.153 + private static class BinderArg { 1.154 + 1.155 + final TypeVariable[] params; 1.156 + final Type[] args; 1.157 + 1.158 + BinderArg(TypeVariable[] params, Type[] args) { 1.159 + this.params = params; 1.160 + this.args = args; 1.161 + assert params.length == args.length; 1.162 + } 1.163 + 1.164 + public BinderArg(GenericDeclaration decl, Type[] args) { 1.165 + this(decl.getTypeParameters(), args); 1.166 + } 1.167 + 1.168 + Type replace(TypeVariable v) { 1.169 + for (int i = 0; i < params.length; i++) { 1.170 + if (params[i].equals(v)) { 1.171 + return args[i]; 1.172 + } 1.173 + } 1.174 + return v; // this is a free variable 1.175 + } 1.176 + } 1.177 + private static final TypeVisitor<Type, BinderArg> binder = new TypeVisitor<Type, BinderArg>() { 1.178 + 1.179 + public Type onClass(Class c, BinderArg args) { 1.180 + return c; 1.181 + } 1.182 + 1.183 + public Type onParameterizdType(ParameterizedType p, BinderArg args) { 1.184 + Type[] params = p.getActualTypeArguments(); 1.185 + 1.186 + boolean different = false; 1.187 + for (int i = 0; i < params.length; i++) { 1.188 + Type t = params[i]; 1.189 + params[i] = visit(t, args); 1.190 + different |= t != params[i]; 1.191 + } 1.192 + 1.193 + Type newOwner = p.getOwnerType(); 1.194 + if (newOwner != null) { 1.195 + newOwner = visit(newOwner, args); 1.196 + } 1.197 + different |= p.getOwnerType() != newOwner; 1.198 + 1.199 + if (!different) { 1.200 + return p; 1.201 + } 1.202 + 1.203 + return new ParameterizedTypeImpl((Class<?>) p.getRawType(), params, newOwner); 1.204 + } 1.205 + 1.206 + public Type onGenericArray(GenericArrayType g, BinderArg types) { 1.207 + Type c = visit(g.getGenericComponentType(), types); 1.208 + if (c == g.getGenericComponentType()) { 1.209 + return g; 1.210 + } 1.211 + 1.212 + return new GenericArrayTypeImpl(c); 1.213 + } 1.214 + 1.215 + public Type onVariable(TypeVariable v, BinderArg types) { 1.216 + return types.replace(v); 1.217 + } 1.218 + 1.219 + public Type onWildcard(WildcardType w, BinderArg types) { 1.220 + // TODO: this is probably still incorrect 1.221 + // bind( "? extends T" ) with T= "? extends Foo" should be "? extends Foo", 1.222 + // not "? extends (? extends Foo)" 1.223 + Type[] lb = w.getLowerBounds(); 1.224 + Type[] ub = w.getUpperBounds(); 1.225 + boolean diff = false; 1.226 + 1.227 + for (int i = 0; i < lb.length; i++) { 1.228 + Type t = lb[i]; 1.229 + lb[i] = visit(t, types); 1.230 + diff |= (t != lb[i]); 1.231 + } 1.232 + 1.233 + for (int i = 0; i < ub.length; i++) { 1.234 + Type t = ub[i]; 1.235 + ub[i] = visit(t, types); 1.236 + diff |= (t != ub[i]); 1.237 + } 1.238 + 1.239 + if (!diff) { 1.240 + return w; 1.241 + } 1.242 + 1.243 + return new WildcardTypeImpl(lb, ub); 1.244 + } 1.245 + }; 1.246 + 1.247 + public Type getBaseClass(Type t, Class sup) { 1.248 + return baseClassFinder.visit(t, sup); 1.249 + } 1.250 + 1.251 + public String getClassName(Class clazz) { 1.252 + return clazz.getName(); 1.253 + } 1.254 + 1.255 + public String getTypeName(Type type) { 1.256 + if (type instanceof Class) { 1.257 + Class c = (Class) type; 1.258 + if (c.isArray()) { 1.259 + return getTypeName(c.getComponentType()) + "[]"; 1.260 + } 1.261 + return c.getName(); 1.262 + } 1.263 + return type.toString(); 1.264 + } 1.265 + 1.266 + public String getClassShortName(Class clazz) { 1.267 + return clazz.getSimpleName(); 1.268 + } 1.269 + 1.270 + public Collection<? extends Field> getDeclaredFields(Class clazz) { 1.271 + return Arrays.asList(clazz.getDeclaredFields()); 1.272 + } 1.273 + 1.274 + public Field getDeclaredField(Class clazz, String fieldName) { 1.275 + try { 1.276 + return clazz.getDeclaredField(fieldName); 1.277 + } catch (NoSuchFieldException e) { 1.278 + return null; 1.279 + } 1.280 + } 1.281 + 1.282 + public Collection<? extends Method> getDeclaredMethods(Class clazz) { 1.283 + return Arrays.asList(clazz.getDeclaredMethods()); 1.284 + } 1.285 + 1.286 + public Class getDeclaringClassForField(Field field) { 1.287 + return field.getDeclaringClass(); 1.288 + } 1.289 + 1.290 + public Class getDeclaringClassForMethod(Method method) { 1.291 + return method.getDeclaringClass(); 1.292 + } 1.293 + 1.294 + public Type getFieldType(Field field) { 1.295 + if (field.getType().isArray()) { 1.296 + Class c = field.getType().getComponentType(); 1.297 + if (c.isPrimitive()) { 1.298 + return Array.newInstance(c, 0).getClass(); 1.299 + } 1.300 + } 1.301 + return fix(field.getGenericType()); 1.302 + } 1.303 + 1.304 + public String getFieldName(Field field) { 1.305 + return field.getName(); 1.306 + } 1.307 + 1.308 + public String getMethodName(Method method) { 1.309 + return method.getName(); 1.310 + } 1.311 + 1.312 + public Type getReturnType(Method method) { 1.313 + return fix(method.getGenericReturnType()); 1.314 + } 1.315 + 1.316 + public Type[] getMethodParameters(Method method) { 1.317 + return method.getGenericParameterTypes(); 1.318 + } 1.319 + 1.320 + public boolean isStaticMethod(Method method) { 1.321 + return Modifier.isStatic(method.getModifiers()); 1.322 + } 1.323 + 1.324 + public boolean isFinalMethod(Method method) { 1.325 + return Modifier.isFinal(method.getModifiers()); 1.326 + } 1.327 + 1.328 + public boolean isSubClassOf(Type sub, Type sup) { 1.329 + return erasure(sup).isAssignableFrom(erasure(sub)); 1.330 + } 1.331 + 1.332 + public Class ref(Class c) { 1.333 + return c; 1.334 + } 1.335 + 1.336 + public Class use(Class c) { 1.337 + return c; 1.338 + } 1.339 + 1.340 + public Class asDecl(Type t) { 1.341 + return erasure(t); 1.342 + } 1.343 + 1.344 + public Class asDecl(Class c) { 1.345 + return c; 1.346 + } 1.347 + /** 1.348 + * Implements the logic for {@link #erasure(Type)}. 1.349 + */ 1.350 + private static final TypeVisitor<Class, Void> eraser = new TypeVisitor<Class, Void>() { 1.351 + 1.352 + public Class onClass(Class c, Void v) { 1.353 + return c; 1.354 + } 1.355 + 1.356 + public Class onParameterizdType(ParameterizedType p, Void v) { 1.357 + // TODO: why getRawType returns Type? not Class? 1.358 + return visit(p.getRawType(), null); 1.359 + } 1.360 + 1.361 + public Class onGenericArray(GenericArrayType g, Void v) { 1.362 + return Array.newInstance( 1.363 + visit(g.getGenericComponentType(), null), 1.364 + 0).getClass(); 1.365 + } 1.366 + 1.367 + public Class onVariable(TypeVariable tv, Void v) { 1.368 + return visit(tv.getBounds()[0], null); 1.369 + } 1.370 + 1.371 + public Class onWildcard(WildcardType w, Void v) { 1.372 + return visit(w.getUpperBounds()[0], null); 1.373 + } 1.374 + }; 1.375 + 1.376 + /** 1.377 + * Returns the runtime representation of the given type. 1.378 + * 1.379 + * This corresponds to the notion of the erasure in JSR-14. 1.380 + * 1.381 + * <p> 1.382 + * Because of the difference in the way Annotation Processing and the Java reflection 1.383 + * treats primitive type and array type, we can't define this method 1.384 + * on {@link Navigator}. 1.385 + * 1.386 + * <p> 1.387 + * It made me realize how difficult it is to define the common navigation 1.388 + * layer for two different underlying reflection library. The other way 1.389 + * is to throw away the entire parameterization and go to the wrapper approach. 1.390 + */ 1.391 + public <T> Class<T> erasure(Type t) { 1.392 + return eraser.visit(t, null); 1.393 + } 1.394 + 1.395 + public boolean isAbstract(Class clazz) { 1.396 + return Modifier.isAbstract(clazz.getModifiers()); 1.397 + } 1.398 + 1.399 + public boolean isFinal(Class clazz) { 1.400 + return Modifier.isFinal(clazz.getModifiers()); 1.401 + } 1.402 + 1.403 + /** 1.404 + * Returns the {@link Type} object that represents {@code clazz<T1,T2,T3>}. 1.405 + */ 1.406 + public Type createParameterizedType(Class rawType, Type... arguments) { 1.407 + return new ParameterizedTypeImpl(rawType, arguments, null); 1.408 + } 1.409 + 1.410 + public boolean isArray(Type t) { 1.411 + if (t instanceof Class) { 1.412 + Class c = (Class) t; 1.413 + return c.isArray(); 1.414 + } 1.415 + if (t instanceof GenericArrayType) { 1.416 + return true; 1.417 + } 1.418 + return false; 1.419 + } 1.420 + 1.421 + public boolean isArrayButNotByteArray(Type t) { 1.422 + if (t instanceof Class) { 1.423 + Class c = (Class) t; 1.424 + return c.isArray() && c != byte[].class; 1.425 + } 1.426 + if (t instanceof GenericArrayType) { 1.427 + t = ((GenericArrayType) t).getGenericComponentType(); 1.428 + return t != Byte.TYPE; 1.429 + } 1.430 + return false; 1.431 + } 1.432 + 1.433 + public Type getComponentType(Type t) { 1.434 + if (t instanceof Class) { 1.435 + Class c = (Class) t; 1.436 + return c.getComponentType(); 1.437 + } 1.438 + if (t instanceof GenericArrayType) { 1.439 + return ((GenericArrayType) t).getGenericComponentType(); 1.440 + } 1.441 + 1.442 + throw new IllegalArgumentException(); 1.443 + } 1.444 + 1.445 + public Type getTypeArgument(Type type, int i) { 1.446 + if (type instanceof ParameterizedType) { 1.447 + ParameterizedType p = (ParameterizedType) type; 1.448 + return fix(p.getActualTypeArguments()[i]); 1.449 + } else { 1.450 + throw new IllegalArgumentException(); 1.451 + } 1.452 + } 1.453 + 1.454 + public boolean isParameterizedType(Type type) { 1.455 + return type instanceof ParameterizedType; 1.456 + } 1.457 + 1.458 + public boolean isPrimitive(Type type) { 1.459 + if (type instanceof Class) { 1.460 + Class c = (Class) type; 1.461 + return c.isPrimitive(); 1.462 + } 1.463 + return false; 1.464 + } 1.465 + 1.466 + public Type getPrimitive(Class primitiveType) { 1.467 + assert primitiveType.isPrimitive(); 1.468 + return primitiveType; 1.469 + } 1.470 + 1.471 + public Location getClassLocation(final Class clazz) { 1.472 + return new Location() { 1.473 + 1.474 + @Override 1.475 + public String toString() { 1.476 + return clazz.getName(); 1.477 + } 1.478 + }; 1.479 + } 1.480 + 1.481 + public Location getFieldLocation(final Field field) { 1.482 + return new Location() { 1.483 + 1.484 + @Override 1.485 + public String toString() { 1.486 + return field.toString(); 1.487 + } 1.488 + }; 1.489 + } 1.490 + 1.491 + public Location getMethodLocation(final Method method) { 1.492 + return new Location() { 1.493 + 1.494 + @Override 1.495 + public String toString() { 1.496 + return method.toString(); 1.497 + } 1.498 + }; 1.499 + } 1.500 + 1.501 + public boolean hasDefaultConstructor(Class c) { 1.502 + try { 1.503 + c.getDeclaredConstructor(); 1.504 + return true; 1.505 + } catch (NoSuchMethodException e) { 1.506 + return false; // todo: do this WITHOUT exception throw 1.507 + } 1.508 + } 1.509 + 1.510 + public boolean isStaticField(Field field) { 1.511 + return Modifier.isStatic(field.getModifiers()); 1.512 + } 1.513 + 1.514 + public boolean isPublicMethod(Method method) { 1.515 + return Modifier.isPublic(method.getModifiers()); 1.516 + } 1.517 + 1.518 + public boolean isPublicField(Field field) { 1.519 + return Modifier.isPublic(field.getModifiers()); 1.520 + } 1.521 + 1.522 + public boolean isEnum(Class c) { 1.523 + return Enum.class.isAssignableFrom(c); 1.524 + } 1.525 + 1.526 + public Field[] getEnumConstants(Class clazz) { 1.527 + try { 1.528 + Object[] values = clazz.getEnumConstants(); 1.529 + Field[] fields = new Field[values.length]; 1.530 + for (int i = 0; i < values.length; i++) { 1.531 + fields[i] = clazz.getField(((Enum) values[i]).name()); 1.532 + } 1.533 + return fields; 1.534 + } catch (NoSuchFieldException e) { 1.535 + // impossible 1.536 + throw new NoSuchFieldError(e.getMessage()); 1.537 + } 1.538 + } 1.539 + 1.540 + public Type getVoidType() { 1.541 + return Void.class; 1.542 + } 1.543 + 1.544 + public String getPackageName(Class clazz) { 1.545 + String name = clazz.getName(); 1.546 + int idx = name.lastIndexOf('.'); 1.547 + if (idx < 0) { 1.548 + return ""; 1.549 + } else { 1.550 + return name.substring(0, idx); 1.551 + } 1.552 + } 1.553 + 1.554 + @Override 1.555 + public Class loadObjectFactory(Class referencePoint, String pkg) { 1.556 + ClassLoader cl= SecureLoader.getClassClassLoader(referencePoint); 1.557 + if (cl == null) 1.558 + cl = SecureLoader.getSystemClassLoader(); 1.559 + 1.560 + try { 1.561 + return cl.loadClass(pkg + ".ObjectFactory"); 1.562 + } catch (ClassNotFoundException e) { 1.563 + return null; 1.564 + } 1.565 + } 1.566 + 1.567 + public boolean isBridgeMethod(Method method) { 1.568 + return method.isBridge(); 1.569 + } 1.570 + 1.571 + public boolean isOverriding(Method method, Class base) { 1.572 + // this isn't actually correct, 1.573 + // as the JLS considers 1.574 + // class Derived extends Base<Integer> { 1.575 + // Integer getX() { ... } 1.576 + // } 1.577 + // class Base<T> { 1.578 + // T getX() { ... } 1.579 + // } 1.580 + // to be overrided. Handling this correctly needs a careful implementation 1.581 + 1.582 + String name = method.getName(); 1.583 + Class[] params = method.getParameterTypes(); 1.584 + 1.585 + while (base != null) { 1.586 + try { 1.587 + if (base.getDeclaredMethod(name, params) != null) { 1.588 + return true; 1.589 + } 1.590 + } catch (NoSuchMethodException e) { 1.591 + // recursively go into the base class 1.592 + } 1.593 + 1.594 + base = base.getSuperclass(); 1.595 + } 1.596 + 1.597 + return false; 1.598 + } 1.599 + 1.600 + public boolean isInterface(Class clazz) { 1.601 + return clazz.isInterface(); 1.602 + } 1.603 + 1.604 + public boolean isTransient(Field f) { 1.605 + return Modifier.isTransient(f.getModifiers()); 1.606 + } 1.607 + 1.608 + public boolean isInnerClass(Class clazz) { 1.609 + return clazz.getEnclosingClass() != null && !Modifier.isStatic(clazz.getModifiers()); 1.610 + } 1.611 + 1.612 + @Override 1.613 + public boolean isSameType(Type t1, Type t2) { 1.614 + return t1.equals(t2); 1.615 + } 1.616 + 1.617 + /** 1.618 + * JDK 5.0 has a bug of creating {@link GenericArrayType} where it shouldn't. 1.619 + * fix that manually to work around the problem. 1.620 + * 1.621 + * See bug 6202725. 1.622 + */ 1.623 + private Type fix(Type t) { 1.624 + if (!(t instanceof GenericArrayType)) { 1.625 + return t; 1.626 + } 1.627 + 1.628 + GenericArrayType gat = (GenericArrayType) t; 1.629 + if (gat.getGenericComponentType() instanceof Class) { 1.630 + Class c = (Class) gat.getGenericComponentType(); 1.631 + return Array.newInstance(c, 0).getClass(); 1.632 + } 1.633 + 1.634 + return t; 1.635 + } 1.636 +}