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;
}
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());
}
}
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());
}
}
Aggregations