作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试找到一种方法来列出运行时对象所引用的对象。我知道有一种方法可以使用 oql 查询 jvm,但我想做的是从程序内部查询它。有没有我可以使用的 API?
最佳答案
您可以通过反射 ( java.lang.reflect
) 实现。
如何描述in this article .基本上,鉴于此类具有私有(private)成员:
public class Secret {
private String secretCode = "It's a secret";
private String getSecretCode(){
return secretCode;
}
}
通过反射,您可以访问其所有成员(包括私有(private)成员),包括它们的值。因此,您查看它的所有数据成员以查看它们引用的内容(当然,如果它们还引用其他对象,您可以重复该过程)。以下是访问其成员的方法(这段代码也显示了方法,如果您只对数据感兴趣,您可能不需要这些方法,但我没有看到任何充分的理由将那部分拉出来):
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
public class Hacker {
private static final Object[] EMPTY = {};
public void reflect(Object instance)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class secretClass = instance.getClass();
// Print all the method names & execution result
Method methods[] = secretClass.getDeclaredMethods();
System.out.println("Access all the methods");
for (int i = 0; i < methods.length; i++) {
System.out.println("Method Name: " + methods[i].getName());
System.out.println("Return type: " + methods[i].getReturnType());
methods[i].setAccessible(true);
System.out.println(methods[i].invoke(instance, EMPTY) + "\n");
}
// Print all the field names & values
Field fields[] = secretClass.getDeclaredFields();
System.out.println("Access all the fields");
for (int i = 0; i < fields.length; i++){
System.out.println("Field Name: " + fields[i].getName());
fields[i].setAccessible(true);
System.out.println(fields[i].get(instance) + "\n");
}
}
public static void main(String[] args){
Hacker newHacker = new Hacker();
try {
newHacker.reflect(new Secret());
}
catch (Exception e) {
e.printStackTrace();
}
}
}
我修复了他们原始代码中的一个错误并做了一个小改动,以使其更清楚 Hacker
与 Secret
没有任何关系(除了在 main
中)。
更新:关于您在下面关于基类字段的问题,这里有一个更新的 Hacker
可以做到这一点(我假设您不想尝试枚举 Object
上的字段,所以我就到此为止了):
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
public class Hacker {
private static final Object[] EMPTY = {};
public void reflect(Object instance)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class cls = instance.getClass();
while (cls != null && cls != Object.class) {
System.out.println("From class: " + cls.getName());
// Print all the method names & execution result
Method methods[] = cls.getDeclaredMethods();
System.out.println("Access all the methods");
for (int i = 0; i < methods.length; i++) {
System.out.println("Method Name: " + methods[i].getName());
System.out.println("Return type: " + methods[i].getReturnType());
methods[i].setAccessible(true);
System.out.println(methods[i].invoke(instance, EMPTY) + "\n");
}
// Print all the field names & values
Field fields[] = cls.getDeclaredFields();
System.out.println("Access all the fields");
for (int i = 0; i < fields.length; i++){
System.out.println("Field Name: " + fields[i].getName());
fields[i].setAccessible(true);
System.out.println(fields[i].get(instance) + "\n");
}
// Go to the base class
cls = cls.getSuperclass();
}
}
public static void main(String[] args){
Hacker newHacker = new Hacker();
try {
newHacker.reflect(new Secret());
}
catch (Exception e) {
e.printStackTrace();
}
}
}
当结合
public class BaseSecret {
private String baseSecretCode = "It's a base secret";
}
和
public class Secret extends BaseSecret {
private String secretCode = "It's a secret";
private String getSecretCode(){
return secretCode;
}
}
你得到:
$ java Hacker From class: SecretAccess all the methodsMethod Name: getSecretCodeReturn type: class java.lang.StringIt's a secretAccess all the fieldsField Name: secretCodeIt's a secretFrom class: BaseSecretAccess all the methodsAccess all the fieldsField Name: baseSecretCodeIt's a base secret
关于java - 我如何找到一个对象指的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8398683/
我是一名优秀的程序员,十分优秀!