From 26dd7dce6429ce3877a42591a3f7159fa510f308 Mon Sep 17 00:00:00 2001 From: Ralph-Sanders Date: Mon, 11 Nov 2024 14:07:51 -0600 Subject: [PATCH 1/5] Add new test --- .../openrewrite/java/ExtendInterfaceTest.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 rewrite-java/src/test/java/org/openrewrite/java/ExtendInterfaceTest.java diff --git a/rewrite-java/src/test/java/org/openrewrite/java/ExtendInterfaceTest.java b/rewrite-java/src/test/java/org/openrewrite/java/ExtendInterfaceTest.java new file mode 100644 index 00000000000..0392b12810a --- /dev/null +++ b/rewrite-java/src/test/java/org/openrewrite/java/ExtendInterfaceTest.java @@ -0,0 +1,54 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.java; + +import org.intellij.lang.annotations.Language; +import org.junit.jupiter.api.Test; +import org.openrewrite.java.tree.J; +import org.openrewrite.test.RewriteTest; + +import static org.junit.jupiter.api.Assertions.*; + +class ExtendInterfaceTest implements RewriteTest { + + @Test + void interfaceExtendsInterface() { + @Language("java") + String parentInterface = """ + package parent; + public interface parentInterface {} + """; + + @Language("java") + String childInterface = """ + package child; + public interface childInterface extends parentInterface {} + """; + + J.CompilationUnit sourceJavaFile = (J.CompilationUnit) JavaParser.fromJavaVersion() + .dependsOn(parentInterface) + .build() + .parse(childInterface).findFirst().get(); + + J.ClassDeclaration cd = sourceJavaFile.getClasses().get(0); + + // Check that childInterface does not implement any interfaces + assertNull(cd.getImplements()); + + // Check that childInterface extends an interface + assertNotNull(cd.getExtends()); + } +} From 1a5e7db93bcf3ca0318d569359632ce96540bb88 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 11 Nov 2024 21:54:18 +0100 Subject: [PATCH 2/5] Apply suggestions from code review --- .../src/test/java/org/openrewrite/java/ExtendInterfaceTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/rewrite-java/src/test/java/org/openrewrite/java/ExtendInterfaceTest.java b/rewrite-java/src/test/java/org/openrewrite/java/ExtendInterfaceTest.java index 0392b12810a..05cbb60ecde 100644 --- a/rewrite-java/src/test/java/org/openrewrite/java/ExtendInterfaceTest.java +++ b/rewrite-java/src/test/java/org/openrewrite/java/ExtendInterfaceTest.java @@ -35,6 +35,7 @@ public interface parentInterface {} @Language("java") String childInterface = """ package child; + import parent.parentInterface; public interface childInterface extends parentInterface {} """; From 396837c0a09a4e64367f9f3cd88a5ce6a9b0dbb2 Mon Sep 17 00:00:00 2001 From: Ralph-Sanders Date: Mon, 11 Nov 2024 15:33:14 -0600 Subject: [PATCH 3/5] Fix test --- .../isolated/ReloadableJava17ParserVisitor.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java index 25b78628c07..fdce1cd175f 100644 --- a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java +++ b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java @@ -430,15 +430,20 @@ public J visitClass(ClassTree node, Space fmt) { padLeft(sourceBefore("extends"), convert(node.getExtendsClause())); JContainer implementings = null; + boolean isInterface = kind.getType() == J.ClassDeclaration.Kind.Type.Interface; if (node.getImplementsClause() != null && !node.getImplementsClause().isEmpty()) { - Space implementsPrefix = sourceBefore(kind.getType() == J.ClassDeclaration.Kind.Type.Interface ? + Space implementsPrefix = sourceBefore(isInterface ? "extends" : "implements"); - implementings = JContainer.build( - implementsPrefix, - convertAll(node.getImplementsClause(), commaDelim, noDelim), - Markers.EMPTY - ); + if (isInterface) { + extendings = padLeft(implementsPrefix, convert(node.getImplementsClause().get(0))); + } else { + implementings = JContainer.build( + implementsPrefix, + convertAll(node.getImplementsClause(), commaDelim, noDelim), + Markers.EMPTY + ); + } } JContainer permitting = null; From da36251096530c6f612bb1ad5332f40f78e30bb9 Mon Sep 17 00:00:00 2001 From: Ralph-Sanders Date: Mon, 11 Nov 2024 15:52:27 -0600 Subject: [PATCH 4/5] Clean up --- .../java/isolated/ReloadableJava17ParserVisitor.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java index fdce1cd175f..95441c7d41b 100644 --- a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java +++ b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java @@ -430,16 +430,12 @@ public J visitClass(ClassTree node, Space fmt) { padLeft(sourceBefore("extends"), convert(node.getExtendsClause())); JContainer implementings = null; - boolean isInterface = kind.getType() == J.ClassDeclaration.Kind.Type.Interface; if (node.getImplementsClause() != null && !node.getImplementsClause().isEmpty()) { - Space implementsPrefix = sourceBefore(isInterface ? - "extends" : "implements"); - - if (isInterface) { - extendings = padLeft(implementsPrefix, convert(node.getImplementsClause().get(0))); + if (kind.getType() == J.ClassDeclaration.Kind.Type.Interface) { + extendings = padLeft(sourceBefore("extends"), convert(node.getImplementsClause().get(0))); } else { implementings = JContainer.build( - implementsPrefix, + sourceBefore("implements"), convertAll(node.getImplementsClause(), commaDelim, noDelim), Markers.EMPTY ); From c5d04ca922797a60cafd51b4d55fc94ec9ec6ca6 Mon Sep 17 00:00:00 2001 From: Ralph-Sanders Date: Wed, 13 Nov 2024 09:31:34 -0600 Subject: [PATCH 5/5] Added javadoc --- .../ReloadableJava17ParserVisitor.java | 17 +++--- .../java/org/openrewrite/java/tree/J.java | 13 +++++ .../openrewrite/java/ExtendInterfaceTest.java | 55 ------------------- 3 files changed, 21 insertions(+), 64 deletions(-) delete mode 100644 rewrite-java/src/test/java/org/openrewrite/java/ExtendInterfaceTest.java diff --git a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java index 95441c7d41b..25b78628c07 100644 --- a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java +++ b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java @@ -431,15 +431,14 @@ public J visitClass(ClassTree node, Space fmt) { JContainer implementings = null; if (node.getImplementsClause() != null && !node.getImplementsClause().isEmpty()) { - if (kind.getType() == J.ClassDeclaration.Kind.Type.Interface) { - extendings = padLeft(sourceBefore("extends"), convert(node.getImplementsClause().get(0))); - } else { - implementings = JContainer.build( - sourceBefore("implements"), - convertAll(node.getImplementsClause(), commaDelim, noDelim), - Markers.EMPTY - ); - } + Space implementsPrefix = sourceBefore(kind.getType() == J.ClassDeclaration.Kind.Type.Interface ? + "extends" : "implements"); + + implementings = JContainer.build( + implementsPrefix, + convertAll(node.getImplementsClause(), commaDelim, noDelim), + Markers.EMPTY + ); } JContainer permitting = null; diff --git a/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java b/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java index 1f4ab2a97a6..8e16d3b757d 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/tree/J.java @@ -1222,6 +1222,12 @@ public ClassDeclaration withPrimaryConstructor(@Nullable List primary @Nullable JLeftPadded extendings; + /** + * This is used to access the parent class. + * + * @return The parent class of the ClassDeclaration. If the ClassDeclaration is a class, this will return the + * class specified by the 'extends' keyword. If the ClassDeclaration is an interface, this will return null. + */ public @Nullable TypeTree getExtends() { return extendings == null ? null : extendings.getElement(); } @@ -1233,6 +1239,13 @@ public ClassDeclaration withExtends(@Nullable TypeTree extendings) { @Nullable JContainer implementings; + /** + * This is used to access the parent interfaces. + * + * @return A list of the parent interfaces of the ClassDeclaration. If the ClassDeclaration is a class, this + * will return the interfaces specified by the 'implements' keyword. If the ClassDeclaration is an interface, + * this will return the interfaces specified by the 'extends' keyword. + */ public @Nullable List getImplements() { return implementings == null ? null : implementings.getElements(); } diff --git a/rewrite-java/src/test/java/org/openrewrite/java/ExtendInterfaceTest.java b/rewrite-java/src/test/java/org/openrewrite/java/ExtendInterfaceTest.java deleted file mode 100644 index 05cbb60ecde..00000000000 --- a/rewrite-java/src/test/java/org/openrewrite/java/ExtendInterfaceTest.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2024 the original author or authors. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.java; - -import org.intellij.lang.annotations.Language; -import org.junit.jupiter.api.Test; -import org.openrewrite.java.tree.J; -import org.openrewrite.test.RewriteTest; - -import static org.junit.jupiter.api.Assertions.*; - -class ExtendInterfaceTest implements RewriteTest { - - @Test - void interfaceExtendsInterface() { - @Language("java") - String parentInterface = """ - package parent; - public interface parentInterface {} - """; - - @Language("java") - String childInterface = """ - package child; - import parent.parentInterface; - public interface childInterface extends parentInterface {} - """; - - J.CompilationUnit sourceJavaFile = (J.CompilationUnit) JavaParser.fromJavaVersion() - .dependsOn(parentInterface) - .build() - .parse(childInterface).findFirst().get(); - - J.ClassDeclaration cd = sourceJavaFile.getClasses().get(0); - - // Check that childInterface does not implement any interfaces - assertNull(cd.getImplements()); - - // Check that childInterface extends an interface - assertNotNull(cd.getExtends()); - } -}