-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
proof-of-concept: ideally we'd leverage palantir/palantir-java-format…
- Loading branch information
1 parent
45b5308
commit e23da71
Showing
5 changed files
with
198 additions
and
52 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
goethe/src/main/java/com/palantir/goethe/BootstrappingFormatterFacade.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* (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 com.google.common.io.ByteStreams; | ||
import com.palantir.javaformat.java.Main; | ||
import com.squareup.javapoet.JavaFile; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
final class BootstrappingFormatterFacade implements FormatterFacade { | ||
|
||
@Override | ||
public String formatSource(JavaFile file) throws GoetheException { | ||
try { | ||
Process process = new ProcessBuilder( | ||
new File(System.getProperty("java.home"), "bin/java").getAbsolutePath(), | ||
// Exports | ||
"--add-exports", | ||
"jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED", | ||
"--add-exports", | ||
"jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED", | ||
"--add-exports", | ||
"jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED", | ||
"--add-exports", | ||
"jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED", | ||
"--add-exports", | ||
"jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED", | ||
// Classpath | ||
"-cp", | ||
System.getProperty("java.class.path"), | ||
// Main class | ||
Main.class.getName(), | ||
// Args | ||
"--palantir", | ||
"--skip-reflowing-long-strings", | ||
"-") | ||
.start(); | ||
StringBuilder rawSource = new StringBuilder(); | ||
file.writeTo(rawSource); | ||
try (OutputStream outputStream = process.getOutputStream()) { | ||
outputStream.write(rawSource.toString().getBytes(StandardCharsets.UTF_8)); | ||
} | ||
byte[] data; | ||
try (InputStream inputStream = process.getInputStream()) { | ||
data = ByteStreams.toByteArray(inputStream); | ||
} | ||
int exitStatus = process.waitFor(); | ||
if (exitStatus != 0) { | ||
throw new GoetheException("Bootstrapped formatter exited non-zero: " + exitStatus + " with output:\n" | ||
+ getErrorOutput(process)); | ||
} | ||
return new String(data, StandardCharsets.UTF_8); | ||
} catch (IOException | InterruptedException e) { | ||
throw new GoetheException("Failed to bootstrap jdk", e); | ||
} | ||
} | ||
|
||
private static String getErrorOutput(Process process) { | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
try (InputStream inputStream = process.getErrorStream()) { | ||
ByteStreams.copy(inputStream, baos); | ||
} catch (IOException | RuntimeException ignored) { | ||
// continue | ||
} | ||
return baos.toString(StandardCharsets.UTF_8); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
goethe/src/main/java/com/palantir/goethe/DirectFormatterFacade.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* (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 com.google.common.base.CharMatcher; | ||
import com.google.common.base.Splitter; | ||
import com.google.common.base.Strings; | ||
import com.palantir.javaformat.java.Formatter; | ||
import com.palantir.javaformat.java.FormatterDiagnostic; | ||
import com.palantir.javaformat.java.FormatterException; | ||
import com.palantir.javaformat.java.JavaFormatterOptions; | ||
import com.squareup.javapoet.JavaFile; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
final class DirectFormatterFacade implements FormatterFacade { | ||
|
||
private final Formatter formatter = Formatter.createFormatter(JavaFormatterOptions.builder() | ||
.style(JavaFormatterOptions.Style.PALANTIR) | ||
.build()); | ||
|
||
@Override | ||
public String formatSource(JavaFile file) throws GoetheException { | ||
StringBuilder rawSource = new StringBuilder(); | ||
try { | ||
file.writeTo(rawSource); | ||
return formatter.formatSource(rawSource.toString()); | ||
} catch (FormatterException e) { | ||
throw new GoetheException(generateMessage(file, rawSource.toString(), e.diagnostics()), e); | ||
} catch (IOException e) { | ||
throw new GoetheException("Formatting failed", e); | ||
} | ||
} | ||
|
||
/** | ||
* Attempt to provide as much actionable information as possible to understand why formatting is failing. This | ||
* is common when generated code is incorrect and cannot compile, so we mustn't make it difficult to understand | ||
* the problem. | ||
*/ | ||
private static String generateMessage( | ||
JavaFile file, String unformattedSource, List<FormatterDiagnostic> formatterDiagnostics) { | ||
try { | ||
List<String> lines = Splitter.on('\n').splitToList(unformattedSource); | ||
StringBuilder failureText = new StringBuilder(); | ||
failureText | ||
.append("Failed to format '") | ||
.append(file.packageName) | ||
.append('.') | ||
.append(file.typeSpec.name) | ||
.append("'\n"); | ||
for (FormatterDiagnostic formatterDiagnostic : formatterDiagnostics) { | ||
failureText | ||
.append(formatterDiagnostic.message()) | ||
.append("\n") | ||
// Diagnostic values are one-indexed, while our list is zero-indexed. | ||
.append(lines.get(formatterDiagnostic.line() - 1)) | ||
.append('\n') | ||
// Offset by two to convert from one-indexed to zero indexed values, and account for the caret. | ||
.append(Strings.repeat(" ", Math.max(0, formatterDiagnostic.column() - 2))) | ||
.append("^\n\n"); | ||
} | ||
return CharMatcher.is('\n').trimFrom(failureText.toString()); | ||
} catch (RuntimeException e) { | ||
return "Failed to format:\n" + unformattedSource; | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
goethe/src/main/java/com/palantir/goethe/FormatterFacade.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* (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 com.squareup.javapoet.JavaFile; | ||
|
||
interface FormatterFacade { | ||
|
||
String formatSource(JavaFile file) throws GoetheException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters