From 5392e54664720327ed8f43116df7514bfd2b8ba7 Mon Sep 17 00:00:00 2001 From: George Gastaldi Date: Tue, 28 Oct 2014 11:55:47 -0200 Subject: [PATCH] ROASTER-42: Fixed FieldImpl.setType implementation --- .../forge/roaster/model/impl/FieldImpl.java | 50 ++++--------------- .../test/roaster/model/FieldTypeTest.java | 17 +++++-- .../roaster/model/JavaClassGenericsTest.java | 1 - 3 files changed, 23 insertions(+), 45 deletions(-) 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/FieldTypeTest.java b/impl/src/test/java/org/jboss/forge/test/roaster/model/FieldTypeTest.java index 7d2a68ce..45184761 100644 --- a/impl/src/test/java/org/jboss/forge/test/roaster/model/FieldTypeTest.java +++ b/impl/src/test/java/org/jboss/forge/test/roaster/model/FieldTypeTest.java @@ -229,7 +229,6 @@ public void testFieldMultidimensionalArray() Assert.assertEquals(3, type.getArrayDimensions()); } - @Test public void testFieldMultidimensionalArray2() { @@ -243,7 +242,7 @@ public void testFieldMultidimensionalArray2() Assert.assertEquals(3, type.getArrayDimensions()); Assert.assertEquals("Vector[][][]", field.getType().getName()); } - + @Test public void testFieldTypeByteArrayAlternativeDeclarationTest() { @@ -254,7 +253,7 @@ public void testFieldTypeByteArrayAlternativeDeclarationTest() Assert.assertTrue(field.getType().isArray()); Assert.assertEquals(1, field.getType().getArrayDimensions()); } - + @Test public void testFieldTypeObjectArrayAlternativeDeclarationTest() { @@ -265,7 +264,7 @@ public void testFieldTypeObjectArrayAlternativeDeclarationTest() Assert.assertTrue(field.getType().isArray()); Assert.assertEquals(1, field.getType().getArrayDimensions()); } - + @Test public void testFieldTypeObjectArrayMixedDimensionTest() { @@ -276,4 +275,14 @@ public void testFieldTypeObjectArrayMixedDimensionTest() Assert.assertTrue(field.getType().isArray()); Assert.assertEquals(2, field.getType().getArrayDimensions()); } + + @Test + public void testGenericFieldType() 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()); + } } 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..5037d540 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 @@ -184,5 +184,4 @@ public void getClassGenericsName() throws ClassNotFoundException Assert.assertEquals("O", typeVariables.get(1).getName()); Assert.assertTrue(typeVariables.get(1).getBounds().isEmpty()); } - }