KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.Modifier JavaDoc;
18
19 class StringMaker {
20     boolean shortTypeNames = true;
21     boolean includeArgs = true;
22     boolean includeThrows = false;
23     boolean includeModifiers = false;
24     boolean shortPrimaryTypeNames = false;
25     
26     boolean includeJoinPointTypeName = true;
27     boolean includeEnclosingPoint = true;
28     boolean shortKindName = true;
29     int cacheOffset;
30     
31     static StringMaker shortStringMaker;
32     static {
33         shortStringMaker = new StringMaker();
34         shortStringMaker.shortTypeNames = true;
35         shortStringMaker.includeArgs = false;
36         shortStringMaker.includeThrows = false;
37         shortStringMaker.includeModifiers = false;
38         shortStringMaker.shortPrimaryTypeNames = true;
39         
40         shortStringMaker.includeJoinPointTypeName = false;
41         shortStringMaker.includeEnclosingPoint = false;
42         
43         shortStringMaker.cacheOffset = 0;
44     }
45     
46     static StringMaker middleStringMaker;
47     static {
48         middleStringMaker = new StringMaker();
49         middleStringMaker.shortTypeNames = true;
50         middleStringMaker.includeArgs = true;
51         middleStringMaker.includeThrows = false;
52         middleStringMaker.includeModifiers = false;
53         middleStringMaker.shortPrimaryTypeNames = false;
54
55         shortStringMaker.cacheOffset = 1;
56     }
57     
58     static StringMaker longStringMaker;
59     static {
60         longStringMaker = new StringMaker();
61         longStringMaker.shortTypeNames = false;
62         longStringMaker.includeArgs = true;
63         longStringMaker.includeThrows = false;
64         longStringMaker.includeModifiers = true;
65         longStringMaker.shortPrimaryTypeNames = false;
66         longStringMaker.shortKindName = false;
67
68         longStringMaker.cacheOffset = 2;
69     }
70     
71     String JavaDoc makeKindName(String JavaDoc name) {
72         int dash = name.lastIndexOf('-');
73         if (dash == -1) return name;
74         return name.substring(dash+1);
75     }
76     
77     String JavaDoc makeModifiersString(int modifiers) {
78         if (!includeModifiers) return "";
79         String JavaDoc str = Modifier.toString(modifiers);
80         if (str.length() == 0) return "";
81         return str + " ";
82     }
83     
84     String JavaDoc stripPackageName(String JavaDoc name) {
85         int dot = name.lastIndexOf('.');
86         if (dot == -1) return name;
87         return name.substring(dot+1);
88     }
89     
90     String JavaDoc makeTypeName(Class JavaDoc type, String JavaDoc typeName, boolean shortName) {
91         if (type == null) return "ANONYMOUS";
92         if (type.isArray()) {
93             Class JavaDoc componentType = type.getComponentType();
94             return makeTypeName(componentType, componentType.getName(), shortName) + "[]";
95         }
96         if (shortName) {
97             return stripPackageName(typeName).replace('$', '.');
98         } else {
99             return typeName.replace('$', '.');
100         }
101     }
102        
103     public String JavaDoc makeTypeName(Class JavaDoc type) {
104         return makeTypeName(type, type.getName(),shortTypeNames);
105     }
106     
107     public String JavaDoc makePrimaryTypeName(Class JavaDoc type, String JavaDoc typeName) {
108         return makeTypeName(type, typeName, shortPrimaryTypeNames);
109     }
110     
111     public void addTypeNames(StringBuffer JavaDoc buf, Class JavaDoc[] types) {
112         for (int i = 0; i < types.length; i++) {
113             if (i > 0) buf.append(", ");
114             buf.append(makeTypeName(types[i]));
115         }
116     }
117     
118     public void addSignature(StringBuffer JavaDoc buf, Class JavaDoc[] types) {
119         if (types == null) return;
120         if (!includeArgs) {
121             if (types.length == 0) {
122                 buf.append("()");
123                 return;
124             } else {
125                 buf.append("(..)");
126                 return;
127             }
128         }
129         buf.append("(");
130         addTypeNames(buf, types);
131         buf.append(")");
132     }
133     
134     public void addThrows(StringBuffer JavaDoc buf, Class JavaDoc[] types) {
135         if (!includeThrows || types == null || types.length == 0) return;
136
137         buf.append(" throws ");
138         addTypeNames(buf, types);
139     }
140 }
141
Popular Tags