diff options
| author | 2018-07-18 13:46:00 +0100 | |
|---|---|---|
| committer | 2018-07-18 13:46:00 +0100 | |
| commit | 1ebe691c12f68beea378b133ddc4bcbde7f3f795 (patch) | |
| tree | fb051d9fde5644bd144a7e9d7bcecc70a256359c /src/main/java/cuchaz/enigma/bytecode/ClassProtectifier.java | |
| parent | Recursively rebuild method names (diff) | |
| parent | Update version number (diff) | |
| download | enigma-fork-1ebe691c12f68beea378b133ddc4bcbde7f3f795.tar.gz enigma-fork-1ebe691c12f68beea378b133ddc4bcbde7f3f795.tar.xz enigma-fork-1ebe691c12f68beea378b133ddc4bcbde7f3f795.zip | |
Merge pull request #62 from OpenModLoader/asm
ASM based class translator
Diffstat (limited to 'src/main/java/cuchaz/enigma/bytecode/ClassProtectifier.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/bytecode/ClassProtectifier.java | 54 |
1 files changed, 26 insertions, 28 deletions
diff --git a/src/main/java/cuchaz/enigma/bytecode/ClassProtectifier.java b/src/main/java/cuchaz/enigma/bytecode/ClassProtectifier.java index 6ec576e..9ed6db9 100644 --- a/src/main/java/cuchaz/enigma/bytecode/ClassProtectifier.java +++ b/src/main/java/cuchaz/enigma/bytecode/ClassProtectifier.java | |||
| @@ -11,41 +11,39 @@ | |||
| 11 | 11 | ||
| 12 | package cuchaz.enigma.bytecode; | 12 | package cuchaz.enigma.bytecode; |
| 13 | 13 | ||
| 14 | import javassist.CtBehavior; | 14 | import org.objectweb.asm.ClassVisitor; |
| 15 | import javassist.CtClass; | 15 | import org.objectweb.asm.FieldVisitor; |
| 16 | import javassist.CtField; | 16 | import org.objectweb.asm.MethodVisitor; |
| 17 | import javassist.bytecode.AccessFlag; | ||
| 18 | import javassist.bytecode.InnerClassesAttribute; | ||
| 19 | 17 | ||
| 20 | public class ClassProtectifier { | 18 | public class ClassProtectifier extends ClassVisitor { |
| 21 | 19 | ||
| 22 | public static CtClass protectify(CtClass c) { | 20 | public ClassProtectifier(int api, ClassVisitor cv) { |
| 23 | 21 | super(api, cv); | |
| 24 | // protectify all the fields | 22 | } |
| 25 | for (CtField field : c.getDeclaredFields()) { | ||
| 26 | field.setModifiers(protectify(field.getModifiers())); | ||
| 27 | } | ||
| 28 | 23 | ||
| 29 | // protectify all the methods and constructors | 24 | @Override |
| 30 | for (CtBehavior behavior : c.getDeclaredBehaviors()) { | 25 | public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { |
| 31 | behavior.setModifiers(protectify(behavior.getModifiers())); | 26 | access = protectify(access); |
| 32 | } | 27 | return super.visitMethod(access, name, desc, signature, exceptions); |
| 28 | } | ||
| 33 | 29 | ||
| 34 | // protectify all the inner classes | 30 | @Override |
| 35 | InnerClassesAttribute attr = (InnerClassesAttribute) c.getClassFile().getAttribute(InnerClassesAttribute.tag); | 31 | public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { |
| 36 | if (attr != null) { | 32 | access = protectify(access); |
| 37 | for (int i = 0; i < attr.tableLength(); i++) { | 33 | return super.visitField(access, name, desc, signature, value); |
| 38 | attr.setAccessFlags(i, protectify(attr.accessFlags(i))); | 34 | } |
| 39 | } | ||
| 40 | } | ||
| 41 | 35 | ||
| 42 | return c; | 36 | @Override |
| 37 | public void visitInnerClass(String name, String outerName, String innerName, int access) { | ||
| 38 | access = protectify(access); | ||
| 39 | super.visitInnerClass(name, outerName, innerName, access); | ||
| 43 | } | 40 | } |
| 44 | 41 | ||
| 45 | private static int protectify(int flags) { | 42 | private static int protectify(int access) { |
| 46 | if (AccessFlag.isPrivate(flags)) { | 43 | AccessFlags accessFlags = new AccessFlags(access); |
| 47 | flags = AccessFlag.setProtected(flags); | 44 | if (accessFlags.isPrivate()) { |
| 45 | accessFlags.setProtected(); | ||
| 48 | } | 46 | } |
| 49 | return flags; | 47 | return accessFlags.getFlags(); |
| 50 | } | 48 | } |
| 51 | } | 49 | } |