in modules/jdktools/src/main/java/org/apache/harmony/tools/javah/Main.java [144:253]
public static int run(Map options, Set classNames) {
File outDirectory = getFile(options, "-d");
File outFile = getFile(options, "-o");
if (outDirectory != null && outFile != null) {
System.err.println("Error: You can not use both -d and -o options.");
System.err.println();
usage();
return 1;
}
String bootClasspath = getString(options, "-bootclasspath");
String classpath = getString(options, "-classpath");
boolean inner = getBoolean(options, "-inner");
boolean verbose = getBoolean(options, "-verbose");
ClassProvider classProvider = new ClassProvider(bootClasspath,
classpath, verbose);
try {
String n = System.getProperty("line.separator");
String warning =
"/*" + n
+ " * THE FILE HAS BEEN AUTOGENERATED BY THE javah TOOL." + n
+ " * Please be aware that all changes made to this file manually" + n
+ " * will be overwritten by the tool if it runs again." + n
+ " */" + n
+ n
+ "#include <jni.h>" + n
+ n;
StringBuffer result = new StringBuffer();
Set innerNames = new HashSet();
File file = outFile;
Iterator namesIter = classNames.iterator();
while (namesIter.hasNext()) {
// Parse the next class.
Clazz clazz = new Clazz(classProvider, (String) namesIter.next());
if (inner) {
// Get the inner class names and store them
// in a separate set.
String innerClassNames[] = clazz.getInnerClassNames();
for (int i = 0; i < innerClassNames.length; i++) {
innerNames.add(innerClassNames[i]);
}
}
if (outFile == null) {
// Each header should be written into the separate file.
String fileName = Mangler.mangleFileName(
clazz.getName()) + ".h";
if (outDirectory != null) {
if (!outDirectory.exists()) {
outDirectory.mkdir();
}
fileName = outDirectory.getPath() + File.separator
+ fileName;
}
// Reset the output file.
file = new File(fileName);
// Reset the result buffer.
result = new StringBuffer();
}
// Append the next header.
result.append(Main.getHeader(clazz));
// Process the inner class names, if any.
if (!namesIter.hasNext() && !innerNames.isEmpty()) {
// Remove the inner class names that have already
// been processed.
innerNames.removeAll(classNames);
// Reset the loop iterator.
classNames = new HashSet(innerNames);
namesIter = classNames.iterator();
// Clear the set of the inner class names.
innerNames.clear();
}
// Write the result into a file.
if (outFile == null || !namesIter.hasNext()) {
FileWriter writer = new FileWriter(file);
try {
writer.write(warning);
writer.write(result.toString());
if (verbose) {
System.out.println(file.getName() + " created");
}
} finally {
writer.close();
}
}
}
} catch (Exception e) {
System.err.println("Error:");
e.printStackTrace();
return 1;
}
return 0;
}