diff --git a/changelog/@unreleased/pr-558.v2.yml b/changelog/@unreleased/pr-558.v2.yml new file mode 100644 index 0000000..327d453 --- /dev/null +++ b/changelog/@unreleased/pr-558.v2.yml @@ -0,0 +1,5 @@ +type: break +break: + description: Remove square javapoet references + links: + - https://github.com/palantir/goethe/pull/558 diff --git a/goethe/build.gradle b/goethe/build.gradle index 3e9c04f..6cb2f8c 100644 --- a/goethe/build.gradle +++ b/goethe/build.gradle @@ -3,14 +3,12 @@ apply plugin: 'com.palantir.shadow-jar' dependencies { compileOnlyApi 'com.palantir.javapoet:javapoet' - compileOnlyApi 'com.squareup:javapoet' // Avoid conflicts with formatters used elsewhere shadeTransitively 'com.palantir.javaformat:palantir-java-format' shadeTransitively 'com.palantir.javaformat:palantir-java-format-spi' testImplementation 'com.palantir.javapoet:javapoet' - testImplementation 'com.squareup:javapoet' testImplementation 'org.junit.jupiter:junit-jupiter' testImplementation 'org.assertj:assertj-core' testImplementation 'org.mockito:mockito-core' diff --git a/goethe/src/main/java/com/palantir/goethe/Goethe.java b/goethe/src/main/java/com/palantir/goethe/Goethe.java index 25fbb30..62ed07b 100644 --- a/goethe/src/main/java/com/palantir/goethe/Goethe.java +++ b/goethe/src/main/java/com/palantir/goethe/Goethe.java @@ -18,6 +18,7 @@ import com.google.common.base.Preconditions; import com.google.common.base.Splitter; +import com.palantir.javapoet.JavaFile; import java.io.IOException; import java.io.Writer; import java.nio.file.Files; @@ -40,12 +41,12 @@ public final class Goethe { private static final FormatterFacade JAVA_FORMATTER = FormatterFacadeFactory.create(); /** - * Format a {@link com.palantir.javapoet.JavaFile javapoet java file} into a {@link String}. + * Format a {@link JavaFile javapoet java file} into a {@link String}. * * @param file Javapoet file to format * @return Formatted source code */ - public static String formatAsString(com.palantir.javapoet.JavaFile file) { + public static String formatAsString(JavaFile file) { StringBuilder rawSource = new StringBuilder(); try { file.writeTo(rawSource); @@ -57,29 +58,13 @@ public static String formatAsString(com.palantir.javapoet.JavaFile file) { } /** - * Format a {@link com.squareup.javapoet.JavaFile javapoet java file} into a {@link String}. - * - * @param file Javapoet file to format - * @return Formatted source code - */ - public static String formatAsString(com.squareup.javapoet.JavaFile file) { - StringBuilder rawSource = new StringBuilder(); - try { - file.writeTo(rawSource); - return JAVA_FORMATTER.formatSource(file.packageName + '.' + file.typeSpec.name, rawSource.toString()); - } catch (IOException e) { - throw new GoetheException("Formatting failed", e); - } - } - - /** - * Format a {@link com.palantir.javapoet.JavaFile javapoet java file} and write the result to an {@link Filer annotation processing + * Format a {@link JavaFile javapoet java file} and write the result to an {@link Filer annotation processing * filer}. * * @param file Javapoet file to format * @param filer Destination for the formatted file */ - public static void formatAndEmit(com.palantir.javapoet.JavaFile file, Filer filer) { + public static void formatAndEmit(JavaFile file, Filer filer) { String formatted = formatAsString(file); JavaFileObject filerSourceFile = null; @@ -104,37 +89,6 @@ public static void formatAndEmit(com.palantir.javapoet.JavaFile file, Filer file } } - /** - * Format a {@link com.squareup.javapoet.JavaFile javapoet java file} and write the result to an {@link Filer annotation processing - * filer}. - * - * @param file Javapoet file to format - * @param filer Destination for the formatted file - */ - public static void formatAndEmit(com.squareup.javapoet.JavaFile file, Filer filer) { - String formatted = formatAsString(file); - - JavaFileObject filerSourceFile = null; - try { - String className = - file.packageName.isEmpty() ? file.typeSpec.name : file.packageName + "." + file.typeSpec.name; - filerSourceFile = - filer.createSourceFile(className, file.typeSpec.originatingElements.toArray(new Element[0])); - try (Writer writer = filerSourceFile.openWriter()) { - writer.write(formatted); - } - } catch (IOException e) { - if (filerSourceFile != null) { - try { - filerSourceFile.delete(); - } catch (Exception deletionFailure) { - e.addSuppressed(deletionFailure); - } - } - throw new GoetheException("Failed to write formatted code to the filer", e); - } - } - /** * Formats the given Java file and emits it to the appropriate directory under {@code baseDir}. * @@ -142,7 +96,7 @@ public static void formatAndEmit(com.squareup.javapoet.JavaFile file, Filer file * @param baseDir Source set root where the formatted file will be written * @return the new file location */ - public static Path formatAndEmit(com.palantir.javapoet.JavaFile file, Path baseDir) { + public static Path formatAndEmit(JavaFile file, Path baseDir) { String formatted = formatAsString(file); try { Path output = @@ -154,24 +108,6 @@ public static Path formatAndEmit(com.palantir.javapoet.JavaFile file, Path baseD } } - /** - * Formats the given Java file and emits it to the appropriate directory under {@code baseDir}. - * - * @param file Javapoet file to format - * @param baseDir Source set root where the formatted file will be written - * @return the new file location - */ - public static Path formatAndEmit(com.squareup.javapoet.JavaFile file, Path baseDir) { - String formatted = formatAsString(file); - try { - Path output = getFilePath(baseDir, file.packageName, file.typeSpec.name); - Files.writeString(output, formatted); - return output; - } catch (IOException e) { - throw new GoetheException("Failed to write formatted sources", e); - } - } - /** * Returns the full path for the given Java file and Java base dir. In a nutshell, turns packages into directories, * e.g., {@code com.foo.bar.MyClass -> //com/foo/bar/MyClass.java} and creates all directories. diff --git a/goethe/src/test/java/com/palantir/goethe/GoetheSquareTest.java b/goethe/src/test/java/com/palantir/goethe/GoetheSquareTest.java deleted file mode 100644 index 60af4c8..0000000 --- a/goethe/src/test/java/com/palantir/goethe/GoetheSquareTest.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * (c) Copyright 2021 Palantir Technologies Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.palantir.goethe; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.when; - -import com.squareup.javapoet.CodeBlock; -import com.squareup.javapoet.JavaFile; -import com.squareup.javapoet.TypeSpec; -import java.io.IOException; -import java.io.StringWriter; -import java.nio.file.Path; -import javax.annotation.processing.Filer; -import javax.lang.model.element.Element; -import javax.tools.JavaFileObject; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; -import org.mockito.Mockito; - -class GoetheSquareTest { - - @TempDir - Path tempDir; - - @Test - public void testDiagnosticException() { - JavaFile javaFile = JavaFile.builder( - "com.palantir.foo", - TypeSpec.classBuilder("Foo") - .addStaticBlock(CodeBlock.builder() - .addStatement("type oops name = bar") - .build()) - .build()) - .build(); - assertThatThrownBy(() -> Goethe.formatAsString(javaFile)) - .isInstanceOf(GoetheException.class) - .hasMessageContaining("Failed to format 'com.palantir.foo.Foo'") - .hasMessageContaining("';' expected") - .hasMessageContaining( - "" // newline to align the output - + " type oops name = bar;\n" - + " ^"); - } - - @Test - public void testFormatting() { - String padding = "a".repeat(90); - JavaFile javaFile = JavaFile.builder( - "com.palantir.foo", - TypeSpec.classBuilder("Foo") - .addStaticBlock(CodeBlock.builder() - .addStatement("$T.out.println($S)", System.class, padding) - .build()) - .build()) - .build(); - String raw = javaFile.toString(); - String formatted = Goethe.formatAsString(javaFile); - assertThat(formatted) - .as("Expected the formatted output to differ from original") - .isNotEqualTo(raw) - .as("Formatting does not match the expected output, the expectation may need to be updated") - .isEqualTo("package com.palantir.foo;\n\n" - + "import java.lang.System;\n" - + "\n" - + "class Foo {\n" - + " static {\n" - + " System.out.println(\n" - + " \"" + padding + "\");\n" - + " }\n" - + "}\n"); - } - - @Test - public void testFormattingToFiler() throws IOException { - String padding = "a".repeat(90); - Element originatingElement = Mockito.mock(Element.class); - JavaFile javaFile = JavaFile.builder( - "com.palantir.foo", - TypeSpec.classBuilder("Foo") - .addStaticBlock(CodeBlock.builder() - .addStatement("$T.out.println($S)", System.class, padding) - .build()) - .addOriginatingElement(originatingElement) - .build()) - .build(); - StringWriter writer = new StringWriter(); - JavaFileObject javaFileObject = Mockito.mock(JavaFileObject.class); - when(javaFileObject.openWriter()).thenReturn(writer); - Filer filer = Mockito.mock(Filer.class); - // If the originating elements aren't passed through to the Filer, this test will fail with: - // 'Cannot invoke "javax.tools.JavaFileObject.openWriter()" because "filerSourceFile" is null' - when(filer.createSourceFile(eq("com.palantir.foo.Foo"), eq(originatingElement))) - .thenReturn(javaFileObject); - Goethe.formatAndEmit(javaFile, filer); - assertThat(writer.toString()) - .as("Expected the formatted output to differ from original") - .isNotEqualTo(javaFile.toString()) - .as("Expected identical output to 'formatAsString'") - .isEqualTo(Goethe.formatAsString(javaFile)); - } - - @Test - public void testFormattingToDirectory() { - JavaFile javaFile = JavaFile.builder( - "com.palantir.foo", - TypeSpec.classBuilder("Foo") - .addStaticBlock(CodeBlock.builder() - .addStatement("$T.out.println($S)", System.class, "a".repeat(90)) - .build()) - .build()) - .build(); - Path location = Goethe.formatAndEmit(javaFile, tempDir); - assertThat(location.toString()).endsWith("com/palantir/foo/Foo.java"); - assertThat(location) - .as("Expected contents on disk to be formatted") - .hasContent(Goethe.formatAsString(javaFile)); - } -} diff --git a/goethe/src/test/java/com/palantir/goethe/GoethePalantirTest.java b/goethe/src/test/java/com/palantir/goethe/GoetheTest.java similarity index 99% rename from goethe/src/test/java/com/palantir/goethe/GoethePalantirTest.java rename to goethe/src/test/java/com/palantir/goethe/GoetheTest.java index a44ef44..aa35eca 100644 --- a/goethe/src/test/java/com/palantir/goethe/GoethePalantirTest.java +++ b/goethe/src/test/java/com/palantir/goethe/GoetheTest.java @@ -34,7 +34,7 @@ import org.junit.jupiter.api.io.TempDir; import org.mockito.Mockito; -class GoethePalantirTest { +class GoetheTest { @TempDir Path tempDir; diff --git a/versions.lock b/versions.lock index e9a4570..5fbae11 100644 --- a/versions.lock +++ b/versions.lock @@ -14,7 +14,6 @@ com.google.j2objc:j2objc-annotations:3.0.0 (1 constraints: 150aeab4) com.palantir.javaformat:palantir-java-format:2.50.0 (1 constraints: 39053f3b) com.palantir.javaformat:palantir-java-format-spi:2.50.0 (2 constraints: 42183e8e) com.palantir.javapoet:javapoet:0.1.0 (1 constraints: 0305ee35) -com.squareup:javapoet:1.13.0 (1 constraints: 3705323b) org.checkerframework:checker-qual:3.43.0 (1 constraints: 4c0a4abf) org.functionaljava:functionaljava:4.8 (1 constraints: 81129900) diff --git a/versions.props b/versions.props index 379dcdd..7ca5b69 100644 --- a/versions.props +++ b/versions.props @@ -1,5 +1,4 @@ com.fasterxml.jackson.core:jackson-databind = 2.18.1 -com.squareup:javapoet = 1.13.0 com.palantir.javapoet:javapoet = 0.1.0 com.palantir.javaformat:* = 2.50.0 com.google.guava:guava = 33.3.1-jre