diff --git a/impl/src/main/java/org/jboss/forge/roaster/model/impl/FieldImpl.java b/impl/src/main/java/org/jboss/forge/roaster/model/impl/FieldImpl.java index 2e78c591..c5247a38 100644 --- a/impl/src/main/java/org/jboss/forge/roaster/model/impl/FieldImpl.java +++ b/impl/src/main/java/org/jboss/forge/roaster/model/impl/FieldImpl.java @@ -15,9 +15,6 @@ import org.eclipse.jdt.core.dom.Expression; import org.eclipse.jdt.core.dom.FieldDeclaration; import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword; -import org.eclipse.jdt.core.dom.Name; -import org.eclipse.jdt.core.dom.PrimitiveType; -import org.eclipse.jdt.core.dom.PrimitiveType.Code; import org.eclipse.jdt.core.dom.VariableDeclarationFragment; import org.jboss.forge.roaster.Roaster; import org.jboss.forge.roaster.model.Annotation; @@ -29,6 +26,7 @@ import org.jboss.forge.roaster.model.ast.ModifierAccessor; import org.jboss.forge.roaster.model.source.AnnotationSource; import org.jboss.forge.roaster.model.source.FieldSource; +import org.jboss.forge.roaster.model.source.JavaClassSource; import org.jboss.forge.roaster.model.source.JavaSource; import org.jboss.forge.roaster.model.util.Strings; import org.jboss.forge.roaster.model.util.Types; @@ -339,43 +337,15 @@ public FieldSource setType(final String typeName) origin.addImport(typeName); } - Code primitive = PrimitiveType.toCode(typeName); + String stub = "public class Stub { " + simpleName + " field; }"; + JavaClassSource temp = (JavaClassSource) Roaster.parse(stub); + List> fields = temp.getFields(); + org.eclipse.jdt.core.dom.Type fieldType = ((FieldDeclaration) ((VariableDeclarationFragment) fields.get(0) + .getInternal()).getParent()).getType(); + + fieldType = (org.eclipse.jdt.core.dom.Type) ASTNode.copySubtree(field.getAST(), fieldType); + field.setType(fieldType); - org.eclipse.jdt.core.dom.Type type = null; - if (primitive != null) - { - type = ast.newPrimitiveType(primitive); - } - else - { - if (!origin.requiresImport(typeName)) - { - if (Types.isArray(typeName)) - { - String arrayType = Types.stripArray(typeName); - int arrayDimension = Types.getArrayDimension(typeName); - if (Types.isPrimitive(arrayType)) - { - type = ast.newArrayType(ast.newPrimitiveType(PrimitiveType.toCode(arrayType)), arrayDimension); - } - else - { - type = ast.newArrayType(ast.newSimpleType(ast.newSimpleName(arrayType)), arrayDimension); - } - } - else - { - type = ast.newSimpleType(ast.newSimpleName(simpleName)); - } - } - else - { - String[] className = Types.tokenizeClassName(typeName); - Name name = ast.newName(className); - type = ast.newSimpleType(name); - } - } - field.setType(type); return this; } @@ -434,7 +404,7 @@ public boolean equals(Object obj) if (other.field != null) return false; } - else if (!field.equals(other.field )) + else if (!field.equals(other.field)) return false; if (fragment == null) { diff --git a/impl/src/test/java/org/jboss/forge/test/roaster/model/JavaClassGenericsTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/JavaClassGenericsTest.java index 087aeeb6..3f967c71 100644 --- a/impl/src/test/java/org/jboss/forge/test/roaster/model/JavaClassGenericsTest.java +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/JavaClassGenericsTest.java @@ -11,6 +11,7 @@ import org.jboss.forge.roaster.Roaster; import org.jboss.forge.roaster.model.JavaInterface; +import org.jboss.forge.roaster.model.source.FieldSource; import org.jboss.forge.roaster.model.source.JavaClassSource; import org.jboss.forge.roaster.model.source.JavaInterfaceSource; import org.jboss.forge.roaster.model.source.TypeVariableSource; @@ -185,4 +186,13 @@ public void getClassGenericsName() throws ClassNotFoundException Assert.assertTrue(typeVariables.get(1).getBounds().isEmpty()); } + @Test + public void testGenericTypeName() throws Exception + { + JavaClassSource javaClass = Roaster.create(JavaClassSource.class); + FieldSource field = javaClass.addField(); + field.setPrivate().setName("email").setType("java.util.List"); + Assert.assertTrue(javaClass.hasImport(List.class)); + Assert.assertEquals("List", field.getType().toString()); + } }