From 9e8bb35d02e69753ccdb108cfddf99b29f3499fb Mon Sep 17 00:00:00 2001 From: Martin Wittlinger Date: Wed, 17 Apr 2024 15:42:41 +0200 Subject: [PATCH] feat: Add newline at EOF and refactor type calculation (#5747) --- .../reflect/visitor/DefaultJavaPrettyPrinter.java | 12 ++++++++---- .../java/spoon/support/JavaOutputProcessorTest.java | 2 +- src/test/java/spoon/test/ctClass/CtClassTest.java | 6 +++--- src/test/java/spoon/test/field/FieldTest.java | 2 +- src/test/java/spoon/test/imports/ImportTest.java | 2 +- .../test/prettyprinter/DefaultPrettyPrinterTest.java | 6 +++--- .../spoon/test/module/simple_module/module-info-tpl | 1 + 7 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java index 969e27bce8b..19be75b642c 100644 --- a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java +++ b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java @@ -1212,6 +1212,11 @@ public void visitCtCompilationUnit(CtCompilationUnit compilationUnit) { } finally { this.sourceCompilationUnit = outerCompilationUnit; } + // by convention, we add a newline at the end of the file + // we guard this with a check to avoid adding a newline if there is already one + if (!getResult().endsWith(System.lineSeparator())) { + printer.writeln(); + } } protected ModelList getImports(CtCompilationUnit compilationUnit) { @@ -2182,9 +2187,8 @@ public void reset() { @Override public void calculate(CtCompilationUnit sourceCompilationUnit, List> types) { reset(); - if (types.isEmpty()) { - // is package-info.java, we cannot call types.get(0) in the then branch - } else { + // if empty => is package-info.java, we cannot call types.get(0) in the then branch + if (!types.isEmpty()) { CtType type = types.get(0); if (sourceCompilationUnit == null) { sourceCompilationUnit = type.getFactory().CompilationUnit().getOrCreate(type); @@ -2302,7 +2306,7 @@ protected boolean isMinimizeRoundBrackets() { * When set to true, this activates round bracket minimization for expressions. This means that * the printer will attempt to only write round brackets strictly necessary for preserving * syntactical structure (and by extension, semantics). - * + * * As an example, the expression 1 + 2 + 3 + 4 is written as * ((1 + 2) + 3) + 4 without round bracket minimization, but entirely without * parentheses when minimization is enabled. However, an expression 1 + 2 + (3 + 4) diff --git a/src/test/java/spoon/support/JavaOutputProcessorTest.java b/src/test/java/spoon/support/JavaOutputProcessorTest.java index ada432e4798..29e9149fea5 100644 --- a/src/test/java/spoon/support/JavaOutputProcessorTest.java +++ b/src/test/java/spoon/support/JavaOutputProcessorTest.java @@ -48,7 +48,7 @@ void testCreateJavaFileAssertFileEncodingChanged(@TempDir File tempDir) throws E Factory factory = launcher.getFactory(); // use characters encoded differently in ISO-8859-01 and UTFs - String code = "class ÈmptyÇlàss {}"; + String code = "class ÈmptyÇlàss {}" + System.lineSeparator(); CtClass ctClass = Launcher.parseClass(code); JavaOutputProcessor javaOutputProcessor = new JavaOutputProcessor(); diff --git a/src/test/java/spoon/test/ctClass/CtClassTest.java b/src/test/java/spoon/test/ctClass/CtClassTest.java index 0ca555cdf2e..de048253789 100644 --- a/src/test/java/spoon/test/ctClass/CtClassTest.java +++ b/src/test/java/spoon/test/ctClass/CtClassTest.java @@ -261,7 +261,7 @@ public void toStringWithImports() { " return 0;" + newLine + " }" + newLine + " }.compare(1, 2);" + newLine + - "}", aClass2.toStringWithImports()); + "}" + newLine, aClass2.toStringWithImports()); // contract: a class can be printed with full context in autoimports aClass2.getFactory().getEnvironment().setAutoImports(true); @@ -277,12 +277,12 @@ public void toStringWithImports() { " return 0;" + newLine + " }" + newLine + " }.compare(1, 2);" + newLine + - "}", aClass2.toStringWithImports()); + "}" + newLine, aClass2.toStringWithImports()); // contract: toStringWithImports works with a new class with no position assertEquals("package foo;" + newLine + "import java.io.File;" + newLine + - "class Bar extends File {}", launcher2.getFactory().createClass("foo.Bar").setSuperclass(launcher2.getFactory().Type().get(File.class).getReference()).toStringWithImports()); + "class Bar extends File {}" + newLine, launcher2.getFactory().createClass("foo.Bar").setSuperclass(launcher2.getFactory().Type().get(File.class).getReference()).toStringWithImports()); } @Test diff --git a/src/test/java/spoon/test/field/FieldTest.java b/src/test/java/spoon/test/field/FieldTest.java index d3257eb749c..df84452523d 100644 --- a/src/test/java/spoon/test/field/FieldTest.java +++ b/src/test/java/spoon/test/field/FieldTest.java @@ -216,7 +216,7 @@ public void bugAfterRefactoringImports() { "import java.io.File;\n" + "class A {\n" + " public static final String separator = File.separator;\n" + - "}", klass.toStringWithImports()); + "}\n", klass.toStringWithImports()); } diff --git a/src/test/java/spoon/test/imports/ImportTest.java b/src/test/java/spoon/test/imports/ImportTest.java index 2e0be0a815d..4d885791f76 100644 --- a/src/test/java/spoon/test/imports/ImportTest.java +++ b/src/test/java/spoon/test/imports/ImportTest.java @@ -1858,7 +1858,7 @@ void testAutoimportConflictingSimpleNames() { " System.out.println(Locale.HELLO);\n" + " System.out.println(java.util.Locale.GERMANY);\n" + " }\n" + - "}", + "}\n", user.toStringWithImports() ); } diff --git a/src/test/java/spoon/test/prettyprinter/DefaultPrettyPrinterTest.java b/src/test/java/spoon/test/prettyprinter/DefaultPrettyPrinterTest.java index 716c7b40f44..a8a41a070ec 100644 --- a/src/test/java/spoon/test/prettyprinter/DefaultPrettyPrinterTest.java +++ b/src/test/java/spoon/test/prettyprinter/DefaultPrettyPrinterTest.java @@ -104,7 +104,7 @@ public void testPrintClassWithStaticImportOfMethod() { " findFirst();" + nl + " new ClassWithStaticMethod().notStaticFindFirst();" + nl + " }" + nl + - "}"; + "}" + nl; final CtClass classUsingStaticMethod = (CtClass) factory.Type().get(ClassUsingStaticMethod.class); final String printed = factory.getEnvironment().createPrettyPrinter().printTypes(classUsingStaticMethod); @@ -343,7 +343,7 @@ public void printClassCreatedWithSpoon() throws Exception { File javaFile = new File(pathname); assertTrue(javaFile.exists()); - assertEquals("package foo;" + nl + "class Bar {}", + assertEquals("package foo;" + nl + "class Bar {}" + nl, Files.readString(javaFile.toPath(), StandardCharsets.UTF_8)); } @@ -464,7 +464,7 @@ public void testElseIf() { " } else if (a == 3) {\n" + " }\n" + " }\n" + - "}"; + "}\n"; assertEquals(expected, result); } diff --git a/src/test/resources/spoon/test/module/simple_module/module-info-tpl b/src/test/resources/spoon/test/module/simple_module/module-info-tpl index 8797f5ddf0e..c848daae83e 100644 --- a/src/test/resources/spoon/test/module/simple_module/module-info-tpl +++ b/src/test/resources/spoon/test/module/simple_module/module-info-tpl @@ -7,3 +7,4 @@ module simple_module { provides com.greetings.pkg.ConsumedService with com.greetings.pkg.ProvidedClass1, com.greetings.otherpkg.ProvidedClass2; provides java.logging.Service with com.greetings.logging.Logger; } +