Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / June 2008

Tip: Looking for answers? Try searching our database.

Creating a array class for a given class name

Thread view: 
Daniel Hohenberger - 27 Jun 2008 06:44 GMT
Hi all,

I'm currently using the following code to load a array class for a given class name:

Class<?> clazz = Class.forName(className);
clazz = Array.newInstance(clazz, 0).getClass();

I don't like instantiating the array, so my question is: Is there a better way
to create a (multidemensional) array class for a given class name?

TIA,
    Daniel
Patricia Shanahan - 27 Jun 2008 14:18 GMT
> Hi all,
>
[quoted text clipped - 10 lines]
> TIA,
>     Daniel

I think this may work:

Class.forName("[L"+className+";");

For more dimensions, add leading "[" symbols. This calls forName on
the string that the array's class object's getName() returns.

Patricia
Piotr Kobzda - 28 Jun 2008 11:59 GMT
> I think this may work:
>
> Class.forName("[L"+className+";");
>
> For more dimensions, add leading "[" symbols. This calls forName on
> the string that the array's class object's getName() returns.

And here is a bit extended use of your advice:

public class ArrayUtil {

    public static Class<?> arrayType(Class<?> componentType, int dims)
            throws ClassNotFoundException {
        char[] da = new char[dims];
        java.util.Arrays.fill(da, '[');
        return Class.forName(
                new String(da) + nativeTypeDescriptor(componentType),
                false, componentType.getClassLoader());
    }

    private static String nativeTypeDescriptor(Class<?> type) {
        String name = type.getName();
        char c = name.charAt(0);
        if (c == '[') {
            return name;
        }
        if (type.isPrimitive()) {
            if (c == 'l') { // long
                return "J";
            } else if (c == 'b' && name.length() == 7) { // boolean
                return "Z";
            } else {
                return String.valueOf(Character.toUpperCase(c));
            }
        }
        return "L" + name + ";";
    }

}

piotr
Daniel Hohenberger - 30 Jun 2008 07:58 GMT
Thanks a lot.

Signature

my homepage : http://hd42.de

'Life is wasted on the living' - Zaphod Beeblebrox the Fourth



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.