KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > runtime > reflect > SignatureImpl


1 /* *******************************************************************
2  * Copyright (c) 1999-2001 Xerox Corporation,
3  * 2002 Palo Alto Research Center, Incorporated (PARC).
4  * All rights reserved.
5  * This program and the accompanying materials are made available
6  * under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * Xerox/PARC initial implementation
12  * ******************************************************************/

13
14
15 package org.aspectj.runtime.reflect;
16
17 import org.aspectj.lang.Signature;
18
19 import java.util.StringTokenizer JavaDoc;
20
21 abstract class SignatureImpl implements Signature {
22
23     private static boolean useCache = true;
24     
25     int modifiers = -1;
26     String JavaDoc name;
27     String JavaDoc declaringTypeName;
28     Class JavaDoc declaringType;
29     Cache stringCache;
30     
31     SignatureImpl(int modifiers, String JavaDoc name, Class JavaDoc declaringType) {
32         this.modifiers = modifiers;
33         this.name = name;
34         this.declaringType = declaringType;
35     }
36     
37     protected abstract String JavaDoc createToString (StringMaker sm);
38     
39     /* Use a soft cache for the short, middle and long String representations */
40     String JavaDoc toString (StringMaker sm) {
41         String JavaDoc result = null;
42         if (useCache) {
43             if (stringCache == null) {
44                 try {
45                     stringCache = new CacheImpl();
46                 } catch (Throwable JavaDoc 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 JavaDoc toString() { return toString(StringMaker.middleStringMaker); }
63     public final String JavaDoc toShortString() { return toString(StringMaker.shortStringMaker); }
64     public final String JavaDoc toLongString() { return toString(StringMaker.longStringMaker); }
65
66     public int getModifiers() {
67         if (modifiers == -1) modifiers = extractInt(0);
68         return modifiers;
69     }
70     public String JavaDoc getName() {
71         if (name == null) name = extractString(1);
72         return name;
73     }
74     public Class JavaDoc getDeclaringType() {
75         if (declaringType == null) declaringType = extractType(2);
76         return declaringType;
77     }
78     public String JavaDoc getDeclaringTypeName() {
79         if (declaringTypeName == null) {
80             declaringTypeName = getDeclaringType().getName();
81         }
82         return declaringTypeName;
83     }
84     
85     String JavaDoc fullTypeName(Class JavaDoc type) {
86         if (type == null) return "ANONYMOUS";
87         if (type.isArray()) return fullTypeName(type.getComponentType()) + "[]";
88         return type.getName().replace('$', '.');
89     }
90     
91     String JavaDoc stripPackageName(String JavaDoc name) {
92         int dot = name.lastIndexOf('.');
93         if (dot == -1) return name;
94         return name.substring(dot+1);
95     }
96     
97     String JavaDoc shortTypeName(Class JavaDoc 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 JavaDoc buf, Class JavaDoc[] 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 JavaDoc buf, Class JavaDoc[] 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 JavaDoc buf, Class JavaDoc[] types) {
117         addFullTypeNames(buf, types);
118     }
119     
120     // lazy version
121
private String JavaDoc stringRep;
122     ClassLoader JavaDoc lookupClassLoader = null;
123     
124     public void setLookupClassLoader(ClassLoader JavaDoc loader) {
125         this.lookupClassLoader = loader;
126     }
127     
128     private ClassLoader JavaDoc getLookupClassLoader() {
129         if (lookupClassLoader == null) lookupClassLoader = this.getClass().getClassLoader();
130         return lookupClassLoader;
131     }
132     
133     public SignatureImpl(String JavaDoc stringRep) {
134         this.stringRep = stringRep;
135     }
136     
137     static final char SEP = '-';
138    
139     String JavaDoc extractString(int n) {
140         //System.out.println(n + ": from " + stringRep);
141

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         //System.out.println(" " + stringRep.substring(startIndex, endIndex));
151

152         return stringRep.substring(startIndex, endIndex);
153     }
154     
155     int extractInt(int n) {
156         String JavaDoc s = extractString(n);
157         return Integer.parseInt(s, 16);
158     }
159     
160     Class JavaDoc extractType(int n) {
161         String JavaDoc s = extractString(n);
162         return Factory.makeClass(s,getLookupClassLoader());
163     }
164     
165  
166     
167     static String JavaDoc[] EMPTY_STRING_ARRAY = new String JavaDoc[0];
168     static Class JavaDoc[] EMPTY_CLASS_ARRAY = new Class JavaDoc[0];
169     
170     static final String JavaDoc INNER_SEP = ":";
171     
172     String JavaDoc[] extractStrings(int n) {
173         String JavaDoc s = extractString(n);
174         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s, INNER_SEP);
175         final int N = st.countTokens();
176         String JavaDoc[] ret = new String JavaDoc[N];
177         for (int i = 0; i < N; i++) ret[i]= st.nextToken();
178         return ret;
179     }
180     Class JavaDoc[] extractTypes(int n) {
181         String JavaDoc s = extractString(n);
182         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s, INNER_SEP);
183         final int N = st.countTokens();
184         Class JavaDoc[] ret = new Class JavaDoc[N];
185         for (int i = 0; i < N; i++) ret[i]= Factory.makeClass(st.nextToken(),getLookupClassLoader());
186         return ret;
187     }
188
189     /*
190      * Used for testing
191      */

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 JavaDoc get(int cacheOffset);
203
204         void set(int cacheOffset, String JavaDoc result);
205         
206     }
207     
208     // separate implementation so we don't need SoftReference to hold the field...
209
private static final class CacheImpl implements Cache {
210         private java.lang.ref.SoftReference JavaDoc toStringCacheRef;
211
212         public CacheImpl() {
213             makeCache();
214         }
215
216         public String JavaDoc get(int cacheOffset) {
217             String JavaDoc[] cachedArray = array();
218             if (cachedArray == null) {
219                 return null;
220             }
221             return cachedArray[cacheOffset];
222         }
223
224         public void set(int cacheOffset, String JavaDoc result) {
225             String JavaDoc[] cachedArray = array();
226             if (cachedArray == null) {
227                 cachedArray = makeCache();
228             }
229             cachedArray[cacheOffset] = result;
230         }
231
232         private String JavaDoc[] array() {
233             return (String JavaDoc[]) toStringCacheRef.get();
234         }
235
236         private String JavaDoc[] makeCache() {
237             String JavaDoc[] array = new String JavaDoc[3];
238             toStringCacheRef = new java.lang.ref.SoftReference JavaDoc(array);
239             return array;
240         }
241
242     }
243 }
244
Popular Tags