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