Skip to content

Commit 1a17112

Browse files
fixes #2340 merged UtilityElf reflection instantiation improvements
1 parent 483b833 commit 1a17112

3 files changed

Lines changed: 42 additions & 4 deletions

File tree

src/main/java/com/zaxxer/hikari/util/UtilityElf.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919
import org.slf4j.Logger;
2020
import org.slf4j.LoggerFactory;
2121

22+
import java.lang.reflect.Constructor;
23+
import java.util.Arrays;
2224
import java.util.Locale;
2325
import java.util.concurrent.*;
2426
import java.util.regex.Pattern;
27+
import java.util.stream.IntStream;
2528

2629
import static java.lang.Thread.currentThread;
2730
import static java.util.concurrent.TimeUnit.SECONDS;
@@ -134,7 +137,18 @@ public static <T> T createInstance(final String className, final Class<T> clazz,
134137
for (int i = 0; i < totalArgs; i++) {
135138
argClasses[i] = args[i].getClass();
136139
}
137-
var constructor = loaded.getConstructor(argClasses);
140+
141+
Constructor<?> constructor = Arrays.stream(loaded.getConstructors())
142+
.filter(c -> {
143+
if (c.getParameterCount() != totalArgs) return false;
144+
145+
Class<?>[] params = c.getParameterTypes();
146+
return IntStream.range(0, totalArgs)
147+
.allMatch(i -> params[i].isAssignableFrom(argClasses[i]));
148+
})
149+
.findFirst()
150+
.orElseThrow(() -> new RuntimeException("No suitable constructor found for class " + className + " with arguments " + Arrays.toString(args)));
151+
138152
return clazz.cast(constructor.newInstance(args));
139153
}
140154
catch (Exception e) {

src/test/java/com/zaxxer/hikari/pool/TestValidation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void validateBadDriver()
106106
fail();
107107
}
108108
catch (RuntimeException ise) {
109-
assertTrue(ise.getMessage().startsWith("Failed to load driver class invalid "));
109+
assertTrue(ise.getMessage().startsWith("Failed to load driver class invalid"));
110110
}
111111
}
112112

src/test/java/com/zaxxer/hikari/util/UtilityElfTest.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void shouldReturnValidTransactionIsolationLevel()
2929
int expectedLevel = UtilityElf.getTransactionIsolation("TRANSACTION_SQL_SERVER_SNAPSHOT_ISOLATION_LEVEL");
3030

3131
//Assert
32-
assertEquals(expectedLevel, 4096);
32+
assertEquals(4096, expectedLevel);
3333
}
3434

3535
@Test(expected = IllegalArgumentException.class)
@@ -43,7 +43,7 @@ public void shouldThrowWhenInvalidTransactionNameGiven()
4343
public void shouldReturnTransationIsolationLevelFromInteger()
4444
{
4545
int expectedLevel = UtilityElf.getTransactionIsolation("4096");
46-
assertEquals(expectedLevel, 4096);
46+
assertEquals(4096, expectedLevel);
4747
}
4848

4949
@Test(expected = IllegalArgumentException.class)
@@ -52,4 +52,28 @@ public void shouldThrowWhenInvalidTransactionIntegerGiven()
5252
//Act
5353
UtilityElf.getTransactionIsolation("9999");
5454
}
55+
56+
@Test
57+
public void shouldCreateInstanceOfClassWithConstructorThatAcceptsSuperClassAndInterfaceAndClassOfArguments() {
58+
//Act
59+
UtilityElf.createInstance("com.zaxxer.hikari.util.UtilityElfTest$ClassZ",
60+
Object.class,
61+
new ClassB(),
62+
new ClassC(),
63+
new ClassD());
64+
}
65+
66+
public static class ClassA {}
67+
68+
public static final class ClassB extends ClassA {}
69+
70+
public interface InterfaceC {}
71+
72+
public final static class ClassC implements InterfaceC {}
73+
74+
public final static class ClassD {}
75+
76+
public final static class ClassZ {
77+
public ClassZ(ClassA _superClassA, InterfaceC _interfaceC, ClassD _classD) {}
78+
}
5579
}

0 commit comments

Comments
 (0)