Skip to content

Commit

Permalink
ROASTER-11: Use full qualified type if conflicting import exists when…
Browse files Browse the repository at this point in the history
… setting new super type (#72)
  • Loading branch information
windmueller authored and gastaldi committed Nov 22, 2016
1 parent d9a17b8 commit 3843e8d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import org.eclipse.jdt.core.dom.BodyDeclaration;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jface.text.Document;
Expand All @@ -24,7 +25,7 @@

/**
* Represents a Java Source File
*
*
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
public class JavaClassImpl extends AbstractGenericCapableJavaSource<JavaClassSource> implements JavaClassSource
Expand Down Expand Up @@ -170,11 +171,11 @@ public JavaClassSource setSuperType(final String type)
else if (Types.isGeneric(type))
{
String typeD = Types.stripGenerics(type);
String sympleTypeDName = Types.toSimpleName(typeD);
String simpleTypeDName = Types.toSimpleName(typeD);
String typesGeneric = Types.getGenericsTypeParameter(type);

org.eclipse.jdt.core.dom.ParameterizedType pt = body.getAST().newParameterizedType(
body.getAST().newSimpleType(body.getAST().newSimpleName(sympleTypeDName)));
body.getAST().newSimpleType(body.getAST().newSimpleName(simpleTypeDName)));

if (!hasImport(typeD) && Types.isQualified(typeD))
{
Expand All @@ -195,13 +196,27 @@ else if (Types.isGeneric(type))
}
else
{
SimpleType simpleType = body.getAST().newSimpleType(body.getAST().newSimpleName(Types.toSimpleName(type)));
getBodyDeclaration().setStructuralProperty(TypeDeclaration.SUPERCLASS_TYPE_PROPERTY, simpleType);
final SimpleName simpleName = body.getAST().newSimpleName(Types.toSimpleName(type));

final Type superType;

if (!hasImport(type) && Types.isQualified(type))
if (!hasImport(type) && hasImport(simpleName.getIdentifier()) && Types.isQualified(type))
{
addImport(type);
// Conflicting import found, use qualified name for new super type
final Name qualifier = body.getAST().newName(Types.getPackage(type));
superType = body.getAST().newNameQualifiedType(qualifier, simpleName);
}
else
{
// Same type as existing import or not qualified at all (maybe from same package)
superType = body.getAST().newSimpleType(simpleName);
if (Types.isQualified(type))
{
addImport(type);
}
}

getBodyDeclaration().setStructuralProperty(TypeDeclaration.SUPERCLASS_TYPE_PROPERTY, superType);
}

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,19 @@ public void testSuperTypeGenericsWithSpaces()
Assert.assertThat(myClass.getSuperType(), equalTo("test.MyClassParent<String,Object>"));
}

@Test
public void testSuperTypeWithConflictingImport()
{
final JavaClassSource myClass = Roaster.create(JavaClassSource.class);
myClass.setPackage("test");
final Import utilListImport = myClass.addImport(List.class);
myClass.setSuperType("java.awt.List");

assertEquals("Class should only contain one import.", 1, myClass.getImports().size());
assertEquals("Wrong import detected.", utilListImport, myClass.getImport("List"));
assertEquals("Wrong super type set.", "java.awt.List", myClass.getSuperType());
}

@Test
public void testFinal() throws Exception
{
Expand Down

0 comments on commit 3843e8d

Please sign in to comment.