1 32 package net.sf.retrotranslator.transformer; 33 34 import net.sf.retrotranslator.runtime.asm.Type; 35 import net.sf.retrotranslator.runtime.asm.signature.*; 36 37 40 class NameTranslator { 41 42 protected String identifier(String s) { 43 return s; 44 } 45 46 protected String typeName(String s) { 47 return s; 48 } 49 50 protected final Type type(Type type) { 51 if (type == null) { 52 return null; 53 } 54 if (type.getSort() == Type.OBJECT) { 55 return TransformerTools.getTypeByInternalName(typeName(type.getInternalName())); 56 } 57 if (type.getSort() == Type.ARRAY) { 58 Type elementType = type.getElementType(); 59 if (elementType.getSort() == Type.OBJECT) { 60 return TransformerTools.getArrayTypeByInternalName( 61 typeName(elementType.getInternalName()), type.getDimensions()); 62 } 63 } 64 return type; 65 } 66 67 protected final String typeDescriptor(String s) { 68 return s == null ? null : type(Type.getType(s)).getDescriptor(); 69 } 70 71 protected final String methodDescriptor(String s) { 72 return s == null ? null : Type.getMethodDescriptor( 73 type(Type.getReturnType(s)), 74 types(Type.getArgumentTypes(s))); 75 } 76 77 protected final String [] typeNames(String [] names) { 78 if (names == null) return null; 79 for (int i = 0; i < names.length; i++) { 80 names[i] = typeName(names[i]); 81 } 82 return names; 83 } 84 85 protected final Type[] types(Type[] types) { 86 if (types == null) return null; 87 for (int i = 0; i < types.length; i++) { 88 types[i] = type(types[i]); 89 } 90 return types; 91 } 92 93 protected final Object typeOrValue(Object object) { 94 return object instanceof Type ? type((Type) object) : object; 95 } 96 97 protected final String typeNameOrTypeDescriptor(String s) { 98 return s != null && s.startsWith("[") ? typeDescriptor(s) : typeName(s); 99 } 100 101 protected final String typeSignature(String s) { 102 if (s == null) return null; 103 SignatureWriter writer = new SignatureWriter(); 104 new SignatureReader(s).acceptType(new TranslatingSignatureVisitor(writer)); 105 return writer.toString(); 106 } 107 108 protected final String declarationSignature(String s) { 109 if (s == null) return null; 110 SignatureWriter writer = new SignatureWriter(); 111 new SignatureReader(s).accept(new TranslatingSignatureVisitor(writer)); 112 return writer.toString(); 113 } 114 115 private class TranslatingSignatureVisitor extends SignatureAdapter { 116 117 public TranslatingSignatureVisitor(SignatureVisitor visitor) { 118 super(visitor); 119 } 120 121 protected SignatureVisitor visitStart(SignatureVisitor visitor) { 122 return new TranslatingSignatureVisitor(visitor); 123 } 124 125 public void visitClassType(String name) { 126 super.visitClassType(typeNameOrTypeDescriptor(name)); 127 } 128 129 public void visitInnerClassType(String name) { 130 super.visitInnerClassType(identifier(name)); 131 } 132 } 133 134 } 135 | Popular Tags |