Skip to content

Commit

Permalink
FORGE-918 Fix for ignoring extra dimensions of arrays
Browse files Browse the repository at this point in the history
This ensures that underlying variable fragments having additional dimensions are considered when treating a field as an array type or not.
  • Loading branch information
VineetReynolds committed Aug 8, 2013
1 parent ef58a40 commit 04396e9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
27 changes: 22 additions & 5 deletions impl/src/main/java/org/jboss/forge/parser/java/impl/FieldImpl.java
Expand Up @@ -301,12 +301,29 @@ public String getQualifiedType()
{
Type fieldType = field.getType();
String result = parent.resolveType(fieldType.toString());
if (fieldType != null && fieldType.isArrayType())
int extraDimensions = 0;
for (Object f : field.fragments())
{
if (f instanceof VariableDeclarationFragment)
{
VariableDeclarationFragment frag = (VariableDeclarationFragment) f;
extraDimensions = frag.getExtraDimensions();
break;
}
}
if (fieldType != null)
{
// The resolved type lacks information about arrays since arrays would be stripped from it
// We recreate it using the dimensions in the JDT Type to ensure that arrays are not lost in the return type.
int dimensions = ((ArrayType) fieldType).getDimensions();
for (int ctr = 0; ctr < dimensions; ctr++)
if (fieldType.isArrayType())
{
// The resolved type lacks information about arrays since arrays would be stripped from it
// We recreate it using the dimensions in the JDT Type to ensure that arrays are not lost in the return value.
int dimensions = ((ArrayType) fieldType).getDimensions();
for (int ctr = 0; ctr < dimensions; ctr++)
{
result += "[]";
}
}
for(int dimensionsToAdd = 0; dimensionsToAdd < extraDimensions; dimensionsToAdd++)
{
result += "[]";
}
Expand Down
Expand Up @@ -13,8 +13,10 @@
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ArrayType;
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.ParameterizedType;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.jboss.forge.parser.JavaParser;
import org.jboss.forge.parser.java.JavaClass;
import org.jboss.forge.parser.java.JavaSource;
Expand Down Expand Up @@ -103,7 +105,19 @@ public List<Type<O>> getTypeArguments()
@Override
public boolean isArray()
{
return type.isArrayType();
int extraDimensions = 0;
ASTNode parent = type.getParent();
if(parent instanceof FieldDeclaration)
{
for(Object f: ((FieldDeclaration) parent).fragments())
{
if(f instanceof VariableDeclarationFragment)
{
extraDimensions = ((VariableDeclarationFragment) f).getExtraDimensions();
}
}
}
return type.isArrayType() || extraDimensions > 0;
}

@Override
Expand Down
Expand Up @@ -14,7 +14,6 @@
import org.jboss.forge.parser.java.JavaClass;
import org.jboss.forge.parser.java.Type;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

/**
Expand Down Expand Up @@ -245,12 +244,23 @@ public void testFieldMultidimensionalArray2()
}

@Test
@Ignore("FORGE-918")
public void testFieldTypeByteArrayAlternativeDeclarationTest()
{
final JavaClass javaClass = JavaParser.create(JavaClass.class);
final Field<JavaClass> field = javaClass.addField("public byte content[];");
Assert.assertEquals("byte[]", field.getQualifiedType());
Assert.assertEquals("byte[]", field.getType());
Assert.assertTrue(field.getTypeInspector().isArray());
}

@Test
public void testFieldTypeObjectArrayAlternativeDeclarationTest()
{
final JavaClass javaClass = JavaParser.create(JavaClass.class);
final Field<JavaClass> field = javaClass.addField("public Long content[];");
Assert.assertEquals("java.lang.Long[]", field.getQualifiedType());
Assert.assertEquals("Long[]", field.getType());
Assert.assertTrue(field.getTypeInspector().isArray());
}

}

0 comments on commit 04396e9

Please sign in to comment.