How to get all object classes in some class.
for example :
如何在某个类中获取所有对象类。例如 :
class ex{
Scanner in=new Scanner(System.in);
void test(){
String out=in.nextLine();
JOptionPane.showMessageDialog(null,out);
}
}
class ex2{
void test(){
java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
String output=in.readLine();
}
}
from this class i want to print out
从这个课我想打印出来
class ex :
java.util.Scanner
java.lang.String
javax.swing.JOptionPane
class ex2 :
java.io.BufferedReader
java.io.InputStreamReader
java.lang.String
3 个解决方案
#1
One possible way can be to read your own java class file source from your own class and list out all classes used in your java class
一种可能的方法是从您自己的类中读取您自己的java类文件源,并列出您的java类中使用的所有类
#2
You can use ASM library to read the class file. The documentation is here. You can google some examples as well. Basically you need to read the Class entries of the class constant pool.
您可以使用ASM库来读取类文件。文档在这里。你也可以谷歌一些例子。基本上你需要读取类常量池的Class条目。
#3
Not possible as you are trying to print the names of all imported classes inside the class of the instance what you have in your hand. Think about some classes that have imported to the class file, but the usage of that class(imported classes) will be in restricted scope.(private
: The private modifier specifies that the member can only be accessed in its own class.) Unless you read the class file as in you read a text file and grab the imported class file names.(that may not be a practical solution)
这是不可能的,因为您尝试在实例的类中打印所有导入的类的名称。考虑一些已导入到类文件的类,但该类的使用(导入的类)将在限制范围内。(private:private修饰符指定只能在其自己的类中访问该成员。)除非您像读取文本文件一样读取类文件并获取导入的类文件名。(这可能不是一个实用的解决方案)
If you want the hierarchy, you can do something like below.
如果您想要层次结构,可以执行以下操作。
JOptionPane.showMessageDialog(null,out.getClass());
public static String printx(Object obj, String output){
String str2 = new String(obj.getClass().toString());
if(!str2.equalsIgnoreCase("class java.lang.Object") && !str2.equalsIgnoreCase("class java.lang.Class")){
output += printx(obj.getClass().getSuperclass(),output);
}
output += (","+str2);
return output;
}
public static void main(String[]args){
Scanner c = new Scanner(System.in);
String elzz = printx(c,"");
elzz = elzz.substring(1);
System.out.println(elzz);
}
#1
One possible way can be to read your own java class file source from your own class and list out all classes used in your java class
一种可能的方法是从您自己的类中读取您自己的java类文件源,并列出您的java类中使用的所有类
#2
You can use ASM library to read the class file. The documentation is here. You can google some examples as well. Basically you need to read the Class entries of the class constant pool.
您可以使用ASM库来读取类文件。文档在这里。你也可以谷歌一些例子。基本上你需要读取类常量池的Class条目。
#3
Not possible as you are trying to print the names of all imported classes inside the class of the instance what you have in your hand. Think about some classes that have imported to the class file, but the usage of that class(imported classes) will be in restricted scope.(private
: The private modifier specifies that the member can only be accessed in its own class.) Unless you read the class file as in you read a text file and grab the imported class file names.(that may not be a practical solution)
这是不可能的,因为您尝试在实例的类中打印所有导入的类的名称。考虑一些已导入到类文件的类,但该类的使用(导入的类)将在限制范围内。(private:private修饰符指定只能在其自己的类中访问该成员。)除非您像读取文本文件一样读取类文件并获取导入的类文件名。(这可能不是一个实用的解决方案)
If you want the hierarchy, you can do something like below.
如果您想要层次结构,可以执行以下操作。
JOptionPane.showMessageDialog(null,out.getClass());
public static String printx(Object obj, String output){
String str2 = new String(obj.getClass().toString());
if(!str2.equalsIgnoreCase("class java.lang.Object") && !str2.equalsIgnoreCase("class java.lang.Class")){
output += printx(obj.getClass().getSuperclass(),output);
}
output += (","+str2);
return output;
}
public static void main(String[]args){
Scanner c = new Scanner(System.in);
String elzz = printx(c,"");
elzz = elzz.substring(1);
System.out.println(elzz);
}