langtools/src/share/classes/com/sun/tools/javac/api/JavacTrees.java
changeset 17273 d7ec6654aac9
parent 14952 d0022ae20516
child 17549 65ea6f3b72d9
equal deleted inserted replaced
16977:aa655625a6b9 17273:d7ec6654aac9
     1 /*
     1 /*
     2  * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    41 import javax.tools.Diagnostic;
    41 import javax.tools.Diagnostic;
    42 import javax.tools.JavaCompiler;
    42 import javax.tools.JavaCompiler;
    43 import javax.tools.JavaFileObject;
    43 import javax.tools.JavaFileObject;
    44 
    44 
    45 import com.sun.source.doctree.DocCommentTree;
    45 import com.sun.source.doctree.DocCommentTree;
       
    46 import com.sun.source.doctree.DocTree;
    46 import com.sun.source.doctree.ReferenceTree;
    47 import com.sun.source.doctree.ReferenceTree;
    47 import com.sun.source.tree.CatchTree;
    48 import com.sun.source.tree.CatchTree;
    48 import com.sun.source.tree.CompilationUnitTree;
    49 import com.sun.source.tree.CompilationUnitTree;
    49 import com.sun.source.tree.Scope;
    50 import com.sun.source.tree.Scope;
    50 import com.sun.source.tree.Tree;
    51 import com.sun.source.tree.Tree;
       
    52 import com.sun.source.util.DocSourcePositions;
       
    53 import com.sun.source.util.DocTreeScanner;
    51 import com.sun.source.util.DocTrees;
    54 import com.sun.source.util.DocTrees;
    52 import com.sun.source.util.JavacTask;
    55 import com.sun.source.util.JavacTask;
    53 import com.sun.source.util.SourcePositions;
       
    54 import com.sun.source.util.TreePath;
    56 import com.sun.source.util.TreePath;
    55 import com.sun.tools.javac.code.Flags;
    57 import com.sun.tools.javac.code.Flags;
    56 import com.sun.tools.javac.code.Kinds;
    58 import com.sun.tools.javac.code.Kinds;
    57 import com.sun.tools.javac.code.Symbol;
    59 import com.sun.tools.javac.code.Symbol;
    58 import com.sun.tools.javac.code.Symbol.ClassSymbol;
    60 import com.sun.tools.javac.code.Symbol.ClassSymbol;
    74 import com.sun.tools.javac.comp.MemberEnter;
    76 import com.sun.tools.javac.comp.MemberEnter;
    75 import com.sun.tools.javac.comp.Resolve;
    77 import com.sun.tools.javac.comp.Resolve;
    76 import com.sun.tools.javac.model.JavacElements;
    78 import com.sun.tools.javac.model.JavacElements;
    77 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
    79 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
    78 import com.sun.tools.javac.tree.DCTree;
    80 import com.sun.tools.javac.tree.DCTree;
       
    81 import com.sun.tools.javac.tree.DCTree.DCBlockTag;
    79 import com.sun.tools.javac.tree.DCTree.DCDocComment;
    82 import com.sun.tools.javac.tree.DCTree.DCDocComment;
       
    83 import com.sun.tools.javac.tree.DCTree.DCEndPosTree;
       
    84 import com.sun.tools.javac.tree.DCTree.DCErroneous;
       
    85 import com.sun.tools.javac.tree.DCTree.DCIdentifier;
       
    86 import com.sun.tools.javac.tree.DCTree.DCParam;
    80 import com.sun.tools.javac.tree.DCTree.DCReference;
    87 import com.sun.tools.javac.tree.DCTree.DCReference;
       
    88 import com.sun.tools.javac.tree.DCTree.DCText;
    81 import com.sun.tools.javac.tree.EndPosTable;
    89 import com.sun.tools.javac.tree.EndPosTable;
    82 import com.sun.tools.javac.tree.JCTree;
    90 import com.sun.tools.javac.tree.JCTree;
    83 import com.sun.tools.javac.tree.JCTree.*;
    91 import com.sun.tools.javac.tree.JCTree.*;
    84 import com.sun.tools.javac.tree.TreeCopier;
    92 import com.sun.tools.javac.tree.TreeCopier;
    85 import com.sun.tools.javac.tree.TreeInfo;
    93 import com.sun.tools.javac.tree.TreeInfo;
    92 import com.sun.tools.javac.util.ListBuffer;
   100 import com.sun.tools.javac.util.ListBuffer;
    93 import com.sun.tools.javac.util.Log;
   101 import com.sun.tools.javac.util.Log;
    94 import com.sun.tools.javac.util.Name;
   102 import com.sun.tools.javac.util.Name;
    95 import com.sun.tools.javac.util.Names;
   103 import com.sun.tools.javac.util.Names;
    96 import com.sun.tools.javac.util.Pair;
   104 import com.sun.tools.javac.util.Pair;
       
   105 import com.sun.tools.javac.util.Position;
    97 import static com.sun.tools.javac.code.TypeTag.*;
   106 import static com.sun.tools.javac.code.TypeTag.*;
    98 
   107 
    99 /**
   108 /**
   100  * Provides an implementation of Trees.
   109  * Provides an implementation of Trees.
   101  *
   110  *
   164         JavacTask t = context.get(JavacTask.class);
   173         JavacTask t = context.get(JavacTask.class);
   165         if (t instanceof JavacTaskImpl)
   174         if (t instanceof JavacTaskImpl)
   166             javacTaskImpl = (JavacTaskImpl) t;
   175             javacTaskImpl = (JavacTaskImpl) t;
   167     }
   176     }
   168 
   177 
   169     public SourcePositions getSourcePositions() {
   178     public DocSourcePositions getSourcePositions() {
   170         return new SourcePositions() {
   179         return new DocSourcePositions() {
   171                 public long getStartPosition(CompilationUnitTree file, Tree tree) {
   180                 public long getStartPosition(CompilationUnitTree file, Tree tree) {
   172                     return TreeInfo.getStartPos((JCTree) tree);
   181                     return TreeInfo.getStartPos((JCTree) tree);
   173                 }
   182                 }
   174 
   183 
   175                 public long getEndPosition(CompilationUnitTree file, Tree tree) {
   184                 public long getEndPosition(CompilationUnitTree file, Tree tree) {
   176                     EndPosTable endPosTable = ((JCCompilationUnit) file).endPositions;
   185                     EndPosTable endPosTable = ((JCCompilationUnit) file).endPositions;
   177                     return TreeInfo.getEndPos((JCTree) tree, endPosTable);
   186                     return TreeInfo.getEndPos((JCTree) tree, endPosTable);
   178                 }
   187                 }
       
   188 
       
   189                 public long getStartPosition(CompilationUnitTree file, DocCommentTree comment, DocTree tree) {
       
   190                     return ((DCTree) tree).getSourcePosition((DCDocComment) comment);
       
   191                 }
       
   192                 @SuppressWarnings("fallthrough")
       
   193                 public long getEndPosition(CompilationUnitTree file, DocCommentTree comment, DocTree tree) {
       
   194                     DCDocComment dcComment = (DCDocComment) comment;
       
   195                     if (tree instanceof DCEndPosTree) {
       
   196                         int endPos = ((DCEndPosTree) tree).getEndPos(dcComment);
       
   197 
       
   198                         if (endPos != Position.NOPOS) {
       
   199                             return endPos;
       
   200                         }
       
   201                     }
       
   202                     int correction = 0;
       
   203                     switch (tree.getKind()) {
       
   204                         case TEXT:
       
   205                             DCText text = (DCText) tree;
       
   206 
       
   207                             return dcComment.comment.getSourcePos(text.pos + text.text.length());
       
   208                         case ERRONEOUS:
       
   209                             DCErroneous err = (DCErroneous) tree;
       
   210 
       
   211                             return dcComment.comment.getSourcePos(err.pos + err.body.length());
       
   212                         case IDENTIFIER:
       
   213                             DCIdentifier ident = (DCIdentifier) tree;
       
   214 
       
   215                             return dcComment.comment.getSourcePos(ident.pos + (ident.name != names.error ? ident.name.length() : 0));
       
   216                         case PARAM:
       
   217                             DCParam param = (DCParam) tree;
       
   218 
       
   219                             if (param.isTypeParameter && param.getDescription().isEmpty()) {
       
   220                                 correction = 1;
       
   221                             }
       
   222                         case AUTHOR: case DEPRECATED: case RETURN: case SEE:
       
   223                         case SERIAL: case SERIAL_DATA: case SERIAL_FIELD: case SINCE:
       
   224                         case THROWS: case UNKNOWN_BLOCK_TAG: case VERSION: {
       
   225                             DocTree last = getLastChild(tree);
       
   226 
       
   227                             if (last != null) {
       
   228                                 return getEndPosition(file, comment, last) + correction;
       
   229                             }
       
   230 
       
   231                             DCBlockTag block = (DCBlockTag) tree;
       
   232 
       
   233                             return dcComment.comment.getSourcePos(block.pos + block.getTagName().length() + 1);
       
   234                         }
       
   235                         default:
       
   236                             DocTree last = getLastChild(tree);
       
   237 
       
   238                             if (last != null) {
       
   239                                 return getEndPosition(file, comment, last);
       
   240                             }
       
   241                             break;
       
   242                     }
       
   243 
       
   244                     return Position.NOPOS;
       
   245                 }
   179             };
   246             };
       
   247     }
       
   248 
       
   249     private DocTree getLastChild(DocTree tree) {
       
   250         final DocTree[] last = new DocTree[] {null};
       
   251 
       
   252         tree.accept(new DocTreeScanner<Void, Void>() {
       
   253             @Override public Void scan(DocTree node, Void p) {
       
   254                 if (node != null) last[0] = node;
       
   255                 return null;
       
   256             }
       
   257         }, null);
       
   258 
       
   259         return last[0];
   180     }
   260     }
   181 
   261 
   182     public JCClassDecl getTree(TypeElement element) {
   262     public JCClassDecl getTree(TypeElement element) {
   183         return (JCClassDecl) getTree((Element) element);
   263         return (JCClassDecl) getTree((Element) element);
   184     }
   264     }