From ecfda21f3db9e62e3acf074e9842e92ace4cb3ab Mon Sep 17 00:00:00 2001 From: jeff Date: Tue, 17 Mar 2015 20:01:55 -0400 Subject: parsing generic signatures is tricky. don't write custom code to do it. switching to library instead --- src/cuchaz/enigma/bytecode/ClassRenamer.java | 9 +- src/cuchaz/enigma/mapping/ParameterizedType.java | 54 -------- src/cuchaz/enigma/mapping/Type.java | 61 +-------- test/cuchaz/enigma/TestType.java | 167 ++--------------------- 4 files changed, 22 insertions(+), 269 deletions(-) delete mode 100644 src/cuchaz/enigma/mapping/ParameterizedType.java diff --git a/src/cuchaz/enigma/bytecode/ClassRenamer.java b/src/cuchaz/enigma/bytecode/ClassRenamer.java index d88daa5..d7acd30 100644 --- a/src/cuchaz/enigma/bytecode/ClassRenamer.java +++ b/src/cuchaz/enigma/bytecode/ClassRenamer.java @@ -34,7 +34,6 @@ import javassist.bytecode.SignatureAttribute.MethodSignature; import javassist.bytecode.SignatureAttribute.ObjectType; import cuchaz.enigma.mapping.ClassEntry; import cuchaz.enigma.mapping.ClassNameReplacer; -import cuchaz.enigma.mapping.ParameterizedType; import cuchaz.enigma.mapping.Translator; import cuchaz.enigma.mapping.Type; @@ -103,8 +102,8 @@ public class ClassRenamer { } public String getFramed(String typeName) { - ParameterizedType type = new ParameterizedType(new Type(typeName)); - ParameterizedType renamedType = new ParameterizedType(type, m_replacer); + Type type = new Type(typeName); + Type renamedType = new Type(type, m_replacer); if (!type.equals(renamedType)) { return renamedType.toString(); } @@ -115,6 +114,7 @@ public class ClassRenamer { // we can deal with the ones that start with a class String signature = type.encode(); + /* if (signature.startsWith("L") || signature.startsWith("[")) { // TEMP: skip special characters for now @@ -134,10 +134,11 @@ public class ClassRenamer { // don't need to care about template names return null; } else { + */ // TEMP System.out.println("Skipping translating: " + signature); return null; - } + //} } } diff --git a/src/cuchaz/enigma/mapping/ParameterizedType.java b/src/cuchaz/enigma/mapping/ParameterizedType.java deleted file mode 100644 index af24ef4..0000000 --- a/src/cuchaz/enigma/mapping/ParameterizedType.java +++ /dev/null @@ -1,54 +0,0 @@ -package cuchaz.enigma.mapping; - -import cuchaz.enigma.Util; - - - -public class ParameterizedType extends Type { - - private static final long serialVersionUID = 1758975507937309011L; - - public ParameterizedType(Type other) { - super(other); - for (int i=0; i;"); - return buf.toString(); - } else { - return m_name; - } - } - - @Override - public boolean equals(Object other) { - if (other instanceof ParameterizedType) { - return equals((ParameterizedType)other); - } - return false; - } - - public boolean equals(ParameterizedType other) { - return m_name.equals(other.m_name) && m_parameters.equals(other.m_parameters); - } - - public int hashCode() { - return Util.combineHashesOrdered(m_name.hashCode(), m_parameters.hashCode()); - } - -} diff --git a/src/cuchaz/enigma/mapping/Type.java b/src/cuchaz/enigma/mapping/Type.java index d530083..3f441e2 100644 --- a/src/cuchaz/enigma/mapping/Type.java +++ b/src/cuchaz/enigma/mapping/Type.java @@ -1,10 +1,8 @@ package cuchaz.enigma.mapping; import java.io.Serializable; -import java.util.List; import java.util.Map; -import com.beust.jcommander.internal.Lists; import com.google.common.collect.Maps; public class Type implements Serializable { @@ -85,43 +83,22 @@ public class Type implements Serializable { throw new IllegalArgumentException("don't know how to parse: " + in); } - + protected String m_name; - protected List m_parameters; public Type(String name) { - m_name = null; - m_parameters = Lists.newArrayList(); - int start = name.indexOf('<'); - int stop = name.lastIndexOf('>'); - if (start > 0 && stop > start) { - - // deal with generic parameters - m_name = name.substring(0, start) + name.substring(stop + 1); - - String parameters = name.substring(start + 1, stop); - int i=0; - while (i= 0 || name.indexOf('>') >= 0) { + throw new IllegalArgumentException("don't use with generic types or templates: " + name); } + + m_name = name; } public Type(Type other) { m_name = other.m_name; - m_parameters = Lists.newArrayList(); - for (Type parameter : other.m_parameters) { - m_parameters.add(new Type(parameter)); - } } public Type(ClassEntry classEntry) { @@ -141,11 +118,6 @@ public class Type implements Serializable { m_name = Type.getArrayPrefix(other.getArrayDimension()) + "L" + replacedName + ";"; } } - - m_parameters = Lists.newArrayList(); - for (Type parameter : other.m_parameters) { - m_parameters.add(new Type(parameter, replacer)); - } } @Override @@ -191,17 +163,6 @@ public class Type implements Serializable { } } - public boolean isTemplate() { - return m_name.charAt(0) == 'T' && m_name.charAt(m_name.length() - 1) == ';'; - } - - public String getTemplate() { - if (!isTemplate()) { - throw new IllegalStateException("not an template"); - } - return m_name.substring(1, m_name.length() - 1); - } - public boolean isArray() { return m_name.charAt(0) == '['; } @@ -232,14 +193,6 @@ public class Type implements Serializable { return isClass() || (isArray() && getArrayType().hasClass()); } - public boolean hasParameters() { - return !m_parameters.isEmpty(); - } - - public Iterable parameters() { - return m_parameters; - } - @Override public boolean equals(Object other) { if (other instanceof Type) { diff --git a/test/cuchaz/enigma/TestType.java b/test/cuchaz/enigma/TestType.java index 544a10c..71aaaee 100644 --- a/test/cuchaz/enigma/TestType.java +++ b/test/cuchaz/enigma/TestType.java @@ -6,7 +6,6 @@ import static org.hamcrest.Matchers.*; import org.junit.Test; -import cuchaz.enigma.mapping.ParameterizedType; import cuchaz.enigma.mapping.Type; @@ -24,7 +23,6 @@ public class TestType { assertThat(new Type("D").isVoid(), is(false)); assertThat(new Type("LFoo;").isVoid(), is(false)); assertThat(new Type("[I").isVoid(), is(false)); - assertThat(new Type("TFoo;").isVoid(), is(false)); } @Test @@ -39,7 +37,6 @@ public class TestType { assertThat(new Type("D").isPrimitive(), is(true)); assertThat(new Type("LFoo;").isPrimitive(), is(false)); assertThat(new Type("[I").isPrimitive(), is(false)); - assertThat(new Type("TFoo;").isPrimitive(), is(false)); } @Test @@ -64,26 +61,19 @@ public class TestType { assertThat(new Type("F").isClass(), is(false)); assertThat(new Type("D").isClass(), is(false)); assertThat(new Type("LFoo;").isClass(), is(true)); - assertThat(new Type("LFoo;").isClass(), is(true)); - assertThat(new Type("LFoo;").isClass(), is(true)); - assertThat(new Type("LFoo;>;").isClass(), is(true)); assertThat(new Type("[I").isClass(), is(false)); - assertThat(new Type("TFoo;").isClass(), is(false)); } @Test public void getClassEntry() { assertThat(new Type("LFoo;").getClassEntry(), is(newClass("Foo"))); - assertThat(new Type("LFoo;").getClassEntry(), is(newClass("Foo"))); - assertThat(new Type("LFoo;").getClassEntry(), is(newClass("Foo"))); - assertThat(new Type("LFoo;").getClassEntry(), is(newClass("Foo"))); - assertThat(new Type("LFoo;>;").getClassEntry(), is(newClass("Foo"))); + assertThat(new Type("Ljava/lang/String;").getClassEntry(), is(newClass("java/lang/String"))); } @Test public void getArrayClassEntry() { assertThat(new Type("[LFoo;").getClassEntry(), is(newClass("Foo"))); - assertThat(new Type("[[[LFoo;").getClassEntry(), is(newClass("Foo"))); + assertThat(new Type("[[[Ljava/lang/String;").getClassEntry(), is(newClass("java/lang/String"))); } @Test @@ -98,7 +88,6 @@ public class TestType { assertThat(new Type("D").isArray(), is(false)); assertThat(new Type("LFoo;").isArray(), is(false)); assertThat(new Type("[I").isArray(), is(true)); - assertThat(new Type("TFoo;").isArray(), is(false)); } @Test @@ -116,34 +105,10 @@ public class TestType { assertThat(new Type("[Ljava/lang/String;").getArrayType(), is(new Type("Ljava/lang/String;"))); } - @Test - public void isTemplate() { - assertThat(new Type("V").isTemplate(), is(false)); - assertThat(new Type("Z").isTemplate(), is(false)); - assertThat(new Type("B").isTemplate(), is(false)); - assertThat(new Type("C").isTemplate(), is(false)); - assertThat(new Type("I").isTemplate(), is(false)); - assertThat(new Type("J").isTemplate(), is(false)); - assertThat(new Type("F").isTemplate(), is(false)); - assertThat(new Type("D").isTemplate(), is(false)); - assertThat(new Type("LFoo;").isTemplate(), is(false)); - assertThat(new Type("LFoo;").isTemplate(), is(false)); - assertThat(new Type("LFoo;").isTemplate(), is(false)); - assertThat(new Type("LFoo;>;").isTemplate(), is(false)); - assertThat(new Type("[I").isTemplate(), is(false)); - assertThat(new Type("TFoo;").isTemplate(), is(true)); - } - - @Test - public void getTemplate() { - assertThat(new Type("TT;").getTemplate(), is("T")); - assertThat(new Type("TFoo;").getTemplate(), is("Foo")); - } - @Test public void hasClass() { assertThat(new Type("LFoo;").hasClass(), is(true)); - assertThat(new Type("LCow;").hasClass(), is(true)); + assertThat(new Type("Ljava/lang/String;").hasClass(), is(true)); assertThat(new Type("[LBar;").hasClass(), is(true)); assertThat(new Type("[[[LCat;").hasClass(), is(true)); @@ -151,37 +116,6 @@ public class TestType { assertThat(new Type("[I").hasClass(), is(false)); assertThat(new Type("[[[I").hasClass(), is(false)); assertThat(new Type("Z").hasClass(), is(false)); - assertThat(new Type("TFoo;").hasClass(), is(false)); - } - - @Test - public void parameters() { - assertThat(new Type("LFoo;").parameters(), contains( - new Type("I") - )); - assertThat(new Type("LFoo;").parameters(), contains( - new Type("I"), - new Type("I"), - new Type("I"), - new Type("I") - )); - assertThat(new Type("LFoo;").parameters(), contains( - new Type("LBar;") - )); - assertThat(new Type("LFoo;").parameters(), contains( - new Type("LBar;"), - new Type("LCow;"), - new Type("LCheese;") - )); - - assertThat(new Type("LFoo;>;").parameters(), contains( - new Type("LBar;") - )); - - assertThat(new Type("LFoo;>;").parameters().iterator().next().parameters(), contains( - new Type("LCow;"), - new Type("LCheese;") - )); } @Test @@ -218,36 +152,16 @@ public class TestType { assertThat(Type.parseFirst("LFoo;[LFoo;"), is(answer)); } { - final String answer = "LFoo;"; - assertThat(Type.parseFirst("LFoo;"), is(answer)); - assertThat(Type.parseFirst("LFoo;I"), is(answer)); - assertThat(Type.parseFirst("LFoo;JZ"), is(answer)); - assertThat(Type.parseFirst("LFoo;[I"), is(answer)); - assertThat(Type.parseFirst("LFoo;LFoo;"), is(answer)); - assertThat(Type.parseFirst("LFoo;[LFoo;"), is(answer)); - } - { - final String answer = "LFoo;"; - assertThat(Type.parseFirst("LFoo;"), is(answer)); - assertThat(Type.parseFirst("LFoo;I"), is(answer)); - assertThat(Type.parseFirst("LFoo;JZ"), is(answer)); - assertThat(Type.parseFirst("LFoo;[I"), is(answer)); - assertThat(Type.parseFirst("LFoo;LFoo;"), is(answer)); - assertThat(Type.parseFirst("LFoo;[LFoo;"), is(answer)); + final String answer = "Ljava/lang/String;"; + assertThat(Type.parseFirst("Ljava/lang/String;"), is(answer)); + assertThat(Type.parseFirst("Ljava/lang/String;I"), is(answer)); + assertThat(Type.parseFirst("Ljava/lang/String;JZ"), is(answer)); + assertThat(Type.parseFirst("Ljava/lang/String;[I"), is(answer)); + assertThat(Type.parseFirst("Ljava/lang/String;LFoo;"), is(answer)); + assertThat(Type.parseFirst("Ljava/lang/String;[LFoo;"), is(answer)); } } - @Test - public void parseTemplate() { - final String answer = "TFoo;"; - assertThat(Type.parseFirst("TFoo;"), is(answer)); - assertThat(Type.parseFirst("TFoo;I"), is(answer)); - assertThat(Type.parseFirst("TFoo;JZ"), is(answer)); - assertThat(Type.parseFirst("TFoo;[I"), is(answer)); - assertThat(Type.parseFirst("TFoo;LFoo;"), is(answer)); - assertThat(Type.parseFirst("TFoo;[LFoo;"), is(answer)); - } - @Test public void parseArray() { { @@ -290,9 +204,6 @@ public class TestType { assertThat(new Type("[I"), is(new Type("[I"))); assertThat(new Type("[[[I"), is(new Type("[[[I"))); assertThat(new Type("[LFoo;"), is(new Type("[LFoo;"))); - assertThat(new Type("LFoo;"), is(new Type("LFoo;"))); - assertThat(new Type("LFoo;"), is(new Type("LFoo;"))); - assertThat(new Type("TFoo;"), is(new Type("TFoo;"))); assertThat(new Type("V"), is(not(new Type("I")))); assertThat(new Type("I"), is(not(new Type("J")))); @@ -302,7 +213,6 @@ public class TestType { assertThat(new Type("[I"), is(not(new Type("[Z")))); assertThat(new Type("[[[I"), is(not(new Type("[I")))); assertThat(new Type("[LFoo;"), is(not(new Type("[LBar;")))); - assertThat(new Type("TFoo;"), is(not(new Type("TBar;")))); } @Test @@ -319,62 +229,5 @@ public class TestType { assertThat(new Type("[I").toString(), is("[I")); assertThat(new Type("[[[I").toString(), is("[[[I")); assertThat(new Type("[LFoo;").toString(), is("[LFoo;")); - assertThat(new Type("LFoo;").toString(), is("LFoo;")); - assertThat(new Type("LFoo;").toString(), is("LFoo;")); - assertThat(new Type("LFoo;>;").toString(), is("LFoo;")); - assertThat(new Type("TFoo;").toString(), is("TFoo;")); - } - - private ParameterizedType ptype(String name) { - return new ParameterizedType(new Type(name)); - } - - @Test - public void equalsWithParameters() { - assertThat(ptype("V"), is(ptype("V"))); - assertThat(ptype("Z"), is(ptype("Z"))); - assertThat(ptype("B"), is(ptype("B"))); - assertThat(ptype("C"), is(ptype("C"))); - assertThat(ptype("I"), is(ptype("I"))); - assertThat(ptype("J"), is(ptype("J"))); - assertThat(ptype("F"), is(ptype("F"))); - assertThat(ptype("D"), is(ptype("D"))); - assertThat(ptype("LFoo;"), is(ptype("LFoo;"))); - assertThat(ptype("[I"), is(ptype("[I"))); - assertThat(ptype("[[[I"), is(ptype("[[[I"))); - assertThat(ptype("[LFoo;"), is(ptype("[LFoo;"))); - assertThat(ptype("LFoo;"), is(ptype("LFoo;"))); - assertThat(ptype("TFoo;"), is(ptype("TFoo;"))); - - assertThat(ptype("V"), is(not(ptype("I")))); - assertThat(ptype("I"), is(not(ptype("J")))); - assertThat(ptype("I"), is(not(ptype("LBar;")))); - assertThat(ptype("I"), is(not(ptype("[I")))); - assertThat(ptype("LFoo;"), is(not(ptype("LBar;")))); - assertThat(ptype("[I"), is(not(ptype("[Z")))); - assertThat(ptype("[[[I"), is(not(ptype("[I")))); - assertThat(ptype("[LFoo;"), is(not(ptype("[LBar;")))); - assertThat(ptype("LFoo;"), is(not(ptype("LFoo;")))); - assertThat(ptype("TFoo;"), is(not(ptype("TBar;")))); - } - - @Test - public void testToStringWithParams() { - assertThat(ptype("V").toString(), is("V")); - assertThat(ptype("Z").toString(), is("Z")); - assertThat(ptype("B").toString(), is("B")); - assertThat(ptype("C").toString(), is("C")); - assertThat(ptype("I").toString(), is("I")); - assertThat(ptype("J").toString(), is("J")); - assertThat(ptype("F").toString(), is("F")); - assertThat(ptype("D").toString(), is("D")); - assertThat(ptype("LFoo;").toString(), is("LFoo;")); - assertThat(ptype("[I").toString(), is("[I")); - assertThat(ptype("[[[I").toString(), is("[[[I")); - assertThat(ptype("[LFoo;").toString(), is("[LFoo;")); - assertThat(ptype("LFoo;").toString(), is("LFoo;")); - assertThat(ptype("LFoo;").toString(), is("LFoo;")); - assertThat(ptype("LFoo;>;").toString(), is("LFoo;>;")); - assertThat(ptype("TFoo;").toString(), is("TFoo;")); } } -- cgit v1.2.3