1 13 14 15 package org.aspectj.runtime.reflect; 16 17 import org.aspectj.lang.Signature; 18 19 import java.util.StringTokenizer ; 20 21 abstract class SignatureImpl implements Signature { 22 23 private static boolean useCache = true; 24 25 int modifiers = -1; 26 String name; 27 String declaringTypeName; 28 Class declaringType; 29 Cache stringCache; 30 31 SignatureImpl(int modifiers, String name, Class declaringType) { 32 this.modifiers = modifiers; 33 this.name = name; 34 this.declaringType = declaringType; 35 } 36 37 protected abstract String createToString (StringMaker sm); 38 39 40 String toString (StringMaker sm) { 41 String result = null; 42 if (useCache) { 43 if (stringCache == null) { 44 try { 45 stringCache = new CacheImpl(); 46 } catch (Throwable t) { 47 useCache = false; 48 } 49 } else { 50 result = stringCache.get(sm.cacheOffset); 51 } 52 } 53 if (result == null) { 54 result = createToString(sm); 55 } 56 if (useCache) { 57 stringCache.set(sm.cacheOffset, result); 58 } 59 return result; 60 } 61 62 public final String toString() { return toString(StringMaker.middleStringMaker); } 63 public final String toShortString() { return toString(StringMaker.shortStringMaker); } 64 public final String toLongString() { return toString(StringMaker.longStringMaker); } 65 66 public int getModifiers() { 67 if (modifiers == -1) modifiers = extractInt(0); 68 return modifiers; 69 } 70 public String getName() { 71 if (name == null) name = extractString(1); 72 return name; 73 } 74 public Class getDeclaringType() { 75 if (declaringType == null) declaringType = extractType(2); 76 return declaringType; 77 } 78 public String getDeclaringTypeName() { 79 if (declaringTypeName == null) { 80 declaringTypeName = getDeclaringType().getName(); 81 } 82 return declaringTypeName; 83 } 84 85 String fullTypeName(Class type) { 86 if (type == null) return "ANONYMOUS"; 87 if (type.isArray()) return fullTypeName(type.getComponentType()) + "[]"; 88 return type.getName().replace('$', '.'); 89 } 90 91 String stripPackageName(String name) { 92 int dot = name.lastIndexOf('.'); 93 if (dot == -1) return name; 94 return name.substring(dot+1); 95 } 96 97 String shortTypeName(Class type) { 98 if (type == null) return "ANONYMOUS"; 99 if (type.isArray()) return shortTypeName(type.getComponentType()) + "[]"; 100 return stripPackageName(type.getName()).replace('$', '.'); 101 } 102 103 void addFullTypeNames(StringBuffer buf, Class [] types) { 104 for (int i = 0; i < types.length; i++) { 105 if (i > 0) buf.append(", "); 106 buf.append(fullTypeName(types[i])); 107 } 108 } 109 void addShortTypeNames(StringBuffer buf, Class [] types) { 110 for (int i = 0; i < types.length; i++) { 111 if (i > 0) buf.append(", "); 112 buf.append(shortTypeName(types[i])); 113 } 114 } 115 116 void addTypeArray(StringBuffer buf, Class [] types) { 117 addFullTypeNames(buf, types); 118 } 119 120 private String stringRep; 122 ClassLoader lookupClassLoader = null; 123 124 public void setLookupClassLoader(ClassLoader loader) { 125 this.lookupClassLoader = loader; 126 } 127 128 private ClassLoader getLookupClassLoader() { 129 if (lookupClassLoader == null) lookupClassLoader = this.getClass().getClassLoader(); 130 return lookupClassLoader; 131 } 132 133 public SignatureImpl(String stringRep) { 134 this.stringRep = stringRep; 135 } 136 137 static final char SEP = '-'; 138 139 String extractString(int n) { 140 142 int startIndex = 0; 143 int endIndex = stringRep.indexOf(SEP); 144 while (n-- > 0) { 145 startIndex = endIndex+1; 146 endIndex = stringRep.indexOf(SEP, startIndex); 147 } 148 if (endIndex == -1) endIndex = stringRep.length(); 149 150 152 return stringRep.substring(startIndex, endIndex); 153 } 154 155 int extractInt(int n) { 156 String s = extractString(n); 157 return Integer.parseInt(s, 16); 158 } 159 160 Class extractType(int n) { 161 String s = extractString(n); 162 return Factory.makeClass(s,getLookupClassLoader()); 163 } 164 165 166 167 static String [] EMPTY_STRING_ARRAY = new String [0]; 168 static Class [] EMPTY_CLASS_ARRAY = new Class [0]; 169 170 static final String INNER_SEP = ":"; 171 172 String [] extractStrings(int n) { 173 String s = extractString(n); 174 StringTokenizer st = new StringTokenizer (s, INNER_SEP); 175 final int N = st.countTokens(); 176 String [] ret = new String [N]; 177 for (int i = 0; i < N; i++) ret[i]= st.nextToken(); 178 return ret; 179 } 180 Class [] extractTypes(int n) { 181 String s = extractString(n); 182 StringTokenizer st = new StringTokenizer (s, INNER_SEP); 183 final int N = st.countTokens(); 184 Class [] ret = new Class [N]; 185 for (int i = 0; i < N; i++) ret[i]= Factory.makeClass(st.nextToken(),getLookupClassLoader()); 186 return ret; 187 } 188 189 192 static void setUseCache (boolean b) { 193 useCache = b; 194 } 195 196 static boolean getUseCache () { 197 return useCache; 198 } 199 200 private static interface Cache { 201 202 String get(int cacheOffset); 203 204 void set(int cacheOffset, String result); 205 206 } 207 208 private static final class CacheImpl implements Cache { 210 private java.lang.ref.SoftReference toStringCacheRef; 211 212 public CacheImpl() { 213 makeCache(); 214 } 215 216 public String get(int cacheOffset) { 217 String [] cachedArray = array(); 218 if (cachedArray == null) { 219 return null; 220 } 221 return cachedArray[cacheOffset]; 222 } 223 224 public void set(int cacheOffset, String result) { 225 String [] cachedArray = array(); 226 if (cachedArray == null) { 227 cachedArray = makeCache(); 228 } 229 cachedArray[cacheOffset] = result; 230 } 231 232 private String [] array() { 233 return (String []) toStringCacheRef.get(); 234 } 235 236 private String [] makeCache() { 237 String [] array = new String [3]; 238 toStringCacheRef = new java.lang.ref.SoftReference (array); 239 return array; 240 } 241 242 } 243 } 244 | Popular Tags |