How to create an array of a class for which I only have a text name?

I am currently creating a library that is designed to view multiple lines and create windows with them. Currently, only a few lines of code and a huge configuration file will install it with a lot of windows, etc. I configured it to read some lines and load classes from them, for parameters for methods. Here is the way that does this:

    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
    }

This works fine on regular classes. For example, if I put java.lang.String in it, it will return the String class to me. If I put javax.swing.JTextField, it will return the correct class to me. However, if I put java.lang.String [] in it to request an array of strings, it is reset as such:

    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)

- , "" - , , Class.getDeclaredMethod() JTextField.class, getText(). - .

, , / ( java.lang.String [], javax.swing.JTextField [] [] [], int [] [] )?

, ?

+3
4

[Ljava.lang.String; java.lang.String[]

System.out.println(String[].class.getName());
+2

Array names in Java are denoted differently. You can change your method as follows to handle arrays:

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
}
+1
source

If you are creating a library, be nice to your customers and use the Collections classes instead of arrays.

For primitive types, you can always pass in a box version ( java.lang.Integeretc.)

0
source

All Articles