KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > util > JavaClass


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
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 implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.util;
19
20 import java.io.File JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.apache.geronimo.interop.properties.StringProperty;
25 import org.apache.geronimo.interop.properties.SystemProperties;
26
27
28 public abstract class JavaClass {
29
30     public static final StringProperty classDirProperty =
31             new StringProperty(SystemProperties.class, "org.apache.geronimo.interop.classDir")
32             .defaultValue(FileUtil.pretty(SystemProperties.getHome() + "/genfiles/java/classes"));
33
34     public static final StringProperty classPathProperty =
35             new StringProperty(SystemProperties.class, "org.apache.geronimo.interop.classPath")
36             .defaultValue(FileUtil.pretty(SystemProperties.getHome() + "/genfiles/java/classes"));
37
38     public static final StringProperty sourceDirProperty =
39             new StringProperty(SystemProperties.class, "org.apache.geronimo.interop.sourceDir")
40             .defaultValue(FileUtil.pretty(SystemProperties.getHome() + "/genfiles/java/src"));
41
42     public static final StringProperty sourcePathProperty =
43             new StringProperty(SystemProperties.class, "org.apache.geronimo.interop.sourcePath")
44             .defaultValue(FileUtil.pretty(SystemProperties.getHome() + "/src/java")
45                           + File.pathSeparator
46                           + FileUtil.pretty(SystemProperties.getHome() + "/genfiles/java/src"));
47
48     private static String JavaDoc _classDir = classDirProperty.getString();
49     private static List JavaDoc _classPath = ListUtil.getPathList(classPathProperty.getString());
50     private static String JavaDoc _sourceDir = sourceDirProperty.getString();
51     private static List JavaDoc _sourcePath = ListUtil.getPathList(sourcePathProperty.getString());
52
53     public static String JavaDoc addPackageSuffix(String JavaDoc className, String JavaDoc suffix) {
54         String JavaDoc jp = getPackagePrefix(className);
55         if (jp.length() == 0) {
56             jp = suffix;
57         } else {
58             jp += "." + suffix;
59         }
60         return jp + "." + getNameSuffix(className);
61     }
62
63     public static String JavaDoc getClassDir() {
64         return _classDir;
65     }
66
67     public static List JavaDoc getClassPath() {
68         return _classPath;
69     }
70
71     public static File JavaDoc getClassFile(Class JavaDoc theClass) {
72         return getClassFile(theClass.getName());
73     }
74
75     public static File JavaDoc getClassFile(String JavaDoc className) {
76         for (Iterator JavaDoc i = _classPath.iterator(); i.hasNext();) {
77             String JavaDoc dir = (String JavaDoc) i.next();
78             String JavaDoc fileName = FileUtil.pretty(dir + "/" + className.replace('.', '/') + ".class");
79             File JavaDoc classFile = new File JavaDoc(fileName);
80             if (classFile.exists()) {
81                 return classFile;
82             }
83         }
84         return null;
85     }
86
87     public static String JavaDoc getName(String JavaDoc packagePrefix, String JavaDoc nameSuffix) {
88         if (packagePrefix == null || packagePrefix.length() == 0) {
89             return nameSuffix;
90         } else {
91             return packagePrefix + "." + nameSuffix;
92         }
93     }
94
95     public static String JavaDoc getNamePrefix(String JavaDoc className) {
96         return StringUtil.beforeLast(".", className);
97     }
98
99     public static String JavaDoc getNameSuffix(String JavaDoc className) {
100         return StringUtil.afterLast(".", className);
101     }
102
103     public static String JavaDoc getPackagePrefix(String JavaDoc className) {
104         return getNamePrefix(className);
105     }
106
107     /**
108      * * Compute the JVM signature for a class.
109      */

110     public static String JavaDoc getSignature(Class JavaDoc clazz) {
111         String JavaDoc type = null;
112         if (clazz.isArray()) {
113             Class JavaDoc cl = clazz;
114             int dimensions = 0;
115             while (cl.isArray()) {
116                 dimensions++;
117                 cl = cl.getComponentType();
118             }
119             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
120             for (int i = 0; i < dimensions; i++) {
121                 sb.append("[");
122             }
123             sb.append(getSignature(cl));
124             type = sb.toString();
125         } else if (clazz.isPrimitive()) {
126             if (clazz == Integer.TYPE) {
127                 type = "I";
128             } else if (clazz == Byte.TYPE) {
129                 type = "B";
130             } else if (clazz == Long.TYPE) {
131                 type = "J";
132             } else if (clazz == Float.TYPE) {
133                 type = "F";
134             } else if (clazz == Double.TYPE) {
135                 type = "D";
136             } else if (clazz == Short.TYPE) {
137                 type = "S";
138             } else if (clazz == Character.TYPE) {
139                 type = "C";
140             } else if (clazz == Boolean.TYPE) {
141                 type = "Z";
142             } else if (clazz == Void.TYPE) {
143                 type = "V";
144             }
145
146         } else {
147             type = "L" + clazz.getName().replace('.', '/') + ";";
148         }
149         return type;
150     }
151
152     public static String JavaDoc getSourceDir() {
153         return _sourceDir;
154     }
155
156     public static File JavaDoc getSourceFile(Class JavaDoc theClass) {
157         return getSourceFile(theClass.getName());
158     }
159
160     public static File JavaDoc getSourceFile(String JavaDoc className) {
161         for (Iterator JavaDoc i = _sourcePath.iterator(); ;) {
162             String JavaDoc dir;
163             if (i.hasNext()) {
164                 dir = (String JavaDoc) i.next();
165             } else {
166                 dir = _sourceDir;
167             }
168             String JavaDoc fileName = FileUtil.pretty(dir + "/" + className.replace('.', '/') + ".java");
169             File JavaDoc sourceFile = new File JavaDoc(fileName);
170             if (sourceFile.exists()) {
171                 return sourceFile;
172             }
173             if (dir == _sourceDir) {
174                 break;
175             }
176         }
177         return null;
178     }
179
180     public static List JavaDoc getSourcePath() {
181         return _sourcePath;
182     }
183 }
184
Popular Tags