langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTrees.java
changeset 26266 2d24bda701dc
parent 26264 a09fedde76be
child 27224 228abfa87080
equal deleted inserted replaced
26265:46aacfffd3b5 26266:2d24bda701dc
    94 import com.sun.tools.javac.tree.TreeInfo;
    94 import com.sun.tools.javac.tree.TreeInfo;
    95 import com.sun.tools.javac.tree.TreeMaker;
    95 import com.sun.tools.javac.tree.TreeMaker;
    96 import com.sun.tools.javac.util.Abort;
    96 import com.sun.tools.javac.util.Abort;
    97 import com.sun.tools.javac.util.Assert;
    97 import com.sun.tools.javac.util.Assert;
    98 import com.sun.tools.javac.util.Context;
    98 import com.sun.tools.javac.util.Context;
       
    99 import com.sun.tools.javac.util.DefinedBy;
       
   100 import com.sun.tools.javac.util.DefinedBy.Api;
    99 import com.sun.tools.javac.util.JCDiagnostic;
   101 import com.sun.tools.javac.util.JCDiagnostic;
   100 import com.sun.tools.javac.util.List;
   102 import com.sun.tools.javac.util.List;
   101 import com.sun.tools.javac.util.ListBuffer;
   103 import com.sun.tools.javac.util.ListBuffer;
   102 import com.sun.tools.javac.util.Log;
   104 import com.sun.tools.javac.util.Log;
   103 import com.sun.tools.javac.util.Name;
   105 import com.sun.tools.javac.util.Name;
   174         JavacTask t = context.get(JavacTask.class);
   176         JavacTask t = context.get(JavacTask.class);
   175         if (t instanceof JavacTaskImpl)
   177         if (t instanceof JavacTaskImpl)
   176             javacTaskImpl = (JavacTaskImpl) t;
   178             javacTaskImpl = (JavacTaskImpl) t;
   177     }
   179     }
   178 
   180 
       
   181     @DefinedBy(Api.COMPILER_TREE)
   179     public DocSourcePositions getSourcePositions() {
   182     public DocSourcePositions getSourcePositions() {
   180         return new DocSourcePositions() {
   183         return new DocSourcePositions() {
       
   184                 @DefinedBy(Api.COMPILER_TREE)
   181                 public long getStartPosition(CompilationUnitTree file, Tree tree) {
   185                 public long getStartPosition(CompilationUnitTree file, Tree tree) {
   182                     return TreeInfo.getStartPos((JCTree) tree);
   186                     return TreeInfo.getStartPos((JCTree) tree);
   183                 }
   187                 }
   184 
   188 
       
   189                 @DefinedBy(Api.COMPILER_TREE)
   185                 public long getEndPosition(CompilationUnitTree file, Tree tree) {
   190                 public long getEndPosition(CompilationUnitTree file, Tree tree) {
   186                     EndPosTable endPosTable = ((JCCompilationUnit) file).endPositions;
   191                     EndPosTable endPosTable = ((JCCompilationUnit) file).endPositions;
   187                     return TreeInfo.getEndPos((JCTree) tree, endPosTable);
   192                     return TreeInfo.getEndPos((JCTree) tree, endPosTable);
   188                 }
   193                 }
   189 
   194 
       
   195                 @DefinedBy(Api.COMPILER_TREE)
   190                 public long getStartPosition(CompilationUnitTree file, DocCommentTree comment, DocTree tree) {
   196                 public long getStartPosition(CompilationUnitTree file, DocCommentTree comment, DocTree tree) {
   191                     return ((DCTree) tree).getSourcePosition((DCDocComment) comment);
   197                     return ((DCTree) tree).getSourcePosition((DCDocComment) comment);
   192                 }
   198                 }
   193                 @SuppressWarnings("fallthrough")
   199                 @SuppressWarnings("fallthrough") @DefinedBy(Api.COMPILER_TREE)
   194                 public long getEndPosition(CompilationUnitTree file, DocCommentTree comment, DocTree tree) {
   200                 public long getEndPosition(CompilationUnitTree file, DocCommentTree comment, DocTree tree) {
   195                     DCDocComment dcComment = (DCDocComment) comment;
   201                     DCDocComment dcComment = (DCDocComment) comment;
   196                     if (tree instanceof DCEndPosTree) {
   202                     if (tree instanceof DCEndPosTree) {
   197                         int endPos = ((DCEndPosTree) tree).getEndPos(dcComment);
   203                         int endPos = ((DCEndPosTree) tree).getEndPos(dcComment);
   198 
   204 
   249 
   255 
   250     private DocTree getLastChild(DocTree tree) {
   256     private DocTree getLastChild(DocTree tree) {
   251         final DocTree[] last = new DocTree[] {null};
   257         final DocTree[] last = new DocTree[] {null};
   252 
   258 
   253         tree.accept(new DocTreeScanner<Void, Void>() {
   259         tree.accept(new DocTreeScanner<Void, Void>() {
   254             @Override public Void scan(DocTree node, Void p) {
   260             @Override @DefinedBy(Api.COMPILER_TREE)
       
   261  public Void scan(DocTree node, Void p) {
   255                 if (node != null) last[0] = node;
   262                 if (node != null) last[0] = node;
   256                 return null;
   263                 return null;
   257             }
   264             }
   258         }, null);
   265         }, null);
   259 
   266 
   260         return last[0];
   267         return last[0];
   261     }
   268     }
   262 
   269 
       
   270     @DefinedBy(Api.COMPILER_TREE)
   263     public JCClassDecl getTree(TypeElement element) {
   271     public JCClassDecl getTree(TypeElement element) {
   264         return (JCClassDecl) getTree((Element) element);
   272         return (JCClassDecl) getTree((Element) element);
   265     }
   273     }
   266 
   274 
       
   275     @DefinedBy(Api.COMPILER_TREE)
   267     public JCMethodDecl getTree(ExecutableElement method) {
   276     public JCMethodDecl getTree(ExecutableElement method) {
   268         return (JCMethodDecl) getTree((Element) method);
   277         return (JCMethodDecl) getTree((Element) method);
   269     }
   278     }
   270 
   279 
       
   280     @DefinedBy(Api.COMPILER_TREE)
   271     public JCTree getTree(Element element) {
   281     public JCTree getTree(Element element) {
   272         return getTree(element, null);
   282         return getTree(element, null);
   273     }
   283     }
   274 
   284 
       
   285     @DefinedBy(Api.COMPILER_TREE)
   275     public JCTree getTree(Element e, AnnotationMirror a) {
   286     public JCTree getTree(Element e, AnnotationMirror a) {
   276         return getTree(e, a, null);
   287         return getTree(e, a, null);
   277     }
   288     }
   278 
   289 
       
   290     @DefinedBy(Api.COMPILER_TREE)
   279     public JCTree getTree(Element e, AnnotationMirror a, AnnotationValue v) {
   291     public JCTree getTree(Element e, AnnotationMirror a, AnnotationValue v) {
   280         Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
   292         Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
   281         if (treeTopLevel == null)
   293         if (treeTopLevel == null)
   282             return null;
   294             return null;
   283         return treeTopLevel.fst;
   295         return treeTopLevel.fst;
   284     }
   296     }
   285 
   297 
       
   298     @DefinedBy(Api.COMPILER_TREE)
   286     public TreePath getPath(CompilationUnitTree unit, Tree node) {
   299     public TreePath getPath(CompilationUnitTree unit, Tree node) {
   287         return TreePath.getPath(unit, node);
   300         return TreePath.getPath(unit, node);
   288     }
   301     }
   289 
   302 
       
   303     @DefinedBy(Api.COMPILER_TREE)
   290     public TreePath getPath(Element e) {
   304     public TreePath getPath(Element e) {
   291         return getPath(e, null, null);
   305         return getPath(e, null, null);
   292     }
   306     }
   293 
   307 
       
   308     @DefinedBy(Api.COMPILER_TREE)
   294     public TreePath getPath(Element e, AnnotationMirror a) {
   309     public TreePath getPath(Element e, AnnotationMirror a) {
   295         return getPath(e, a, null);
   310         return getPath(e, a, null);
   296     }
   311     }
   297 
   312 
       
   313     @DefinedBy(Api.COMPILER_TREE)
   298     public TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v) {
   314     public TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v) {
   299         final Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
   315         final Pair<JCTree, JCCompilationUnit> treeTopLevel = elements.getTreeAndTopLevel(e, a, v);
   300         if (treeTopLevel == null)
   316         if (treeTopLevel == null)
   301             return null;
   317             return null;
   302         return TreePath.getPath(treeTopLevel.snd, treeTopLevel.fst);
   318         return TreePath.getPath(treeTopLevel.snd, treeTopLevel.fst);
   303     }
   319     }
   304 
   320 
       
   321     @DefinedBy(Api.COMPILER_TREE)
   305     public Symbol getElement(TreePath path) {
   322     public Symbol getElement(TreePath path) {
   306         JCTree tree = (JCTree) path.getLeaf();
   323         JCTree tree = (JCTree) path.getLeaf();
   307         Symbol sym = TreeInfo.symbolFor(tree);
   324         Symbol sym = TreeInfo.symbolFor(tree);
   308         if (sym == null) {
   325         if (sym == null) {
   309             if (TreeInfo.isDeclaration(tree)) {
   326             if (TreeInfo.isDeclaration(tree)) {
   323             }
   340             }
   324         }
   341         }
   325         return sym;
   342         return sym;
   326     }
   343     }
   327 
   344 
   328     @Override
   345     @Override @DefinedBy(Api.COMPILER_TREE)
   329     public Element getElement(DocTreePath path) {
   346     public Element getElement(DocTreePath path) {
   330         DocTree forTree = path.getLeaf();
   347         DocTree forTree = path.getLeaf();
   331         if (forTree instanceof DCReference)
   348         if (forTree instanceof DCReference)
   332             return attributeDocReference(path.getTreePath(), ((DCReference) forTree));
   349             return attributeDocReference(path.getTreePath(), ((DCReference) forTree));
   333         if (forTree instanceof DCIdentifier) {
   350         if (forTree instanceof DCIdentifier) {
   673             return s.hasTag(CLASS)
   690             return s.hasTag(CLASS)
   674                     && t.tsym.name == ((ClassType) s).tsym.name;
   691                     && t.tsym.name == ((ClassType) s).tsym.name;
   675         }
   692         }
   676     };
   693     };
   677 
   694 
       
   695     @DefinedBy(Api.COMPILER_TREE)
   678     public TypeMirror getTypeMirror(TreePath path) {
   696     public TypeMirror getTypeMirror(TreePath path) {
   679         Tree t = path.getLeaf();
   697         Tree t = path.getLeaf();
   680         return ((JCTree)t).type;
   698         return ((JCTree)t).type;
   681     }
   699     }
   682 
   700 
       
   701     @DefinedBy(Api.COMPILER_TREE)
   683     public JavacScope getScope(TreePath path) {
   702     public JavacScope getScope(TreePath path) {
   684         return JavacScope.create(getAttrContext(path));
   703         return JavacScope.create(getAttrContext(path));
   685     }
   704     }
   686 
   705 
       
   706     @DefinedBy(Api.COMPILER_TREE)
   687     public String getDocComment(TreePath path) {
   707     public String getDocComment(TreePath path) {
   688         CompilationUnitTree t = path.getCompilationUnit();
   708         CompilationUnitTree t = path.getCompilationUnit();
   689         Tree leaf = path.getLeaf();
   709         Tree leaf = path.getLeaf();
   690         if (t instanceof JCTree.JCCompilationUnit && leaf instanceof JCTree) {
   710         if (t instanceof JCTree.JCCompilationUnit && leaf instanceof JCTree) {
   691             JCCompilationUnit cu = (JCCompilationUnit) t;
   711             JCCompilationUnit cu = (JCCompilationUnit) t;
   694             }
   714             }
   695         }
   715         }
   696         return null;
   716         return null;
   697     }
   717     }
   698 
   718 
       
   719     @DefinedBy(Api.COMPILER_TREE)
   699     public DocCommentTree getDocCommentTree(TreePath path) {
   720     public DocCommentTree getDocCommentTree(TreePath path) {
   700         CompilationUnitTree t = path.getCompilationUnit();
   721         CompilationUnitTree t = path.getCompilationUnit();
   701         Tree leaf = path.getLeaf();
   722         Tree leaf = path.getLeaf();
   702         if (t instanceof JCTree.JCCompilationUnit && leaf instanceof JCTree) {
   723         if (t instanceof JCTree.JCCompilationUnit && leaf instanceof JCTree) {
   703             JCCompilationUnit cu = (JCCompilationUnit) t;
   724             JCCompilationUnit cu = (JCCompilationUnit) t;
   706             }
   727             }
   707         }
   728         }
   708         return null;
   729         return null;
   709     }
   730     }
   710 
   731 
       
   732     @DefinedBy(Api.COMPILER_TREE)
   711     public boolean isAccessible(Scope scope, TypeElement type) {
   733     public boolean isAccessible(Scope scope, TypeElement type) {
   712         if (scope instanceof JavacScope && type instanceof ClassSymbol) {
   734         if (scope instanceof JavacScope && type instanceof ClassSymbol) {
   713             Env<AttrContext> env = ((JavacScope) scope).env;
   735             Env<AttrContext> env = ((JavacScope) scope).env;
   714             return resolve.isAccessible(env, (ClassSymbol)type, true);
   736             return resolve.isAccessible(env, (ClassSymbol)type, true);
   715         } else
   737         } else
   716             return false;
   738             return false;
   717     }
   739     }
   718 
   740 
       
   741     @DefinedBy(Api.COMPILER_TREE)
   719     public boolean isAccessible(Scope scope, Element member, DeclaredType type) {
   742     public boolean isAccessible(Scope scope, Element member, DeclaredType type) {
   720         if (scope instanceof JavacScope
   743         if (scope instanceof JavacScope
   721                 && member instanceof Symbol
   744                 && member instanceof Symbol
   722                 && type instanceof com.sun.tools.javac.code.Type) {
   745                 && type instanceof com.sun.tools.javac.code.Type) {
   723             Env<AttrContext> env = ((JavacScope) scope).env;
   746             Env<AttrContext> env = ((JavacScope) scope).env;
   850      * Returns the original type from the ErrorType object.
   873      * Returns the original type from the ErrorType object.
   851      * @param errorType The errorType for which we want to get the original type.
   874      * @param errorType The errorType for which we want to get the original type.
   852      * @returns TypeMirror corresponding to the original type, replaced by the ErrorType.
   875      * @returns TypeMirror corresponding to the original type, replaced by the ErrorType.
   853      *          noType (type.tag == NONE) is returned if there is no original type.
   876      *          noType (type.tag == NONE) is returned if there is no original type.
   854      */
   877      */
       
   878     @DefinedBy(Api.COMPILER_TREE)
   855     public TypeMirror getOriginalType(javax.lang.model.type.ErrorType errorType) {
   879     public TypeMirror getOriginalType(javax.lang.model.type.ErrorType errorType) {
   856         if (errorType instanceof com.sun.tools.javac.code.Type.ErrorType) {
   880         if (errorType instanceof com.sun.tools.javac.code.Type.ErrorType) {
   857             return ((com.sun.tools.javac.code.Type.ErrorType)errorType).getOriginalType();
   881             return ((com.sun.tools.javac.code.Type.ErrorType)errorType).getOriginalType();
   858         }
   882         }
   859 
   883 
   867      * @param kind the kind of message
   891      * @param kind the kind of message
   868      * @param msg  the message, or an empty string if none
   892      * @param msg  the message, or an empty string if none
   869      * @param t    the tree to use as a position hint
   893      * @param t    the tree to use as a position hint
   870      * @param root the compilation unit that contains tree
   894      * @param root the compilation unit that contains tree
   871      */
   895      */
       
   896     @DefinedBy(Api.COMPILER_TREE)
   872     public void printMessage(Diagnostic.Kind kind, CharSequence msg,
   897     public void printMessage(Diagnostic.Kind kind, CharSequence msg,
   873             com.sun.source.tree.Tree t,
   898             com.sun.source.tree.Tree t,
   874             com.sun.source.tree.CompilationUnitTree root) {
   899             com.sun.source.tree.CompilationUnitTree root) {
   875         printMessage(kind, msg, ((JCTree) t).pos(), root);
   900         printMessage(kind, msg, ((JCTree) t).pos(), root);
   876     }
   901     }
   877 
   902 
       
   903     @DefinedBy(Api.COMPILER_TREE)
   878     public void printMessage(Diagnostic.Kind kind, CharSequence msg,
   904     public void printMessage(Diagnostic.Kind kind, CharSequence msg,
   879             com.sun.source.doctree.DocTree t,
   905             com.sun.source.doctree.DocTree t,
   880             com.sun.source.doctree.DocCommentTree c,
   906             com.sun.source.doctree.DocCommentTree c,
   881             com.sun.source.tree.CompilationUnitTree root) {
   907             com.sun.source.tree.CompilationUnitTree root) {
   882         printMessage(kind, msg, ((DCTree) t).pos((DCDocComment) c), root);
   908         printMessage(kind, msg, ((DCTree) t).pos((DCDocComment) c), root);
   921             if (oldSource != null)
   947             if (oldSource != null)
   922                 log.useSource(oldSource);
   948                 log.useSource(oldSource);
   923         }
   949         }
   924     }
   950     }
   925 
   951 
   926     @Override
   952     @Override @DefinedBy(Api.COMPILER_TREE)
   927     public TypeMirror getLub(CatchTree tree) {
   953     public TypeMirror getLub(CatchTree tree) {
   928         JCCatch ct = (JCCatch) tree;
   954         JCCatch ct = (JCCatch) tree;
   929         JCVariableDecl v = ct.param;
   955         JCVariableDecl v = ct.param;
   930         if (v.type != null && v.type.getKind() == TypeKind.UNION) {
   956         if (v.type != null && v.type.getKind() == TypeKind.UNION) {
   931             UnionClassType ut = (UnionClassType) v.type;
   957             UnionClassType ut = (UnionClassType) v.type;