|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.palantir.javaformat.gradle; |
| 16 | + |
| 17 | +import java.io.File; |
| 18 | +import java.lang.reflect.InvocationTargetException; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.List; |
| 22 | +import java.util.concurrent.Callable; |
| 23 | +import org.gradle.api.GradleException; |
| 24 | +import org.gradle.api.Project; |
| 25 | +import org.gradle.api.file.FileCollection; |
| 26 | +import org.gradle.api.file.FileTree; |
| 27 | +import org.jspecify.annotations.Nullable; |
| 28 | + |
| 29 | +/** |
| 30 | + * The Java files of an Android project: those in the {@code java.srcDirs} of every source set of its {@code android} |
| 31 | + * extension, main, test and androidTest, and those of the build types and flavors. |
| 32 | + * |
| 33 | + * <p>Read by reflection. The plugin has no dependency on the Android Gradle plugin: it never brings a version of its |
| 34 | + * own into a build that has one, and it does not need the classes of the one the build has, which the class loader of |
| 35 | + * a project that applies the plugin may not see. The three getters it calls are those of the Android DSL. |
| 36 | + */ |
| 37 | +final class AndroidSources { |
| 38 | + |
| 39 | + /** The Android plugins whose {@code android} extension has {@code sourceSets}. None of them applies java. */ |
| 40 | + static final List<String> PLUGIN_IDS = List.of( |
| 41 | + "com.android.application", "com.android.library", "com.android.dynamic-feature", "com.android.test"); |
| 42 | + |
| 43 | + private AndroidSources() {} |
| 44 | + |
| 45 | + /** |
| 46 | + * The Java files of the project's Android source sets. A callable: the source sets of the build types and flavors |
| 47 | + * are added once the build script declares them, after the plugin is applied. |
| 48 | + */ |
| 49 | + static FileCollection javaFiles(Project project) { |
| 50 | + var android = project.getExtensions().getByName("android"); |
| 51 | + var objects = project.getObjects(); |
| 52 | + var projectPath = project.getPath(); |
| 53 | + return objects.fileCollection().from((Callable<List<FileTree>>) () -> |
| 54 | + javaDirectories(android, projectPath).stream() |
| 55 | + .map(dir -> objects.fileTree().from(dir).matching(files -> files.include("**/*.java"))) |
| 56 | + .toList()); |
| 57 | + } |
| 58 | + |
| 59 | + /** The {@code java.srcDirs} of every source set of {@code android}, the project's Android extension. */ |
| 60 | + static List<File> javaDirectories(Object android, String projectPath) { |
| 61 | + var directories = new ArrayList<File>(); |
| 62 | + for (var sourceSet : get(android, "getSourceSets", Iterable.class, projectPath)) { |
| 63 | + var java = get(sourceSet, "getJava", Object.class, projectPath); |
| 64 | + for (var directory : get(java, "getSrcDirs", Collection.class, projectPath)) { |
| 65 | + if (!(directory instanceof File file)) { |
| 66 | + throw unreadable(projectPath, "getSrcDirs() holds " + describe(directory) + ", not a File", null); |
| 67 | + } |
| 68 | + directories.add(file); |
| 69 | + } |
| 70 | + } |
| 71 | + return directories; |
| 72 | + } |
| 73 | + |
| 74 | + private static <T> T get(Object object, String getter, Class<T> type, String projectPath) { |
| 75 | + @Nullable Object value; |
| 76 | + try { |
| 77 | + value = object.getClass().getMethod(getter).invoke(object); |
| 78 | + } catch (NoSuchMethodException | IllegalAccessException e) { |
| 79 | + throw unreadable(projectPath, object.getClass().getName() + " has no public " + getter + "()", e); |
| 80 | + } catch (InvocationTargetException e) { |
| 81 | + throw unreadable(projectPath, getter + "() failed", e.getCause()); |
| 82 | + } |
| 83 | + if (!type.isInstance(value)) { |
| 84 | + throw unreadable( |
| 85 | + projectPath, getter + "() returned " + describe(value) + ", not a " + type.getName(), null); |
| 86 | + } |
| 87 | + return type.cast(value); |
| 88 | + } |
| 89 | + |
| 90 | + private static String describe(@Nullable Object value) { |
| 91 | + return value == null ? "null" : "a " + value.getClass().getName(); |
| 92 | + } |
| 93 | + |
| 94 | + private static GradleException unreadable(String projectPath, String detail, @Nullable Throwable cause) { |
| 95 | + return new GradleException( |
| 96 | + "open-java-format could not read android.sourceSets[*].java.srcDirs of project " + projectPath |
| 97 | + + ", the directories it formats: " + detail + ". This version of the Android Gradle plugin" |
| 98 | + + " may need a newer open-java-format; please report it at" |
| 99 | + + " https://github.com/openjavaformat/open-java-format/issues", |
| 100 | + cause); |
| 101 | + } |
| 102 | +} |
0 commit comments