Skip to content

Commit

Permalink
Do not throw NPE when intermediate field is an interface
Browse files Browse the repository at this point in the history
and the interface does not contain given attribute

The issue was originally reported by @wimdeblauwe
 at hazelcast#6151 Thanks to @serkan-ozal for analysis!

(cherry picked from commit 924e459)
  • Loading branch information
jerrinot committed Sep 11, 2015
1 parent cf709ca commit e0f29e7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private static Getter createGetter(Object obj, String attribute) {
}
if (localGetter == null) {
Class c = clazz;
while (!Object.class.equals(c)) {
while (!c.isInterface() && !Object.class.equals(c)) {
try {
final Field field = c.getDeclaredField(name);
field.setAccessible(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.hazelcast.query.impl.getters;

import com.hazelcast.query.QueryException;
import com.hazelcast.query.impl.AttributeType;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

@RunWith(HazelcastParallelClassRunner.class)
@Category(QuickTest.class)
Expand All @@ -19,4 +22,31 @@ public void test_getAttributeType() throws Exception {

assertNull(attributeType);
}


@Test
public void extractValue_whenIntermediateFieldIsInterfaceAndDoesNotContainField_thenThrowIllegalArgumentException() throws Exception {
OuterObject object = new OuterObject();
try {
ReflectionHelper.extractValue(object, "emptyInterface.doesNotExist");
fail("Non-existing field has been ignored");
} catch (QueryException e) {
// createGetter() method is catching everything throwable and wraps it in QueryException
// I don't think it's the right thing to do, but I don't want to change this behaviour.
// Hence I have to use try/catch in this test instead of just declaring
// IllegalArgumentException as expected exception.
assertEquals(IllegalArgumentException.class, e.getCause().getClass());
}
}



private static class OuterObject {
private EmptyInterface emptyInterface;
}

private interface EmptyInterface {

}

}

0 comments on commit e0f29e7

Please sign in to comment.