|
| 1 | +package net.neoforged.neodev; |
| 2 | + |
| 3 | +import net.neoforged.neodev.utils.FileUtils; |
| 4 | +import net.neoforged.neodev.utils.SerializablePredicate; |
| 5 | +import net.neoforged.neodev.utils.structure.ClassInfo; |
| 6 | +import net.neoforged.neodev.utils.structure.ClassStructureVisitor; |
| 7 | +import net.neoforged.neodev.utils.structure.FieldInfo; |
| 8 | +import net.neoforged.neodev.utils.structure.MethodInfo; |
| 9 | +import org.gradle.api.DefaultTask; |
| 10 | +import org.gradle.api.Named; |
| 11 | +import org.gradle.api.file.RegularFileProperty; |
| 12 | +import org.gradle.api.provider.ListProperty; |
| 13 | +import org.gradle.api.tasks.Input; |
| 14 | +import org.gradle.api.tasks.InputFile; |
| 15 | +import org.gradle.api.tasks.OutputFile; |
| 16 | +import org.gradle.api.tasks.TaskAction; |
| 17 | +import org.objectweb.asm.Opcodes; |
| 18 | + |
| 19 | +import javax.annotation.Nullable; |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.Serializable; |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | +import java.util.stream.Collectors; |
| 27 | + |
| 28 | +/** |
| 29 | + * This task is used to generate access transformers based on a set of rules defined in the buildscript. |
| 30 | + */ |
| 31 | +public abstract class GenerateAccessTransformers extends DefaultTask { |
| 32 | + public static final Modifier PUBLIC = new Modifier("public", false, Opcodes.ACC_PUBLIC); |
| 33 | + public static final Modifier PROTECTED = new Modifier("protected", false, Opcodes.ACC_PUBLIC, Opcodes.ACC_PROTECTED); |
| 34 | + |
| 35 | + @InputFile |
| 36 | + public abstract RegularFileProperty getInput(); |
| 37 | + |
| 38 | + @OutputFile |
| 39 | + public abstract RegularFileProperty getAccessTransformer(); |
| 40 | + |
| 41 | + @Input |
| 42 | + public abstract ListProperty<AtGroup> getGroups(); |
| 43 | + |
| 44 | + @TaskAction |
| 45 | + public void exec() throws IOException { |
| 46 | + // First we collect all classes |
| 47 | + var targets = ClassStructureVisitor.readJar(getInput().getAsFile().get()); |
| 48 | + |
| 49 | + var groupList = getGroups().get(); |
| 50 | + |
| 51 | + List<String>[] groups = new List[groupList.size()]; |
| 52 | + for (int i = 0; i < groupList.size(); i++) { |
| 53 | + groups[i] = new ArrayList<>(); |
| 54 | + } |
| 55 | + |
| 56 | + // Now we check each class against each group and see if the group wants to handle it |
| 57 | + for (ClassInfo value : targets.values()) { |
| 58 | + for (int i = 0; i < groupList.size(); i++) { |
| 59 | + var group = groupList.get(i); |
| 60 | + if (group.classMatch.test(value)) { |
| 61 | + var lastInner = value.name().lastIndexOf("$"); |
| 62 | + // Skip anonymous classes |
| 63 | + if (lastInner >= 0 && Character.isDigit(value.name().charAt(lastInner + 1))) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + // fieldMatch is non-null only for field ATs |
| 68 | + if (group.fieldMatch != null) { |
| 69 | + for (var field : value.fields()) { |
| 70 | + if (group.fieldMatch.test(field) && !group.modifier.test(field.access())) { |
| 71 | + groups[i].add(group.modifier.name + " " + value.name().replace('/', '.') + " " + field.name()); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + // methodMatch is non-null only for group ATs |
| 76 | + else if (group.methodMatch != null) { |
| 77 | + for (var method : value.methods()) { |
| 78 | + if (group.methodMatch.test(method) && !group.modifier.test(method.access())) { |
| 79 | + groups[i].add(group.modifier.name + " " + value.name().replace('/', '.') + " " + method.name() + method.descriptor()); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + // If there's neither a field nor a method predicate, this is a class AT |
| 84 | + else if (!group.modifier.test(value.access().intValue())) { |
| 85 | + groups[i].add(group.modifier.name + " " + value.name().replace('/', '.')); |
| 86 | + |
| 87 | + // If we AT a record we must ensure that its constructors have the same AT |
| 88 | + if (value.hasSuperclass("java/lang/Record")) { |
| 89 | + for (MethodInfo method : value.methods()) { |
| 90 | + if (method.name().equals("<init>")) { |
| 91 | + groups[i].add(group.modifier.name + " " + value.name().replace('/', '.') + " " + method.name() + method.descriptor()); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // Dump the ATs |
| 101 | + var text = new StringBuilder(); |
| 102 | + |
| 103 | + text.append("# This file is generated based on the rules defined in the buildscript. DO NOT modify it manually.\n# Add more rules in the buildscript and then run the generateAccessTransformers task to update this file.\n\n"); |
| 104 | + |
| 105 | + for (int i = 0; i < groups.length; i++) { |
| 106 | + // Check if the group found no targets. If it didn't, there's probably an error in the test and it should be reported |
| 107 | + if (groups[i].isEmpty()) { |
| 108 | + throw new IllegalStateException("Generated AT group '" + groupList.get(i).name + "' found no entries!"); |
| 109 | + } |
| 110 | + text.append("# ").append(groupList.get(i).name).append('\n'); |
| 111 | + text.append(groups[i].stream().sorted().collect(Collectors.joining("\n"))); |
| 112 | + text.append('\n'); |
| 113 | + |
| 114 | + if (i < groups.length - 1) text.append('\n'); |
| 115 | + } |
| 116 | + |
| 117 | + var outFile = getAccessTransformer().getAsFile().get().toPath(); |
| 118 | + if (!Files.exists(outFile.getParent())) { |
| 119 | + Files.createDirectories(outFile.getParent()); |
| 120 | + } |
| 121 | + |
| 122 | + FileUtils.writeStringSafe(outFile, text.toString(), StandardCharsets.UTF_8); |
| 123 | + } |
| 124 | + |
| 125 | + public void classGroup(String name, Modifier modifier, SerializablePredicate<ClassInfo> match) { |
| 126 | + getGroups().add(new AtGroup(name, modifier, match, null, null)); |
| 127 | + } |
| 128 | + |
| 129 | + public void methodGroup(String name, Modifier modifier, SerializablePredicate<ClassInfo> targetTest, SerializablePredicate<MethodInfo> methodTest) { |
| 130 | + getGroups().add(new AtGroup(name, modifier, targetTest, methodTest, null)); |
| 131 | + } |
| 132 | + |
| 133 | + public void fieldGroup(String name, Modifier modifier, SerializablePredicate<ClassInfo> targetTest, SerializablePredicate<FieldInfo> fieldTest) { |
| 134 | + getGroups().add(new AtGroup(name, modifier, targetTest, null, fieldTest)); |
| 135 | + } |
| 136 | + |
| 137 | + public <T extends Named> SerializablePredicate<T> named(String name) { |
| 138 | + return target -> target.getName().equals(name); |
| 139 | + } |
| 140 | + |
| 141 | + public SerializablePredicate<ClassInfo> classesWithSuperclass(String superClass) { |
| 142 | + return target -> target.hasSuperclass(superClass); |
| 143 | + } |
| 144 | + |
| 145 | + public SerializablePredicate<ClassInfo> innerClassesOf(String parent) { |
| 146 | + var parentFullName = parent + "$"; |
| 147 | + return target -> target.name().startsWith(parentFullName); |
| 148 | + } |
| 149 | + |
| 150 | + public SerializablePredicate<MethodInfo> methodsReturning(String type) { |
| 151 | + var endMatch = ")L" + type + ";"; |
| 152 | + return methodInfo -> methodInfo.descriptor().endsWith(endMatch); |
| 153 | + } |
| 154 | + |
| 155 | + public SerializablePredicate<FieldInfo> fieldsOfType(SerializablePredicate<ClassInfo> type) { |
| 156 | + return value -> type.test(value.type()); |
| 157 | + } |
| 158 | + |
| 159 | + public <T> SerializablePredicate<T> matchAny() { |
| 160 | + return value -> true; |
| 161 | + } |
| 162 | + |
| 163 | + public record AtGroup(String name, Modifier modifier, SerializablePredicate<ClassInfo> classMatch, |
| 164 | + @Nullable SerializablePredicate<MethodInfo> methodMatch, @Nullable SerializablePredicate<FieldInfo> fieldMatch) implements Serializable { |
| 165 | + } |
| 166 | + |
| 167 | + public record Modifier(String name, boolean isFinal, int... validOpcodes) implements Serializable { |
| 168 | + public boolean test(int value) { |
| 169 | + if (isFinal && (value & Opcodes.ACC_FINAL) == 0) return false; |
| 170 | + |
| 171 | + for (int validOpcode : validOpcodes) { |
| 172 | + if ((value & validOpcode) != 0) { |
| 173 | + return true; |
| 174 | + } |
| 175 | + } |
| 176 | + return false; |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments