|
| 1 | +/* |
| 2 | + * Copyright 2016-2022 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.spotless.java; |
| 17 | + |
| 18 | +import java.io.Serializable; |
| 19 | +import java.lang.reflect.Constructor; |
| 20 | +import java.util.Objects; |
| 21 | + |
| 22 | +import com.diffplug.spotless.*; |
| 23 | + |
| 24 | +/** Wraps up <a href="https://github.com/palantir/palantir-java-format">palantir-java-format</a> fork of |
| 25 | + * <a href="https://github.com/google/google-java-format">google-java-format</a> as a FormatterStep. */ |
| 26 | +public class PalantirJavaFormatStep { |
| 27 | + // prevent direct instantiation |
| 28 | + private PalantirJavaFormatStep() {} |
| 29 | + |
| 30 | + private static final String NAME = "palantir-java-format"; |
| 31 | + private static final String MAVEN_COORDINATE = "com.palantir.javaformat:palantir-java-format:"; |
| 32 | + private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(8, "1.1.0").add(11, "2.10.0"); |
| 33 | + |
| 34 | + /** Creates a step which formats everything - code, import order, and unused imports. */ |
| 35 | + public static FormatterStep create(Provisioner provisioner) { |
| 36 | + return create(defaultVersion(), provisioner); |
| 37 | + } |
| 38 | + |
| 39 | + /** Creates a step which formats everything - code, import order, and unused imports. */ |
| 40 | + public static FormatterStep create(String version, Provisioner provisioner) { |
| 41 | + Objects.requireNonNull(version, "version"); |
| 42 | + Objects.requireNonNull(provisioner, "provisioner"); |
| 43 | + |
| 44 | + return FormatterStep.createLazy(NAME, |
| 45 | + () -> new State(JarState.from(MAVEN_COORDINATE + version, provisioner), version), |
| 46 | + State::createFormat); |
| 47 | + } |
| 48 | + |
| 49 | + /** Get default formatter version */ |
| 50 | + public static String defaultVersion() { |
| 51 | + return JVM_SUPPORT.getRecommendedFormatterVersion(); |
| 52 | + } |
| 53 | + |
| 54 | + private static final class State implements Serializable { |
| 55 | + private static final long serialVersionUID = 1L; |
| 56 | + |
| 57 | + /** The jar that contains the formatter. */ |
| 58 | + private final JarState jarState; |
| 59 | + /** Version of the formatter jar. */ |
| 60 | + private final String formatterVersion; |
| 61 | + |
| 62 | + State(JarState jarState, String formatterVersion) { |
| 63 | + this.jarState = jarState; |
| 64 | + this.formatterVersion = formatterVersion; |
| 65 | + } |
| 66 | + |
| 67 | + FormatterFunc createFormat() throws Exception { |
| 68 | + final ClassLoader classLoader = jarState.getClassLoader(); |
| 69 | + final Class<?> formatterFunc = classLoader.loadClass("com.diffplug.spotless.glue.pjf.PalantirJavaFormatFormatterFunc"); |
| 70 | + final Constructor<?> constructor = formatterFunc.getConstructor(); |
| 71 | + return JVM_SUPPORT.suggestLaterVersionOnError(formatterVersion, (FormatterFunc) constructor.newInstance()); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments