blob: 00e40cfb97a8a7694b4aa3cd1370c544b024298f [file] [log] [blame]
/*
* Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This software, including documentation is protected by copyright controlled
* by Nokia Corporation. Copying, including reproducing, storing, adapting or
* translating, any or all of this material can only be done in accordance
* with the Nokia Symbian License version 1.0 (or any subsequent update) or
* any other license terms expressly agreed between you and Nokia.
* This material contains Nokia's confidential information which
* may not be disclosed to others without the prior written consent of Nokia.
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:
*
*/
package org.eclipse.cdt.debug.edc.tests;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class TestReflectionHelper {
public static Object getPrivateField(final String fieldName, final Object o)
throws NoSuchFieldException, IllegalAccessException {
Class<?> c = o.getClass();
Field f = c.getDeclaredField(fieldName);
f.setAccessible(true);
return f.get(o);
}
public static boolean privateStringEquality(final String left,
final String rightFieldName, final Object o)
throws SecurityException, IllegalArgumentException,
NoSuchFieldException, IllegalAccessException {
return left.equals(getPrivateField(rightFieldName, o));
}
public static String stringFromPrivateFunction(final Object o,
final String funcName) throws NoSuchMethodException,
IllegalAccessException, InvocationTargetException {
Class<?> c = o.getClass();
Method m = c.getDeclaredMethod(funcName, (Class<?>[]) null);
if (!m.getReturnType().equals(String.class))
throw new IllegalArgumentException("member function '" + funcName + "' must return type String");
m.setAccessible(true);
return (String)m.invoke(o, (Object[])null);
}
private static Class<?>[] getArgClasses(final Object[] argList) {
Class<?>[] argClasses = null;
if (argList != null && argList.length > 0) {
argClasses = new Class<?>[argList.length];
for (int i = 0 ; i < argList.length; ++i) {
argClasses[i] = argList[i].getClass();
}
}
return argClasses;
}
public static Object objectFromPrivateFunctionWithArgs(
final Object o, final String funcName, final Object[] argList)
throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
return objectFromPrivateFunctionWithArgs(o, funcName, argList, getArgClasses(argList));
}
public static Object objectFromPrivateFunctionWithArgs(
final Object o, final Class<?> c, final String funcName, final Object[] argList)
throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
return objectFromPrivateFunctionWithArgs(o, c, funcName, argList, getArgClasses(argList));
}
public static Object objectFromPrivateFunctionWithArgs(
final Object o, final String funcName,
final Object[] argList, final Class<?>[] argClasses)
throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
Class<?> c = o.getClass();
return objectFromPrivateFunctionWithArgs(o, c, funcName, argList, argClasses);
}
public static Object objectFromPrivateFunctionWithArgs(
final Object o, final Class<?> c, final String funcName,
final Object[] argList, final Class<?>[] argClasses)
throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
Method m = c.getDeclaredMethod(funcName, argClasses);
m.setAccessible(true);
return m.invoke(o, argList);
}
}