java.lang.IllegalAccessException Java Examples

The following examples show how to use java.lang.IllegalAccessException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: constructor.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Runs the test using the specified harness.
 *
 * @param harness  the test harness (<code>null</code> not permitted).
 */
public void test(TestHarness harness)
{
    IllegalAccessException object1 = new IllegalAccessException();
    harness.check(object1 != null);
    harness.check(object1.toString(), "java.lang.IllegalAccessException");

    IllegalAccessException object2 = new IllegalAccessException("nothing happens");
    harness.check(object2 != null);
    harness.check(object2.toString(), "java.lang.IllegalAccessException: nothing happens");

    IllegalAccessException object3 = new IllegalAccessException(null);
    harness.check(object3 != null);
    harness.check(object3.toString(), "java.lang.IllegalAccessException");

}
 
Example #2
Source File: Loader.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
  * Get the Thread Context Loader which is a JDK 1.2 feature. If we
  * are running under JDK 1.1 or anything else goes wrong the method
  * returns <code>null<code>.
  *
  *  */
private static ClassLoader getTCL() throws IllegalAccessException, 
  InvocationTargetException {

  // Are we running on a JDK 1.2 or later system?
  Method method = null;
  try {
    method = Thread.class.getMethod("getContextClassLoader", null);
  } catch (NoSuchMethodException e) {
    // We are running on JDK 1.1
    return null;
  }
  
  return (ClassLoader) method.invoke(Thread.currentThread(), null);
}
 
Example #3
Source File: TestUtils.java    From Spyglass with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static Object invokePrivateMethod(Object target, String methodName, Object... parameters)
    throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    Method method = getDeclaredMethodIgnoreCase(target.getClass(), methodName, parameters);
    method.setAccessible(true);

    if (parameters != null && parameters.length > 0) {
        return method.invoke(target, (Object[]) parameters);
    }
    return method.invoke(target);
}
 
Example #4
Source File: TryCatch.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Runs the test using the specified harness.
 *
 * @param harness  the test harness (<code>null</code> not permitted).
 */
public void test(TestHarness harness)
{
    // flag that is set when exception is caught
    boolean caught = false;
    try {
        throw new IllegalAccessException("IllegalAccessException");
    }
    catch (IllegalAccessException e) {
        // correct exception was caught
        caught = true;
    }
    harness.check(caught);
}
 
Example #5
Source File: TEnumHelper.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
/**
 * Given a TEnum class and integer value, this method will return
 * the associated constant from the given TEnum class.
 * This method MUST be modified should the name of the 'findByValue' method
 * change.
 *
 * @param enumClass TEnum from which to return a matching constant.
 * @param value Value for which to return the constant.
 *
 * @return The constant in 'enumClass' whose value is 'value' or null if
 *         something went wrong.
 */
public static TEnum getByValue(Class<? extends TEnum> enumClass, int value) {
  try {
    Method method = enumClass.getMethod("findByValue", int.class);
    return (TEnum) method.invoke(null, value);
  } catch (NoSuchMethodException nsme) {
    return null;
  } catch (IllegalAccessException iae) {
    return null;
  } catch (InvocationTargetException ite) {
    return null;
  }
}
 
Example #6
Source File: TEnumHelper.java    From galaxy-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * Given a TEnum class and integer value, this method will return
 * the associated constant from the given TEnum class.
 * This method MUST be modified should the name of the 'findByValue' method
 * change.
 *
 * @param enumClass TEnum from which to return a matching constant.
 * @param value Value for which to return the constant.
 *
 * @return The constant in 'enumClass' whose value is 'value' or null if
 *         something went wrong.
 */
public static TEnum getByValue(Class<? extends TEnum> enumClass, int value) {
  try {
    Method method = enumClass.getMethod("findByValue", int.class);
    return (TEnum) method.invoke(null, value);
  } catch (NoSuchMethodException nsme) {
    return null;
  } catch (IllegalAccessException iae) {
    return null;
  } catch (InvocationTargetException ite) {
    return null;
  }
}
 
