Skip to content

Commit 2cb3a11

Browse files
committed
Added support for google-java-format (#33).
1 parent 751dd4a commit 2cb3a11

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.gradle.spotless.java;
17+
18+
import java.io.File;
19+
import java.lang.reflect.Method;
20+
import java.net.URL;
21+
import java.net.URLClassLoader;
22+
import java.util.Set;
23+
24+
import org.gradle.api.Project;
25+
import org.gradle.api.artifacts.Configuration;
26+
import org.gradle.api.artifacts.Dependency;
27+
28+
import com.diffplug.common.base.Errors;
29+
import com.diffplug.common.base.Throwing;
30+
31+
/** Wraps up [google-java-format](https://github.com/google/google-java-format) as a FormatterStep. */
32+
class GoogleJavaFormat {
33+
static final String NAME = "google-java-format";
34+
static final String DEFAULT_VERSION = "1.1";
35+
static final String MAVEN_COORDINATE = "com.google.googlejavaformat:google-java-format:";
36+
static final String FORMATTER_CLASS = "com.google.googlejavaformat.java.Formatter";
37+
static final String FORMATTER_METHOD = "formatSource";
38+
39+
/** Returns a function which will call the google-java-format tool. */
40+
static Throwing.Function<String, String> createRule(String version, Project project) throws Exception {
41+
// get the googleJavaFormat configuration
42+
Dependency googleJavaFormatJar = project.getDependencies().create(MAVEN_COORDINATE + version);
43+
Configuration configuration = project.getConfigurations().detachedConfiguration(googleJavaFormatJar);
44+
configuration.setDescription("google-java-format");
45+
46+
// get a classloader which contains only the jars in this configuration
47+
Set<File> jars;
48+
try {
49+
jars = configuration.resolve();
50+
} catch (Exception e) {
51+
System.err.println("You probably need to add a repository containing the `google-java-format` artifact to your buildscript,");
52+
System.err.println("e.g.: repositories { mavenCentral() }");
53+
throw e;
54+
}
55+
URL[] jarUrls = jars.stream().map(Errors.rethrow().wrapFunction(
56+
file -> file.toURI().toURL())).toArray(size -> new URL[size]);
57+
URLClassLoader classLoader = new URLClassLoader(jarUrls);
58+
// TODO: dispose the classloader when the function
59+
// that we return gets garbage-collected
60+
61+
// instantiate the formatter and get its format method
62+
Class<?> formatterClazz = classLoader.loadClass(FORMATTER_CLASS);
63+
Object formatter = formatterClazz.getConstructor().newInstance();
64+
Method method = formatterClazz.getMethod(FORMATTER_METHOD, String.class);
65+
return input -> (String) method.invoke(formatter, input);
66+
}
67+
}

src/main/java/com/diffplug/gradle/spotless/java/JavaExtension.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ public void eclipseFormatFile(Object eclipseFormatFile) {
5656
customLazy(EclipseFormatterStep.NAME, () -> EclipseFormatterStep.load(getProject().file(eclipseFormatFile))::format);
5757
}
5858

59+
/** Uses the [google-java-format](https://github.com/google/google-java-format) jar to format source code. */
60+
public void googleJavaFormat() {
61+
googleJavaFormat(GoogleJavaFormat.DEFAULT_VERSION);
62+
}
63+
64+
/**
65+
* Uses the given version of [google-java-format](https://github.com/google/google-java-format) to format source code.
66+
*
67+
* Limited to published versions. See [issue #33](https://github.com/diffplug/spotless/issues/33#issuecomment-252315095)
68+
* for an workaround for using snapshot versions.
69+
*/
70+
public void googleJavaFormat(String version) {
71+
customLazy(GoogleJavaFormat.NAME, () -> GoogleJavaFormat.createRule(version, getProject()));
72+
}
73+
5974
/** If the user hasn't specified the files yet, we'll assume he/she means all of the java files. */
6075
@Override
6176
protected void setupTask(FormatTask task) throws Exception {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.gradle.spotless.java;
17+
18+
import java.io.IOException;
19+
20+
import org.junit.Assert;
21+
import org.junit.Test;
22+
23+
import com.diffplug.gradle.spotless.GradleIntegrationTest;
24+
25+
public class GoogleJavaFormatTest extends GradleIntegrationTest {
26+
@Test
27+
public void integration() throws IOException {
28+
write("build.gradle",
29+
"plugins {",
30+
" id 'com.diffplug.gradle.spotless'",
31+
"}",
32+
"repositories { mavenCentral() }",
33+
"",
34+
"spotless {",
35+
" java {",
36+
" target file('test.java')",
37+
" googleJavaFormat()",
38+
" }",
39+
"}");
40+
String input = getTestResource("java/googlejavaformat/JavaCodeUnformatted.test");
41+
write("test.java", input);
42+
gradleRunner().withArguments("spotlessApply").forwardOutput().build();
43+
44+
String result = read("test.java");
45+
String output = getTestResource("java/googlejavaformat/JavaCodeFormatted.test");
46+
Assert.assertEquals(output, result);
47+
}
48+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Java {
2+
public static void main(String[] args) {
3+
System.out.println("hello");
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Java {
2+
public static void main(String[] args) {
3+
System.out.println("hello");
4+
}
5+
}

0 commit comments

Comments
 (0)