Bạn có thể sử dụng các bước sau để tìm tất cả các tên lớp bao gồm tên phương thức đích trong tệp Jar.
- Nhận các mục nhập cho một tệp Jar được chỉ định.
- Kiểm tra từng JarEntry cho tên. Nếu tên kết thúc bằng '.class'. Sau đó Điền tên lớp.
- Sử dụng tên lớp được điền để nhận tất cả các phương thức thông qua phản ánh.
- So sánh tên và tên phương pháp đích. Nếu chúng bằng nhau, sau đó in tên phương thức và tên lớp trong bảng điều khiển.
Sử dụng các bước trên, chúng tôi có thể tìm tất cả tên lớp trong tệp jar bằng tên phương pháp đích được chỉ định.
Tôi đã viết một mã và chạy một ví dụ cho việc tìm kiếm tên lớp trong jar 'commons-lang-2.4.jar' bằng tên phương pháp mục tiêu 'removeCauseMethodName'.
Và thông báo sau được hiển thị trong bảng điều khiển.
Phương pháp [removeCauseMethodName] được bao gồm trong Class [org.apache.commons.lang.exception.ExceptionUtils]
Từ thông điệp, chúng ta có thể thấy tên lớp, trong đó bao gồm tên phương pháp mục tiêu.
Mã này là như sau:
Lưu ý: trước khi chạy mã này, chúng ta cần thêm jar 'commons-lang-2.4.jar' để build path.
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class SearchMetodInJarFile {
private static final String CLASS_SUFFIX = ".class";
public static void main(String[] args) throws IOException,
SecurityException, ClassNotFoundException {
/** target method name to be searched */
String targetMethodClass = "removeCauseMethodName";
/**
* Specify a target method name as 'removeCauseMethodName'. Find class
* name that includes the target method name in Jar File.
*/
new SearchMetodInJarFile().searchMethodName(new JarFile(
"D:\\Develop\\workspace\\Test\\commons-lang-2.4.jar"),
targetMethodClass);
}
/**
* Search target method name in multiple Jar files.
*/
public void searchMethodName(JarFile[] jarFiles, String targetMethodName)
throws SecurityException, ClassNotFoundException {
for (JarFile jarFile : jarFiles) {
searchMethodName(jarFile, targetMethodName);
}
}
/**
* Search target method name in one Jar file.
*/
public void searchMethodName(JarFile jarFile, String targetMethodName)
throws SecurityException, ClassNotFoundException {
Enumeration<JarEntry> entryEnum = jarFile.entries();
while (entryEnum.hasMoreElements()) {
doSearchMethodName(entryEnum.nextElement(), targetMethodName);
}
}
/**
* Check the name of JarEntry, if its name ends with '.class'. Then do the
* following 3 steps: 1. Populate Class name. 2. Get the methods by
* reflection. 3. Compare the target method name with the names. If the
* methood name is equal to target method name. Then print the method name
* and class name in console.
*/
private void doSearchMethodName(JarEntry entry, String targetMethodName)
throws SecurityException, ClassNotFoundException {
String name = entry.getName();
if (name.endsWith(CLASS_SUFFIX)) {
/**
* Populate the class name
*/
name = name.replaceAll("/", ".")
.substring(0, name.lastIndexOf("."));
/**
* Retrieve the methods via reflection.
*/
Method[] methods = Class.forName(name).getDeclaredMethods();
for (Method m : methods) {
/**
* Print the message in console if the method name is expected.
*/
if (targetMethodName.equals(m.getName())) {
System.out.println(String.format(
"Method [%s] is included in Class [%s]",
targetMethodName, name));
break;
}
}
}
}
}
Hy vọng điều này có thể giúp ích cho bạn.
Bạn đang cố gắng xem nguồn của một phương thức hoặc chỉ tìm kiếm theo tên? – hexafraction
hexafraction, chỉ cần tra cứu theo tên. – smilyface