소스 파일 목록 출력
GenerationFileList.java
package com.web.test;
import java.io.File; import java.io.FilenameFilter;
public class GenerationFileList {
private static final String path = "D:/workspaces/eclipse-jee-juno-SR1-win32/ewppis"; private static void searchFile(File file) throws Exception { File[] files = file.listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { if(dir.toString().endsWith("ewppis\\log")) { return false; } else if(dir.toString().endsWith("ewppis\\ref")) { return false; } else if(dir.toString().endsWith("ewppis\\source")) { return false; } else if(dir.toString().endsWith("WebContent\\resources\\temp")) { return false; } else if(dir.toString().endsWith("WebContent\\html")) { return false; } else if(dir.toString().endsWith("ewppis\\src\\com\\web\\test")) { return false; } else if(dir.toString().endsWith("WebContent\\META-INF")) { return false; } else if(dir.toString().endsWith("WebContent\\test")) { return false; } else if(dir.toString().endsWith("WebContent\\WEB-INF\\classes")) { return false; } else if(dir.toString().endsWith("WebContent\\WEB-INF\\upload")) { return false; } else if(dir.toString().endsWith(".svn")) { return false; } if (name.startsWith(".")) { return false; } else if (name.equals("rebel.xml")) { return false; } else if (name.equals("build.xml")) { return false; } return true; } });
for (int i = 0; i < files.length; i++) { getFile(files[i]); } } private static void getFile(File file) throws Exception { if (file.isDirectory()) { searchFile(file); } else { //System.out.println(file.getCanonicalPath()); String path = file.getCanonicalPath().replaceAll("\\\\", "/"); path = path.replaceAll("D:/workspaces/eclipse\\-jee\\-juno\\-SR1\\-win32/ewppis/", ""); int idx = path.lastIndexOf("/"); System.out.println(path.substring(0, idx) + " : " + path.substring(idx+1)); } } public static void main(String[] args) throws Exception { File file = new File(path); if (file.isDirectory()) { searchFile(file); } } }
|
-----------------------------------------------------------------------------------------------------------------------
패키지 클래스 / 메소드 출력
import java.io.File; import java.io.IOException; import java.lang.reflect.Method; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; import java.util.List;
import org.junit.Test;
public class MethodPrintTest {
@Test public void test1() throws Exception { Class[] classes = getClasses("패키지경로"); for(int i=0; i<classes.length; i++) { Class clazz = classes[i]; Method[] methods = clazz.getDeclaredMethods(); for(int j=0; j<methods.length; j++) { Method method = methods[j]; System.out.println(method.toString()); } } } private static Class[] getClasses(String packageName) throws ClassNotFoundException, IOException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); assert classLoader != null; String path = packageName.replace('.', '/'); Enumeration<URL> resources = classLoader.getResources(path); List<File> dirs = new ArrayList<File>(); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); dirs.add(new File(resource.getFile())); } ArrayList<Class> classes = new ArrayList<Class>(); for (File directory : dirs) { classes.addAll(findClasses(directory, packageName)); } return classes.toArray(new Class[classes.size()]); }
private static List<Class> findClasses(File directory, String packageName) throws ClassNotFoundException { List<Class> classes = new ArrayList<Class>(); if (!directory.exists()) { return classes; } File[] files = directory.listFiles(); for (File file : files) { if (file.isDirectory()) { assert !file.getName().contains("."); classes.addAll(findClasses(file, packageName + "." + file.getName())); } else if (file.getName().endsWith(".class")) { classes.add(Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6))); } } return classes; } } |