langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/CommentHelper.java
changeset 45417 f7479ee8de69
parent 42831 feff6f296019
equal deleted inserted replaced
45416:0d8bb33bdfa7 45417:f7479ee8de69
     1 /*
     1 /*
     2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2015, 2017, 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
    67 import com.sun.source.doctree.VersionTree;
    67 import com.sun.source.doctree.VersionTree;
    68 import com.sun.source.util.DocTreePath;
    68 import com.sun.source.util.DocTreePath;
    69 import com.sun.source.util.DocTrees;
    69 import com.sun.source.util.DocTrees;
    70 import com.sun.source.util.SimpleDocTreeVisitor;
    70 import com.sun.source.util.SimpleDocTreeVisitor;
    71 import com.sun.source.util.TreePath;
    71 import com.sun.source.util.TreePath;
    72 import jdk.javadoc.internal.doclets.toolkit.Configuration;
    72 import jdk.javadoc.internal.doclets.toolkit.BaseConfiguration;
    73 
    73 
    74 import static com.sun.source.doctree.DocTree.Kind.*;
    74 import static com.sun.source.doctree.DocTree.Kind.*;
    75 
    75 
    76 /**
    76 /**
    77  *  A utility class.
    77  *  A utility class.
    87     public final Element element;
    87     public final Element element;
    88     private Element overriddenElement;
    88     private Element overriddenElement;
    89 
    89 
    90     public static final String SPACER = " ";
    90     public static final String SPACER = " ";
    91 
    91 
    92     public CommentHelper(Configuration configuration, Element element, TreePath path, DocCommentTree dctree) {
    92     public CommentHelper(BaseConfiguration configuration, Element element, TreePath path, DocCommentTree dctree) {
    93         //this.configuration = configuration;
    93         //this.configuration = configuration;
    94         this.element = element;
    94         this.element = element;
    95         this.path = path;
    95         this.path = path;
    96         this.dctree = dctree;
    96         this.dctree = dctree;
    97     }
    97     }
   141         } else {
   141         } else {
   142             return null;
   142             return null;
   143         }
   143         }
   144     }
   144     }
   145 
   145 
   146     Element getElement(Configuration c, ReferenceTree rtree) {
   146     Element getElement(BaseConfiguration c, ReferenceTree rtree) {
   147         // likely a synthesized tree
   147         // likely a synthesized tree
   148         if (path == null) {
   148         if (path == null) {
   149             TypeMirror symbol = c.utils.getSymbol(rtree.getSignature());
   149             TypeMirror symbol = c.utils.getSymbol(rtree.getSignature());
   150             if (symbol == null) {
   150             if (symbol == null) {
   151                 return null;
   151                 return null;
   176         }
   176         }
   177         DocTrees doctrees = c.docEnv.getDocTrees();
   177         DocTrees doctrees = c.docEnv.getDocTrees();
   178         return doctrees.getElement(docTreePath);
   178         return doctrees.getElement(docTreePath);
   179     }
   179     }
   180 
   180 
   181     public Element getException(Configuration c, DocTree dtree) {
   181     public Element getException(BaseConfiguration c, DocTree dtree) {
   182         if (dtree.getKind() == THROWS || dtree.getKind() == EXCEPTION) {
   182         if (dtree.getKind() == THROWS || dtree.getKind() == EXCEPTION) {
   183             ThrowsTree tt = (ThrowsTree)dtree;
   183             ThrowsTree tt = (ThrowsTree)dtree;
   184             ReferenceTree exceptionName = tt.getExceptionName();
   184             ReferenceTree exceptionName = tt.getExceptionName();
   185             return getElement(c, exceptionName);
   185             return getElement(c, exceptionName);
   186         }
   186         }
   187         return null;
   187         return null;
   188     }
   188     }
   189 
   189 
   190     public List<? extends DocTree> getDescription(Configuration c, DocTree dtree) {
   190     public List<? extends DocTree> getDescription(BaseConfiguration c, DocTree dtree) {
   191         return getTags(c, dtree);
   191         return getTags(c, dtree);
   192     }
   192     }
   193 
   193 
   194     public String getText(List<? extends DocTree> list) {
   194     public String getText(List<? extends DocTree> list) {
   195         StringBuilder sb = new StringBuilder();
   195         StringBuilder sb = new StringBuilder();
   332             }
   332             }
   333         }.visit(dt, null);
   333         }.visit(dt, null);
   334         return sb;
   334         return sb;
   335     }
   335     }
   336 
   336 
   337     public String getLabel(Configuration c, DocTree dtree) {
   337     public String getLabel(BaseConfiguration c, DocTree dtree) {
   338         return new SimpleDocTreeVisitor<String, Void>() {
   338         return new SimpleDocTreeVisitor<String, Void>() {
   339             @Override
   339             @Override
   340             public String visitLink(LinkTree node, Void p) {
   340             public String visitLink(LinkTree node, Void p) {
   341                 StringBuilder sb = new StringBuilder();
   341                 StringBuilder sb = new StringBuilder();
   342                 node.getLabel().stream().forEach((dt) -> {
   342                 node.getLabel().stream().forEach((dt) -> {
   359                 return "";
   359                 return "";
   360             }
   360             }
   361         }.visit(dtree, null);
   361         }.visit(dtree, null);
   362     }
   362     }
   363 
   363 
   364     public TypeElement getReferencedClass(Configuration c, DocTree dtree) {
   364     public TypeElement getReferencedClass(BaseConfiguration c, DocTree dtree) {
   365         Element e = getReferencedElement(c, dtree);
   365         Element e = getReferencedElement(c, dtree);
   366         if (e == null) {
   366         if (e == null) {
   367             return null;
   367             return null;
   368         } else if (c.utils.isTypeElement(e)) {
   368         } else if (c.utils.isTypeElement(e)) {
   369             return (TypeElement) e;
   369             return (TypeElement) e;
   371             return c.utils.getEnclosingTypeElement(e);
   371             return c.utils.getEnclosingTypeElement(e);
   372         }
   372         }
   373         return null;
   373         return null;
   374     }
   374     }
   375 
   375 
   376     public String getReferencedClassName(Configuration c, DocTree dtree) {
   376     public String getReferencedClassName(BaseConfiguration c, DocTree dtree) {
   377         Element e = getReferencedClass(c, dtree);
   377         Element e = getReferencedClass(c, dtree);
   378         if (e != null) {
   378         if (e != null) {
   379             return c.utils.isTypeElement(e) ? c.utils.getSimpleName(e) : null;
   379             return c.utils.isTypeElement(e) ? c.utils.getSimpleName(e) : null;
   380         }
   380         }
   381         String s = getReferencedSignature(dtree);
   381         String s = getReferencedSignature(dtree);
   384         }
   384         }
   385         int n = s.indexOf("#");
   385         int n = s.indexOf("#");
   386         return (n == -1) ? s : s.substring(0, n);
   386         return (n == -1) ? s : s.substring(0, n);
   387     }
   387     }
   388 
   388 
   389     public Element getReferencedMember(Configuration c, DocTree dtree) {
   389     public Element getReferencedMember(BaseConfiguration c, DocTree dtree) {
   390         Element e = getReferencedElement(c, dtree);
   390         Element e = getReferencedElement(c, dtree);
   391         if (e == null) {
   391         if (e == null) {
   392             return null;
   392             return null;
   393         }
   393         }
   394         return (c.utils.isExecutableElement(e) || c.utils.isVariableElement(e)) ? e : null;
   394         return (c.utils.isExecutableElement(e) || c.utils.isVariableElement(e)) ? e : null;
   401         }
   401         }
   402         int n = s.indexOf("#");
   402         int n = s.indexOf("#");
   403         return (n == -1) ? null : s.substring(n + 1);
   403         return (n == -1) ? null : s.substring(n + 1);
   404     }
   404     }
   405 
   405 
   406     public String getReferencedMemberName(Configuration c, Element e) {
   406     public String getReferencedMemberName(BaseConfiguration c, Element e) {
   407         if (e == null) {
   407         if (e == null) {
   408             return null;
   408             return null;
   409         }
   409         }
   410         return c.utils.isExecutableElement(e)
   410         return c.utils.isExecutableElement(e)
   411                 ? c.utils.getSimpleName(e) + c.utils.makeSignature((ExecutableElement) e, true, true)
   411                 ? c.utils.getSimpleName(e) + c.utils.makeSignature((ExecutableElement) e, true, true)
   412                 : c.utils.getSimpleName(e);
   412                 : c.utils.getSimpleName(e);
   413     }
   413     }
   414 
   414 
   415     public PackageElement getReferencedPackage(Configuration c, DocTree dtree) {
   415     public PackageElement getReferencedPackage(BaseConfiguration c, DocTree dtree) {
   416         Element e = getReferencedElement(c, dtree);
   416         Element e = getReferencedElement(c, dtree);
   417         if (e != null) {
   417         if (e != null) {
   418             return c.utils.containingPackage(e);
   418             return c.utils.containingPackage(e);
   419         }
   419         }
   420         return null;
   420         return null;
   421     }
   421     }
   422 
   422 
   423     public List<? extends DocTree> getFirstSentenceTrees(Configuration c, List<? extends DocTree> body) {
   423     public List<? extends DocTree> getFirstSentenceTrees(BaseConfiguration c, List<? extends DocTree> body) {
   424         List<DocTree> firstSentence = c.docEnv.getDocTrees().getFirstSentence(body);
   424         List<DocTree> firstSentence = c.docEnv.getDocTrees().getFirstSentence(body);
   425         return firstSentence;
   425         return firstSentence;
   426     }
   426     }
   427 
   427 
   428     public List<? extends DocTree> getFirstSentenceTrees(Configuration c, DocTree dtree) {
   428     public List<? extends DocTree> getFirstSentenceTrees(BaseConfiguration c, DocTree dtree) {
   429         return getFirstSentenceTrees(c, getBody(c, dtree));
   429         return getFirstSentenceTrees(c, getBody(c, dtree));
   430     }
   430     }
   431 
   431 
   432     private Element getReferencedElement(Configuration c, DocTree dtree) {
   432     private Element getReferencedElement(BaseConfiguration c, DocTree dtree) {
   433         return new SimpleDocTreeVisitor<Element, Void>() {
   433         return new SimpleDocTreeVisitor<Element, Void>() {
   434             @Override
   434             @Override
   435             public Element visitSee(SeeTree node, Void p) {
   435             public Element visitSee(SeeTree node, Void p) {
   436                 for (DocTree dt : node.getReference()) {
   436                 for (DocTree dt : node.getReference()) {
   437                     return visit(dt, null);
   437                     return visit(dt, null);
   474                return null;
   474                return null;
   475             }
   475             }
   476         }.visit(dtree, null);
   476         }.visit(dtree, null);
   477     }
   477     }
   478 
   478 
   479     public TypeElement getServiceType(Configuration c, DocTree dtree) {
   479     public TypeElement getServiceType(BaseConfiguration c, DocTree dtree) {
   480         Element e = getReferencedElement(c, dtree);
   480         Element e = getReferencedElement(c, dtree);
   481         if (e != null) {
   481         if (e != null) {
   482             return c.utils.isTypeElement(e) ? (TypeElement) e : null;
   482             return c.utils.isTypeElement(e) ? (TypeElement) e : null;
   483         }
   483         }
   484         return null;
   484         return null;
   540             default:
   540             default:
   541                 return null;
   541                 return null;
   542             }
   542             }
   543     }
   543     }
   544 
   544 
   545     public List<? extends DocTree> getTags(Configuration c, DocTree dtree) {
   545     public List<? extends DocTree> getTags(BaseConfiguration c, DocTree dtree) {
   546         return new SimpleDocTreeVisitor<List<? extends DocTree>, Void>() {
   546         return new SimpleDocTreeVisitor<List<? extends DocTree>, Void>() {
   547             List<? extends DocTree> asList(String content) {
   547             List<? extends DocTree> asList(String content) {
   548                 List<DocTree> out = new ArrayList<>();
   548                 List<DocTree> out = new ArrayList<>();
   549                 out.add((TextTree)c.cmtUtils.makeTextTree(content));
   549                 out.add((TextTree)c.cmtUtils.makeTextTree(content));
   550                 return out;
   550                 return out;
   645                return Collections.emptyList();
   645                return Collections.emptyList();
   646             }
   646             }
   647         }.visit(dtree, null);
   647         }.visit(dtree, null);
   648     }
   648     }
   649 
   649 
   650     public List<? extends DocTree> getBody(Configuration c, DocTree dtree) {
   650     public List<? extends DocTree> getBody(BaseConfiguration c, DocTree dtree) {
   651         return getTags(c, dtree);
   651         return getTags(c, dtree);
   652     }
   652     }
   653 
   653 
   654     public ReferenceTree getType(DocTree dtree) {
   654     public ReferenceTree getType(DocTree dtree) {
   655         if (dtree.getKind() == SERIAL_FIELD) {
   655         if (dtree.getKind() == SERIAL_FIELD) {