Andrew D.
Andrew D.

Reputation: 1022

Java: How to pass typed-array class as parameter?

I have some 3rd-party API with method someFn(int code, int[] data).

How can I express types (particulary array-typed argument) outside API container (interface class) for usage in Class.getMethod(name, Integer.class, ???.class);? How can I describe argument of array type properly?

As far as I know, I may use workaround like, but is there a better way of doing this?

class C { void f(int[] a) {} } }

C.class.getDeclaredMethods()[0].getParameterTypes()[0]

Thanks in advance

Upvotes: 0

Views: 1086

Answers (2)

Yogendra Singh
Yogendra Singh

Reputation: 34367

    Class.getMethod(name, Integer.class, int[].class);

int[] is class and hence int[].class is a valid statement to refer int array class.

Upvotes: 2

reprogrammer
reprogrammer

Reputation: 14728

You can use int[].class to get the class literal of int [] and pass it to Class.getMethod:

Class.getMethod(name, Integer.class, int[].class);

Upvotes: 3

Related Questions