4

I define a classloader name MyClassLoader as such.

cl = new MyClassLoader();

I could use cl to load my class A.

Class c = cl.loadClass();

but how to load a class which is A[].class?

4
  • What is your end goal? Commented Dec 23, 2013 at 6:39
  • I want get a Class instance which is equal to A[].class. A is loaded by MyClassLoader Commented Dec 23, 2013 at 6:40
  • Why can't you just do A[].class? Do you want to do it at run time where you don't know what A is? Commented Dec 23, 2013 at 6:45
  • yes,"A" is in a config file.I use classloader to get A.class.Then i need to call decode(String s,Class<T> t) to get an instance of A[]. Commented Dec 23, 2013 at 6:47

3 Answers 3

3

I think you want to use java.lang.reflect.Array.newInstance() like this -

A[] arr = java.lang.reflect.Array.newInstance(A.class, 10); /* for example */
Sign up to request clarification or add additional context in comments.

3 Comments

I want get a Class instance which is equal to A[].class. A is loaded by MyClassLoader. I think java.lang.reflect.Array.newInstance(A.class, 10) is an instance of A[].class.
Java arrays are first class Objects. I'm not sure I follow.
maybe arr.getClass() is what I want
2

Assuming your ClassLoader implementation, MyClassLoader is properly implemented, you'd simply pass the canonical name of the array class to the loadClass method. For example, if you were trying to load the Class for the class com.something.Foo, you'd use

cl.loadClass("[Lcom.something.Foo");

You have to know that the class name for Array types is the character [ appended X number of times, where X is the number of array dimensions, then the character L. You then append the qualified class name of the type of the array. And finally, you append the character ;.

Therefore

Class<?> clazz = Class.forName("[Lcom.something.Examples;");

would get you the Class instance returned by A[].class if it was loaded by the same ClassLoader.

Note that you won't be able to instantiate this type this way. You'll have to use Array.newInstance() passing in the Class object for A, for a one dimensional array, the Class object for A[] for a two dimensional array and so on.

4 Comments

how to use methods like decode(String s,Class<T> t) where t is A[].class? I had tried to load "[Ljava.lang.String;" , but throws Exception in thread "main" java.lang.ClassNotFoundException: [Ljava.lang.String;
@user I don't know. I don't get any ClassNotFoundException with that class. What is decode?
I tried with it ,syntax like "[Ljava.lang.String"; can be used with Class.forName(),but can't used with classloader.loadClass method.
The question was how to load it with a ClassLoader, and these kind of names won't work there. Doing this with the static Class.forName method is not the same, as you have no good control over what ClassLoader will it use.
1

In Java 12, you can call someClass.arrayType() to get the "array of someClass" class. If you need a two dimensional array, that will be someClass.arrayType().arrayType(). Of course, you can load someClass itself like Class<?> someClass = someClassLoader.loadClass("com.example.A").

I couldn't find a solution in the standard API of earlier Java versions, so I have written this utility method:

/**
 * Returns the array type that corresponds to the element type and the given number of array dimensions.
 * If the dimension is 0, it just returns the element type as is.
 */
public static Class<?> getArrayClass(Class<?> elementType, int dimensions) {
    return dimensions == 0 ? elementType : Array.newInstance(elementType, new int[dimensions]).getClass();
}

And then you can do this:

Class<?> arrayClass = getArrayClass(someClass, 1);

Kind of an ugly workaround, but... unlike with Class.forName, you control what ClassLoader is used, and you don't have to write another utility to generate funny names like "[[I" or "[Lcom.example.A;".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.