Skip to content

Commit

Permalink
Fix multi-field name parsing (#340)
Browse files Browse the repository at this point in the history
- Update test suite to include validation for multiple field declarations using AssertJ.
- Refactor `getName` method in `FieldImpl` for more efficient retrieval of fully qualified names.
  • Loading branch information
gastaldi authored Dec 11, 2024
1 parent fa31230 commit b5c08a5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -291,17 +291,7 @@ public FieldSource<O> setVisibility(final Visibility scope)
@Override
public String getName()
{
String result = null;
for (Object f : field.fragments())
{
if (f instanceof VariableDeclarationFragment)
{
VariableDeclarationFragment frag = (VariableDeclarationFragment) f;
result = frag.getName().getFullyQualifiedName();
break;
}
}
return result;
return fragment.getName().getFullyQualifiedName();
}

@Override
Expand Down Expand Up @@ -499,4 +489,4 @@ public int getColumnNumber()
{
return cu.getColumnNumber(getStartPosition());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.jboss.forge.roaster.model.source.JavaClassSource;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -125,4 +126,10 @@ public void testMultipleFieldDeclarationWithInitializers()
assertEquals(1, fields.get(2).getType().getArrayDimensions());
}

@Test
public void testMultipleFieldDeclarationParse()
{
JavaClassSource javaClassSource = Roaster.parse(JavaClassSource.class, "public class Test { private String a,b; }");
assertThat(javaClassSource.getFields()).hasSize(2).extracting(FieldSource::getName).containsExactly("a", "b");
}
}

0 comments on commit b5c08a5

Please sign in to comment.