forked from joeytwiddle/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassMenuBuilder.java
More file actions
executable file
·189 lines (174 loc) · 6.7 KB
/
ClassMenuBuilder.java
File metadata and controls
executable file
·189 lines (174 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package visualjava;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.neuralyte.Logger;
public class ClassMenuBuilder {
public static void printClassListFromResource(final PrintWriter writer,
String jarOrDir) {
File jarOrDirFile = new File(jarOrDir);
if (jarOrDirFile.isFile() && jarOrDirFile.getName().endsWith(".jar")) {
// if (jarOrDirFile.getName().equals("rt.jar")) {
Logger.log("Extracting class list from jar: " + jarOrDirFile);
ClassMenuBuilder.printClassesInJarTo(jarOrDirFile, writer);
// } else {
// org.neuralyte.Logger.log("Skipping non rt.jar: " + jarOrDirFile);
// }
} else if (jarOrDirFile.isDirectory()){
Logger.log("Extracting class list from directory: " + jarOrDirFile);
ClassMenuBuilder.printClassesInDirTo(jarOrDirFile, jarOrDirFile,
writer);
// org.neuralyte.Logger.log("Do not yet know how to parse class list from directory or non-jar: "
// + jarOrDirFile);
} else {
Logger.warn("Could not parse "+jarOrDirFile+" for classes.");
}
}
public static void printClassesInDirTo(File topDir, File dir,
PrintWriter writer) {
try {
Thread.yield();
if (dir.isDirectory()) {
File[] list = dir.listFiles();
for (int i = 0; i < list.length; i++) {
File file = list[i];
if (file.getName().startsWith(".")) {
org.neuralyte.Logger.log("Skipping " + file
+ " because it starts with a '.'.");
continue;
}
if (file.isDirectory()) {
if (file.getName().equals("CVS")
&& new File(file, "Repository").isFile()) {
org.neuralyte.Logger.log("Skipping " + file
+ " because it looks like a CVS directory.");
continue;
}
printClassesInDirTo(topDir, file, writer);
} else if (file.isFile() && file.getName().endsWith(".class")) {
// // Can get munged by symlinks!
// String relative = file.getCanonicalPath();
// relative = relative.substring(
// topDir.getCanonicalPath().length() + 1);
String relative = file.getAbsolutePath();
relative = relative.substring(topDir.getAbsolutePath()
.length() + 1);
if (!relative.endsWith(".class")) {
org.neuralyte.Logger.log("Managed to munge \""
+ file.getAbsolutePath() + "\" into \"" + relative
+ "\" when topDir was \"" + topDir.getAbsolutePath()
+ "\".");
}
relative = ClassMenuBuilder.getClassFromPath(relative);
if (relative == null) continue;
writer.println(relative);
}
}
}
} catch (Exception e) {
org.neuralyte.Logger.error(e);
}
}
public static void printClassesInJarTo(File jarFile, PrintWriter writer) {
try {
/*
* //// Method 1 - Call an external command to retrieve the list of
* classes in the jar. //// This method can potentially work under Unix
* and Windows. // final Process process = Runtime.getRuntime().
* exec("jar tf /usr/lib/j2se/1.4/jre/lib/rt.jar"); // final Process
* process =Runtime.getRuntime().exec(
* "/home/joey/j/jsh memo jar tf /usr/lib/j2se/1.4/jre/lib/rt.jar"); //
* final Process process =Runtime.getRuntime().exec(
* "sh -c 'jar tf `locate rt.jar | grep \"/rt.jar$\" | head -1`'");
*
* // String[] cmdArray = { "jar", "tf", jarFile.getCanonicalPath() };
* String[] cmdArray = { "sh", "-c",
* "unzip -v "+jarFile.getCanonicalPath()+" | fromcol 9" };
*
* final Process process = Runtime.getRuntime().exec(cmdArray); final
* BufferedReader reader = new BufferedReader(new
* InputStreamReader(process.getInputStream())); while (true) {
* Thread.yield(); String line = reader.readLine(); if (line == null)
* break; // Strip .class extension if (!line.endsWith(".class")) {
* continue; } line = getClassFromPath(line); if (line == null)
* continue; if (line.indexOf("WebResponse")>=0)
* org.neuralyte.Logger.log("> "+line); writer.println(line); }
*/
/*
* //// Method 2 - Use a JarInputStream (means streaming the whole
* jar!). // JarInputStream jis = new JarInputStream(new
* BufferedInputStream(new FileInputStream(jarFile))); JarInputStream
* jis = new JarInputStream(new FileInputStream(jarFile)); while (true)
* { JarEntry jarEntry = jis.getNextJarEntry(); if (jarEntry == null)
* break;
*/
// // Method 3 - Use a jarfile.
JarFile realJarFile = new JarFile(jarFile);
Enumeration<JarEntry> entries = realJarFile.entries();
while (entries.hasMoreElements()) {
JarEntry jarEntry = entries.nextElement();
if (jarEntry.getName().endsWith(".class")) {
// writer.println(getClassFromPath(jarEntry.getName()));
String path = jarEntry.getName();
String className = ClassMenuBuilder.getClassFromPath(path);
// org.neuralyte.Logger.log("VJS adding: "+path+" -> "+className);
if (className == null) {
// Still breaks on inner classes (with $s)
// org.neuralyte.Logger.log("VJS problem: "+path+" -> "+className);
} else {
writer.println(className);
}
}
}
} catch (IOException e) {
org.neuralyte.Logger.error(e);
}
}
public static String getPackageFromClass(String className) {
int i = className.lastIndexOf(".");
if (i >= 0) {
return className.substring(0, i);
} else {
return "";
}
}
public static String getLastInPath(String fullPackage) {
int i = fullPackage.lastIndexOf(".");
if (i >= 0) {
return fullPackage.substring(i + 1);
} else {
return fullPackage;
}
}
public static String getClassFromPath(String line) {
// Assertion.assertThat(line.endsWith(".class"));
line = line.substring(0, line.length() - ".class".length());
// Convert path '/'s into package '.'s
int i;
/*
* while ((i = line.indexOf("/")) >= 0) { line = line.substring(0,i) + "."
* + line.substring(i+1); } while ((i = line.indexOf("\\")) >= 0) { line =
* line.substring(0,i) + "." + line.substring(i+1); }
*/
line = line.replaceAll("[/\\\\]", ".");
// Inner classes
i = line.indexOf("$");
if (i >= 0) { return null;
/**
* @todo It appears inner classes never have constructors We may need to
* access their fields at some point, but for the moment I have
* disabled them.
**/
/*
* String after = line.substring(i+1); try { Integer.parseInt(after); //
* Is an anonymous class: skip continue; } catch (NumberFormatException e)
* { // Is not anonymous =) line = line.substring(0,i) + "." +
* line.substring(i+1); }
*/
}
return line;
}
}