< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTrees.java

Print this page


   1 /*
   2  * Copyright (c) 2005, 2017, 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


 392         if (sym == null) {
 393             for (TreePath p = path; p != null; p = p.getParentPath()) {
 394                 JCTree t = (JCTree) p.getLeaf();
 395                 if (t.hasTag(JCTree.Tag.CLASSDEF)) {
 396                     JCClassDecl ct = (JCClassDecl) t;
 397                     if (ct.sym != null) {
 398                         if ((ct.sym.flags_field & Flags.UNATTRIBUTED) != 0) {
 399                             attr.attribClass(ct.pos(), ct.sym);
 400                             sym = TreeInfo.symbolFor(tree);
 401                         }
 402                         break;
 403                     }
 404                 }
 405             }
 406         }
 407         return sym;
 408     }
 409 
 410     @Override @DefinedBy(Api.COMPILER_TREE)
 411     public Element getElement(DocTreePath path) {
 412         DocTree forTree = path.getLeaf();
 413         if (forTree instanceof DCReference)
 414             return attributeDocReference(path.getTreePath(), ((DCReference) forTree));
 415         if (forTree instanceof DCIdentifier) {
 416             if (path.getParentPath().getLeaf() instanceof DCParam) {
 417                 return attributeParamIdentifier(path.getTreePath(), (DCParam) path.getParentPath().getLeaf());
 418             }
 419         }
 420         return null;
 421     }
 422 
 423     @Override @DefinedBy(Api.COMPILER_TREE)
 424     public java.util.List<DocTree> getFirstSentence(java.util.List<? extends DocTree> list) {
 425         return docTreeMaker.getFirstSentence(list);
 426     }
 427 
 428     private Symbol attributeDocReference(TreePath path, DCReference ref) {
 429         Env<AttrContext> env = getAttrContext(path);
 430         if (env == null) return null;
 431 
 432         Log.DeferredDiagnosticHandler deferredDiagnosticHandler =
 433                 new Log.DeferredDiagnosticHandler(log);
 434         try {
 435             final TypeSymbol tsym;


 508                 // explicit (possibly empty) arg list given, so cannot be a field
 509                 return msym;
 510             }
 511 
 512             VarSymbol vsym = (ref.paramTypes != null) ? null : findField(sym, memberName);
 513             // prefer a field over a method with no parameters
 514             if (vsym != null &&
 515                     (msym == null ||
 516                         types.isSubtypeUnchecked(vsym.enclClass().asType(), msym.enclClass().asType()))) {
 517                 return vsym;
 518             } else {
 519                 return msym;
 520             }
 521         } catch (Abort e) { // may be thrown by Check.completionError in case of bad class file
 522             return null;
 523         } finally {
 524             log.popDiagnosticHandler(deferredDiagnosticHandler);
 525         }
 526     }
 527 
 528     private Symbol attributeParamIdentifier(TreePath path, DCParam ptag) {
 529         Symbol javadocSymbol = getElement(path);
 530         if (javadocSymbol == null)
 531             return null;
 532         ElementKind kind = javadocSymbol.getKind();
 533         List<? extends Symbol> params = List.nil();
 534         if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
 535             MethodSymbol ee = (MethodSymbol) javadocSymbol;
 536             params = ptag.isTypeParameter()
 537                     ? ee.getTypeParameters()
 538                     : ee.getParameters();
 539         } else if (kind.isClass() || kind.isInterface()) {
 540             ClassSymbol te = (ClassSymbol) javadocSymbol;
 541             params = te.getTypeParameters();


 542         }
 543 
 544         for (Symbol param : params) {
 545             if (param.getSimpleName() == ptag.getName().getName()) {
 546                 return param;
 547             }
 548         }
 549         return null;
 550     }
 551 
 552     /** @see com.sun.tools.javadoc.ClassDocImpl#findField */
 553     private VarSymbol findField(ClassSymbol tsym, Name fieldName) {
 554         return searchField(tsym, fieldName, new HashSet<>());
 555     }
 556 
 557     /** @see com.sun.tools.javadoc.ClassDocImpl#searchField */
 558     private VarSymbol searchField(ClassSymbol tsym, Name fieldName, Set<ClassSymbol> searched) {
 559         if (searched.contains(tsym)) {
 560             return null;
 561         }
 562         searched.add(tsym);
 563 
 564         for (Symbol sym : tsym.members().getSymbolsByName(fieldName)) {
 565             if (sym.kind == VAR) {
 566                 return (VarSymbol)sym;
 567             }
 568         }
 569 
 570         //### If we found a VarSymbol above, but which did not pass
 571         //### the modifier filter, we should return failure here!
 572 
 573         ClassSymbol encl = tsym.owner.enclClass();
 574         if (encl != null) {
 575             VarSymbol vsym = searchField(encl, fieldName, searched);
 576             if (vsym != null) {
 577                 return vsym;


   1 /*
   2  * Copyright (c) 2005, 2019, 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


 392         if (sym == null) {
 393             for (TreePath p = path; p != null; p = p.getParentPath()) {
 394                 JCTree t = (JCTree) p.getLeaf();
 395                 if (t.hasTag(JCTree.Tag.CLASSDEF)) {
 396                     JCClassDecl ct = (JCClassDecl) t;
 397                     if (ct.sym != null) {
 398                         if ((ct.sym.flags_field & Flags.UNATTRIBUTED) != 0) {
 399                             attr.attribClass(ct.pos(), ct.sym);
 400                             sym = TreeInfo.symbolFor(tree);
 401                         }
 402                         break;
 403                     }
 404                 }
 405             }
 406         }
 407         return sym;
 408     }
 409 
 410     @Override @DefinedBy(Api.COMPILER_TREE)
 411     public Element getElement(DocTreePath path) {
 412         DocTree tree = path.getLeaf();
 413         if (tree instanceof DCReference)
 414             return attributeDocReference(path.getTreePath(), ((DCReference) tree));
 415         if (tree instanceof DCIdentifier) {
 416             if (path.getParentPath().getLeaf() instanceof DCParam) {
 417                 return attributeParamIdentifier(path.getTreePath(), (DCParam) path.getParentPath().getLeaf());
 418             }
 419         }
 420         return null;
 421     }
 422 
 423     @Override @DefinedBy(Api.COMPILER_TREE)
 424     public java.util.List<DocTree> getFirstSentence(java.util.List<? extends DocTree> list) {
 425         return docTreeMaker.getFirstSentence(list);
 426     }
 427 
 428     private Symbol attributeDocReference(TreePath path, DCReference ref) {
 429         Env<AttrContext> env = getAttrContext(path);
 430         if (env == null) return null;
 431 
 432         Log.DeferredDiagnosticHandler deferredDiagnosticHandler =
 433                 new Log.DeferredDiagnosticHandler(log);
 434         try {
 435             final TypeSymbol tsym;


 508                 // explicit (possibly empty) arg list given, so cannot be a field
 509                 return msym;
 510             }
 511 
 512             VarSymbol vsym = (ref.paramTypes != null) ? null : findField(sym, memberName);
 513             // prefer a field over a method with no parameters
 514             if (vsym != null &&
 515                     (msym == null ||
 516                         types.isSubtypeUnchecked(vsym.enclClass().asType(), msym.enclClass().asType()))) {
 517                 return vsym;
 518             } else {
 519                 return msym;
 520             }
 521         } catch (Abort e) { // may be thrown by Check.completionError in case of bad class file
 522             return null;
 523         } finally {
 524             log.popDiagnosticHandler(deferredDiagnosticHandler);
 525         }
 526     }
 527 
 528     private Symbol attributeParamIdentifier(TreePath path, DCParam paramTag) {
 529         Symbol javadocSymbol = getElement(path);
 530         if (javadocSymbol == null)
 531             return null;
 532         ElementKind kind = javadocSymbol.getKind();
 533         List<? extends Symbol> params = List.nil();
 534         if (kind == ElementKind.METHOD || kind == ElementKind.CONSTRUCTOR) {
 535             MethodSymbol ee = (MethodSymbol) javadocSymbol;
 536             params = paramTag.isTypeParameter()
 537                     ? ee.getTypeParameters()
 538                     : ee.getParameters();
 539         } else if (kind.isClass() || kind.isInterface()) {
 540             ClassSymbol te = (ClassSymbol) javadocSymbol;
 541             params = paramTag.isTypeParameter()
 542                     ? te.getTypeParameters()
 543                     : te.getStateComponents();
 544         }
 545 
 546         for (Symbol param : params) {
 547             if (param.getSimpleName() == paramTag.getName().getName()) {
 548                 return param;
 549             }
 550         }
 551         return null;
 552     }
 553 

 554     private VarSymbol findField(ClassSymbol tsym, Name fieldName) {
 555         return searchField(tsym, fieldName, new HashSet<>());
 556     }
 557 

 558     private VarSymbol searchField(ClassSymbol tsym, Name fieldName, Set<ClassSymbol> searched) {
 559         if (searched.contains(tsym)) {
 560             return null;
 561         }
 562         searched.add(tsym);
 563 
 564         for (Symbol sym : tsym.members().getSymbolsByName(fieldName)) {
 565             if (sym.kind == VAR) {
 566                 return (VarSymbol)sym;
 567             }
 568         }
 569 
 570         //### If we found a VarSymbol above, but which did not pass
 571         //### the modifier filter, we should return failure here!
 572 
 573         ClassSymbol encl = tsym.owner.enclClass();
 574         if (encl != null) {
 575             VarSymbol vsym = searchField(encl, fieldName, searched);
 576             if (vsym != null) {
 577                 return vsym;


< prev index next >