Java 如何创建只有文本名称的类的数组?
我目前正在创建一个库,用于查看一组字符串并使用它们创建窗口。目前,只有几行代码和一个巨大的配置文件将启动它与许多窗口和东西。我将它设置为读取一些字符串并从中加载类,以获取方法的参数。以下是实现此功能的方法:Java 如何创建只有文本名称的类的数组?,java,arrays,class,Java,Arrays,Class,我目前正在创建一个库,用于查看一组字符串并使用它们创建窗口。目前,只有几行代码和一个巨大的配置文件将启动它与许多窗口和东西。我将它设置为读取一些字符串并从中加载类,以获取方法的参数。以下是实现此功能的方法: private Class<?> getClass(String classname){ switch(classname){//Check for some primitive type references, since they don't work
private Class<?> getClass(String classname){
switch(classname){//Check for some primitive type references, since they don't work in the below area
case "int":
return int.class;
case "double":
return double.class;
case "boolean":
return boolean.class;
case "long":
return long.class;
case "short":
return short.class;
}
Class<?> value = null;
try{
value = Config.class.getClassLoader().loadClass(classname);
}catch(ClassNotFoundException ex){
Error.error(ErrorLevel.severe, "Could not find class- "+classname, ex, ErrorCatagory.classFinding);//A custom error handling system
}
return value;//Return the class found from the name
}
第一行(以“Warning”开头)是因为如果我尝试在JTextField.Class上使用Class.getDeclaredMethod()来获取getText()方法,它会崩溃。剩下的就是坠机报告
有没有一种方法可以解决这个问题,并使它在任何类/数组深度下工作(这样就可以在不硬编码的情况下使用java.lang.String[]、javax.swing.JTextField[][]和int[][])
另外,另一方面,是否有更好的方法基于字符串获取基本类型的类?
[Ljava.lang.string;
是类的运行时签名java.lang.string[]
System.out.println(String[].class.getName());
您是否尝试过:
Class.forName(“[Ljava.lang.String;”);用于字符串数组
)
如果要创建库,请善待客户,使用Collections类而不是数组
对于基本类型,您始终可以传递装箱版本(
java.lang.Integer
等)。数组名称在java中的表示方式不同。您可以按如下方式修改方法以处理数组:
private Class<?> getClass(String classname){
switch(classname){//Check for some primitive type references, since they don't work in the below area
case "int":
return int.class;
case "int[]":
return int[].class;
case "double":
return double.class;
case "double[]":
return double[].class;
case "boolean":
return boolean.class;
case "boolean[]":
return boolean[].class;
case "long":
return long.class;
case "long[]":
return long[].class;
case "short":
return short.class;
case "short[]":
return short[].class;
case "char":
return char.class;
case "char[]":
return char[].class;
}
Class<?> value = null;
int arrayLevel = 0;
while (classname.endsWith("[]")) {
classname = classname.substring(0, classname.length()-2);
arrayLevel++;
}
while (arrayLevel-- != 0) {
className = "[L" + className + ";";
}
try{
value = Config.class.getClassLoader().loadClass(classname);
}catch(ClassNotFoundException ex){
Error.error(ErrorLevel.severe, "Could not find class- "+classname, ex, ErrorCatagory.classFinding);//A custom error handling system
}
return value;//Return the class found from the name
}
私有类getClass(字符串类名称){
开关(classname){//检查一些基本类型引用,因为它们在下面的区域中不起作用
案例“int”:
返回int.class;
案例“int[]”:
返回int[]类;
“双重”情况:
返回double.class;
“double[]”案例:
返回双[]类;
案例“布尔”:
返回boolean.class;
大小写“布尔[]”:
返回布尔[]。类;
案例“long”:
返回long.class;
案例“long[]”:
返回长[]类;
案例“短”:
返回short.class;
“short[]”一案:
返回短[]类;
案例“char”:
返回char.class;
案例“char[]”:
返回char[]类;
}
类值=null;
int-arrayLevel=0;
while(classname.endsWith(“[]”){
classname=classname.substring(0,classname.length()-2);
arrayLevel++;
}
while(arrayLevel——!=0){
className=“[L”+className+”;”;
}
试一试{
value=Config.class.getClassLoader().loadClass(类名);
}捕获(ClassNotFoundException ex){
Error.Error(ErrorLevel.severy,“找不到类-”+classname,ex,ErrorCatagory.classFinding);//自定义错误处理系统
}
返回值;//返回从名称中找到的类
}
以前没有,但现在有了。它对非数组部分有效。我尝试运行数组部分,结果崩溃了:java.lang.ClassNotFoundException:[Ljava/lang/String;
我试图删除字符串中的分号,但它向我吐出了同样的东西,但这次没有分号。我也尝试了括号,但同样的东西。在这里工作。Java 1.7 build 4一旦我更新,它也工作了。它不适用于build 3,但上面是的。嗯……我没有看到任何可以让它重新运行的东西urn类似于JTextField[].class
的值,这就是我要查找的(返回的值作为参数类型的一部分输入到class.getDeclaredMethod()
)中)。这可能在哪里?@Superinventor如果它能找到JTextField
,检查endsWith(“[]”)的代码
应该能够修改类名以符合Java的期望(即,在元素类的名称前面加上“[L”
”;“”)。是的,我看到了这一部分……问题是,当我添加括号时,它只返回JTextField.class
。我希望它返回JTextField[].class
当我添加括号时,而不是当我不添加括号时,这样当返回值稍后在类中用作参数类型之一时,它会寻找正确的方法。getDeclaredMethod()+
分支被采用了吗?不。现在我看了它,我可以看到它实际上会像上面提到的ptay89那样,只是功能更加通用。它起作用了。现在我只需要一个for
循环,使它与堆叠阵列兼容(例如int[][
)。不过,我自己也可以这样做。除了我这样做时,它找不到方法…抱歉,我忘了提到返回的值的位置。它作为参数类型的一部分转到类。getDeclaredMethod()
,因此如果方法需要int,这种方法实际上不起作用。不过,还是谢谢。
private Class<?> getClass(String classname){
switch(classname){//Check for some primitive type references, since they don't work in the below area
case "int":
return int.class;
case "int[]":
return int[].class;
case "double":
return double.class;
case "double[]":
return double[].class;
case "boolean":
return boolean.class;
case "boolean[]":
return boolean[].class;
case "long":
return long.class;
case "long[]":
return long[].class;
case "short":
return short.class;
case "short[]":
return short[].class;
case "char":
return char.class;
case "char[]":
return char[].class;
}
Class<?> value = null;
int arrayLevel = 0;
while (classname.endsWith("[]")) {
classname = classname.substring(0, classname.length()-2);
arrayLevel++;
}
while (arrayLevel-- != 0) {
className = "[L" + className + ";";
}
try{
value = Config.class.getClassLoader().loadClass(classname);
}catch(ClassNotFoundException ex){
Error.error(ErrorLevel.severe, "Could not find class- "+classname, ex, ErrorCatagory.classFinding);//A custom error handling system
}
return value;//Return the class found from the name
}