1 32 33 package com.jeantessier.classreader; 34 35 import java.util.*; 36 37 import org.apache.log4j.*; 38 import org.apache.oro.text.perl.*; 39 40 public final class SignatureHelper { 41 private static final Perl5Util perl = new Perl5Util(); 42 43 private static Map conversion = new HashMap(); 44 45 static { 46 conversion.put("B", "byte"); 47 conversion.put("C", "char"); 48 conversion.put("D", "double"); 49 conversion.put("F", "float"); 50 conversion.put("I", "int"); 51 conversion.put("J", "long"); 52 conversion.put("S", "short"); 53 conversion.put("V", "void"); 54 conversion.put("Z", "boolean"); 55 } 56 57 static String convert(String type) { 58 String result = null; 59 60 Logger.getLogger(SignatureHelper.class).debug("Begin Convert(\"" + type + "\")"); 61 62 if (type.length() == 1) { 63 result = (String ) conversion.get(type); 64 } else if (type.charAt(0) == 'L') { 65 result = path2ClassName(type.substring(1, type.indexOf(';'))); 66 } else if (type.charAt(0) == '[') { 67 result = convert(type.substring(1)) + "[]"; 68 } 69 70 Logger.getLogger(SignatureHelper.class).debug("End Convert(\"" + type + "\"): \"" + result + "\""); 71 72 return result; 73 } 74 75 public static String path2ClassName(String path) { 76 return perl.substitute("s/\\//./g", path); 77 } 78 79 public static String getSignature(String descriptor) { 80 StringBuffer result = new StringBuffer (); 81 82 Logger.getLogger(SignatureHelper.class).debug("Begin Signature(\"" + descriptor + "\")"); 83 84 result.append("("); 85 86 int start = descriptor.indexOf("(") + 1; 87 int end = descriptor.indexOf(")"); 88 89 SignatureIterator i = new SignatureIterator(descriptor.substring(start, end)); 90 while (i.hasNext()) { 91 result.append(i.next()); 92 if (i.hasNext()) { 93 result.append(", "); 94 } 95 } 96 97 result.append(")"); 98 99 Logger.getLogger(SignatureHelper.class).debug("End Signature(\"" + descriptor + "\"): \"" + result + "\""); 100 101 return result.toString(); 102 } 103 104 public static int getParameterCount(String descriptor) { 105 int result = 0; 106 107 Logger.getLogger(SignatureHelper.class).debug("Begin ParameterCount(\"" + descriptor + "\")"); 108 109 int start = descriptor.indexOf("(") + 1; 110 int end = descriptor.indexOf(")"); 111 112 SignatureIterator i = new SignatureIterator(descriptor.substring(start, end)); 113 while (i.hasNext()) { 114 i.next(); 115 result++; 116 } 117 118 Logger.getLogger(SignatureHelper.class).debug("End ParameterCount(\"" + descriptor + "\"): \"" + result + "\""); 119 120 return result; 121 } 122 123 public static String getReturnType(String descriptor) { 124 return convert(descriptor.substring(descriptor.lastIndexOf(")") + 1)); 125 } 126 127 public static String getType(String descriptor) { 128 return convert(descriptor); 129 } 130 } 131 132 class SignatureIterator implements Iterator { 133 private String descriptor; 134 private int currentPos = 0; 135 136 public SignatureIterator(String descriptor) { 137 this.descriptor = descriptor; 138 } 139 140 public boolean hasNext() { 141 return currentPos < descriptor.length(); 142 } 143 144 public Object next() { 145 String result; 146 147 if (hasNext()) { 148 int nextPos = currentPos; 149 150 while (descriptor.charAt(nextPos) == '[') { 151 nextPos++; 152 } 153 154 if (descriptor.charAt(nextPos) == 'L') { 155 nextPos = descriptor.indexOf(";", nextPos); 156 } 157 158 result = SignatureHelper.convert(descriptor.substring(currentPos, nextPos + 1)); 159 160 currentPos = nextPos + 1; 161 } else { 162 throw new NoSuchElementException(); 163 } 164 165 return result; 166 } 167 168 public void remove() { 169 throw new UnsupportedOperationException (); 170 } 171 } 172 | Popular Tags |