-
-
Notifications
You must be signed in to change notification settings - Fork 342
/
DefaultJavaPrettyPrinterTest.java
428 lines (376 loc) · 20.3 KB
/
DefaultJavaPrettyPrinterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package spoon.reflect.visitor;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.ValueSource;
import spoon.Launcher;
import spoon.SpoonException;
import spoon.SpoonModelBuilder;
import spoon.compiler.SpoonResourceHelper;
import spoon.reflect.CtModel;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtExecutableReferenceExpression;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtLiteral;
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.code.CtNewArray;
import spoon.reflect.code.CtStatement;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtCompilationUnit;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.factory.Factory;
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.SpoonTestHelpers;
import spoon.testing.utils.GitHubIssue;
import spoon.testing.utils.ModelTest;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static spoon.test.SpoonTestHelpers.containsRegexMatch;
public class DefaultJavaPrettyPrinterTest {
@ParameterizedTest
@ValueSource(strings = {
"1 + 2 + 3",
"1 + (2 + 3)",
"1 + 2 + -3",
"1 + 2 + -(2 + 3)",
"\"Sum: \" + (1 + 2)",
"\"Sum: \" + 1 + 2",
"-(1 + 2 + 3)",
"true || true && false",
"(true || false) && false",
"1 | 2 | 3",
"1 | (2 | 3)",
"1 | 2 & 3",
"(1 | 2) & 3",
"1 | 2 ^ 3",
"(1 | 2) ^ 3",
"((int) (1 + 2)) * 3",
"(int) (int) (1 + 1)",
})
public void testParenOptimizationCorrectlyPrintsParenthesesForExpressions(String rawExpression) {
// contract: When input expressions are minimally parenthesized, pretty-printed output
// should match the input
CtExpression<?> expr = createLauncherWithOptimizeParenthesesPrinter()
.getFactory().createCodeSnippetExpression(rawExpression).compile();
assertThat(expr.toString(), equalTo(rawExpression));
}
@ParameterizedTest
@ValueSource(strings = {
"int sum = 1 + 2 + 3",
"java.lang.String s = \"Sum: \" + (1 + 2)",
"java.lang.String s = \"Sum: \" + 1 + 2",
"java.lang.System.out.println(\"1\" + \"2\" + \"3\" + \"4\")",
"int myInt = (int) 0.0",
"int myInt = (int) (float) 0.0",
})
public void testParenOptimizationCorrectlyPrintsParenthesesForStatements(String rawStatement) {
// contract: When input expressions as part of statements are minimally parenthesized,
// pretty-printed output should match the input
CtStatement statement = createLauncherWithOptimizeParenthesesPrinter()
.getFactory().createCodeSnippetStatement(rawStatement).compile();
assertThat(statement.toString(), equalTo(rawStatement));
}
private static Launcher createLauncherWithOptimizeParenthesesPrinter() {
Launcher launcher = new Launcher();
launcher.getEnvironment().setPrettyPrinterCreator(() -> {
DefaultJavaPrettyPrinter printer = new DefaultJavaPrettyPrinter(launcher.getEnvironment());
printer.setMinimizeRoundBrackets(true);
return printer;
});
return launcher;
}
@Test
void testAutoImportPrinterDoesNotImportFunctionalInterfaceTargetedInLambda() {
// contract: The auto-import printer should not import functional interfaces that are
// targeted in lambdas, but are not explicitly referenced anywhere
Launcher launcher = new Launcher();
launcher.addInputResource("src/test/resources/target-functional-interface-in-lambda");
launcher.buildModel();
CtCompilationUnit cu = launcher.getFactory().Type().get("TargetsFunctionalInterface")
.getPosition().getCompilationUnit();
PrettyPrinter autoImportPrettyPrinter = launcher.getEnvironment().createPrettyPrinterAutoImport();
String output = autoImportPrettyPrinter.prettyprint(cu);
assertThat(output, not(containsString("import java.util.function.IntFunction;")));
}
@Test
void testLocalTypesPrintedWithoutLeadingDigits() {
// contract: leading digits should be stripped from simple names when printing
Launcher launcher = new Launcher();
launcher.getEnvironment().setComplianceLevel(16);
launcher.addInputResource("src/test/resources/localtypes");
launcher.buildModel();
CtCompilationUnit cu = launcher.getFactory().Type().get("LocalTypesHolder")
.getPosition().getCompilationUnit();
String output = cu.prettyprint();
// local classes will always have a leading space, without leading digits
assertThat(output, containsString(" MyClass"));
assertThat(output, containsString(" MyEnum"));
assertThat(output, containsString(" MyInterface"));
// the code does not contain a 1, which would be the prefix of the local types' binary names
// in the given code snippet
assertThat(output, not(containsString("1")));
}
@Test
void testEmptyIfBlocksArePrintedWithoutError() {
// contract: empty if blocks don't crash the DJJP
Launcher launcher = new Launcher();
var ctIf = launcher.getFactory().createIf()
.setThenStatement(launcher.getFactory().createBlock())
.setElseStatement(launcher.getFactory().createBlock());
assertDoesNotThrow(() -> ctIf.toString());
}
@Test
void testEmptyIfBlocksWithCommentsArePrintedWithoutError() {
// contract: empty if blocks don't crash the DJJP
Launcher launcher = new Launcher();
CtBlock<Object> thenBlock = launcher.getFactory().createBlock();
CtBlock<Object> elseBlock = launcher.getFactory().createBlock();
thenBlock.addComment(launcher.getFactory().createComment().setContent("then"));
elseBlock.addComment(launcher.getFactory().createComment().setContent("else"));
var ctIf = launcher.getFactory().createIf()
.setThenStatement(thenBlock)
.setElseStatement(elseBlock);
assertDoesNotThrow(() -> ctIf.toString());
}
@ParameterizedTest
@ValueSource(strings = {
"int a = (new int[10])[1];",
"java.lang.Object b = (new java.lang.Object[6])[4];",
"double c = (new double[1][1][1])[0][0][0];",
"boolean d = new boolean[]{ true, false }[0];",
"float e = (new float[3][])[0][0];", // compiles, but throws a NPE on execution
"(new byte[8])[3] = 64;",
"(new byte[8][])[3] = new byte[16];",
})
void testPrintNewArrayWithImmediateAccess(String line) {
// contract: immediate access to a new array must be surrounded by parentheses if
// no elements are passed directly
String code = SpoonTestHelpers.wrapLocal(line);
CtModel model = SpoonTestHelpers.createModelFromString(code, 8);
CtType<?> first = model.getAllTypes().iterator().next();
assertThat(first.toString(), containsString(line));
}
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
void testPrintMethodReferenceTypeParameters() {
// contract: type parameters of method references are printed
Launcher launcher = new Launcher();
Factory factory = launcher.getFactory();
CtTypeReference<?> typeReference = factory.Type().createReference("Arrays");
CtExecutableReferenceExpression exeExpression = factory.createExecutableReferenceExpression();
exeExpression.setExecutable(factory.createExecutableReference());
exeExpression.getExecutable().setSimpleName("binarySearch");
exeExpression.setTarget(factory.createTypeAccess(typeReference));
// if no type parameters are given, no < and no > should be printed
assertThat(exeExpression.toString(), not(anyOf(containsString("<"), containsString(">"))));
exeExpression.getExecutable().addActualTypeArgument(factory.Type().integerType());
// with type parameters, the < and > should be printed
assertThat(exeExpression.toString(), allOf(containsString("<"), containsString(">")));
// the type parameter should appear
assertThat(exeExpression.toString(), containsString("Integer"));
exeExpression.getExecutable().addActualTypeArgument(factory.Type().integerType());
// more than one parameter type should be separated (with .* to allow imports)
assertThat(exeExpression.toString(), containsRegexMatch("<(.*)Integer, (.*)Integer>"));
// remove type arguments again, we want to try something else
exeExpression.getExecutable().setActualTypeArguments(null);
// we want to have a bit more complex type and construct a fake Comparable<Integer, Comhyperbola<Integer>>
CtTypeReference<?> complexTypeReference = factory.Type().integerType().getSuperInterfaces().stream()
.filter(t -> t.getSimpleName().equals("Comparable"))
.findAny()
.orElseThrow();
complexTypeReference.addActualTypeArgument(complexTypeReference.clone().setSimpleName("Comhyperbola"));
exeExpression.getExecutable().addActualTypeArgument(complexTypeReference);
assertThat(exeExpression.toString(), containsRegexMatch("<(.*)Comparable<(.*)Integer, (.*)Comhyperbola<(.*)Integer>>>"));
}
@ParameterizedTest
@ArgumentsSource(SealedTypesProvider.class)
void testPrintSealedTypes(CtType<?> sealedType, List<String> explicitPermitted) {
// contract: sealed types are printed correctly
String printed = sealedType.toString();
// the sealed keyword is always required
assertThat(printed, containsString("sealed"));
if (explicitPermitted.isEmpty()) {
// the permits keyword should only exist if explicit permitted types are printed
assertThat(printed, not(containsString("permits")));
} else {
assertThat(printed, containsString("permits"));
for (String permitted : explicitPermitted) {
assertThat(printed, containsRegexMatch("\\s" + permitted));
}
}
}
@Test
void testPrintNonSealedTypes() {
// contract: the non-sealed modifier is printed
Launcher launcher = new Launcher();
CtClass<?> ctClass = launcher.getFactory().Class().create("MyClass");
ctClass.addModifier(ModifierKind.NON_SEALED);
assertThat(ctClass.toString(), containsString("non-sealed "));
}
static class SealedTypesProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) {
Launcher launcher = new Launcher();
launcher.getEnvironment().setComplianceLevel(17);
launcher.addInputResource("src/test/resources/sealedclasses");
launcher.buildModel();
Factory factory = launcher.getFactory();
return Stream.of(
Arguments.of(factory.Type().get("SealedClassWithPermits"), List.of("ExtendingClass", "OtherExtendingClass")),
Arguments.of(factory.Type().get("SealedInterfaceWithPermits"), List.of("ExtendingClass", "OtherExtendingClass")),
Arguments.of(factory.Type().get("SealedClassWithNestedSubclasses"), List.of()), // implicit
Arguments.of(factory.Type().get("SealedInterfaceWithNestedSubclasses"), List.of()) // implicit
);
}
}
@Nested
class SquareBracketsForArrayInitialization_ArrayIsBuiltUsingFactoryMethods {
@Test
@GitHubIssue(issueNumber = 4887, fixed = true)
void bracketsShouldBeAttachedToTypeByDefault() {
// contract: the square brackets should be attached to type by default when array is built using factory methods
// arrange
Launcher launcher = new Launcher();
Factory factory = launcher.getFactory();
CtArrayTypeReference<Integer> arrayTypeReference = factory.createArrayTypeReference();
arrayTypeReference.setComponentType(factory.Type().integerPrimitiveType());
CtNewArray<Integer> newArray = factory.createNewArray();
newArray.setValueByRole(CtRole.TYPE, arrayTypeReference);
List<CtLiteral<Integer>> elements = new ArrayList<>(List.of(factory.createLiteral(3)));
newArray.setValueByRole(CtRole.EXPRESSION, elements);
CtLocalVariable<Integer> localVariable = factory.createLocalVariable(arrayTypeReference, "intArray", newArray);
// act
String actualStringRepresentation = localVariable.toString();
// assert
assertThat(actualStringRepresentation, equalTo("int[] intArray = new int[]{ 3 }"));
}
@Test
void bracketsShouldBeAttachedToIdentifierIfSpecified() {
// contract: the square brackets should be attached to identifier if specified explicitly
// arrange
Launcher launcher = new Launcher();
Factory factory = launcher.getFactory();
CtArrayTypeReference<CtElement> arrayTypeReference = factory.createArrayTypeReference();
CtTypeReference<CtElement> arrayType = factory.Type().createReference(CtElement.class);
arrayTypeReference.setComponentType(arrayType);
CtNewArray<CtElement> newArray = factory.createNewArray();
newArray.setValueByRole(CtRole.TYPE, arrayTypeReference);
List<CtElement> elements = new ArrayList<>(List.of(factory.createLiteral(1.0f)));
newArray.setValueByRole(CtRole.EXPRESSION, elements);
CtLocalVariable<CtElement> localVariable = factory.createLocalVariable(arrayTypeReference, "spoonElements", newArray);
// act
((CtArrayTypeReferenceImpl<?>) arrayTypeReference).setDeclarationKind(CtArrayTypeReferenceImpl.DeclarationKind.IDENTIFIER);
String actualStringRepresentation = localVariable.toString();
// assert
assertThat(actualStringRepresentation,
equalTo("spoon.reflect.declaration.CtElement spoonElements[] = new spoon.reflect.declaration.CtElement[]{ 1.0F }"));
}
}
@ModelTest(value = "src/test/resources/patternmatching/InstanceofGenerics.java", complianceLevel = 16)
void testKeepGenericType(Factory factory) {
// contract: generic type parameters can appear in instanceof expressions if they are only carried over
CtType<?> x = factory.Type().get("InstanceofGenerics");
String printed = x.toString();
assertThat(printed, containsString("Set<T>"));
assertThat(printed, containsString("List<T> list"));
assertThat(printed, containsRegexMatch("Collection<.*String>"));
assertThat(printed, containsRegexMatch("List<.*List<T>>"));
assertThat(printed, containsRegexMatch("List<.*List<\\? extends T>>"));
assertThat(printed, containsRegexMatch("List<.*List<\\? super T>>"));
}
@Test
@GitHubIssue(issueNumber = 4881, fixed = true)
void bracketsShouldBeMinimallyPrintedForTypeCastOnFieldRead() throws FileNotFoundException {
// 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");
Launcher launcherForCompilingPrettyPrintedString = createLauncherWithOptimizeParenthesesPrinter();
// act
CtModel model = launcher.buildModel();
launcher.prettyprint();
SpoonModelBuilder spoonModelBuilder = launcherForCompilingPrettyPrintedString.createCompiler(SpoonResourceHelper.resources("spooned/TypeCastOnFieldRead.java"));
// assert
assertThat(spoonModelBuilder.build(), equalTo(true));
CtLocalVariable<Integer> localVariable = model.getElements(new TypeFilter<>(CtLocalVariable.class)).get(0);
assertThat(localVariable.toString(), equalTo("int myInt = (int) myDouble"));
CtLocalVariable<Integer> localVariable2 = model.getElements(new TypeFilter<>(CtLocalVariable.class)).get(1);
assertThat(localVariable2.toString(), equalTo("int myInt2 = ((java.lang.Double) myDouble).intValue()"));
CtLocalVariable<Integer> localVariable3 = model.getElements(new TypeFilter<>(CtLocalVariable.class)).get(2);
assertThat(localVariable3.toString(), equalTo("double withoutTypeCast = myDoubleObject.doubleValue()"));
}
@Test
void bracketsShouldBeMinimallyPrintedOnShadowedFields() throws FileNotFoundException {
// contract: the brackets should be minimally printed for type cast on shadowed field read
// arrange
Launcher launcher = createLauncherWithOptimizeParenthesesPrinter();
launcher.addInputResource("src/test/resources/printer-test/ShadowFieldRead.java");
Launcher launcherForCompilingPrettyPrintedString = createLauncherWithOptimizeParenthesesPrinter();
// act
CtModel model = launcher.buildModel();
launcher.prettyprint();
SpoonModelBuilder spoonModelBuilder = launcherForCompilingPrettyPrintedString.createCompiler(SpoonResourceHelper.resources("spooned/ShadowFieldRead.java", "spooned/A.java", "spooned/C.java"));
// assert
assertThat(spoonModelBuilder.build(), equalTo(true));
CtLocalVariable<Integer> localVariable = model.getElements(new TypeFilter<>(CtLocalVariable.class)).get(1);
assertThat(localVariable.toString(), equalTo("int fieldReadOfA = ((A) c).a.i"));
}
@ParameterizedTest(name = "Printing literal ''{0}'' throws an error")
@ValueSource(doubles = {
Double.NEGATIVE_INFINITY,
Double.POSITIVE_INFINITY,
Double.NaN
})
void throwsExceptionWhenPrintingInvalidFloatingLiteral(double value) {
// contract: Printing invalid floating literals throws an exception
Factory factory = new Launcher().getFactory();
assertThrows(SpoonException.class, () -> factory.createLiteral(value).toString());
assertThrows(SpoonException.class, () -> factory.createLiteral((float) value).toString());
}
@ModelTest("src/test/java/spoon/reflect/visitor/DefaultJavaPrettyPrinterTest.java")
void printAnnotationsInOrphanTypeReference(Factory factory) {
// contract: Spoon should print annotations for orphaned type references
// Used by the test
java.lang.@TypeUseAnnotation String ignored;
CtTypeReference<?> type = factory.Type()
.get(getClass().getName())
.getMethodsByName("printAnnotationsInOrphanTypeReference")
.get(0)
.getElements(new TypeFilter<>(CtLocalVariable.class))
.get(0)
.getType();
assertEquals(
"java.lang.@spoon.reflect.visitor.DefaultJavaPrettyPrinterTest.TypeUseAnnotation String",
type.toString().replace(System.lineSeparator(), " ")
);
}
@Target({ElementType.TYPE_USE})
@Retention(RetentionPolicy.SOURCE)
private @interface TypeUseAnnotation {
}
}