私は、実行時オブジェクトが参照しているオブジェクトをリストする方法を見つけることを試みています。私はovを使ってjvmに問い合わせる方法があることを知っていますが、私がやりたいことはプログラムの中からそれを照会することです。使用できるAPIはありますか?オブジェクトの意味を知るにはどうしたらいいですか?
3
A
答えて
3
Reflection(java.lang.reflect
)で行うことができます。
説明はin this articleです。リフレクションで
public class Secret {
private String secretCode = "It's a secret";
private String getSecretCode(){
return secretCode;
}
}
は、あなたが(私立含む)、そのメンバーのすべてにアクセスすることができ、それらの値を含む:基本的には、民間のメンバーを持っているこのクラスを与えられました。そして、あなたが参照しているものを見るために、そのデータメンバー全員を見ています(もちろん、他のオブジェクトも参照している場合は、プロセスを繰り返すことができます)。メンバーにアクセスする方法は次のとおりです(このコードにはメソッドも表示されていますが、データに興味があるだけの場合は必要ありませんが、その部分を引き出す正当な理由は見当たりませんでした)。
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
以外)に結び付けられていないことをより明確にするために、元のコードにバグを修正して少し変更しました。
更新:基底クラスからフィールドについて以下のご質問は、ここに(私はあなたがObject
のフィールドを列挙しようとしたくないと仮定したことありません更新Hacker
ですので、私は停止しました再そこ):
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";
}
と
と組み合わせますあなたが得る:
$ java Hacker From class: Secret Access all the methods Method Name: getSecretCode Return type: class java.lang.String It's a secret Access all the fields Field Name: secretCode It's a secret From class: BaseSecret Access all the methods Access all the fields Field Name: baseSecretCode It's a base secret
-1
あなたがオブジェクトの実行時のクラスを取得するためにObjectクラスのはgetClass()メソッドを使用することができます。
関連する問題
- 1. linuxの意味はどういう意味ですか?
- 2. %%はどういう意味ですか?
- 3. "javascript://"とはどういう意味ですか?そのコードには意味:
- 4. オブジェクトにjavascriptでメソッドがないときはどういう意味ですか?
- 5. オブジェクトが書かれていないとはどういう意味ですか?
- 6. 「殺した」とはどういう意味ですか?
- 7. 0x0Fは何を意味していますか?そして、このコードはどういう意味ですか?
- 8. これらのF5 iRulesはどういう意味ですか?
- 9. これらの出力はどういう意味ですか?
- 10. どういう意味ですか? OR?
- 11. どういう意味ですか?:意味ですか? Apacheの設定ファイルで
- 12. オブジェクトがデータベーステーブルをラップすることはどういう意味ですか?
- 13. これらはどういう意味ですか?
- 14. 私のnginxウェブサーバーに対するこれらの未知のrequest_uri値はどういう意味ですか?
- 15. pythonでオブジェクトを再構築するか、オブジェクトを再作成するのはどういう意味ですか?
- 16. 全体のユーザーデータの代わりに[オブジェクト、オブジェクト]を取得しています。どういう意味ですか?
- 17. ジュリアで「ファイナライズ」するのはどういう意味ですか?
- 18. ディレクトリ名とは何ですか? 'と '..'という意味で、faDirectoryはどういう意味ですか?
- 19. Java NIO:IOException:壊れたパイプはどういう意味ですか?
- 20. 「ハードコードされた」とはどういう意味ですか?
- 21. 「ループ外に出る」とはどういう意味ですか?
- 22. 引数のないpaper.path()はどういう意味ですか?
- 23. VSコードの赤い点はどういう意味ですか
- 24. BackboneJs:elとtagNameの違いはどういう意味ですか?
- 25. 終了コンテキストがWaitHandle.WaitOneの意味をどういう意味ですか?
- 26. データクラスという用語はどういう意味ですか?
- 27. パンジャブを使うのはどういう意味ですか
- 28. "ビットマップロック"とは実際にはどういう意味ですか?
- 29. このインポートはScalaではどういう意味ですか?
- 30. gitkではエッジの色はどういう意味ですか?
機能要件でない場合は、プロファイラまたはデバッガを使用できます。私はjhat/jstatがスナップショットをダンプするのに役立つと思うので、あなたがオフラインのイントロスペクションをしたいのであれば、それは簡単でしょう。それが機能的要件であれば、それは興味深いものです! – questzen