Example #7
Source File: TestUtils.java    From Spyglass with Apache License 2.0 4 votes vote down vote up
private static Method getDeclaredMethodIgnoreCase(Class clazz, String methodName, Object... parameters)
    throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    if (clazz == null) {
        throw new NoSuchMethodException("Unable to find [" + methodName + "] in this class or any super classes");
    }

    for (Method method : clazz.getDeclaredMethods()) {
        // name match
        if (!method.getName().equalsIgnoreCase(methodName)) {
            continue;
        }

        // length match - null == 0
        Class[] types = method.getParameterTypes();
        boolean parametersIsEmpty = parameters == null || parameters.length == 0;
        boolean typesIsEmpty = types == null || types.length == 0;

        if ((parametersIsEmpty != typesIsEmpty) || (parameters.length != types.length)) {
            continue;
        }

        // isInstance match
        if (!typesIsEmpty) {
            boolean incompatibleTypeFound = false;
            for (int i = 0; i < types.length; i++) {
                if (!types[i].isInstance(parameters[i]) && parameters[i] != null){
                    incompatibleTypeFound = true;
                    break;
                }
            }

            if (incompatibleTypeFound) {
                continue;
            }
        }

        return method;
    }

    return getDeclaredMethodIgnoreCase(clazz.getSuperclass(), methodName, parameters);
}
 
Example #8
Source File: TestUtils.java    From Spyglass with Apache License 2.0 3 votes vote down vote up
/**
 * Allows us to query any member field of an object (including a private one). This is useful for inspecting and
 * verifying the private member variables of an object in a test. Note that if you change a variable name, you'll
 * no longer get a compile failure for name mismatching, it'll only happen at runtime. This is why this method is
 * only used in tests (We don't do this style of reflection in the app itself). Example usage might be:
 *
 * class Dummy {
 *     private Context context;
 * }
 *
 * Dummy dummy = new Dummy();
 *
 * Context context = TestUtils.getPrivateField(dummy, "context");
 *
 * Return type is based on expected type. If expected type is ambiguous (say for a method that has multiple
 * implementations with various types), you can specify the exact return type with:
 *
 * someFunction(TestUtils.<Context>getPrivateField(dummy, "context");
 *
 * The fieldName is caseInsensitive. If more than one member variable has the same case-insensitive name, the results
 * are indeterminate. This method will check SuperClasses as well. Ordering always returns the value of the variable
 * closest to the current class. For instance, if the class of the object passed in has the field, that one will be
 * retrieved even if the SuperClass also has a field with the same name. If the SuperClass has the field and the
 * current class does not, the SuperClasses will be used, even if it's superClass also has the field declared.
 * Also, never use the same variable name with different cases. Seriously.
 *
 * @param target object to get a field of
 * @param fieldName case insensitive name of the field to retrieve the value of
 * @param <T> type of the field.
 * @return the value of the field named by fieldName in the passed in target object.
 * @throws NoSuchFieldException if the field named does not exist.
 * @throws IllegalAccessException in case it's impossible to access the field due to a permission error. Normally
 * shouldn't happen.
 */
@SuppressWarnings("unchecked")
public static <T> T getPrivateField(Object target, String fieldName)
    throws NoSuchFieldException, IllegalAccessException
{
    Field field = getDeclaredFieldIgnoreCase(target.getClass(), fieldName);
    field.setAccessible(true);
    return (T) field.get(target);
}
 
Example #9
Source File: TestUtils.java    From Spyglass with Apache License 2.0 3 votes vote down vote up
/**
 * Allows us to set the value of a member variable of an object (even a private one) using reflection. This method
 * is ok to use in test cases but this type of reflection should never be used in the main app.  Note that if you
 * change a variable name, you'll no longer get a compile failure for name mismatching, it'll only happen at runtime.
 * Example of usage might be:
 *
 * class Dummy {
 *     private Context context;
 * }
 *
 * Dummy dummy = new Dummy();
 * Context context = new Context();
 *
 * TestUtils.setPrivateField(dummy, "context", context);
 *
 * The set fieldName is case insensitive. This method will indeterminately set the value to the first fieldName
 * that matches (ordering isn't guaranteed). If a field is not found in the current class, the superClass will also
 * be checked recursively until all SuperClasses are checked for the field. This will be a problem if you have 2 or
 * more member variables with the same name of different cases, which you really shouldn't be doing. Seriously.
 *
 * @param target object to set the value of a member variable
 * @param fieldName is the name of the member variable you will be setting
 * @param fieldValue is the value of that member variable that you want set
 * @throws NoSuchFieldException if the fieldName is not a memberVariable of the given target object
 * @throws IllegalAccessException in case it's impossible to access the field due to a permission error. Normally
 * shouldn't happen.
 */
public static void setPrivateField(Object target, String fieldName, Object fieldValue)
    throws NoSuchFieldException, IllegalAccessException
{
    Field field = getDeclaredFieldIgnoreCase(target.getClass(), fieldName);
    field.setAccessible(true);
    field.set(target, fieldValue);
}