Wed, 12 Jun 2013 14:47:09 +0100
8013021: Rebase 8005432 & 8003542 against the latest jdk8/jaxws
8003542: Improve processing of MTOM attachments
8005432: Update access to JAX-WS
Reviewed-by: mullan
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 */
26 package com.sun.tools.internal.jxc.model.nav;
28 import com.sun.source.tree.CompilationUnitTree;
29 import com.sun.source.util.TreePath;
30 import com.sun.source.util.Trees;
31 import com.sun.xml.internal.bind.v2.model.nav.Navigator;
32 import com.sun.xml.internal.bind.v2.runtime.Location;
34 import javax.annotation.processing.ProcessingEnvironment;
35 import javax.lang.model.element.AnnotationMirror;
36 import javax.lang.model.element.Element;
37 import javax.lang.model.element.ElementKind;
38 import javax.lang.model.element.ExecutableElement;
39 import javax.lang.model.element.Modifier;
40 import javax.lang.model.element.TypeElement;
41 import javax.lang.model.element.TypeParameterElement;
42 import javax.lang.model.element.VariableElement;
43 import javax.lang.model.type.ArrayType;
44 import javax.lang.model.type.DeclaredType;
45 import javax.lang.model.type.PrimitiveType;
46 import javax.lang.model.type.TypeKind;
47 import javax.lang.model.type.TypeMirror;
48 import javax.lang.model.type.TypeVariable;
49 import javax.lang.model.type.TypeVisitor;
50 import javax.lang.model.type.WildcardType;
51 import javax.lang.model.util.ElementFilter;
52 import javax.lang.model.util.Elements;
53 import javax.lang.model.util.SimpleTypeVisitor6;
54 import javax.lang.model.util.Types;
55 import java.lang.annotation.Annotation;
56 import java.util.Collection;
57 import java.util.HashMap;
58 import java.util.HashSet;
59 import java.util.List;
60 import java.util.Map;
62 /**
63 * {@link Navigator} implementation for annotation processing.
64 * TODO: check the spec on how generics are supposed to be handled
65 *
66 * @author Kohsuke Kawaguchi (kk@kohsuke.org)
67 */
68 public class ApNavigator implements Navigator<TypeMirror, TypeElement, VariableElement, ExecutableElement> {
70 private final ProcessingEnvironment env;
72 private final PrimitiveType primitiveByte;
74 public ApNavigator(ProcessingEnvironment env) {
75 this.env = env;
76 this.primitiveByte = env.getTypeUtils().getPrimitiveType(TypeKind.BYTE);
77 }
79 public TypeElement getSuperClass(TypeElement typeElement) {
80 if (typeElement.getKind().equals(ElementKind.CLASS)) {
81 TypeMirror sup = typeElement.getSuperclass();
82 if (!sup.getKind().equals(TypeKind.NONE))
83 return (TypeElement) ((DeclaredType) sup).asElement();
84 else
85 return null;
86 }
87 return env.getElementUtils().getTypeElement(Object.class.getName());
88 }
90 public TypeMirror getBaseClass(TypeMirror type, TypeElement sup) {
91 return baseClassFinder.visit(type, sup);
92 }
94 public String getClassName(TypeElement t) {
95 return t.getQualifiedName().toString();
96 }
98 public String getTypeName(TypeMirror typeMirror) {
99 return typeMirror.toString();
100 }
102 public String getClassShortName(TypeElement t) {
103 return t.getSimpleName().toString();
104 }
106 public Collection<VariableElement> getDeclaredFields(TypeElement typeElement) {
107 return ElementFilter.fieldsIn(typeElement.getEnclosedElements());
108 }
110 public VariableElement getDeclaredField(TypeElement clazz, String fieldName) {
111 for (VariableElement fd : ElementFilter.fieldsIn(clazz.getEnclosedElements())) {
112 if (fd.getSimpleName().toString().equals(fieldName))
113 return fd;
114 }
115 return null;
116 }
118 public Collection<ExecutableElement> getDeclaredMethods(TypeElement typeElement) {
119 return ElementFilter.methodsIn(typeElement.getEnclosedElements());
120 }
122 public TypeElement getDeclaringClassForField(VariableElement f) {
123 return (TypeElement) f.getEnclosingElement();
124 }
126 public TypeElement getDeclaringClassForMethod(ExecutableElement m) {
127 return (TypeElement) m.getEnclosingElement();
128 }
130 public TypeMirror getFieldType(VariableElement f) {
131 return f.asType();
132 }
134 public String getFieldName(VariableElement f) {
135 return f.getSimpleName().toString();
136 }
138 public String getMethodName(ExecutableElement m) {
139 return m.getSimpleName().toString();
140 }
142 public TypeMirror getReturnType(ExecutableElement m) {
143 return m.getReturnType();
144 }
146 public TypeMirror[] getMethodParameters(ExecutableElement m) {
147 Collection<? extends VariableElement> ps = m.getParameters();
148 TypeMirror[] r = new TypeMirror[ps.size()];
149 int i=0;
150 for (VariableElement p : ps)
151 r[i++] = p.asType();
152 return r;
153 }
155 public boolean isStaticMethod(ExecutableElement m) {
156 return hasModifier(m, Modifier.STATIC);
157 }
159 public boolean isFinalMethod(ExecutableElement m) {
160 return hasModifier(m, Modifier.FINAL);
161 }
163 private boolean hasModifier(Element d, Modifier mod) {
164 return d.getModifiers().contains(mod);
165 }
167 public boolean isSubClassOf(TypeMirror sub, TypeMirror sup) {
168 if(sup==DUMMY)
169 // see ref(). if the sub type is known to Annotation Processing,
170 // its base class must be known. Thus if the sup is DUMMY,
171 // it cannot possibly be the super type.
172 return false;
173 return env.getTypeUtils().isSubtype(sub,sup);
174 }
176 private String getSourceClassName(Class clazz) {
177 Class<?> d = clazz.getDeclaringClass();
178 if(d==null)
179 return clazz.getName();
180 else {
181 String shortName = clazz.getName().substring(d.getName().length()+1/*for $*/);
182 return getSourceClassName(d)+'.'+shortName;
183 }
184 }
186 public TypeMirror ref(Class c) {
187 if(c.isArray())
188 return env.getTypeUtils().getArrayType( ref(c.getComponentType()) );
189 if(c.isPrimitive())
190 return getPrimitive(c);
191 TypeElement t = env.getElementUtils().getTypeElement(getSourceClassName(c));
192 // Annotation Processing only operates on a set of classes used in the compilation,
193 // and it won't recognize additional classes (even if they are visible from javac)
194 // and return null.
195 //
196 // this is causing a problem where we check if a type is collection.
197 // so until the problem is fixed in Annotation Processing, work around the issue
198 // by returning a dummy token
199 // TODO: check if this is still valid
200 if(t==null)
201 return DUMMY;
202 return env.getTypeUtils().getDeclaredType(t);
203 }
205 public TypeMirror use(TypeElement t) {
206 assert t != null;
207 return env.getTypeUtils().getDeclaredType(t);
208 }
210 public TypeElement asDecl(TypeMirror m) {
211 m = env.getTypeUtils().erasure(m);
212 if (m.getKind().equals(TypeKind.DECLARED)) {
213 DeclaredType d = (DeclaredType) m;
214 return (TypeElement) d.asElement();
215 } else
216 return null;
217 }
219 public TypeElement asDecl(Class c) {
220 return env.getElementUtils().getTypeElement(getSourceClassName(c));
221 }
223 public TypeMirror erasure(TypeMirror t) {
224 Types tu = env.getTypeUtils();
225 t = tu.erasure(t);
226 if (t.getKind().equals(TypeKind.DECLARED)) {
227 DeclaredType dt = (DeclaredType)t;
228 if (!dt.getTypeArguments().isEmpty())
229 return tu.getDeclaredType((TypeElement) dt.asElement());
230 }
231 return t;
232 }
234 public boolean isAbstract(TypeElement clazz) {
235 return hasModifier(clazz,Modifier.ABSTRACT);
236 }
238 public boolean isFinal(TypeElement clazz) {
239 return hasModifier(clazz,Modifier.FINAL);
240 }
242 public VariableElement[] getEnumConstants(TypeElement clazz) {
243 List<? extends Element> elements = env.getElementUtils().getAllMembers(clazz);
244 Collection<VariableElement> constants = new HashSet<VariableElement>();
245 for (Element element : elements) {
246 if (element.getKind().equals(ElementKind.ENUM_CONSTANT)) {
247 constants.add((VariableElement) element);
248 }
249 }
250 return constants.toArray(new VariableElement[constants.size()]);
251 }
253 public TypeMirror getVoidType() {
254 return env.getTypeUtils().getNoType(TypeKind.VOID);
255 }
257 public String getPackageName(TypeElement clazz) {
258 return env.getElementUtils().getPackageOf(clazz).getQualifiedName().toString();
259 }
261 public TypeElement findClass(String className, TypeElement referencePoint) {
262 return env.getElementUtils().getTypeElement(className);
263 }
265 public boolean isBridgeMethod(ExecutableElement method) {
266 return method.getModifiers().contains(Modifier.VOLATILE);
267 }
269 public boolean isOverriding(ExecutableElement method, TypeElement base) {
270 Elements elements = env.getElementUtils();
272 while (true) {
273 for (ExecutableElement m : ElementFilter.methodsIn(elements.getAllMembers(base))) {
274 if (elements.overrides(method, m, base))
275 return true;
276 }
278 if (base.getSuperclass().getKind().equals(TypeKind.NONE))
279 return false;
280 base = (TypeElement) env.getTypeUtils().asElement(base.getSuperclass());
281 }
282 }
284 public boolean isInterface(TypeElement clazz) {
285 return clazz.getKind().isInterface();
286 }
288 public boolean isTransient(VariableElement f) {
289 return f.getModifiers().contains(Modifier.TRANSIENT);
290 }
292 public boolean isInnerClass(TypeElement clazz) {
293 return clazz.getEnclosingElement() != null && !clazz.getModifiers().contains(Modifier.STATIC);
294 }
296 @Override
297 public boolean isSameType(TypeMirror t1, TypeMirror t2) {
298 return env.getTypeUtils().isSameType(t1, t2);
299 }
301 public boolean isArray(TypeMirror type) {
302 return type != null && type.getKind().equals(TypeKind.ARRAY);
303 }
305 public boolean isArrayButNotByteArray(TypeMirror t) {
306 if(!isArray(t))
307 return false;
309 ArrayType at = (ArrayType) t;
310 TypeMirror ct = at.getComponentType();
312 return !ct.equals(primitiveByte);
313 }
315 public TypeMirror getComponentType(TypeMirror t) {
316 if (isArray(t)) {
317 ArrayType at = (ArrayType) t;
318 return at.getComponentType();
319 }
321 throw new IllegalArgumentException();
322 }
324 public TypeMirror getTypeArgument(TypeMirror typeMirror, int i) {
325 if (typeMirror != null && typeMirror.getKind().equals(TypeKind.DECLARED)) {
326 DeclaredType declaredType = (DeclaredType) typeMirror;
327 TypeMirror[] args = declaredType.getTypeArguments().toArray(new TypeMirror[declaredType.getTypeArguments().size()]);
328 return args[i];
329 } else throw new IllegalArgumentException();
330 }
332 public boolean isParameterizedType(TypeMirror typeMirror) {
333 if (typeMirror != null && typeMirror.getKind().equals(TypeKind.DECLARED)) {
334 DeclaredType d = (DeclaredType) typeMirror;
335 return !d.getTypeArguments().isEmpty();
336 }
337 return false;
338 }
340 public boolean isPrimitive(TypeMirror t) {
341 return t.getKind().isPrimitive();
342 }
344 private static final Map<Class, TypeKind> primitives = new HashMap<Class, TypeKind>();
346 static {
347 primitives.put(Integer.TYPE, TypeKind.INT);
348 primitives.put(Byte.TYPE, TypeKind.BYTE);
349 primitives.put(Float.TYPE, TypeKind.FLOAT);
350 primitives.put(Boolean.TYPE, TypeKind.BOOLEAN);
351 primitives.put(Short.TYPE, TypeKind.SHORT);
352 primitives.put(Long.TYPE, TypeKind.LONG);
353 primitives.put(Double.TYPE, TypeKind.DOUBLE);
354 primitives.put(Character.TYPE, TypeKind.CHAR);
355 }
357 public TypeMirror getPrimitive(Class primitiveType) {
358 assert primitiveType.isPrimitive();
359 if(primitiveType==void.class)
360 return getVoidType();
361 return env.getTypeUtils().getPrimitiveType(primitives.get(primitiveType));
362 }
364 /**
365 * see {@link #ref(Class)}.
366 */
367 private static final TypeMirror DUMMY = new TypeMirror() {
368 @Override
369 public <R, P> R accept(TypeVisitor<R, P> v, P p) {
370 throw new IllegalStateException();
371 }
373 @Override
374 public TypeKind getKind() {
375 throw new IllegalStateException();
376 }
378 // @Override
379 public List<? extends AnnotationMirror> getAnnotationMirrors() {
380 throw new IllegalStateException();
381 }
383 // @Override
384 public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
385 throw new IllegalStateException();
386 }
388 // @Override
389 public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) {
390 throw new IllegalStateException();
391 }
392 };
394 public Location getClassLocation(TypeElement typeElement) {
395 Trees trees = Trees.instance(env);
396 return getLocation(typeElement.getQualifiedName().toString(), trees.getPath(typeElement));
397 }
399 public Location getFieldLocation(VariableElement variableElement) {
400 return getLocation(variableElement);
401 }
403 public Location getMethodLocation(ExecutableElement executableElement) {
404 return getLocation(executableElement);
405 }
407 public boolean hasDefaultConstructor(TypeElement t) {
408 if (t == null || !t.getKind().equals(ElementKind.CLASS))
409 return false;
411 for (ExecutableElement init : ElementFilter.constructorsIn(env.getElementUtils().getAllMembers(t))) {
412 if (init.getParameters().isEmpty())
413 return true;
414 }
415 return false;
416 }
418 public boolean isStaticField(VariableElement f) {
419 return hasModifier(f,Modifier.STATIC);
420 }
422 public boolean isPublicMethod(ExecutableElement m) {
423 return hasModifier(m,Modifier.PUBLIC);
424 }
426 public boolean isPublicField(VariableElement f) {
427 return hasModifier(f,Modifier.PUBLIC);
428 }
430 public boolean isEnum(TypeElement t) {
431 return t != null && t.getKind().equals(ElementKind.ENUM);
432 }
434 private Location getLocation(Element element) {
435 Trees trees = Trees.instance(env);
436 return getLocation(
437 ((TypeElement) element.getEnclosingElement()).getQualifiedName() + "." + element.getSimpleName(),
438 trees.getPath(element)
439 );
440 }
442 private Location getLocation(final String name, final TreePath treePath) {
443 return new Location() {
444 public String toString() {
445 if (treePath == null)
446 return name + " (Unknown Source)";
447 // just like stack trace, we just print the file name and
448 // not the whole path. The idea is that the package name should
449 // provide enough clue on which directory it lives.
450 CompilationUnitTree compilationUnit = treePath.getCompilationUnit();
451 Trees trees = Trees.instance(env);
452 long startPosition = trees.getSourcePositions().getStartPosition(compilationUnit, treePath.getLeaf());
453 return name + "(" +
454 compilationUnit.getSourceFile().getName() + ":" + compilationUnit.getLineMap().getLineNumber(startPosition) +
455 ")";
456 }
457 };
458 }
460 /**
461 * Implements {@link #getBaseClass}.
462 */
463 private final SimpleTypeVisitor6<TypeMirror, TypeElement> baseClassFinder = new SimpleTypeVisitor6<TypeMirror, TypeElement>() {
464 @Override
465 public TypeMirror visitDeclared(DeclaredType t, TypeElement sup) {
466 if (t.asElement().equals(sup))
467 return t;
469 for (TypeMirror i : env.getTypeUtils().directSupertypes(t)) {
470 TypeMirror r = visitDeclared((DeclaredType) i, sup);
471 if (r != null)
472 return r;
473 }
475 // otherwise recursively apply super class and base types
476 TypeMirror superclass = ((TypeElement) t.asElement()).getSuperclass();
477 if (!superclass.getKind().equals(TypeKind.NONE)) {
478 TypeMirror r = visitDeclared((DeclaredType) superclass, sup);
479 if (r != null)
480 return r;
481 }
482 return null;
483 }
485 @Override
486 public TypeMirror visitTypeVariable(TypeVariable t, TypeElement typeElement) {
487 // we are checking if T (declared as T extends A&B&C) is assignable to sup.
488 // so apply bounds recursively.
489 for (TypeMirror typeMirror : ((TypeParameterElement) t.asElement()).getBounds()) {
490 TypeMirror m = visit(typeMirror, typeElement);
491 if (m != null)
492 return m;
493 }
494 return null;
495 }
497 @Override
498 public TypeMirror visitArray(ArrayType t, TypeElement typeElement) {
499 // we are checking if t=T[] is assignable to sup.
500 // the only case this is allowed is sup=Object,
501 // and Object isn't parameterized.
502 return null;
503 }
505 @Override
506 public TypeMirror visitWildcard(WildcardType t, TypeElement typeElement) {
507 // we are checking if T (= ? extends A&B&C) is assignable to sup.
508 // so apply bounds recursively.
509 return visit(t.getExtendsBound(), typeElement);
510 }
512 @Override
513 protected TypeMirror defaultAction(TypeMirror e, TypeElement typeElement) {
514 return e;
515 }
516 };
517 }