Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How do I invoke a setter method which takes a primitive as a parameter

800439Jul 28 2009 — edited Jul 29 2009
Hi. I am trying to write some code that will increase my code coverage by traversing my project and inspecting each class for getters, setters and booleans. The program should invoke these methods under junit. However, I've hit a snag. What if my code calls for a setMethod(int x) or setMethod(char c)? The code below will not work with primitives (it throws an IllegalArgumentException). Could someone tell me how to fix this, please?
void invokeAMethod(Object object, Method method, Class[] paramTypes) {
		String params = "";
		for (Class myclass : paramTypes) {
			params += myclass + " ";
		}
		try {
			
			Object returnValue = method.invoke(object, params);
			System.out.println("done with " + method.getName());
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 26 2009
Added on Jul 28 2009
11 comments
983 views