⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classfinder.java

📁 测试工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        ArrayList listPaths = new ArrayList();
        String strPath = null;
		while (stPaths.hasMoreTokens()) {
			strPath = fixPathEntry(stPaths.nextToken());
			if (strPathsOrJars == null) {
				log.debug("Adding: " + strPath);
				listPaths.add(strPath);
			} else {
				boolean found = false;
				for (int i = 0; i < strPathsOrJars.length; i++) {
					if (strPath.endsWith(strPathsOrJars[i])) {
						found = true;
						log.debug("Adding " + strPath + " found at " + i);
						listPaths.add(strPath);
						break;// no need to look further
					}
				}
				if (!found) {
					log.debug("Did not find: " + strPath);
				}
			}
		}
		return listPaths;
	}

    /**
     * Fix a path:
     * - replace "." by current directory
     * - trim any trailing spaces
     * - replace \ by /
     * - replace // by /
     * - remove all trailing /
     */
    private static String fixPathEntry(String path){
        if (path == null ) return null;
        if (path.equals(".")) { // $NON-NLS-1$
            return System.getProperty("user.dir"); // $NON-NLS-1$
        }
        path = path.trim().replace('\\', '/'); // $NON-NLS-1$ // $NON-NLS-2$
        path = JOrphanUtils.substitute(path, "//", "/"); // $NON-NLS-1$// $NON-NLS-2$

        while (path.endsWith("/")) { // $NON-NLS-1$
            path = path.substring(0, path.length() - 1);
        }
        return path;
    }
    
	/*
	 * NOTUSED * Determine if the class implements the interface.
	 * 
	 * @param theClass
	 *            the class to check
	 * @param theInterface
	 *            the interface to look for
	 * @return boolean true if it implements
	 * 
	 * private static boolean classImplementsInterface( Class theClass, Class
	 * theInterface) { HashMap mapInterfaces = new HashMap(); String strKey =
	 * null; // pass in the map by reference since the method is recursive
	 * getAllInterfaces(theClass, mapInterfaces); Iterator iterInterfaces =
	 * mapInterfaces.keySet().iterator(); while (iterInterfaces.hasNext()) {
	 * strKey = (String) iterInterfaces.next(); if (mapInterfaces.get(strKey) ==
	 * theInterface) { return true; } } return false; }
	 */

	/*
	 * Finds all classes that extend the classes in the listSuperClasses
	 * ArrayList, searching in the listAllClasses ArrayList.
	 * 
	 * @param superClasses
	 *            the base classes to find subclasses for
	 * @param listAllClasses
	 *            the collection of classes to search in
	 * @param innerClasses
	 *            indicate whether to include inner classes in the search
	 * @return ArrayList of the subclasses
	 */
//	private static Set findAllSubclasses(Class []superClasses, Set listAllClasses, boolean innerClasses) {
//		Set listSubClasses = new TreeSet();
//		for (int i=0; i< superClasses.length; i++) {
//			findAllSubclassesOneClass(superClasses[i], listAllClasses, listSubClasses, innerClasses);
//		}
//		return listSubClasses;
//	}

	/*
	 * Finds all classes that extend the class, searching in the listAllClasses
	 * ArrayList.
	 * 
	 * @param theClass
	 *            the parent class
	 * @param listAllClasses
	 *            the collection of classes to search in
	 * @param listSubClasses
	 *            the collection of discovered subclasses
	 * @param innerClasses
	 *            indicates whether inners classes should be included in the
	 *            search
	 */
//	private static void findAllSubclassesOneClass(Class theClass, Set listAllClasses, Set listSubClasses,
//			boolean innerClasses) {
//        Iterator iterClasses = listAllClasses.iterator();
//		while (iterClasses.hasNext()) {
//            String strClassName = (String) iterClasses.next();
//			// only check classes if they are not inner classes
//			// or we intend to check for inner classes
//			if ((strClassName.indexOf("$") == -1) || innerClasses) { // $NON-NLS-1$
//				// might throw an exception, assume this is ignorable
//				try {
//					Class c = Class.forName(strClassName, false, Thread.currentThread().getContextClassLoader());
//
//					if (!c.isInterface() && !Modifier.isAbstract(c.getModifiers())) {
//                        if(theClass.isAssignableFrom(c)){
//                            listSubClasses.add(strClassName);
//                        }
//                    }
//				} catch (Throwable ignored) {
//                    log.debug(ignored.getLocalizedMessage());
//				}
//			}
//		}
//	}

    /**
     * 
     * @param parentClasses list of classes to check for
     * @param strClassName name of class to be checked
     * @param innerClasses should we allow inner classes?
     * @param contextClassLoader the classloader to use
     * @return
     */
    private static boolean isChildOf(Class [] parentClasses, String strClassName,
            ClassLoader contextClassLoader){
            // might throw an exception, assume this is ignorable
            try {
                Class c = Class.forName(strClassName, false, contextClassLoader);

                if (!c.isInterface() && !Modifier.isAbstract(c.getModifiers())) {
                    for (int i=0; i< parentClasses.length; i++) {
                        if(parentClasses[i].isAssignableFrom(c)){
                            return true;
                        }
                    }
                }
            } catch (Throwable ignored) {
                log.debug(ignored.getLocalizedMessage());
            }
        return false;
    }
    
    
	/*
	 * Converts a class file from the text stored in a Jar file to a version
	 * that can be used in Class.forName().
	 * 
	 * @param strClassName
	 *            the class name from a Jar file
	 * @return String the Java-style dotted version of the name
	 */
	private static String fixClassName(String strClassName) {
		strClassName = strClassName.replace('\\', '.'); // $NON-NLS-1$ // $NON-NLS-2$
		strClassName = strClassName.replace('/', '.'); // $NON-NLS-1$ // $NON-NLS-2$
        // remove ".class"
		strClassName = strClassName.substring(0, strClassName.length() - DOT_CLASS_LEN);
		return strClassName;
	}

	private static void findClassesInOnePath(String strPath, Set listClasses) throws IOException {
        File file = new File(strPath);
		if (file.isDirectory()) {
			findClassesInPathsDir(strPath, file, listClasses);
		} else if (file.exists()) {
            ZipFile zipFile = new ZipFile(file);
            Enumeration entries = zipFile.entries();
			while (entries.hasMoreElements()) {
				String strEntry = entries.nextElement().toString();
				if (strEntry.endsWith(DOT_CLASS)) {
					listClasses.add(fixClassName(strEntry));
				}
			}
		}
	}

	private static void findClassesInPaths(List listPaths, Set listClasses) throws IOException {
		Iterator iterPaths = listPaths.iterator();
		while (iterPaths.hasNext()) {
			findClassesInOnePath((String) iterPaths.next(), listClasses);
		}
	}

	private static void findClassesInPathsDir(String strPathElement, File dir, Set listClasses) throws IOException {
		String[] list = dir.list();
		for (int i = 0; i < list.length; i++) {
            File file = new File(dir, list[i]);
			if (file.isDirectory()) {
                // Recursive call
				findClassesInPathsDir(strPathElement, file, listClasses);
			} else if (list[i].endsWith(DOT_CLASS) && file.exists() && (file.length() != 0)) {
				final String path = file.getPath();
                listClasses.add(path.substring(strPathElement.length() + 1, 
                        path.lastIndexOf(".")) // $NON-NLS-1$
						.replace(File.separator.charAt(0), '.')); // $NON-NLS-1$
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -