1 20 21 package net.innig.macker.util; 22 23 import java.util.*; 24 25 import org.apache.regexp.RE; 26 import org.apache.regexp.RESyntaxException; 27 28 public class ClassNameTranslator 29 { 30 static public boolean isJavaIdentifier(String className) 31 { return legalJavaIdentRE.match(className); } 32 33 static public List signatureToClassNames(String signature) 34 { 35 List names = new ArrayList(); 36 for(int pos = 0; pos < signature.length(); ) 37 { 38 String remaining = signature.substring(pos); 39 if(!sigExtractorRE.match(remaining)) 40 throw new IllegalArgumentException ("Unable to extract type info from: " + remaining); 41 if(sigExtractorRE.getParen(2) != null) 42 names.add((String ) primitiveTypeMap.get(sigExtractorRE.getParen(2))); 43 if(sigExtractorRE.getParen(3) != null) 44 names.add(resourceToClassName(sigExtractorRE.getParen(3))); 45 pos += sigExtractorRE.getParenEnd(0); 46 } 47 return names; 48 } 49 50 static public String typeConstantToClassName(String typeName) 51 { 52 if(arrayExtractorRE.match(typeName)) 53 { 54 if(arrayExtractorRE.getParen(2) != null) 55 return (String ) primitiveTypeMap.get(arrayExtractorRE.getParen(2)); 56 if(arrayExtractorRE.getParen(3) != null) 57 return resourceToClassName(arrayExtractorRE.getParen(3)); 58 } 59 return resourceToClassName(typeName); 60 } 61 62 static public String resourceToClassName(String className) 63 { return slashRE.subst(classSuffixRE.subst(className, ""), ".").intern(); } 64 65 static public String classToResourceName(String resourceName) 66 { return (dotRE.subst(resourceName, "/") + ".class").intern(); } 67 68 static private RE classSuffixRE, slashRE, dotRE, arrayExtractorRE, sigExtractorRE, legalJavaIdentRE; 69 static private Map primitiveTypeMap; 70 static 71 { 72 try { 73 classSuffixRE = new RE("\\.class$"); 74 slashRE = new RE("/"); 75 dotRE = new RE("\\."); 76 arrayExtractorRE = new RE( "^(\\[+([BSIJCFDZV])|\\[+L([^;]*);)$"); 77 sigExtractorRE = new RE("^\\(?\\)?(\\[*([BSIJCFDZV])|\\[*L([^;]*);)"); 78 legalJavaIdentRE = new RE("^([:javastart:][:javapart:]*)(\\.([:javastart:][:javapart:]*))*$"); 79 } 80 catch(RESyntaxException rese) 81 { throw new RuntimeException ("Can't initialize ClassNameTranslator: " + rese); } 82 83 primitiveTypeMap = new HashMap(); 84 primitiveTypeMap.put("B", "byte"); 85 primitiveTypeMap.put("S", "short"); 86 primitiveTypeMap.put("I", "int"); 87 primitiveTypeMap.put("J", "long"); 88 primitiveTypeMap.put("C", "char"); 89 primitiveTypeMap.put("F", "float"); 90 primitiveTypeMap.put("D", "double"); 91 primitiveTypeMap.put("Z", "boolean"); 92 primitiveTypeMap.put("V", "void"); 93 } 94 } 95 | Popular Tags |