パッケージに存在するクラスを一覧する †・BeanShellを利用してパッケージに存在するクラスを一覧する。 (1) http://www.beanshell.org/ から bsh-core-2.0b4.jar と bsh-classpath-2.0b4.jar を (2) クラス検索用のJAVAクラスを下記のように作成する import bsh.classpath.BshClassPath;
import java.util.Set;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
public class TestSearchClass {
public static void main( String [] arg ) {
// exampleパッケージ内のクラスを一覧する
String packageName = "example";
List packageList = searchPackage(packageName);
for (int i = 0; i < packageList.size(); i++){
String fullClassName = (String)packageList.get(i);
int lastIdx = fullClassName.lastIndexOf(".");
String className = fullClassName.substring(lastIdx+1);
System.out.println(className + ":" + fullClassName);
}
}
/**
* パッケージ一覧を取得する
* @param name
* @return
*/
public static List<String> searchPackage( String name ) {
List<String> ret = new ArrayList<String>();
BshClassPath bcp = null;
try {
System.err.close()
bcp = BshClassPath.getUserClassPath();
} catch( bsh.ClassPathException e ) {}
// パッケージ名一覧を取得する
Set set = bcp.getPackagesSet();
Iterator i = set.iterator();
while( i.hasNext() ) {
searchClass( bcp, ret, name, (String)i.next() );
}
return ret;
}
/**
* パッケージ内のクラス名を取得する
*/
public static void searchClass(BshClassPath bcp, List<String> ret, String name, String packageName ) {
Set set = bcp.getClassesForPackage( packageName );
Iterator i = set.iterator();
while( i.hasNext() ) {
String className = (String)i.next();
if( className.indexOf( "$" ) >= 0 ) {
continue;
}
if( className.indexOf( name ) != -1 ) {
ret.add( className );
}
}
}
}
・展開デプロイの場合はファイルPathからの検索が可能 public getClassInfo {
/**
* クラス一覧を取得する
*/
public static void main(String[] args){
searchPackage = "example";
m_htInfoList = getClassInfo(searchPackage, null);
}
/**
* 指定されたパッケージ以下のクラス情報を取得する<BR>
*
* @param resourcePath
* @param bootstrap
* @return
*/
private Hashtable getClassInfo(String resourcePath, ClassLoader bootstrap){
Hashtable map = new Hashtable();
try {
if (bootstrap == null){
bootstrap = Thread.currentThread().getContextClassLoader();
}
URL url = bootstrap.getResource(resourcePath);
String path = url.getPath();
try {
URL url2 = this.getClass().getResource(resourcePath);
if (url2 != null){
String path2 = url2.getPath();
}
} catch (Exception e){}
File dir = new File(path);
File[] files = dir.listFiles();
if (files != null){
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.isFile()){
String strPathClass = file.getPath().replaceAll("\\\\", "/");
String strClass = strPathClass.substring( strPathClass.lastIndexOf( "/" ) + 1 ).replaceAll( ".class" , "" );
map.put(strClass.toUpperCase() , strPathClass.substring(strPathClass.lastIndexOf(resourcePath)).replaceAll( ".class" , "" ).replaceAll("/", "."));
} else if (file.isDirectory()) {
Hashtable subMap = getClassInfo(resourcePath + "/" + file.getName(), bootstrap);
map.putAll(subMap);
}
}
}
} catch (Exception e){
e.printStackTrace();
}
return map;
}
}
|