8177280: @see {@link} syntax should allow generic types

8237826: DocTrees should provide getType(DocTreePath) method

Reviewed-by: jjg
This commit is contained in:
Hannes Wallnöfer 2020-05-29 14:28:13 +02:00
parent c0a1a4e4fc
commit b43f356288
15 changed files with 371 additions and 191 deletions

View file

@ -1047,19 +1047,15 @@ public class HtmlDocletWriter {
} else if (refMemName == null) {
// Must be a class reference since refClass is not null and refMemName is null.
if (label.isEmpty()) {
/*
* it seems to me this is the right thing to do, but it causes comparator failures.
*/
if (!configuration.backwardCompatibility) {
StringContent content = utils.isEnclosingPackageIncluded(refClass)
? new StringContent(utils.getSimpleName(refClass))
: new StringContent(utils.getFullyQualifiedName(refClass));
label = plainOrCode(isLinkPlain, content);
} else {
label = plainOrCode(isLinkPlain,
new StringContent(utils.getSimpleName(refClass)));
if (!refClass.getTypeParameters().isEmpty() && seetext.contains("<")) {
// If this is a generic type link try to use the TypeMirror representation.
TypeMirror refType = ch.getReferencedType(see);
if (refType != null) {
return plainOrCode(isLinkPlain, getLink(
new LinkInfoImpl(configuration, LinkInfoImpl.Kind.DEFAULT, refType)));
}
}
label = plainOrCode(isLinkPlain, new StringContent(utils.getSimpleName(refClass)));
}
return getLink(new LinkInfoImpl(configuration, LinkInfoImpl.Kind.DEFAULT, refClass)
.label(label));

View file

@ -136,7 +136,7 @@ public class LinkFactoryImpl extends LinkFactory {
vars.addAll(((DeclaredType) linkInfo.type).getTypeArguments());
} else if (ctype != null && utils.isDeclaredType(ctype)) {
vars.addAll(((DeclaredType) ctype).getTypeArguments());
} else if (linkInfo.typeElement != null) {
} else if (ctype == null && linkInfo.typeElement != null) {
linkInfo.typeElement.getTypeParameters().forEach(t -> vars.add(t.asType()));
} else {
// Nothing to document.

View file

@ -47,11 +47,6 @@ public class LinkInfoImpl extends LinkInfo {
public enum Kind {
DEFAULT,
/**
* Indicate that the link appears in a class list.
*/
ALL_CLASSES_FRAME,
/**
* Indicate that the link appears in a class documentation.
*/
@ -187,11 +182,6 @@ public class LinkInfoImpl extends LinkInfo {
*/
ANNOTATION,
/**
* The header for field documentation copied from parent.
*/
VARIABLE_ELEMENT_COPY,
/**
* The parent nodes in the class tree.
*/
@ -350,12 +340,10 @@ public class LinkInfoImpl extends LinkInfo {
public final void setContext(Kind c) {
//NOTE: Put context specific link code here.
switch (c) {
case ALL_CLASSES_FRAME:
case PACKAGE_FRAME:
case IMPLEMENTED_CLASSES:
case SUBCLASSES:
case EXECUTABLE_ELEMENT_COPY:
case VARIABLE_ELEMENT_COPY:
case PROPERTY_COPY:
case CLASS_USE_HEADER:
includeTypeInClassLinkLabel = false;

View file

@ -97,11 +97,6 @@ public abstract class BaseConfiguration {
*/
public static final String DEFAULT_BUILDER_XML = "resources/doclet.xml";
/**
* Maintain backward compatibility with previous javadoc version
*/
public boolean backwardCompatibility = true;
/**
* The meta tag keywords instance.
*/

View file

@ -191,6 +191,15 @@ public class CommentHelper {
return doctrees.getElement(docTreePath);
}
public TypeMirror getType(ReferenceTree rtree) {
DocTreePath docTreePath = DocTreePath.getPath(path, dcTree, rtree);
if (docTreePath != null) {
DocTrees doctrees = configuration.docEnv.getDocTrees();
return doctrees.getType(docTreePath);
}
return null;
}
public Element getException(DocTree dtree) {
if (dtree.getKind() == THROWS || dtree.getKind() == EXCEPTION) {
ThrowsTree tt = (ThrowsTree)dtree;
@ -423,48 +432,19 @@ public class CommentHelper {
}
private Element getReferencedElement(DocTree dtree) {
return new SimpleDocTreeVisitor<Element, Void>() {
@Override
public Element visitSee(SeeTree node, Void p) {
for (DocTree dt : node.getReference()) {
return visit(dt, null);
}
return null;
}
@Override
public Element visitLink(LinkTree node, Void p) {
return visit(node.getReference(), null);
}
@Override
public Element visitProvides(ProvidesTree node, Void p) {
return visit(node.getServiceType(), null);
}
@Override
public Element visitValue(ValueTree node, Void p) {
return visit(node.getReference(), null);
}
return new ReferenceDocTreeVisitor<Element>() {
@Override
public Element visitReference(ReferenceTree node, Void p) {
return getElement(node);
}
}.visit(dtree, null);
}
public TypeMirror getReferencedType(DocTree dtree) {
return new ReferenceDocTreeVisitor<TypeMirror>() {
@Override
public Element visitSerialField(SerialFieldTree node, Void p) {
return visit(node.getType(), null);
}
@Override
public Element visitUses(UsesTree node, Void p) {
return visit(node.getServiceType(), null);
}
@Override
protected Element defaultAction(DocTree node, Void p) {
return null;
public TypeMirror visitReference(ReferenceTree node, Void p) {
return getType(node);
}
}.visit(dtree, null);
}
@ -479,42 +459,54 @@ public class CommentHelper {
}
public String getReferencedSignature(DocTree dtree) {
return new SimpleDocTreeVisitor<String, Void>() {
@Override
public String visitSee(SeeTree node, Void p) {
for (DocTree dt : node.getReference()) {
return visit(dt, null);
}
return null;
}
@Override
public String visitLink(LinkTree node, Void p) {
return visit(node.getReference(), null);
}
@Override
public String visitValue(ValueTree node, Void p) {
return visit(node.getReference(), null);
}
return new ReferenceDocTreeVisitor<String>() {
@Override
public String visitReference(ReferenceTree node, Void p) {
return node.getSignature();
}
@Override
public String visitSerialField(SerialFieldTree node, Void p) {
return visit(node.getType(), null);
}
@Override
protected String defaultAction(DocTree node, Void p) {
return null;
}
}.visit(dtree, null);
}
private static class ReferenceDocTreeVisitor<R> extends SimpleDocTreeVisitor<R, Void> {
@Override
public R visitSee(SeeTree node, Void p) {
for (DocTree dt : node.getReference()) {
return visit(dt, null);
}
return null;
}
@Override
public R visitLink(LinkTree node, Void p) {
return visit(node.getReference(), null);
}
@Override
public R visitProvides(ProvidesTree node, Void p) {
return visit(node.getServiceType(), null);
}
@Override
public R visitValue(ValueTree node, Void p) {
return visit(node.getReference(), null);
}
@Override
public R visitSerialField(SerialFieldTree node, Void p) {
return visit(node.getType(), null);
}
@Override
public R visitUses(UsesTree node, Void p) {
return visit(node.getServiceType(), null);
}
@Override
protected R defaultAction(DocTree node, Void p) {
return null;
}
}
public List<? extends DocTree> getReference(DocTree dtree) {
return dtree.getKind() == SEE ? ((SeeTree)dtree).getReference() : null;
}