Fix making constructors final

dev/0.11
modmuss50 2020-04-06 22:52:49 +01:00
parent 0ae8535c40
commit 232118e69e
2 changed files with 50 additions and 33 deletions

View File

@ -312,19 +312,29 @@ public class AccessWidener {
return (i & ~(Opcodes.ACC_PRIVATE)) | Opcodes.ACC_PROTECTED; return (i & ~(Opcodes.ACC_PRIVATE)) | Opcodes.ACC_PROTECTED;
} }
private static int makeFinalIfPrivate(int i) { private static int makeFinalIfPrivate(int access, String name, int ownerAccess) {
if ((i & Opcodes.ACC_PRIVATE) != 0) { // Dont make constructors final
return i | Opcodes.ACC_FINAL; if (name.equals("<init>")) {
return access;
} }
return i; // Skip interface and static methods
if ((ownerAccess & (Opcodes.ACC_INTERFACE | Opcodes.ACC_STATIC)) != 0) {
return access;
}
if ((access & Opcodes.ACC_PRIVATE) != 0) {
return access | Opcodes.ACC_FINAL;
}
return access;
} }
private static int removeFinal(int i) { private static int removeFinal(int i) {
return i & ~Opcodes.ACC_FINAL; return i & ~Opcodes.ACC_FINAL;
} }
public interface Access extends IntUnaryOperator { public interface Access extends AccessOperator {
Access makeAccessible(); Access makeAccessible();
Access makeExtendable(); Access makeExtendable();
@ -333,14 +343,14 @@ public class AccessWidener {
} }
public enum ClassAccess implements Access { public enum ClassAccess implements Access {
DEFAULT(i -> i), DEFAULT((access, name, ownerAccess) -> access),
ACCESSIBLE(i -> makePublic(i)), ACCESSIBLE((access, name, ownerAccess) -> makePublic(access)),
EXTENDABLE(i -> makePublic(removeFinal(i))), EXTENDABLE((access, name, ownerAccess) -> makePublic(removeFinal(access))),
ACCESSIBLE_EXTENDABLE(i -> makePublic(removeFinal(i))); ACCESSIBLE_EXTENDABLE((access, name, ownerAccess) -> makePublic(removeFinal(access)));
private final IntUnaryOperator operator; private final AccessOperator operator;
ClassAccess(IntUnaryOperator operator) { ClassAccess(AccessOperator operator) {
this.operator = operator; this.operator = operator;
} }
@ -368,20 +378,20 @@ public class AccessWidener {
} }
@Override @Override
public int applyAsInt(int operand) { public int apply(int access, String targetName, int ownerAccess) {
return operator.applyAsInt(operand); return operator.apply(access, targetName, ownerAccess);
} }
} }
public enum MethodAccess implements Access { public enum MethodAccess implements Access {
DEFAULT(i -> i), DEFAULT((access, name, ownerAccess) -> access),
ACCESSIBLE(i -> makePublic(makeFinalIfPrivate(i))), ACCESSIBLE((access, name, ownerAccess) -> makePublic(makeFinalIfPrivate(access, name, ownerAccess))),
EXTENDABLE(i -> makeProtected(removeFinal(i))), EXTENDABLE((access, name, ownerAccess) -> makeProtected(removeFinal(access))),
ACCESSIBLE_EXTENDABLE(i -> makePublic(removeFinal(i))); ACCESSIBLE_EXTENDABLE((access, name, owner) -> makePublic(removeFinal(access)));
private final IntUnaryOperator operator; private final AccessOperator operator;
MethodAccess(IntUnaryOperator operator) { MethodAccess(AccessOperator operator) {
this.operator = operator; this.operator = operator;
} }
@ -409,20 +419,20 @@ public class AccessWidener {
} }
@Override @Override
public int applyAsInt(int operand) { public int apply(int access, String targetName, int ownerAccess) {
return operator.applyAsInt(operand); return operator.apply(access, targetName, ownerAccess);
} }
} }
public enum FieldAccess implements Access { public enum FieldAccess implements Access {
DEFAULT(i -> i), DEFAULT((access, name, ownerAccess) -> access),
ACCESSIBLE(i -> makePublic(i)), ACCESSIBLE((access, name, ownerAccess) -> makePublic(access)),
MUTABLE(i -> removeFinal(i)), MUTABLE((access, name, ownerAccess) -> removeFinal(access)),
ACCESSIBLE_MUTABLE(i -> makePublic(removeFinal(i))); ACCESSIBLE_MUTABLE((access, name, ownerAccess) -> makePublic(removeFinal(access)));
private final IntUnaryOperator operator; private final AccessOperator operator;
FieldAccess(IntUnaryOperator operator) { FieldAccess(AccessOperator operator) {
this.operator = operator; this.operator = operator;
} }
@ -450,8 +460,13 @@ public class AccessWidener {
} }
@Override @Override
public int applyAsInt(int operand) { public int apply(int access, String targetName, int ownerAccess) {
return operator.applyAsInt(operand); return operator.apply(access, targetName, ownerAccess);
} }
} }
@FunctionalInterface
public interface AccessOperator {
int apply(int access, String targetName, int ownerAccess);
}
} }

View File

@ -168,6 +168,7 @@ public class AccessWidenerJarProcessor implements JarProcessor {
private class AccessTransformer extends ClassVisitor { private class AccessTransformer extends ClassVisitor {
private String className; private String className;
private int classAccess;
private AccessTransformer(ClassVisitor classVisitor) { private AccessTransformer(ClassVisitor classVisitor) {
super(Opcodes.ASM7, classVisitor); super(Opcodes.ASM7, classVisitor);
@ -176,9 +177,10 @@ public class AccessWidenerJarProcessor implements JarProcessor {
@Override @Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
className = name; className = name;
classAccess = access;
super.visit( super.visit(
version, version,
accessWidener.getClassAccess(name).applyAsInt(access), accessWidener.getClassAccess(name).apply(access, name, classAccess),
name, name,
signature, signature,
superName, superName,
@ -192,14 +194,14 @@ public class AccessWidenerJarProcessor implements JarProcessor {
name, name,
outerName, outerName,
innerName, innerName,
accessWidener.getClassAccess(name).applyAsInt(access) accessWidener.getClassAccess(name).apply(access, name, classAccess)
); );
} }
@Override @Override
public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) { public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) {
return super.visitField( return super.visitField(
accessWidener.getFieldAccess(new EntryTriple(className, name, descriptor)).applyAsInt(access), accessWidener.getFieldAccess(new EntryTriple(className, name, descriptor)).apply(access, name, classAccess),
name, name,
descriptor, descriptor,
signature, signature,
@ -210,7 +212,7 @@ public class AccessWidenerJarProcessor implements JarProcessor {
@Override @Override
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) { public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
return new AccessWidenerMethodVisitor(super.visitMethod( return new AccessWidenerMethodVisitor(super.visitMethod(
accessWidener.getMethodAccess(new EntryTriple(className, name, descriptor)).applyAsInt(access), accessWidener.getMethodAccess(new EntryTriple(className, name, descriptor)).apply(access, name, classAccess),
name, name,
descriptor, descriptor,
signature, signature,