View Javadoc
1   /******************************************************************************
2    * Classes.java - Class utilities.
3    * 
4    * PicMan - The BuckoSoft Picture Manager in Java
5    * Copyright(c) 2008 - Dick Balaska
6    * 
7    */
8   package com.buckosoft.PicMan.business.util;
9   
10  import java.io.File;
11  import java.util.ArrayList;
12  
13  /** Class utilities.
14   * @author Dick Balaska
15   * @since 2008/09/20
16   * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/business/util/Classes.java">Classes.java</a>
17   */
18  public class Classes {
19  	/** List Classes inside a given package.
20  	 * <br>
21  	 * author Jon Peck http://jonpeck.com (adapted from http://www.javaworld.com/javaworld/javatips/jw-javatip113.html)
22  	 * @param pckgname String name of a Package, EG "java.lang"
23  	 * @return Class[] classes inside the root of the given package
24  	 * @throws ClassNotFoundException if the Package is invalid
25  	 */
26  	public static String[] getClasses(String pckgname) /*throws ClassNotFoundException*/ {
27  		ArrayList<String> classes=new ArrayList<String>();
28  		// Get a File object for the package
29  		File directory=null;
30  		try {
31  			directory=new File(Thread.currentThread().getContextClassLoader().getResource('/'+pckgname.replace('.', '/')).getFile());
32  		} catch(NullPointerException x) {
33  			//throw new ClassNotFoundException(pckgname+" does not appear to be a valid package");
34  		}
35  		if(directory.exists()) {
36  			// Get the list of the files contained in the package
37  			String[] files=directory.list();
38  			for(int i=0; i<files.length; i++) {
39  				// we are only interested in .class files
40  				if(files[i].endsWith(".class")) {
41  					// removes the .class extension
42  					//classes.add(Class.forName(pckgname+'.'+files[i].substring(0, files[i].length()-6)));
43  					classes.add(files[i].substring(0, files[i].length()-6));
44  				}
45  			}
46  		} else {
47  			//throw new ClassNotFoundException(pckgname+" does not appear to be a valid package");
48  		}
49  		String[] ss = new String[classes.size()];
50  		classes.toArray(ss);
51  		return(ss);
52  	}	
53  
54  }