diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/mapping/Signature.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/mapping/Signature.java | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/main/java/cuchaz/enigma/mapping/Signature.java b/src/main/java/cuchaz/enigma/mapping/Signature.java new file mode 100644 index 0000000..071e4af --- /dev/null +++ b/src/main/java/cuchaz/enigma/mapping/Signature.java | |||
| @@ -0,0 +1,82 @@ | |||
| 1 | package cuchaz.enigma.mapping; | ||
| 2 | |||
| 3 | import cuchaz.enigma.bytecode.translators.TranslationSignatureVisitor; | ||
| 4 | import org.objectweb.asm.signature.SignatureReader; | ||
| 5 | import org.objectweb.asm.signature.SignatureVisitor; | ||
| 6 | import org.objectweb.asm.signature.SignatureWriter; | ||
| 7 | |||
| 8 | import java.util.function.Function; | ||
| 9 | import java.util.regex.Pattern; | ||
| 10 | |||
| 11 | public class Signature { | ||
| 12 | private static final Pattern OBJECT_PATTERN = Pattern.compile(".*:Ljava/lang/Object;:.*"); | ||
| 13 | |||
| 14 | private final String signature; | ||
| 15 | private final boolean isType; | ||
| 16 | |||
| 17 | private Signature(String signature, boolean isType) { | ||
| 18 | if (signature != null && OBJECT_PATTERN.matcher(signature).matches()) { | ||
| 19 | signature = signature.replaceAll(":Ljava/lang/Object;:", "::"); | ||
| 20 | } | ||
| 21 | |||
| 22 | this.signature = signature; | ||
| 23 | this.isType = isType; | ||
| 24 | } | ||
| 25 | |||
| 26 | public static Signature createTypedSignature(String signature) { | ||
| 27 | if (signature != null && !signature.isEmpty()) { | ||
| 28 | return new Signature(signature, true); | ||
| 29 | } | ||
| 30 | return new Signature(null, true); | ||
| 31 | } | ||
| 32 | |||
| 33 | public static Signature createSignature(String signature) { | ||
| 34 | if (signature != null && !signature.isEmpty()) { | ||
| 35 | return new Signature(signature, false); | ||
| 36 | } | ||
| 37 | return new Signature(null, false); | ||
| 38 | } | ||
| 39 | |||
| 40 | public String getSignature() { | ||
| 41 | return signature; | ||
| 42 | } | ||
| 43 | |||
| 44 | public boolean isType() { | ||
| 45 | return isType; | ||
| 46 | } | ||
| 47 | |||
| 48 | public Signature remap(Function<String, String> remapper) { | ||
| 49 | if (signature == null) { | ||
| 50 | return this; | ||
| 51 | } | ||
| 52 | SignatureWriter writer = new SignatureWriter(); | ||
| 53 | SignatureVisitor visitor = new TranslationSignatureVisitor(remapper, writer); | ||
| 54 | if (isType) { | ||
| 55 | new SignatureReader(signature).acceptType(visitor); | ||
| 56 | } else { | ||
| 57 | new SignatureReader(signature).accept(visitor); | ||
| 58 | } | ||
| 59 | return new Signature(writer.toString(), isType); | ||
| 60 | } | ||
| 61 | |||
| 62 | @Override | ||
| 63 | public boolean equals(Object obj) { | ||
| 64 | if (obj instanceof Signature) { | ||
| 65 | Signature other = (Signature) obj; | ||
| 66 | return (other.signature == null && signature == null || other.signature != null | ||
| 67 | && signature != null && other.signature.equals(signature)) | ||
| 68 | && other.isType == this.isType; | ||
| 69 | } | ||
| 70 | return false; | ||
| 71 | } | ||
| 72 | |||
| 73 | @Override | ||
| 74 | public int hashCode() { | ||
| 75 | return signature.hashCode() | (isType ? 1 : 0) << 16; | ||
| 76 | } | ||
| 77 | |||
| 78 | @Override | ||
| 79 | public String toString() { | ||
| 80 | return signature; | ||
| 81 | } | ||
| 82 | } | ||