Fri, 09 May 2014 20:33:21 -0700
8041424: 8u20 l10n resource file translation update 1
Reviewed-by: naoto, yhuang
1 /*
2 * Copyright (c) 2005, 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.javac.api;
28 import java.io.IOException;
29 import java.util.HashSet;
30 import java.util.Set;
32 import javax.annotation.processing.ProcessingEnvironment;
33 import javax.lang.model.element.AnnotationMirror;
34 import javax.lang.model.element.AnnotationValue;
35 import javax.lang.model.element.Element;
36 import javax.lang.model.element.ElementKind;
37 import javax.lang.model.element.ExecutableElement;
38 import javax.lang.model.element.TypeElement;
39 import javax.lang.model.type.DeclaredType;
40 import javax.lang.model.type.TypeKind;
41 import javax.lang.model.type.TypeMirror;
42 import javax.tools.Diagnostic;
43 import javax.tools.JavaCompiler;
44 import javax.tools.JavaFileObject;
46 import com.sun.source.doctree.DocCommentTree;
47 import com.sun.source.doctree.DocTree;
48 import com.sun.source.tree.CatchTree;
49 import com.sun.source.tree.CompilationUnitTree;
50 import com.sun.source.tree.Scope;
51 import com.sun.source.tree.Tree;
52 import com.sun.source.util.DocSourcePositions;
53 import com.sun.source.util.DocTreePath;
54 import com.sun.source.util.DocTreeScanner;
55 import com.sun.source.util.DocTrees;
56 import com.sun.source.util.JavacTask;
57 import com.sun.source.util.TreePath;
58 import com.sun.tools.javac.code.Flags;
59 import com.sun.tools.javac.code.Kinds;
60 import com.sun.tools.javac.code.Symbol;
61 import com.sun.tools.javac.code.Symbol.ClassSymbol;
62 import com.sun.tools.javac.code.Symbol.MethodSymbol;
63 import com.sun.tools.javac.code.Symbol.PackageSymbol;
64 import com.sun.tools.javac.code.Symbol.TypeSymbol;
65 import com.sun.tools.javac.code.Symbol.VarSymbol;
66 import com.sun.tools.javac.code.Type;
67 import com.sun.tools.javac.code.Type.ArrayType;
68 import com.sun.tools.javac.code.Type.ClassType;
69 import com.sun.tools.javac.code.Type.ErrorType;
70 import com.sun.tools.javac.code.Type.UnionClassType;
71 import com.sun.tools.javac.code.Types;
72 import com.sun.tools.javac.code.Types.TypeRelation;
73 import com.sun.tools.javac.comp.Attr;
74 import com.sun.tools.javac.comp.AttrContext;
75 import com.sun.tools.javac.comp.Enter;
76 import com.sun.tools.javac.comp.Env;
77 import com.sun.tools.javac.comp.MemberEnter;
78 import com.sun.tools.javac.comp.Resolve;
79 import com.sun.tools.javac.model.JavacElements;
80 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
81 import com.sun.tools.javac.tree.DCTree;
82 import com.sun.tools.javac.tree.DCTree.DCBlockTag;
83 import com.sun.tools.javac.tree.DCTree.DCDocComment;
84 import com.sun.tools.javac.tree.DCTree.DCEndPosTree;
85 import com.sun.tools.javac.tree.DCTree.DCErroneous;
86 import com.sun.tools.javac.tree.DCTree.DCIdentifier;
87 import com.sun.tools.javac.tree.DCTree.DCParam;
88 import com.sun.tools.javac.tree.DCTree.DCReference;
89 import com.sun.tools.javac.tree.DCTree.DCText;
90 import com.sun.tools.javac.tree.EndPosTable;
91 import com.sun.tools.javac.tree.JCTree;
92 import com.sun.tools.javac.tree.JCTree.*;
93 import com.sun.tools.javac.tree.TreeCopier;
94 import com.sun.tools.javac.tree.TreeInfo;
95 import com.sun.tools.javac.tree.TreeMaker;
96 import com.sun.tools.javac.util.Abort;
97 import com.sun.tools.javac.util.Assert;
98 import com.sun.tools.javac.util.Context;
99 import com.sun.tools.javac.util.JCDiagnostic;
100 import com.sun.tools.javac.util.List;
101 import com.sun.tools.javac.util.ListBuffer;
102 import com.sun.tools.javac.util.Log;
103 import com.sun.tools.javac.util.Name;
104 import com.sun.tools.javac.util.Names;
105 import com.sun.tools.javac.util.Pair;
106 import com.sun.tools.javac.util.Position;
107 import static com.sun.tools.javac.code.TypeTag.*;
109 /**
110 * Provides an implementation of Trees.
111 *
112 * <p><b>This is NOT part of any supported API.
113 * If you write code that depends on this, you do so at your own
114 * risk. This code and its internal interfaces are subject to change
115 * or deletion without notice.</b></p>
116 *
117 * @author Peter von der Ahé
118 */
119 public class JavacTrees extends DocTrees {
121 // in a world of a single context per compilation, these would all be final
122 private Resolve resolve;
123 private Enter enter;
124 private Log log;
125 private MemberEnter memberEnter;
126 private Attr attr;
127 private TreeMaker treeMaker;
128 private JavacElements elements;
129 private JavacTaskImpl javacTaskImpl;
130 private Names names;
131 private Types types;
133 // called reflectively from Trees.instance(CompilationTask task)
134 public static JavacTrees instance(JavaCompiler.CompilationTask task) {
135 if (!(task instanceof BasicJavacTask))
136 throw new IllegalArgumentException();
137 return instance(((BasicJavacTask)task).getContext());
138 }
140 // called reflectively from Trees.instance(ProcessingEnvironment env)
141 public static JavacTrees instance(ProcessingEnvironment env) {
142 if (!(env instanceof JavacProcessingEnvironment))
143 throw new IllegalArgumentException();
144 return instance(((JavacProcessingEnvironment)env).getContext());
145 }
147 public static JavacTrees instance(Context context) {
148 JavacTrees instance = context.get(JavacTrees.class);
149 if (instance == null)
150 instance = new JavacTrees(context);
151 return instance;
152 }
154 protected JavacTrees(Context context) {
155 context.put(JavacTrees.class, this);
156 init(context);
157 }
159 public void updateContext(Context context) {
160 init(context);
161 }
163 private void init(Context context) {
164 attr = Attr.instance(context);
165 enter = Enter.instance(context);
166 elements = JavacElements.instance(context);
167 log = Log.instance(context);
168 resolve = Resolve.instance(context);
169 treeMaker = TreeMaker.instance(context);
170 memberEnter = MemberEnter.instance(context);
171 names = Names.instance(context);
172 types = Types.instance(context);
174 JavacTask t = context.get(JavacTask.class);
175 if (t instanceof JavacTaskImpl)
176 javacTaskImpl = (JavacTaskImpl) t;
177 }
179 public DocSourcePositions getSourcePositions() {
180 return new DocSourcePositions() {
181 public long getStartPosition(CompilationUnitTree file, Tree tree) {
182 return TreeInfo.getStartPos((JCTree) tree);
183 }
185 public long getEndPosition(CompilationUnitTree file, Tree tree) {
186 EndPosTable endPosTable = ((JCCompilationUnit) file).endPositions;
187 return TreeInfo.getEndPos((JCTree) tree, endPosTable);
188 }
190 public long getStartPosition(CompilationUnitTree file, DocCommentTree comment, DocTree tree) {
191 return ((DCTree) tree).getSourcePosition((DCDocComment) comment);
192 }
193 @SuppressWarnings("fallthrough")
194 public long getEndPosition(CompilationUnitTree file, DocCommentTree comment, DocTree tree) {
195 DCDocComment dcComment = (DCDocComment) comment;
196 if (tree instanceof DCEndPosTree) {
197 int endPos = ((DCEndPosTree) tree).getEndPos(dcComment);
199 if (endPos != Position.NOPOS) {
200 return endPos;
201 }
202 }
203 int correction = 0;
204 switch (tree.getKind()) {
205 case TEXT:
206 DCText text = (DCText) tree;
208 return dcComment.comment.getSourcePos(text.pos + text.text.length());
209 case ERRONEOUS:
210 DCErroneous err = (DCErroneous) tree;
212 return dcComment.comment.getSourcePos(err.pos + err.body.length());
213 case IDENTIFIER:
214 DCIdentifier ident = (DCIdentifier) tree;
216 return dcComment.comment.getSourcePos(ident.pos + (ident.name != names.error ? ident.name.length() : 0));
217 case PARAM:
218 DCParam param = (DCParam) tree;
220 if (param.isTypeParameter && param.getDescription().isEmpty()) {
221 correction = 1;
222 }
223 case AUTHOR: case DEPRECATED: case RETURN: case SEE:
224 case SERIAL: case SERIAL_DATA: case SERIAL_FIELD: case SINCE:
225 case THROWS: case UNKNOWN_BLOCK_TAG: case VERSION: {
226 DocTree last = getLastChild(tree);
228 if (last != null) {
229 return getEndPosition(file, comment, last) + correction;
230 }
232 DCBlockTag block = (DCBlockTag) tree;
234 return dcComment.comment.getSourcePos(block.pos + block.getTagName().length() + 1);
235 }
236 default:
237 DocTree last = getLastChild(tree);
239 if (last != null) {
240 return getEndPosition(file, comment, last);
241 }
242 break;
243 }
245 return Position.NOPOS;
246 }
247 };
248 }
250 private DocTree getLastChild(DocTree tree) {
251 final DocTree[] last = new DocTree[] {null};
253 tree.accept(new DocTreeScanner<Void, Void>() {
254 @Override public Void scan(DocTree node, Void p) {
255 if (node != null) last[0] = node;
256 return null;
257 }
258 }, null);
260 return last[0];
261 }
263 public JCClassDecl getTree(TypeElement element) {
264 return (JCClassDecl) getTree((Element) element);
265 }
267 public JCMethodDecl getTree(ExecutableElement method) {
268 return (JCMethodDecl) getTree((Element) method);
269 }
271 public JCTree getTree(Element element) {
272 Symbol symbol = (Symbol) element;
273 TypeSymbol enclosing = symbol.enclClass();
274 Env<AttrContext> env = enter.getEnv(enclosing);
275 if (env == null)
276 return null;
277 JCClassDecl classNode = env.enclClass;
278 if (classNode != null) {
279 if (TreeInfo.symbolFor(classNode) == element)
280 return classNode;
281 for (JCTree node : classNode.getMembers())
282 if (TreeInfo.symbolFor(node) == element)
283 return node;
284 }
285 return null;
286 }
288 public JCTree getTree(Element e, AnnotationMirror a) {
289 return getTree(e, a, null);
290 }
292 public JCTree getTree(Element e, AnnotationMirror a, AnnotationValue v) {
293 Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
294 if (treeTopLevel == null)
295 return null;
296 return treeTopLevel.fst;
297 }
299 public TreePath getPath(CompilationUnitTree unit, Tree node) {
300 return TreePath.getPath(unit, node);
301 }
303 public TreePath getPath(Element e) {
304 return getPath(e, null, null);
305 }
307 public TreePath getPath(Element e, AnnotationMirror a) {
308 return getPath(e, a, null);
309 }
311 public TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v) {
312 final Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
313 if (treeTopLevel == null)
314 return null;
315 return TreePath.getPath(treeTopLevel.snd, treeTopLevel.fst);
316 }
318 public Symbol getElement(TreePath path) {
319 JCTree tree = (JCTree) path.getLeaf();
320 Symbol sym = TreeInfo.symbolFor(tree);
321 if (sym == null) {
322 if (TreeInfo.isDeclaration(tree)) {
323 for (TreePath p = path; p != null; p = p.getParentPath()) {
324 JCTree t = (JCTree) p.getLeaf();
325 if (t.hasTag(JCTree.Tag.CLASSDEF)) {
326 JCClassDecl ct = (JCClassDecl) t;
327 if (ct.sym != null) {
328 if ((ct.sym.flags_field & Flags.UNATTRIBUTED) != 0) {
329 attr.attribClass(ct.pos(), ct.sym);
330 sym = TreeInfo.symbolFor(tree);
331 }
332 break;
333 }
334 }
335 }
336 }
337 }
338 return sym;
339 }
341 @Override
342 public Element getElement(DocTreePath path) {
343 DocTree forTree = path.getLeaf();
344 if (forTree instanceof DCReference)
345 return attributeDocReference(path.getTreePath(), ((DCReference) forTree));
346 if (forTree instanceof DCIdentifier) {
347 if (path.getParentPath().getLeaf() instanceof DCParam) {
348 return attributeParamIdentifier(path.getTreePath(), (DCParam) path.getParentPath().getLeaf());
349 }
350 }
351 return null;
352 }
354 private Symbol attributeDocReference(TreePath path, DCReference ref) {
355 Env<AttrContext> env = getAttrContext(path);
357 Log.DeferredDiagnosticHandler deferredDiagnosticHandler =
358 new Log.DeferredDiagnosticHandler(log);
359 try {
360 final TypeSymbol tsym;
361 final Name memberName;
362 if (ref.qualifierExpression == null) {
363 tsym = env.enclClass.sym;
364 memberName = ref.memberName;
365 } else {
366 // See if the qualifierExpression is a type or package name.
367 // javac does not provide the exact method required, so
368 // we first check if qualifierExpression identifies a type,
369 // and if not, then we check to see if it identifies a package.
370 Type t = attr.attribType(ref.qualifierExpression, env);
371 if (t.isErroneous()) {
372 if (ref.memberName == null) {
373 // Attr/Resolve assume packages exist and create symbols as needed
374 // so use getPackageElement to restrict search to existing packages
375 PackageSymbol pck = elements.getPackageElement(ref.qualifierExpression.toString());
376 if (pck != null) {
377 return pck;
378 } else if (ref.qualifierExpression.hasTag(JCTree.Tag.IDENT)) {
379 // fixup: allow "identifier" instead of "#identifier"
380 // for compatibility with javadoc
381 tsym = env.enclClass.sym;
382 memberName = ((JCIdent) ref.qualifierExpression).name;
383 } else
384 return null;
385 } else {
386 return null;
387 }
388 } else {
389 tsym = t.tsym;
390 memberName = ref.memberName;
391 }
392 }
394 if (memberName == null)
395 return tsym;
397 final List<Type> paramTypes;
398 if (ref.paramTypes == null)
399 paramTypes = null;
400 else {
401 ListBuffer<Type> lb = new ListBuffer<Type>();
402 for (List<JCTree> l = ref.paramTypes; l.nonEmpty(); l = l.tail) {
403 JCTree tree = l.head;
404 Type t = attr.attribType(tree, env);
405 lb.add(t);
406 }
407 paramTypes = lb.toList();
408 }
410 ClassSymbol sym = (ClassSymbol) types.upperBound(tsym.type).tsym;
412 Symbol msym = (memberName == sym.name)
413 ? findConstructor(sym, paramTypes)
414 : findMethod(sym, memberName, paramTypes);
415 if (paramTypes != null) {
416 // explicit (possibly empty) arg list given, so cannot be a field
417 return msym;
418 }
420 VarSymbol vsym = (ref.paramTypes != null) ? null : findField(sym, memberName);
421 // prefer a field over a method with no parameters
422 if (vsym != null &&
423 (msym == null ||
424 types.isSubtypeUnchecked(vsym.enclClass().asType(), msym.enclClass().asType()))) {
425 return vsym;
426 } else {
427 return msym;
428 }
429 } catch (Abort e) { // may be thrown by Check.completionError in case of bad class file
430 return null;
431 } finally {
432 log.popDiagnosticHandler(deferredDiagnosticHandler);
433 }
434 }
436 private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
437 Symbol javadocSymbol = getElement(path);
438 if (javadocSymbol == null)
439 return null;
440 ElementKind kind = javadocSymbol.getKind();
441 List<? extends Symbol> params = List.nil();
442 if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
443 MethodSymbol ee = (MethodSymbol) javadocSymbol;
444 params = ptag.isTypeParameter()
445 ? ee.getTypeParameters()
446 : ee.getParameters();
447 } else if (kind.isClass() || kind.isInterface()) {
448 ClassSymbol te = (ClassSymbol) javadocSymbol;
449 params = te.getTypeParameters();
450 }
452 for (Symbol param : params) {
453 if (param.getSimpleName() == ptag.getName().getName()) {
454 return param;
455 }
456 }
457 return null;
458 }
460 /** @see com.sun.tools.javadoc.ClassDocImpl#findField */
461 private VarSymbol findField(ClassSymbol tsym, Name fieldName) {
462 return searchField(tsym, fieldName, new HashSet<ClassSymbol>());
463 }
465 /** @see com.sun.tools.javadoc.ClassDocImpl#searchField */
466 private VarSymbol searchField(ClassSymbol tsym, Name fieldName, Set<ClassSymbol> searched) {
467 if (searched.contains(tsym)) {
468 return null;
469 }
470 searched.add(tsym);
472 for (com.sun.tools.javac.code.Scope.Entry e = tsym.members().lookup(fieldName);
473 e.scope != null; e = e.next()) {
474 if (e.sym.kind == Kinds.VAR) {
475 return (VarSymbol)e.sym;
476 }
477 }
479 //### If we found a VarSymbol above, but which did not pass
480 //### the modifier filter, we should return failure here!
482 ClassSymbol encl = tsym.owner.enclClass();
483 if (encl != null) {
484 VarSymbol vsym = searchField(encl, fieldName, searched);
485 if (vsym != null) {
486 return vsym;
487 }
488 }
490 // search superclass
491 Type superclass = tsym.getSuperclass();
492 if (superclass.tsym != null) {
493 VarSymbol vsym = searchField((ClassSymbol) superclass.tsym, fieldName, searched);
494 if (vsym != null) {
495 return vsym;
496 }
497 }
499 // search interfaces
500 List<Type> intfs = tsym.getInterfaces();
501 for (List<Type> l = intfs; l.nonEmpty(); l = l.tail) {
502 Type intf = l.head;
503 if (intf.isErroneous()) continue;
504 VarSymbol vsym = searchField((ClassSymbol) intf.tsym, fieldName, searched);
505 if (vsym != null) {
506 return vsym;
507 }
508 }
510 return null;
511 }
513 /** @see com.sun.tools.javadoc.ClassDocImpl#findConstructor */
514 MethodSymbol findConstructor(ClassSymbol tsym, List<Type> paramTypes) {
515 for (com.sun.tools.javac.code.Scope.Entry e = tsym.members().lookup(names.init);
516 e.scope != null; e = e.next()) {
517 if (e.sym.kind == Kinds.MTH) {
518 if (hasParameterTypes((MethodSymbol) e.sym, paramTypes)) {
519 return (MethodSymbol) e.sym;
520 }
521 }
522 }
523 return null;
524 }
526 /** @see com.sun.tools.javadoc.ClassDocImpl#findMethod */
527 private MethodSymbol findMethod(ClassSymbol tsym, Name methodName, List<Type> paramTypes) {
528 return searchMethod(tsym, methodName, paramTypes, new HashSet<ClassSymbol>());
529 }
531 /** @see com.sun.tools.javadoc.ClassDocImpl#searchMethod */
532 private MethodSymbol searchMethod(ClassSymbol tsym, Name methodName,
533 List<Type> paramTypes, Set<ClassSymbol> searched) {
534 //### Note that this search is not necessarily what the compiler would do!
536 // do not match constructors
537 if (methodName == names.init)
538 return null;
540 if (searched.contains(tsym))
541 return null;
542 searched.add(tsym);
544 // search current class
545 com.sun.tools.javac.code.Scope.Entry e = tsym.members().lookup(methodName);
547 //### Using modifier filter here isn't really correct,
548 //### but emulates the old behavior. Instead, we should
549 //### apply the normal rules of visibility and inheritance.
551 if (paramTypes == null) {
552 // If no parameters specified, we are allowed to return
553 // any method with a matching name. In practice, the old
554 // code returned the first method, which is now the last!
555 // In order to provide textually identical results, we
556 // attempt to emulate the old behavior.
557 MethodSymbol lastFound = null;
558 for (; e.scope != null; e = e.next()) {
559 if (e.sym.kind == Kinds.MTH) {
560 if (e.sym.name == methodName) {
561 lastFound = (MethodSymbol)e.sym;
562 }
563 }
564 }
565 if (lastFound != null) {
566 return lastFound;
567 }
568 } else {
569 for (; e.scope != null; e = e.next()) {
570 if (e.sym != null &&
571 e.sym.kind == Kinds.MTH) {
572 if (hasParameterTypes((MethodSymbol) e.sym, paramTypes)) {
573 return (MethodSymbol) e.sym;
574 }
575 }
576 }
577 }
579 //### If we found a MethodSymbol above, but which did not pass
580 //### the modifier filter, we should return failure here!
582 // search superclass
583 Type superclass = tsym.getSuperclass();
584 if (superclass.tsym != null) {
585 MethodSymbol msym = searchMethod((ClassSymbol) superclass.tsym, methodName, paramTypes, searched);
586 if (msym != null) {
587 return msym;
588 }
589 }
591 // search interfaces
592 List<Type> intfs = tsym.getInterfaces();
593 for (List<Type> l = intfs; l.nonEmpty(); l = l.tail) {
594 Type intf = l.head;
595 if (intf.isErroneous()) continue;
596 MethodSymbol msym = searchMethod((ClassSymbol) intf.tsym, methodName, paramTypes, searched);
597 if (msym != null) {
598 return msym;
599 }
600 }
602 // search enclosing class
603 ClassSymbol encl = tsym.owner.enclClass();
604 if (encl != null) {
605 MethodSymbol msym = searchMethod(encl, methodName, paramTypes, searched);
606 if (msym != null) {
607 return msym;
608 }
609 }
611 return null;
612 }
614 /** @see com.sun.tools.javadoc.ClassDocImpl */
615 private boolean hasParameterTypes(MethodSymbol method, List<Type> paramTypes) {
616 if (paramTypes == null)
617 return true;
619 if (method.params().size() != paramTypes.size())
620 return false;
622 List<Type> methodParamTypes = types.erasureRecursive(method.asType()).getParameterTypes();
624 return (Type.isErroneous(paramTypes))
625 ? fuzzyMatch(paramTypes, methodParamTypes)
626 : types.isSameTypes(paramTypes, methodParamTypes);
627 }
629 boolean fuzzyMatch(List<Type> paramTypes, List<Type> methodParamTypes) {
630 List<Type> l1 = paramTypes;
631 List<Type> l2 = methodParamTypes;
632 while (l1.nonEmpty()) {
633 if (!fuzzyMatch(l1.head, l2.head))
634 return false;
635 l1 = l1.tail;
636 l2 = l2.tail;
637 }
638 return true;
639 }
641 boolean fuzzyMatch(Type paramType, Type methodParamType) {
642 Boolean b = fuzzyMatcher.visit(paramType, methodParamType);
643 return (b == Boolean.TRUE);
644 }
646 TypeRelation fuzzyMatcher = new TypeRelation() {
647 @Override
648 public Boolean visitType(Type t, Type s) {
649 if (t == s)
650 return true;
652 if (s.isPartial())
653 return visit(s, t);
655 switch (t.getTag()) {
656 case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT:
657 case DOUBLE: case BOOLEAN: case VOID: case BOT: case NONE:
658 return t.hasTag(s.getTag());
659 default:
660 throw new AssertionError("fuzzyMatcher " + t.getTag());
661 }
662 }
664 @Override
665 public Boolean visitArrayType(ArrayType t, Type s) {
666 if (t == s)
667 return true;
669 if (s.isPartial())
670 return visit(s, t);
672 return s.hasTag(ARRAY)
673 && visit(t.elemtype, types.elemtype(s));
674 }
676 @Override
677 public Boolean visitClassType(ClassType t, Type s) {
678 if (t == s)
679 return true;
681 if (s.isPartial())
682 return visit(s, t);
684 return t.tsym == s.tsym;
685 }
687 @Override
688 public Boolean visitErrorType(ErrorType t, Type s) {
689 return s.hasTag(CLASS)
690 && t.tsym.name == ((ClassType) s).tsym.name;
691 }
692 };
694 public TypeMirror getTypeMirror(TreePath path) {
695 Tree t = path.getLeaf();
696 return ((JCTree)t).type;
697 }
699 public JavacScope getScope(TreePath path) {
700 return new JavacScope(getAttrContext(path));
701 }
703 public String getDocComment(TreePath path) {
704 CompilationUnitTree t = path.getCompilationUnit();
705 Tree leaf = path.getLeaf();
706 if (t instanceof JCTree.JCCompilationUnit && leaf instanceof JCTree) {
707 JCCompilationUnit cu = (JCCompilationUnit) t;
708 if (cu.docComments != null) {
709 return cu.docComments.getCommentText((JCTree) leaf);
710 }
711 }
712 return null;
713 }
715 public DocCommentTree getDocCommentTree(TreePath path) {
716 CompilationUnitTree t = path.getCompilationUnit();
717 Tree leaf = path.getLeaf();
718 if (t instanceof JCTree.JCCompilationUnit && leaf instanceof JCTree) {
719 JCCompilationUnit cu = (JCCompilationUnit) t;
720 if (cu.docComments != null) {
721 return cu.docComments.getCommentTree((JCTree) leaf);
722 }
723 }
724 return null;
725 }
727 public boolean isAccessible(Scope scope, TypeElement type) {
728 if (scope instanceof JavacScope && type instanceof ClassSymbol) {
729 Env<AttrContext> env = ((JavacScope) scope).env;
730 return resolve.isAccessible(env, (ClassSymbol)type, true);
731 } else
732 return false;
733 }
735 public boolean isAccessible(Scope scope, Element member, DeclaredType type) {
736 if (scope instanceof JavacScope
737 && member instanceof Symbol
738 && type instanceof com.sun.tools.javac.code.Type) {
739 Env<AttrContext> env = ((JavacScope) scope).env;
740 return resolve.isAccessible(env, (com.sun.tools.javac.code.Type)type, (Symbol)member, true);
741 } else
742 return false;
743 }
745 private Env<AttrContext> getAttrContext(TreePath path) {
746 if (!(path.getLeaf() instanceof JCTree)) // implicit null-check
747 throw new IllegalArgumentException();
749 // if we're being invoked from a Tree API client via parse/enter/analyze,
750 // we need to make sure all the classes have been entered;
751 // if we're being invoked from JSR 199 or JSR 269, then the classes
752 // will already have been entered.
753 if (javacTaskImpl != null) {
754 try {
755 javacTaskImpl.enter(null);
756 } catch (IOException e) {
757 throw new Error("unexpected error while entering symbols: " + e);
758 }
759 }
762 JCCompilationUnit unit = (JCCompilationUnit) path.getCompilationUnit();
763 Copier copier = createCopier(treeMaker.forToplevel(unit));
765 Env<AttrContext> env = null;
766 JCMethodDecl method = null;
767 JCVariableDecl field = null;
769 List<Tree> l = List.nil();
770 TreePath p = path;
771 while (p != null) {
772 l = l.prepend(p.getLeaf());
773 p = p.getParentPath();
774 }
776 for ( ; l.nonEmpty(); l = l.tail) {
777 Tree tree = l.head;
778 switch (tree.getKind()) {
779 case COMPILATION_UNIT:
780 // System.err.println("COMP: " + ((JCCompilationUnit)tree).sourcefile);
781 env = enter.getTopLevelEnv((JCCompilationUnit)tree);
782 break;
783 case ANNOTATION_TYPE:
784 case CLASS:
785 case ENUM:
786 case INTERFACE:
787 // System.err.println("CLASS: " + ((JCClassDecl)tree).sym.getSimpleName());
788 env = enter.getClassEnv(((JCClassDecl)tree).sym);
789 break;
790 case METHOD:
791 // System.err.println("METHOD: " + ((JCMethodDecl)tree).sym.getSimpleName());
792 method = (JCMethodDecl)tree;
793 env = memberEnter.getMethodEnv(method, env);
794 break;
795 case VARIABLE:
796 // System.err.println("FIELD: " + ((JCVariableDecl)tree).sym.getSimpleName());
797 field = (JCVariableDecl)tree;
798 break;
799 case BLOCK: {
800 // System.err.println("BLOCK: ");
801 if (method != null) {
802 try {
803 Assert.check(method.body == tree);
804 method.body = copier.copy((JCBlock)tree, (JCTree) path.getLeaf());
805 env = attribStatToTree(method.body, env, copier.leafCopy);
806 } finally {
807 method.body = (JCBlock) tree;
808 }
809 } else {
810 JCBlock body = copier.copy((JCBlock)tree, (JCTree) path.getLeaf());
811 env = attribStatToTree(body, env, copier.leafCopy);
812 }
813 return env;
814 }
815 default:
816 // System.err.println("DEFAULT: " + tree.getKind());
817 if (field != null && field.getInitializer() == tree) {
818 env = memberEnter.getInitEnv(field, env);
819 JCExpression expr = copier.copy((JCExpression)tree, (JCTree) path.getLeaf());
820 env = attribExprToTree(expr, env, copier.leafCopy);
821 return env;
822 }
823 }
824 }
825 return (field != null) ? memberEnter.getInitEnv(field, env) : env;
826 }
828 private Env<AttrContext> attribStatToTree(JCTree stat, Env<AttrContext>env, JCTree tree) {
829 JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
830 try {
831 return attr.attribStatToTree(stat, env, tree);
832 } finally {
833 log.useSource(prev);
834 }
835 }
837 private Env<AttrContext> attribExprToTree(JCExpression expr, Env<AttrContext>env, JCTree tree) {
838 JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
839 try {
840 return attr.attribExprToTree(expr, env, tree);
841 } finally {
842 log.useSource(prev);
843 }
844 }
846 /**
847 * Makes a copy of a tree, noting the value resulting from copying a particular leaf.
848 **/
849 protected static class Copier extends TreeCopier<JCTree> {
850 JCTree leafCopy = null;
852 protected Copier(TreeMaker M) {
853 super(M);
854 }
856 @Override
857 public <T extends JCTree> T copy(T t, JCTree leaf) {
858 T t2 = super.copy(t, leaf);
859 if (t == leaf)
860 leafCopy = t2;
861 return t2;
862 }
863 }
865 protected Copier createCopier(TreeMaker maker) {
866 return new Copier(maker);
867 }
869 /**
870 * Gets the original type from the ErrorType object.
871 * @param errorType The errorType for which we want to get the original type.
872 * @returns TypeMirror corresponding to the original type, replaced by the ErrorType.
873 * noType (type.tag == NONE) is returned if there is no original type.
874 */
875 public TypeMirror getOriginalType(javax.lang.model.type.ErrorType errorType) {
876 if (errorType instanceof com.sun.tools.javac.code.Type.ErrorType) {
877 return ((com.sun.tools.javac.code.Type.ErrorType)errorType).getOriginalType();
878 }
880 return com.sun.tools.javac.code.Type.noType;
881 }
883 /**
884 * Prints a message of the specified kind at the location of the
885 * tree within the provided compilation unit
886 *
887 * @param kind the kind of message
888 * @param msg the message, or an empty string if none
889 * @param t the tree to use as a position hint
890 * @param root the compilation unit that contains tree
891 */
892 public void printMessage(Diagnostic.Kind kind, CharSequence msg,
893 com.sun.source.tree.Tree t,
894 com.sun.source.tree.CompilationUnitTree root) {
895 printMessage(kind, msg, ((JCTree) t).pos(), root);
896 }
898 public void printMessage(Diagnostic.Kind kind, CharSequence msg,
899 com.sun.source.doctree.DocTree t,
900 com.sun.source.doctree.DocCommentTree c,
901 com.sun.source.tree.CompilationUnitTree root) {
902 printMessage(kind, msg, ((DCTree) t).pos((DCDocComment) c), root);
903 }
905 private void printMessage(Diagnostic.Kind kind, CharSequence msg,
906 JCDiagnostic.DiagnosticPosition pos,
907 com.sun.source.tree.CompilationUnitTree root) {
908 JavaFileObject oldSource = null;
909 JavaFileObject newSource = null;
911 newSource = root.getSourceFile();
912 if (newSource == null) {
913 pos = null;
914 } else {
915 oldSource = log.useSource(newSource);
916 }
918 try {
919 switch (kind) {
920 case ERROR:
921 boolean prev = log.multipleErrors;
922 try {
923 log.error(pos, "proc.messager", msg.toString());
924 } finally {
925 log.multipleErrors = prev;
926 }
927 break;
929 case WARNING:
930 log.warning(pos, "proc.messager", msg.toString());
931 break;
933 case MANDATORY_WARNING:
934 log.mandatoryWarning(pos, "proc.messager", msg.toString());
935 break;
937 default:
938 log.note(pos, "proc.messager", msg.toString());
939 }
940 } finally {
941 if (oldSource != null)
942 log.useSource(oldSource);
943 }
944 }
946 @Override
947 public TypeMirror getLub(CatchTree tree) {
948 JCCatch ct = (JCCatch) tree;
949 JCVariableDecl v = ct.param;
950 if (v.type != null && v.type.getKind() == TypeKind.UNION) {
951 UnionClassType ut = (UnionClassType) v.type;
952 return ut.getLub();
953 } else {
954 return v.type;
955 }
956 }
957 }