From cf3cd91318187d0aeffb3afb531ba6c2cb8c3eb6 Mon Sep 17 00:00:00 2001 From: George Gastaldi Date: Fri, 14 Oct 2022 23:13:56 -0300 Subject: [PATCH] Simplified org.jboss.forge.roaster.model.impl.AbstractJavaSourceMemberHolder#addInterface(java.lang.String) - Fixes #259 --- .../impl/AbstractJavaSourceMemberHolder.java | 35 ++++++++----------- .../test/roaster/model/JavaInterfaceTest.java | 33 +++++++++++++++++ 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/AbstractJavaSourceMemberHolder.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/AbstractJavaSourceMemberHolder.java index 73e193a8..6d2dff61 100644 --- a/impl/src/main/java/org/jboss/forge/roaster/model/impl/AbstractJavaSourceMemberHolder.java +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/AbstractJavaSourceMemberHolder.java @@ -17,11 +17,15 @@ import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils; -import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; import org.eclipse.jdt.core.dom.BodyDeclaration; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.FieldDeclaration; import org.eclipse.jdt.core.dom.MethodDeclaration; +import org.eclipse.jdt.core.dom.Name; +import org.eclipse.jdt.core.dom.QualifiedName; +import org.eclipse.jdt.core.dom.SimpleType; import org.eclipse.jdt.core.dom.Type; import org.eclipse.jdt.core.dom.VariableDeclarationFragment; import org.eclipse.jface.text.Document; @@ -30,7 +34,6 @@ import org.jboss.forge.roaster.model.JavaInterface; import org.jboss.forge.roaster.model.JavaType; import org.jboss.forge.roaster.model.Method; -import org.jboss.forge.roaster.model.Parameter; import org.jboss.forge.roaster.model.Property; import org.jboss.forge.roaster.model.ast.MethodFinderVisitor; import org.jboss.forge.roaster.model.source.FieldSource; @@ -309,29 +312,21 @@ public List getInterfaces() @Override public O addInterface(final String type) { - if (!this.hasInterface(type)) + if (!hasInterface(type)) { - final String simpleName = Types.toSimpleName(type); - - Type interfaceType = JDTHelper.getInterfaces( - Roaster.parse(JavaInterfaceImpl.class, - "public interface Mock extends " + simpleName + " {}") - .getDeclaration()) - .get(0); - - if (this.hasInterface(simpleName) || this.hasImport(simpleName)) + String typeName; + if (addImport(type) != null) { - interfaceType = JDTHelper.getInterfaces(Roaster.parse(JavaInterfaceImpl.class, - "public interface Mock extends " + type + " {}").getDeclaration()).get(0); + typeName = Types.toSimpleName(type); } - - if (!this.hasImport(simpleName)) + else { - this.addImport(type); + typeName = type; } - - ASTNode node = ASTNode.copySubtree(unit.getAST(), interfaceType); - JDTHelper.getInterfaces(getDeclaration()).add((Type) node); + AbstractTypeDeclaration declaration = getDeclaration(); + AST ast = getDeclaration().getAST(); + SimpleType simpleType = ast.newSimpleType(ast.newName(typeName)); + JDTHelper.getInterfaces(declaration).add(simpleType); } return (O) this; } diff --git a/tests/src/test/java/org/jboss/forge/test/roaster/model/JavaInterfaceTest.java b/tests/src/test/java/org/jboss/forge/test/roaster/model/JavaInterfaceTest.java index af95264f..f8d866d7 100644 --- a/tests/src/test/java/org/jboss/forge/test/roaster/model/JavaInterfaceTest.java +++ b/tests/src/test/java/org/jboss/forge/test/roaster/model/JavaInterfaceTest.java @@ -139,4 +139,37 @@ public void testImportInterfaceFromSamePackage() assertThat(javaImplementation.getInterfaces()).contains("com.foo.forge.MyInterface"); } + @Test + public void testExtendDifferentPackage() { + JavaInterfaceSource outer = Roaster.create(JavaInterfaceSource.class) + .setPackage("outer") + .setName("Buggy") + .setPublic(); + + JavaInterfaceSource inner = Roaster.create(JavaInterfaceSource.class) + .setPackage("outer.inner") + .setName("Buggy") + .setPublic(); + + inner.addInterface(outer); + assertThat(inner.toString()).contains("public interface Buggy extends outer.Buggy"); + } + + @Test + public void testExtendIfImportAlreadyExists() { + + JavaInterfaceSource foo = Roaster.create(JavaInterfaceSource.class) + .setPackage("org.pkg") + .setName("Foo") + .setPublic(); + + JavaInterfaceSource bar = Roaster.create(JavaInterfaceSource.class) + .setPackage("org.pkg") + .setName("Bar") + .setPublic(); + + foo.addImport(bar); + foo.addInterface(bar); + assertThat(foo.toString()).contains("public interface Foo extends Bar"); + } }