Java Reference
In-Depth Information
Example 17−3: MakeAPIDB.java (continued)
* Given a fully qualified classname, this method stores the package name
* in the package table (if it is not already there), stores the class name
* in the class table, and then uses the Java Reflection API to look up all
* methods and fields of the class, and stores those in the member table.
**/
public static void storeClass(String name)
throws SQLException, ClassNotFoundException
{
String packagename, classname;
// Dynamically load the class.
Class c = Class.forName(name);
// Display output so the user knows that the program is progressing
System.out.println("Storing data for: " + name);
// Figure out the packagename and the classname
int pos = name.lastIndexOf('.');
if (pos == -1) {
packagename = "";
classname = name;
}
else {
packagename = name.substring(0,pos);
classname = name.substring(pos+1);
}
// Figure out what the package id is. If there is one, then this
// package has already been stored in the database. Otherwise, assign
// an id, and store it and the packagename in the db.
Integer pid;
pid = (Integer)package_to_id.get(packagename); // Check hashtable
if (pid == null) {
pid = new Integer(++packageId);
// Assign an id
package_to_id.put(packagename, pid);
// Remember it
insertpackage.setInt(1, packageId);
// Set statement args
insertpackage.setString(2, packagename);
insertpackage.executeUpdate();
// Insert into package db
}
// Now, store the classname in the class table of the database.
// This record includes the package id, so that the class is linked to
// the package that contains it. To store the class, we set arguments
// to the PreparedStatement, then execute that statement
insertclass.setInt(1, ++classId); // Set class identifier
insertclass.setInt(2, pid.intValue()); // Set package identifier
insertclass.setString(3, classname);
// Set class name
insertclass.executeUpdate();
// Insert the class record
// Now, get a list of all non-private methods of the class, and
// insert those into the "members" table of the database. Each
// record includes the class id of the containing class, and also
// a value that indicates that these are methods, not fields.
Method[] methods = c.getDeclaredMethods(); // Get a list of methods
for(int i = 0; i < methods.length; i++) { // For all non-private
if (Modifier.isPrivate(methods[i].getModifiers())) continue;
insertmember.setInt(1, classId); // Set the class id
insertmember.setString(2, methods[i].getName()); // Set method name
Search WWH ::




Custom Search