diff --git a/src/test/java/spoon/reflect/visitor/DefaultJavaPrettyPrinterTest.java b/src/test/java/spoon/reflect/visitor/DefaultJavaPrettyPrinterTest.java index 2cc06642dec..43c5bb4018d 100644 --- a/src/test/java/spoon/reflect/visitor/DefaultJavaPrettyPrinterTest.java +++ b/src/test/java/spoon/reflect/visitor/DefaultJavaPrettyPrinterTest.java @@ -27,6 +27,7 @@ import spoon.reflect.path.CtRole; import spoon.reflect.reference.CtArrayTypeReference; import spoon.reflect.reference.CtTypeReference; +import spoon.reflect.visitor.filter.TypeFilter; import spoon.support.reflect.reference.CtArrayTypeReferenceImpl; import spoon.test.GitHubIssue; import spoon.test.SpoonTestHelpers; @@ -324,4 +325,19 @@ void testKeepGenericType(Factory factory) { assertThat(printed, containsRegexMatch("List<.*List<\\? extends T>>")); assertThat(printed, containsRegexMatch("List<.*List<\\? super T>>")); } + + @GitHubIssue(issueNumber = 4881, fixed = true) + void bracketsShouldBeMinimallyPrintedForTypeCastOnFieldRead() { + // contract: the brackets should be minimally printed for type cast on field read + // arrange + Launcher launcher = createLauncherWithOptimizeParenthesesPrinter(); + launcher.addInputResource("src/test/resources/printer-test/TypeCastOnFieldRead.java"); + + // act + CtModel model = launcher.buildModel(); + + // assert + CtLocalVariable localVariable = model.getElements(new TypeFilter<>(CtLocalVariable.class)).get(0); + assertThat(localVariable.toString(), equalTo("int myInt = (int) myDouble")); + } } diff --git a/src/test/resources/printer-test/TypeCastOnFieldRead.java b/src/test/resources/printer-test/TypeCastOnFieldRead.java new file mode 100644 index 00000000000..a3d5f19616e --- /dev/null +++ b/src/test/resources/printer-test/TypeCastOnFieldRead.java @@ -0,0 +1,6 @@ +public class TypeCastOnFieldRead { + double myDouble = 0.0; + public void where() { + int myInt = (int) myDouble; + } +}