KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > util > JavaClass


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.util;
20
21 import gcc.properties.*;
22 import java.io.*;
23 import java.util.*;
24
25 public abstract class JavaClass
26 {
27     // -----------------------------------------------------------------------
28
// properties
29
// -----------------------------------------------------------------------
30

31     public static final StringProperty classDirProperty =
32         new StringProperty(SystemProperties.class, "gcc.classDir")
33         .defaultValue(FileUtil.pretty(SystemProperties.getHome() + "/genfiles/java/classes"));
34
35     public static final StringProperty classPathProperty =
36         new StringProperty(SystemProperties.class, "gcc.classPath")
37         .defaultValue(FileUtil.pretty(SystemProperties.getHome() + "/genfiles/java/classes"));
38
39     public static final StringProperty sourceDirProperty =
40         new StringProperty(SystemProperties.class, "gcc.sourceDir")
41         .defaultValue(FileUtil.pretty(SystemProperties.getHome() + "/genfiles/java/src"));
42
43     public static final StringProperty sourcePathProperty =
44         new StringProperty(SystemProperties.class, "gcc.sourcePath")
45         .defaultValue(FileUtil.pretty(SystemProperties.getHome() + "/src/java")
46         + File.pathSeparator
47         + FileUtil.pretty(SystemProperties.getHome() + "/genfiles/java/src"));
48
49     // -----------------------------------------------------------------------
50
// private data
51
// -----------------------------------------------------------------------
52

53     private static String _classDir = classDirProperty.getString();
54
55     private static List _classPath = ListUtil.getPathList(classPathProperty.getString());
56
57     private static String _sourceDir = sourceDirProperty.getString();
58
59     private static List _sourcePath = ListUtil.getPathList(sourcePathProperty.getString());
60
61     // -----------------------------------------------------------------------
62
// public methods
63
// -----------------------------------------------------------------------
64

65     public static String addPackageSuffix(String className, String suffix)
66     {
67         String jp = getPackagePrefix(className);
68         if (jp.length() == 0)
69         {
70             jp = suffix;
71         }
72         else
73         {
74             jp += "." + suffix;
75         }
76         return jp + "." + getNameSuffix(className);
77     }
78
79     public static String getClassDir()
80     {
81         return _classDir;
82     }
83
84     public static List getClassPath()
85     {
86         return _classPath;
87     }
88
89     public static File getClassFile(Class theClass)
90     {
91         return getClassFile(theClass.getName());
92     }
93
94     public static File getClassFile(String className)
95     {
96         for (Iterator i = _classPath.iterator(); i.hasNext();)
97         {
98             String dir = (String)i.next();
99             String fileName = FileUtil.pretty(dir + "/" + className.replace('.', '/') + ".class");
100             File classFile = new File(fileName);
101             if (classFile.exists())
102             {
103                 return classFile;
104             }
105         }
106         return null;
107     }
108
109     public static String getName(String packagePrefix, String nameSuffix)
110     {
111         if (packagePrefix == null || packagePrefix.length() == 0)
112         {
113             return nameSuffix;
114         }
115         else
116         {
117             return packagePrefix + "." + nameSuffix;
118         }
119     }
120
121     public static String getNamePrefix(String className)
122     {
123         return StringUtil.beforeLast(".", className);
124     }
125
126     public static String getNameSuffix(String className)
127     {
128         return StringUtil.afterLast(".", className);
129     }
130
131     public static String getPackagePrefix(String className)
132     {
133         return getNamePrefix(className);
134     }
135
136     /**
137      ** Compute the JVM signature for a class.
138      **/

139     public static String getSignature(Class clazz)
140     {
141         String type = null;
142         if (clazz.isArray())
143         {
144             Class cl = clazz;
145             int dimensions = 0;
146             while (cl.isArray())
147             {
148                 dimensions++;
149                 cl = cl.getComponentType();
150             }
151             StringBuffer sb = new StringBuffer();
152             for (int i = 0; i < dimensions; i++)
153             {
154                 sb.append("[");
155             }
156             sb.append(getSignature(cl));
157             type = sb.toString();
158         }
159         else if (clazz.isPrimitive())
160         {
161             if (clazz == Integer.TYPE)
162             {
163                 type = "I";
164             }
165             else if (clazz == Byte.TYPE)
166             {
167                 type = "B";
168             }
169             else if (clazz == Long.TYPE)
170             {
171                 type = "J";
172             }
173             else if (clazz == Float.TYPE)
174             {
175                 type = "F";
176             }
177             else if (clazz == Double.TYPE)
178             {
179                 type = "D";
180             }
181             else if (clazz == Short.TYPE)
182             {
183                 type = "S";
184             }
185             else if (clazz == Character.TYPE)
186             {
187                 type = "C";
188             }
189             else if (clazz == Boolean.TYPE)
190             {
191                 type = "Z";
192             }
193             else if (clazz == Void.TYPE)
194             {
195                 type = "V";
196             }
197
198         }
199         else
200         {
201             type = "L" + clazz.getName().replace('.', '/') + ";";
202         }
203         return type;
204     }
205
206     public static String getSourceDir()
207     {
208         return _sourceDir;
209     }
210
211     public static File getSourceFile(Class theClass)
212     {
213         return getSourceFile(theClass.getName());
214     }
215
216     public static File getSourceFile(String className)
217     {
218         for (Iterator i = _sourcePath.iterator();;)
219         {
220             String dir;
221             if (i.hasNext())
222             {
223                 dir = (String)i.next();
224             }
225             else
226             {
227                 dir = _sourceDir;
228             }
229             String fileName = FileUtil.pretty(dir + "/" + className.replace('.', '/') + ".java");
230             File sourceFile = new File(fileName);
231             if (sourceFile.exists())
232             {
233                 return sourceFile;
234             }
235             if (dir == _sourceDir)
236             {
237                 break;
238             }
239         }
240         return null;
241     }
242
243     public static List getSourcePath()
244     {
245         return _sourcePath;
246     }
247 }
248
Popular Tags