Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix pretty-printing of special characters in literals (#3209)
- Loading branch information
Showing
3 changed files
with
47 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package spoon.test.literal; | ||
|
||
import org.junit.Test; | ||
import spoon.Launcher; | ||
import spoon.processing.AbstractProcessor; | ||
import spoon.reflect.CtModel; | ||
import spoon.reflect.code.CtBinaryOperator; | ||
import spoon.reflect.code.CtCodeElement; | ||
import spoon.reflect.code.CtLiteral; | ||
import spoon.reflect.declaration.CtField; | ||
import spoon.reflect.visitor.filter.TypeFilter; | ||
import spoon.support.compiler.VirtualFile; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
// bug case kindly provided by @Banbury | ||
// in https://github.com/INRIA/spoon/issues/3203 | ||
public class UnicodeBugTest { | ||
|
||
@Test | ||
public void testUnicodeBug() { | ||
Launcher launcher = new Launcher(); | ||
launcher.addInputResource(new VirtualFile("class A { String s = \"Hellö \" + \"Wörld!\"; }")); | ||
launcher.buildModel(); | ||
launcher.addProcessor(new StringConcatProcessor()); | ||
launcher.process(); | ||
CtModel model = launcher.getModel(); | ||
CtField<?> field = model.getElements(new TypeFilter<>(CtField.class)).get(0); | ||
assertEquals(field.toString(), "java.lang.String s = \"Hellö Wörld!\";"); | ||
} | ||
|
||
private class StringConcatProcessor extends AbstractProcessor<CtLiteral<?>> { | ||
@Override | ||
public boolean isToBeProcessed(CtLiteral<?> candidate) { | ||
return candidate.getType().isSubtypeOf(getFactory().Type().STRING) && | ||
candidate.getParent() instanceof CtBinaryOperator; | ||
} | ||
|
||
@Override | ||
public void process(CtLiteral<?> element) { | ||
CtCodeElement s = ((CtBinaryOperator<?>) element.getParent()).partiallyEvaluate(); | ||
element.getParent().replace(s); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters