[Java]Javaでrefrection

忘れないようにメモ.

	public static Object invoke(final Object target, final String methodName, final Object... params) throws Exception {

		List<Class<?>> parameterTypes = new ArrayList<Class<?>>();
		for (Object o : params) {
			parameterTypes.add(o.getClass());
		}

		Class<?> targetClass = target.getClass();

		Method method = targetClass.getMethod(methodName, parameterTypes
				.toArray((new Class<?>[] {})));

		return method.invoke(target, params);

	}


Popularity: 8% [?]

  • Digg
  • Google Bookmarks
  • Google Reader
  • Facebook
  • Delicious
  • FriendFeed
  • Evernote
  • Twitter
  • Share/Bookmark
  1. No comments yet.

  1. No trackbacks yet.