Skip to content

Commit

Permalink
Merge pull request #547 from GerardPaligot/fix_annotation_loop
Browse files Browse the repository at this point in the history
  • Loading branch information
monperrus committed Feb 29, 2016
2 parents 3b6a57d + fe11249 commit 8428851
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1440,7 +1440,9 @@ public <T> DefaultJavaPrettyPrinter writeLocalVariable(CtLocalVariable<T> localV
}

public <T> void visitCtLocalVariable(CtLocalVariable<T> localVariable) {
enterCtStatement(localVariable);
if (!context.noTypeDecl) {
enterCtStatement(localVariable);
}
writeLocalVariable(localVariable);
}

Expand Down
36 changes: 36 additions & 0 deletions src/test/java/spoon/test/annotation/AnnotationLoopTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package spoon.test.annotation;

import org.junit.Test;
import spoon.reflect.code.CtFor;
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.declaration.CtType;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.test.annotation.testclasses.Pozole;
import spoon.testing.utils.ModelUtils;

import static org.junit.Assert.assertEquals;

public class AnnotationLoopTest {
@Test
public void testAnnotationDeclaredInForInit() throws Exception {
final CtType<Pozole> aPozole = ModelUtils.buildClass(Pozole.class);

final CtFor aLoop = aPozole.getMethod("cook").getElements(new TypeFilter<>(CtFor.class)).get(0);
assertEquals(3, aLoop.getForInit().size());
assertEquals(SuppressWarnings.class, aLoop.getForInit().get(0).getAnnotations().get(0).getAnnotationType().getActualClass());
assertEquals(SuppressWarnings.class, aLoop.getForInit().get(1).getAnnotations().get(0).getAnnotationType().getActualClass());
assertEquals(SuppressWarnings.class, aLoop.getForInit().get(2).getAnnotations().get(0).getAnnotationType().getActualClass());

assertEquals("u", ((CtLocalVariable) aLoop.getForInit().get(0)).getSimpleName());
assertEquals("p", ((CtLocalVariable) aLoop.getForInit().get(1)).getSimpleName());
assertEquals("e", ((CtLocalVariable) aLoop.getForInit().get(2)).getSimpleName());

assertEquals(aPozole.getFactory().Type().STRING, ((CtLocalVariable) aLoop.getForInit().get(0)).getType());
assertEquals(aPozole.getFactory().Type().STRING, ((CtLocalVariable) aLoop.getForInit().get(1)).getType());
assertEquals(aPozole.getFactory().Type().STRING, ((CtLocalVariable) aLoop.getForInit().get(2)).getType());

final String nl = System.lineSeparator();
final String expected = "for (@java.lang.SuppressWarnings(value = \"rawtypes\")" + nl + "java.lang.String u = \"\", p = \"\", e = \"\" ; u != e ; u = p , p = \"\") {" + nl + "}";
assertEquals(expected, aLoop.toString());
}
}
8 changes: 8 additions & 0 deletions src/test/java/spoon/test/annotation/testclasses/Pozole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package spoon.test.annotation.testclasses;

public class Pozole {
public void cook() {
for (@SuppressWarnings("rawtypes") String u = "", p = "", e = ""; u != e; u = p, p = "") {
}
}
}

0 comments on commit 8428851

Please # to comment.