Just a quick question.
No rush to answer, it is not important, I might discover it later myself, but just in case someone knows it:

I have been trying to create a Method object using this method:
Class.getMethod(String name, Class<?>... parameterTypes)

like this:

obj.getClass().getMethod(methodName, Class.forName(methodType))

My problem is that the method takes as argument an int:

public void setA(int a) { ... }

So when I call this:
Class.forName("int") I get an exception:
java.lang.ClassNotFoundException: int

But when I call it like this:
Class.forName("java.lang.Integer") I get an exception:
java.lang.NoSuchMethodException: pack.ClassR.setA(java.lang.Integer)

Is there a way to fix this other than changing the signature of the method?

7 Years
Discussion Span
Last Post by javaAddict

You don't need to change the signature. You can obtain the `Class' instance of the primitives using either `int.class' or 'Integer.TYPE'.

import java.lang.reflect.Method;

public class Reflection {
	public static void main(final String[] args) throws Exception {		
		Method m = new Test().getClass().getMethod("doForInt", int.class);
		System.out.println(m.invoke(new Test(), 1));


class Test {
	public int doForInt(int i) {
		return i;

Thanks, it worked.

I was searching the Integer class, but I must have missed that part:

static Class<Integer> TYPE
The Class instance representing the primitive type int.

Although I will use your suggestion, I was expecting for something more dynamic:

// code to get the type

String type="int";

I don't know if the above can be done, which is why I started this thread


Though you can't do Class.forName on primitives, you can use the following trick for wrapper classes:

Class klass = (Class)Class.forName("java.lang.Integer").getField("TYPE").get(null);
Method m = new Test().getClass().getMethod("doForInt", klass);		
System.out.println(m.invoke(new Test(), 1));

You can have a map which maintains a mapping of primitive types to their wrapper equivalent i.e. "int" => "java.lang.Integer" and use the above trick to pull in the class object of primitives. A bit convoluted but pretty much does the job.


Well I have finished the first part of my code.
The reason I had this question ~s.o.s~ was that I wanted to create a dynamic XML parser.

I have 1 method that takes as argument an Object and generates an XML file. The program uses reflection to get the names and return types of all the public get methods of the object and after invoking them it writes the file. Recursive was considered in case the get methods return a more complex object.

And I have another method that does the opposite. Takes as argument a Node interface and after "reading" the method names (public set methods), return types and values it invokes the set methods of the objects described at the tags to return an Object type. Again recursive was considered.

I know that this was a waste of time since there are a lot of tools that do that, but the main reason why I thought of doing that was beacause I think it's fun and most important educational.
I have never worked with the java.lang.reflect nor the org.w3c.dom packages so I wanted gain some experience.

My methods accept objects whose get,set methods return primitives, java.lang.* objects and other objects that have again the same get, set method signatures.

I will now try to add Arrays [] and List at the get, set methods

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.