another ~0.03s less :3

This commit is contained in:
moehreag 2024-05-28 12:26:55 +02:00
parent 1dcd0c8c05
commit dd8b87a1cf

View file

@ -103,7 +103,7 @@ class AWProcessor {
classNames.parallelStream().forEach(name -> {
try {
processFile(out.getPath("/" + name + ".class"), classMap, fields, methods, mutations);
processFile(name, out.getPath("/" + name + ".class"), classMap, fields, methods, mutations);
} catch (IOException e) {
throw new Error(e);
}
@ -113,63 +113,60 @@ class AWProcessor {
LOGGER.info(String.format("Applied AccessWideners in %.2fs", (System.currentTimeMillis() - startTime) / 1000f));
}
private static void processFile(Path file, Map<String, Entry> classMap, Map<String, Map<String, Entry>> fields, Map<String, Map<String, Entry>> methods, Map<String, Map<String, Entry>> mutations) throws IOException {
var className = file.toString().substring(1, file.toString().length() - 6);
if (file.getFileName().toString().endsWith(".class") && (classMap.containsKey(className) || fields.containsKey(className) || methods.containsKey(className) || mutations.containsKey(className))) {
ClassReader reader = new ClassReader(Files.newInputStream(file));
ClassWriter writer = new ClassWriter(0);
ClassVisitor mapper = new ClassVisitor(Opcodes.ASM9, writer) {
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
Entry e = classMap.get(className);
private static void processFile(String className, Path file, Map<String, Entry> classMap, Map<String, Map<String, Entry>> fields, Map<String, Map<String, Entry>> methods, Map<String, Map<String, Entry>> mutations) throws IOException {
ClassReader reader = new ClassReader(Files.newInputStream(file));
ClassWriter writer = new ClassWriter(0);
ClassVisitor mapper = new ClassVisitor(Opcodes.ASM9, writer) {
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
Entry e = classMap.get(className);
if (e != null) {
access &= ~(Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED | Opcodes.ACC_PUBLIC);
access |= e.type.access;
}
if (fields.containsKey(className) || methods.containsKey(className) || mutations.containsKey(className)) { // make all classes with modifications public as well
access &= ~(Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED | Opcodes.ACC_PUBLIC);
access |= Opcodes.ACC_PUBLIC;
}
super.visit(version, access, name, signature, superName, interfaces);
}
@Override
public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) {
Map<String, Entry> map = fields.get(className);
if (map != null) {
Entry e = map.get(name + descriptor);
if (e != null) {
access &= ~(Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED | Opcodes.ACC_PUBLIC); // remove all access modifiers
access |= e.type.access; // re-add the new one
}
}
if ((map = mutations.get(className)) != null) {
var e = map.get(name + descriptor);
if (e != null) {
access &= ~Opcodes.ACC_FINAL; // always AccessType.MUTABLE
}
}
return super.visitField(access, name, descriptor, signature, value);
}
@Override
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
Map<String, Entry> map = methods.get(className);
if (map != null) {
Entry e = map.get(name + descriptor);
if (e != null) {
access &= ~(Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED | Opcodes.ACC_PUBLIC);
access |= e.type.access;
}
if (fields.containsKey(className) || methods.containsKey(className) || mutations.containsKey(className)) { // make all classes with modifications public as well
access &= ~(Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED | Opcodes.ACC_PUBLIC);
access |= Opcodes.ACC_PUBLIC;
}
super.visit(version, access, name, signature, superName, interfaces);
}
return super.visitMethod(access, name, descriptor, signature, exceptions);
}
};
@Override
public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) {
Map<String, Entry> map = fields.get(className);
if (map != null) {
Entry e = map.get(name + descriptor);
if (e != null) {
access &= ~(Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED | Opcodes.ACC_PUBLIC); // remove all access modifiers
access |= e.type.access; // re-add the new one
}
}
if ((map = mutations.get(className)) != null) {
var e = map.get(name + descriptor);
if (e != null) {
access &= ~Opcodes.ACC_FINAL; // always AccessType.MUTABLE
}
}
return super.visitField(access, name, descriptor, signature, value);
}
reader.accept(mapper, 0);
@Override
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
Map<String, Entry> map = methods.get(className);
if (map != null) {
Entry e = map.get(name + descriptor);
if (e != null) {
access &= ~(Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED | Opcodes.ACC_PUBLIC);
access |= e.type.access;
}
}
return super.visitMethod(access, name, descriptor, signature, exceptions);
}
};
reader.accept(mapper, 0);
Files.write(file, writer.toByteArray());
}
Files.write(file, writer.toByteArray());
}
private record Entry(AccessType type, String targetType, String className, String name, String descriptor) {