Search in sources :

Example 1 with TooManyFieldsFoundException

use of org.powermock.reflect.exceptions.TooManyFieldsFoundException in project powermock by powermock.

the class WhiteboxImpl method findSingleFieldUsingStrategy.

/**
     * Find single field using strategy.
     *
     * @param strategy       the strategy
     * @param object         the object
     * @param checkHierarchy the check hierarchy
     * @param startClass     the start class
     * @return the field
     */
private static Field findSingleFieldUsingStrategy(FieldMatcherStrategy strategy, Object object, boolean checkHierarchy, Class<?> startClass) {
    assertObjectInGetInternalStateIsNotNull(object);
    Field foundField = null;
    final Class<?> originalStartClass = startClass;
    while (startClass != null) {
        final Field[] declaredFields = startClass.getDeclaredFields();
        for (Field field : declaredFields) {
            if (strategy.matches(field) && hasFieldProperModifier(object, field)) {
                if (foundField != null) {
                    throw new TooManyFieldsFoundException("Two or more fields matching " + strategy + ".");
                }
                foundField = field;
            }
        }
        if (foundField != null) {
            break;
        } else if (!checkHierarchy) {
            break;
        }
        startClass = startClass.getSuperclass();
    }
    if (foundField == null) {
        strategy.notFound(originalStartClass, !isClass(object));
    }
    foundField.setAccessible(true);
    return foundField;
}
Also used : Field(java.lang.reflect.Field) TooManyFieldsFoundException(org.powermock.reflect.exceptions.TooManyFieldsFoundException)

Example 2 with TooManyFieldsFoundException

use of org.powermock.reflect.exceptions.TooManyFieldsFoundException in project powermock by powermock.

the class WhiteBoxTest method testSetInternalMultipleOfSameTypeOnSpecificPlaceInHierarchy.

@Test
public void testSetInternalMultipleOfSameTypeOnSpecificPlaceInHierarchy() throws Exception {
    final int value = 31;
    ClassWithChildThatHasInternalState tested = new ClassWithChildThatHasInternalState();
    try {
        Whitebox.setInternalState(tested, value, ClassWithInternalState.class);
        fail("should throw TooManyFieldsFoundException!");
    } catch (TooManyFieldsFoundException e) {
        assertEquals("Two or more fields matching type int.", e.getMessage());
    }
}
Also used : TooManyFieldsFoundException(org.powermock.reflect.exceptions.TooManyFieldsFoundException) ClassWithChildThatHasInternalState(org.powermock.reflect.testclasses.ClassWithChildThatHasInternalState) Test(org.junit.Test)

Example 3 with TooManyFieldsFoundException

use of org.powermock.reflect.exceptions.TooManyFieldsFoundException in project powermock by powermock.

the class WhiteBoxTest method testSetInternalMultipleOfSameType.

@Test
public void testSetInternalMultipleOfSameType() throws Exception {
    final int value = 31;
    ClassWithInternalState tested = new ClassWithInternalState();
    try {
        Whitebox.setInternalState(tested, value);
        fail("should throw TooManyFieldsFoundException!");
    } catch (TooManyFieldsFoundException e) {
        assertEquals("Two or more fields matching type int.", e.getMessage());
    }
}
Also used : ClassWithInternalState(org.powermock.reflect.testclasses.ClassWithInternalState) TooManyFieldsFoundException(org.powermock.reflect.exceptions.TooManyFieldsFoundException) Test(org.junit.Test)

Aggregations

TooManyFieldsFoundException (org.powermock.reflect.exceptions.TooManyFieldsFoundException)3 Test (org.junit.Test)2 Field (java.lang.reflect.Field)1 ClassWithChildThatHasInternalState (org.powermock.reflect.testclasses.ClassWithChildThatHasInternalState)1 ClassWithInternalState (org.powermock.reflect.testclasses.ClassWithInternalState)1