Java Reflection: Is there an inverse of Class.getComponentType()

  • Thread starter Benjamin Johnston
  • Start date
B

Benjamin Johnston

Is there a simple way of reversing the effect of
Class.getComponentType? I want to find the array type for a given
class.

I could instantiate a new array using Array, and then call getClass on
that. Or I could surround the results from Class.getName with "[L" ...
";" (or just prefix "[") as appropriate and call Class.forName, but
neither of these seem satisfying.
Thanks a lot,

-Benjamin Johnston
(e-mail address removed)
 
M

Mike Schilling

Benjamin Johnston said:
Is there a simple way of reversing the effect of
Class.getComponentType? I want to find the array type for a given
class.

I could instantiate a new array using Array, and then call getClass on
that. Or I could surround the results from Class.getName with "[L" ...
";" (or just prefix "[") as appropriate and call Class.forName, but
neither of these seem satisfying.

I'd do the former. The latter could act oddly if the class in question was
loaded from a class loader other than the context class loader.


It seems likely to me that the Class representing the array type is created
when the first array of that type is created. (The class type for an
N-dimensional array is different for each N, so they can't all be created
when the component class is first loaded.) So it may be necessary to
create the array. At any rate, the cost of doing so is probably negligible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top