Main Page   Packages   Class Hierarchy   Compound List   File List   Compound Members  

RTSI.java

00001 package util;
00002 
00010 import java.io.File;
00011 import java.io.IOException;
00012 import java.net.JarURLConnection;
00013 import java.net.URL;
00014 import java.util.Enumeration;
00015 import java.util.Vector;
00016 import java.util.jar.JarFile;
00017 import java.util.zip.ZipEntry;
00018 
00027 public class RTSI {
00028 
00029 
00035     public static void find(String tosubclassname) {
00036         try {
00037             Class tosubclass = Class.forName(tosubclassname);
00038             Package [] pcks = Package.getPackages();
00039             for (int i=0;i<pcks.length;i++) {
00040                 find(pcks[i].getName(),tosubclass);
00041             }
00042         } catch (ClassNotFoundException ex) {
00043             System.err.println("Class "+tosubclassname+" not found!");
00044         }
00045     }
00046 
00053     public static void find(String pckname, String tosubclassname) {
00054         try {
00055             Class tosubclass = Class.forName(tosubclassname);
00056             find(pckname,tosubclass);
00057         } catch (ClassNotFoundException ex) {
00058             System.err.println("Class "+tosubclassname+" not found!");
00059         }
00060     }
00061 
00068     public static void find(String pckgname, Class tosubclass) {
00069         Vector s = findnames(pckgname, tosubclass);
00070 
00071         for(int i = 0; i < s.size(); i++)
00072             System.out.println(s.get(i));
00073     }
00074 
00075 
00082     public static Vector findnames(String pckgname, Class tosubclass) {
00083     Vector v = new Vector();        
00084         // Code from JWhich
00085         // ======
00086         // Translate the package name into an absolute path
00087     String name = new String(pckgname);
00088     if (!name.startsWith("/")) {
00089         name = "/" + name;
00090     }   
00091     name = name.replace('.','/');
00092 
00093         // Get a File object for the package
00094         URL url = RTSI.class.getResource(name);
00095         // URL url = tosubclass.getResource(name);
00096         // URL url = ClassLoader.getSystemClassLoader().getResource(name);
00097         //System.out.println(name+"->"+url);
00098 
00099         // Happens only if the jar file is not well constructed, i.e.
00100         // if the directories do not appear alone in the jar file like here:
00101         // 
00102         //          meta-inf/
00103         //          meta-inf/manifest.mf
00104         //          commands/                  <== IMPORTANT
00105         //          commands/Command.class
00106         //          commands/DoorClose.class
00107         //          commands/DoorLock.class
00108         //          commands/DoorOpen.class
00109         //          commands/LightOff.class
00110         //          commands/LightOn.class
00111         //          RTSI.class
00112         //
00113         if (url==null) return null;
00114 
00115         File directory = new File(url.getFile());
00116 
00117         // New code
00118         // ======
00119         if (directory.exists()) {
00120             // Get the list of the files contained in the package
00121             String [] files = directory.list();
00122             for (int i=0;i<files.length;i++) {
00123                 
00124                 // we are only interested in .class files
00125                 if (files[i].endsWith(".class")) {
00126                     // removes the .class extension
00127                     String classname = files[i].substring(0,files[i].length()-6);
00128                     try {
00129                         // Try to create an instance of the object
00130                         Object o = Class.forName(pckgname+"."+classname).newInstance();
00131                         if (tosubclass.isInstance(o)) {
00132                 //System.out.println(classname);
00133                             v.add(classname);
00134                         }
00135                     } catch (ClassNotFoundException cnfex) {
00136                         System.err.println(cnfex);
00137                     } catch (InstantiationException iex) {
00138                         // We try to instanciate an interface
00139                         // or an object that does not have a 
00140                         // default constructor
00141                     } catch (IllegalAccessException iaex) {
00142                         // The class is not public
00143                     }
00144                 }
00145             }
00146         } else {
00147             try {
00148                 // It does not work with the filesystem: we must
00149                 // be in the case of a package contained in a jar file.
00150                 JarURLConnection conn = (JarURLConnection)url.openConnection();
00151                 String starts = conn.getEntryName();
00152                 JarFile jfile = conn.getJarFile();
00153                 Enumeration e = jfile.entries();
00154                 while (e.hasMoreElements()) {
00155                     ZipEntry entry = (ZipEntry)e.nextElement();
00156                     String entryname = entry.getName();
00157                     if (entryname.startsWith(starts)
00158                         &&(entryname.lastIndexOf('/')<=starts.length())
00159                         &&entryname.endsWith(".class")) {
00160                         String classname = entryname.substring(0,entryname.length()-6);
00161                         if (classname.startsWith("/")) 
00162                             classname = classname.substring(1);
00163                         classname = classname.replace('/','.');
00164                         try {
00165                             // Try to create an instance of the object
00166                             Object o = Class.forName(classname).newInstance();
00167                             if (tosubclass.isInstance(o)) {
00168                     //System.out.println(classname.substring(classname.lastIndexOf('.')+1));
00169                     v.add(classname.substring(classname.lastIndexOf('.')+1));
00170                             }
00171                         } catch (ClassNotFoundException cnfex) {
00172                             System.err.println(cnfex);
00173                         } catch (InstantiationException iex) {
00174                             // We try to instanciate an interface
00175                             // or an object that does not have a 
00176                             // default constructor
00177                         } catch (IllegalAccessException iaex) {
00178                             // The class is not public
00179                         }
00180                     }
00181                 }
00182             } catch (IOException ioex) {
00183                 System.err.println(ioex);
00184             }   
00185         }
00186     
00187     return v;
00188     }
00189     
00190     public static void main(String []args) {
00191         if (args.length==2) {
00192             find(args[0],args[1]);
00193         } else {
00194             if (args.length==1) {
00195                 find(args[0]);
00196             } else {
00197                 System.out.println("Usage: java RTSI [<package>] <subclass>");
00198             }
00199         }
00200     }
00201 }// RTSI

Generated on Tue Feb 6 19:02:27 2007 for MEAPsoft by doxygen1.2.18