From b511afd6884a152ba4b6755d6b1f2a017da5065c Mon Sep 17 00:00:00 2001 From: I-Al-Istannen Date: Sun, 30 Jun 2024 15:50:16 +0200 Subject: [PATCH] fix: Inherit formal javadoc type parameters (#5869) --- .../api/parsing/InheritanceResolver.java | 27 +++++++++++-------- .../api/parsing/InheritanceResolverTest.java | 15 ++++++----- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/spoon-javadoc/src/main/java/spoon/javadoc/api/parsing/InheritanceResolver.java b/spoon-javadoc/src/main/java/spoon/javadoc/api/parsing/InheritanceResolver.java index 3a87870da5a..103ca39dfcf 100644 --- a/spoon-javadoc/src/main/java/spoon/javadoc/api/parsing/InheritanceResolver.java +++ b/spoon-javadoc/src/main/java/spoon/javadoc/api/parsing/InheritanceResolver.java @@ -146,6 +146,10 @@ public List completeJavadocWithInheritedTags(CtElement element, .stream() .map(CtNamedElement::getSimpleName) .collect(Collectors.toCollection(LinkedHashSet::new)); // keep decl order + method.getFormalCtTypeParameters() + .stream() + .map(it -> "<" + it.getSimpleName() + ">") + .forEach(paramsToFind::add); view.getBlockTagArguments(StandardJavadocTagType.PARAM, JavadocText.class) .forEach(it -> paramsToFind.remove(it.getText())); @@ -161,7 +165,7 @@ public List completeJavadocWithInheritedTags(CtElement element, boolean needsReturn = needsReturn(view); boolean needsBody = view.getBody().isEmpty(); - InheritedJavadoc inheritedJavadoc = lookupInheritedDocForMethod(method); + InheritedJavadoc inheritedJavadoc = lookupInheritedDocForMethod(method, paramsToFind, throwsToFind); // Order by body -> param -> return -> throws List newElements = new ArrayList<>(); @@ -204,12 +208,18 @@ private static boolean needsReturn(JavadocCommentView view) { return start.getTagType() != StandardJavadocTagType.RETURN; } - private static InheritedJavadoc lookupInheritedDocForMethod(CtMethod method) { + private static InheritedJavadoc lookupInheritedDocForMethod( + CtMethod method, + Set paramsToFind, + Set throwsToFind + ) { List> targets = new InheritanceResolver() .findSuperMethodsInCommentInheritanceOrder(method.getDeclaringType(), method); List body = new ArrayList<>(); - JavadocInheritanceCollectionVisitor visitor = new JavadocInheritanceCollectionVisitor(method); + JavadocInheritanceCollectionVisitor visitor = new JavadocInheritanceCollectionVisitor( + paramsToFind, throwsToFind + ); for (CtMethod target : targets) { if (visitor.isFinished() && !body.isEmpty()) { @@ -277,14 +287,9 @@ private static class JavadocInheritanceCollectionVisitor implements JavadocVisit private final Map throwsClauses; private JavadocBlockTag returnTag; - public JavadocInheritanceCollectionVisitor(CtMethod method) { - this.missingParameters = method.getParameters().stream() - .map(CtNamedElement::getSimpleName) - .collect(Collectors.toCollection(HashSet::new)); - this.missingThrowsClauses = method.getThrownTypes().stream() - .map(CtTypeInformation::getQualifiedName) - .collect(Collectors.toCollection(HashSet::new)); - + public JavadocInheritanceCollectionVisitor(Set missingParameters, Set missingThrowsClauses) { + this.missingParameters = new HashSet<>(missingParameters); + this.missingThrowsClauses = new HashSet<>(missingThrowsClauses); this.returnTag = null; this.params = new HashMap<>(); this.throwsClauses = new HashMap<>(); diff --git a/spoon-javadoc/src/test/java/spoon/javadoc/api/parsing/InheritanceResolverTest.java b/spoon-javadoc/src/test/java/spoon/javadoc/api/parsing/InheritanceResolverTest.java index 906589472f9..06813ab03aa 100644 --- a/spoon-javadoc/src/test/java/spoon/javadoc/api/parsing/InheritanceResolverTest.java +++ b/spoon-javadoc/src/test/java/spoon/javadoc/api/parsing/InheritanceResolverTest.java @@ -152,7 +152,7 @@ private static CompletedDoc inheritTestFromSuper(String javadoc) { public static class Sub extends Super { %s @Override - public int test(int a) throws IOException { return a; } + public int test(int a) throws IOException { return a; } } """.formatted(packageName, InheritanceResolverTest.class.getName() + ".Super", javadoc), subclassName.replace(".", "/") @@ -179,10 +179,11 @@ static class Super { * This is a foo method. * * @param a the a param + * @param a type param * @return foo * @throws IOException never. */ - public int test(int a) throws IOException { + public int test(int a) throws IOException { return a; } } @@ -191,7 +192,7 @@ private interface SuperInt1 { /** * A test. */ - int test(int a) throws IOException; + int test(int a) throws IOException; } private interface SuperInt2 extends SuperInt1 { @@ -199,7 +200,7 @@ private interface SuperInt2 extends SuperInt1 { * @param a the a param */ @Override - int test(int a) throws IOException; + int test(int a) throws IOException; } private interface SuperInt3 { @@ -208,21 +209,21 @@ private interface SuperInt3 { * * @throws IOException never */ - int test(int a) throws IOException; + int test(int a) throws IOException; } private interface SuperInt4 { /** * @return foo */ - int test(int a) throws IOException; + int test(int a) throws IOException; } private static class SubMultiSuperParent { /** * Never used in <22, shadowed by the interface. */ - public int test(int a) throws IOException { + public int test(int a) throws IOException { return 0; } }