Skip to content

Commit

Permalink
Simplified org.jboss.forge.roaster.model.impl.AbstractJavaSourceMembe…
Browse files Browse the repository at this point in the history
…rHolder#addInterface(java.lang.String)

- Fixes #259
  • Loading branch information
gastaldi committed Oct 18, 2022
1 parent a00cf44 commit cf3cd91
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -309,29 +312,21 @@ public List<String> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

0 comments on commit cf3cd91

Please sign in to comment.