Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Reflection Bug #20

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/cinchapi/common/reflect/Reflection.java
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ else if(args == null || paramTypes == null
Object arg = args[i];
Class<?> type = paramTypes[i];
Class<?> altType = getAltType(type);
if(!type.isAssignableFrom(arg.getClass())
if(arg != null && !type.isAssignableFrom(arg.getClass())
&& !altType.isAssignableFrom(arg.getClass())) {
continue outer;
}
Expand Down
128 changes: 106 additions & 22 deletions src/test/java/com/cinchapi/common/reflect/ReflectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -399,23 +399,74 @@ public void testNoAmbiguityForOverloadedMethodsWithAutoboxedArgs() {
Reflection.call(new Foo(), "verA", "foo", new Long(1));
Assert.assertTrue(true); // lack of Exception means we pass
}

@Test
public void testCallOverrideWithGenericParameter() {
GenericChild obj = new GenericChild();
Reflection.call(obj, "put", "Company", "Cinchapi");
Assert.assertTrue(true); // lack of Exception means we pass
}

@Test
public void testCallDefaultInterfaceMethod() {
ClassB obj = new ClassB();
String expected = "Jeff Nelson";
String actual = Reflection.call(obj, "foo", expected);
Assert.assertEquals("foo_"+expected, actual);
Assert.assertEquals("foo_" + expected, actual);
Assert.assertEquals("baz", Reflection.call(obj, "baz", expected));
}

@Test
public void testNewInstanceWithNullValue() {
String value = null;
A a = Reflection.newInstance(A.class, value);
Assert.assertEquals(value, a.string);
}

@Test
public void testNewInstanceWithNullValues() {
String string = "foo";
Integer ivalue = 5;
Long lvalue = 8L;
Double dvalue = 3.0;
String label = "label";

F f;

f = Reflection.newInstance(F.class, null, ivalue, label);
Assert.assertNull(f.string);
Assert.assertEquals(f.ivalue, ivalue);
Assert.assertEquals(f.label, label);

f = Reflection.newInstance(F.class, string, (Integer) null, label);
Assert.assertNull(f.ivalue);
Assert.assertEquals(f.string, string);
// FIXME: The the F(String, Double, String) constructor was chosen
// Assert.assertEquals(f.label, label);

f = Reflection.newInstance(F.class, null, null, label);
Assert.assertNull(f.string);
Assert.assertNull(f.ivalue);
Assert.assertNull(f.lvalue);
Assert.assertNull(f.dvalue);
// FIXME: The the F(String, Double, String) constructor was chosen
// Assert.assertEquals(f.label, label);

f = Reflection.newInstance(F.class, null, null, null);
Assert.assertNull(f.string);
Assert.assertNull(f.ivalue);
Assert.assertNull(f.lvalue);
Assert.assertNull(f.dvalue);
Assert.assertNull(f.label);

f = Reflection.newInstance(F.class, string, lvalue, null);
Assert.assertEquals(string, f.string);
Assert.assertNull(f.ivalue);
Assert.assertEquals(lvalue, f.lvalue);
Assert.assertNull(f.dvalue);
Assert.assertNull(f.label);
}

private static class A {

private final String string;
Expand Down Expand Up @@ -579,56 +630,89 @@ public void verA(String arg0, long arg) {}
public void verA(String arg0, Long arg) {}

}

public class GenericBase {

public <T> void put(String key, T value) {

}
}

public class GenericChild extends GenericBase {

@Override
public <T> void put(String key, T value) {

}
}

class ClassA implements InterfaceA, InterfaceB {

class ClassA implements InterfaceA, InterfaceB {

@Override
public String baz(String value) {
return value;
}

}

class ClassB extends ClassA {

@Override
public String baz(String value) {
return "baz";
}
}

interface InterfaceA {

public String baz(String value);

public default String foo(String value) {
return "foo_"+value;
return "foo_" + value;
}

}

interface InterfaceB {
public default String bar(String value) {
return "bar_"+value;
return "bar_" + value;
}

public String baz(String value);
}

public static class F {

String string;
Integer ivalue;
Long lvalue;
String label;
Double dvalue;
String tag;

public F(String string, Integer value, String label) {
this.string = string;
this.ivalue = value;
this.label = label;
}

public F(String string, Long value, String label) {
this.string = string;
this.lvalue = value;
this.label = label;
}

public F(Double value, String string, String label) {
this.dvalue = value;
this.string = string;
this.label = label;
}

public F(String string, Double value, String tag) {
this.string = string;
this.dvalue = value;
this.tag = tag;
}
}

}