我目前正在创建一个用于查看一堆字符串并使用它们创建窗口的库。目前,只有几行代码和一个巨大的配置文件将使用大量的窗口和东西来设置它。我把它设置为读取一些字符串并从中加载类,以获取方法的参数。这是执行此操作的方法:
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
}
它适用于普通类。例如,如果我将java.lang.String放入其中,它会将String类吐回到我身边。如果我放了javax.swing.JTextField,它会向我发出正确的类。但是,如果我将java.lang.String []放入其中,请求一个字符串数组,它会崩溃,因此:
Warning in catagory code- message = Could not find suitable instance for method- Expected JTextComponent but found JTextField!
Severe error in catagory classFinding
May 16, 2012 11:02:35 AM ErrorAdapter severeError
SEVERE: Could not find class- java.lang.String[]
java.lang.ClassNotFoundException: java.lang.String[]
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at Config.getClass(Config.java:389)
at Config.getClasses(Config.java:398)
at Config.methodCall(Config.java:492)
at Config.call(Config.java:415)
at Config.chainCall(Config.java:439)
at Config.call(Config.java:409)
at main.action(main.java:122)
at main$1$5.actionPerformed(main.java:63)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
第一行 - 以&#34;警告&#34;开头的那一行 - 是因为如果我尝试在JTextField.class上使用Class.getDeclaredMethod()来获取getText()方法,它就会崩溃。其余的是崩溃报告。
有没有办法我可以解决这个问题,让它适用于任何类/数组深度(因此它适用于java.lang.String [],javax.swing.JTextField [] [] [],和int [] []没有硬编码任何一个)?
另外,在旁注中,是否有更好的方法可以根据字符串获取基元类型的类?
答案 0 :(得分:3)
你试过了吗?
的Class.forName( “[Ljava.lang.String;”);对于一系列字符串?
http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#forName(java.lang.String) http://java.dzone.com/news/obtaining-array-class-java-ref
答案 1 :(得分:2)
[Ljava.lang.String;
是类java.lang.String[]
System.out.println(String[].class.getName());
答案 2 :(得分:1)
数组名称在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
}
答案 3 :(得分:0)
如果您正在创建一个库,请对您的客户端好,并使用Collections类而不是数组。
对于基本类型,您始终可以传递盒装版本(java.lang.Integer
等)