Ants class: class tools

package com.itmayiedu.utils;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class ClassUtil {

/ **
* all made at an interface class implements the interface
* /
public static List <Class> getAllClassByInterface {(Class C)
List <Class> returnClassList = null;

IF (c.isInterface ()) {
// Get the current package name
String packageName = c.getPackage () getName ();.
// Get the current package and the child package under the lower classes so
List <Class <>> allClass? getClasses = (the packageName);
iF (allClass = null!) {
returnClassList the ArrayList new new = <Class> ();
for (Class classes: allClass) {
// determines whether the same interface
if (c.isAssignableFrom (classes)) {
// itself does not join in
IF {(c.equals (classes)!)
returnClassList.add (classes);
}
}
}
}
}

return returnClassList;
}

/*
* 取得某一类所在包的所有类名 不含迭代
*/
public static String[] getPackageAllClassName(String classLocation, String packageName) {
// 将packageName分解
String[] packagePathSplit = packageName.split("[.]");
String realClassLocation = classLocation;
int packageLength = packagePathSplit.length;
for (int i = 0; i < packageLength; i++) {
realClassLocation = realClassLocation + File.separator + packagePathSplit[i];
}
File packeageDir = new File(realClassLocation);
if (packeageDir.isDirectory()) {
String[] allClassName = packeageDir.list();
return allClassName;
}
return null;
}

/ **
* Get all of the Class package from the package
*
* @param Pack
* @return
* /
public static List <Class <? >> getClasses (String packageName) {

// set of first class like
List <Class <>> classes = new new ArrayList <Class <>> ();??
// if loop iterations
boolean recursive This = to true;
// get the package name and replace
String packageDirName packageName.replace = (, '/' '.');
// definition of an enumerated set of things and loop to handle this directory
the enumeration <the URL> dirs;
the try {
. dirs = Thread.currentThread () getContextClassLoader ( ) .getResources (packageDirName);
// loop iteration continues
the while (dirs.hasMoreElements ()) {
// get the next element in
the uRL of url = dirs.nextElement ();
// get the protocol name
String protocol = url.getProtocol () ;
// if in the form of files stored on the server
IF ( "file" .equals (Protocol)) {
// Get package physical path
String filePath = URLDecoder.decode (url.getFile () , "UTF-8");
// scanned documents as files in the entire package, and added to the collection
findAndAddClassesInPackageByFile (the packageName, filePath, recursive This, classes);
} the else IF ( "jar" .equals (Protocol)) {
// if jar package file
/ / define a JarFile to
JarFile to jar;
the try {
// Get jar
jar = ((JarURLConnection to) url.openConnection ()) getJarFile ();.
// jar package obtained from an enumeration class
enumeration <JarEntry> entries = jar.entries ( );
// same loop that iterates
the while (entries.hasMoreElements ()) {
// a jar in the directory entity, and may be a number of other documents, such as bag jar META-INF and other documents
JarEntry entry = entries.nextElement ( );
string name = entry.getName ();
// if by starting with /
IF (name.charAt (0) == '/') {
// Get string behind
name = name.substring (1);
}
printStackTrace (); } }




















}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}

return classes;
}

/ **
* in the form of files to get all Class in package
*
* @param packageName
* @param PackagePath
* @param recursive This
* @param classes
* /
public static void findAndAddClassesInPackageByFile (packageName String, String PackagePath, boolean recursive This Final,
List ? <Class <>> classes) {
Contents of this package // get the establishment of a File
File dir = new new File (PackagePath);
// If the directory does not exist or is not a direct return
if (dir.exists (!) || ! dir.isDirectory ()) {
return;
}
// if there is to obtain all the files in the directory packet includes
file [] = dir.listFiles dirfiles (the FileFilter new new () {
// if custom filtering rules may be circulated (sub directory) or it is the end of the .class file (compiled java class files)
public boolean the Accept (file file) {
return (recursive This && file.isDirectory ()) || (file.getName () endsWith ( "class.").);
}
});
// loop through all files
for (File File: dirfiles) {
// If the directory continue scanning the
IF (file.isDirectory ()) {
findAndAddClassesInPackageByFile (file.getName the packageName + + (), file.getAbsolutePath (), recursive This, "."
classes);
} the else {
// java class file if it is later removed .class leaving class name
String className = file.getName () the substring (0, file.getName () length () -. 6.);.
the try {
// add to the collection to
classes.add (Class.forName ( className + + the packageName)) '.';
} the catch (a ClassNotFoundException E) {
e.printStackTrace ();
}
}
}
}

// 首字母转小写
public static String toLowerCaseFirstOne(String s) {
if (Character.isLowerCase(s.charAt(0)))
return s;
else
return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
}

// 初始化对象
public static Object newInstance(Class<?> classInfo)
throws ClassNotFoundException, InstantiationException, IllegalAccessException {
return classInfo.newInstance();
}
}

Guess you like

Origin www.cnblogs.com/xjatj/p/11220770.html