Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

review: feat: Add newline at file end and refactor type calculation #5747

Merged
merged 11 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<CtImport> getImports(CtCompilationUnit compilationUnit) {
Expand Down Expand Up @@ -2182,9 +2187,8 @@ public void reset() {
@Override
public void calculate(CtCompilationUnit sourceCompilationUnit, List<CtType<?>> 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);
Expand Down Expand Up @@ -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 <code>1 + 2 + 3 + 4</code> is written as
* <code>((1 + 2) + 3) + 4</code> without round bracket minimization, but entirely without
* parentheses when minimization is enabled. However, an expression <code>1 + 2 + (3 + 4)</code>
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/spoon/support/JavaOutputProcessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/spoon/test/ctClass/CtClassTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/spoon/test/field/FieldTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());

}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/spoon/test/imports/ImportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1858,7 +1858,7 @@ void testAutoimportConflictingSimpleNames() {
" System.out.println(Locale.HELLO);\n" +
" System.out.println(java.util.Locale.GERMANY);\n" +
" }\n" +
"}",
"}\n",
user.toStringWithImports()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -464,7 +464,7 @@ public void testElseIf() {
" } else if (a == 3) {\n" +
" }\n" +
" }\n" +
"}";
"}\n";
assertEquals(expected, result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}