Skip to content

Commit dccf173

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents e814063 + a310702 commit dccf173

File tree

7 files changed

+39
-8
lines changed

7 files changed

+39
-8
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ through multiple converters if a direct one is not available. It contains many b
1515
<dependency>
1616
<groupId>com.github.bbottema</groupId>
1717
<artifactId>java-reflection</artifactId>
18-
<version>3.11.3</version>
18+
<version>3.12.0</version>
1919
</dependency>
2020
```
2121

22+
v3.12.0 (4-November-2019)
23+
24+
- For simple class lookups, In addition to java.lang, also try the packages java.util and java.math
25+
26+
2227
v3.11.1 - v3.11.3 (29-October-2019 - 1-November-2019)
2328

2429
- 3.11.3: Solved an NullPointerException in a collectMethods method

RELEASE.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ https://github.com/bbottema/java-reflection
44
RELEASE NOTES Java Reflection
55

66

7+
v3.12.0 (4-November-2019)
8+
9+
- For simple class lookups, In addition to java.lang, also try the packages java.util and java.math
10+
11+
712
v3.11.1 - v3.11.3 (29-October-2019 - 1-November-2019)
813

914
- 3.11.3: Solved an NullPointerException in a collectMethods method

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<artifactId>java-reflection</artifactId>
88
<packaging>jar</packaging>
99
<name>Java Reflection</name>
10-
<version>3.11.3</version>
10+
<version>3.12.1</version>
1111
<description>Java Reflection provides a small package with nifty reflection features that will help with finding constructors, methods and value conversions</description>
1212
<url>https://github.com/bbottema/java-reflection</url>
1313
<inceptionYear>2011</inceptionYear>

src/main/java/org/bbottema/javareflection/ClassUtils.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* </ul>
3737
*/
3838
@UtilityClass
39+
@SuppressWarnings("WeakerAccess")
3940
public final class ClassUtils {
4041

4142
/**
@@ -53,12 +54,18 @@ public static <T> Class<T> locateClass(final String className, final boolean ful
5354
if (CLASS_CACHE.containsKey(cacheKey)) {
5455
return (Class<T>) CLASS_CACHE.get(cacheKey);
5556
}
56-
final Class<?> _class;
57+
Class<?> _class;
5758
if (fullscan) {
5859
_class = locateClass(className, null, classLoader);
5960
} else {
6061
// try standard package used for most common classes
6162
_class = locateClass(className, "java.lang", classLoader);
63+
if (_class == null) {
64+
_class = locateClass(className, "java.util", classLoader);
65+
}
66+
if (_class == null) {
67+
_class = locateClass(className, "java.math", classLoader);
68+
}
6269
}
6370
CLASS_CACHE.put(cacheKey, _class);
6471
return (Class<T>) _class;

src/main/java/org/bbottema/javareflection/MethodUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public final class MethodUtils {
7373
*/
7474
@Nullable
7575
@SuppressWarnings({"unchecked"})
76-
public static <T> T invokeMethodSimple(final Method method, final Object subject, final Object... args) {
76+
public static <T> T invokeMethodSimple(final Method method, @Nullable final Object subject, final Object... args) {
7777
try {
7878
return (T) method.invoke(subject, args);
7979
} catch (SecurityException e) {

src/test/java/org/bbottema/javareflection/ClassUtilsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.net.Socket;
1717
import java.util.Collection;
1818
import java.util.List;
19+
import java.util.Locale;
1920
import java.util.Map;
2021

2122
import static java.util.EnumSet.of;
@@ -33,6 +34,7 @@ public void resetStaticCaches() {
3334

3435
@Test
3536
public void testLocateClass() {
37+
assertThat(ClassUtils.locateClass("Locale", false, null)).isEqualTo(Locale.class);
3638
assertThat(ClassUtils.locateClass("Math", false, null)).isEqualTo(Math.class);
3739
assertThat(ClassUtils.locateClass("Mathh", false, null)).isNull();
3840
assertThat(ClassUtils.locateClass("ClassUtils", false, null)).isNull();

src/test/java/org/bbottema/javareflection/MethodUtilsTest.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.bbottema.javareflection.valueconverter.ValueConversionHelper;
1919
import org.junit.Before;
2020
import org.junit.Test;
21+
import org.mockito.internal.util.collections.Iterables;
2122
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;
2223

2324
import javax.annotation.Nonnull;
@@ -38,12 +39,14 @@
3839

3940
import static java.util.Arrays.asList;
4041
import static java.util.EnumSet.allOf;
42+
import static java.util.EnumSet.of;
4143
import static java.util.Objects.requireNonNull;
4244
import static org.assertj.core.api.Assertions.assertThat;
4345
import static org.assertj.core.api.Assertions.fail;
4446
import static org.bbottema.javareflection.ClassUtils.collectMethodsByName;
4547
import static org.bbottema.javareflection.MethodUtils.onlyMethod;
4648
import static org.bbottema.javareflection.model.MethodModifier.MATCH_ANY;
49+
import static org.bbottema.javareflection.model.MethodModifier.PUBLIC;
4750

4851
public class MethodUtilsTest {
4952

@@ -208,10 +211,10 @@ public void testIsMethodCompatible_Simple() throws NoSuchMethodException {
208211
@Test
209212
public void testIsMethodCompatible_TestLookupModes() throws NoSuchMethodException {
210213
Set<LookupMode> noConversions = EnumSet.noneOf(LookupMode.class);
211-
Set<LookupMode> commonConversions = EnumSet.of(LookupMode.COMMON_CONVERT);
212-
Set<LookupMode> castConvert = EnumSet.of(LookupMode.CAST_TO_SUPER, LookupMode.CAST_TO_INTERFACE);
213-
Set<LookupMode> castThenCommonsConvert = EnumSet.of(LookupMode.CAST_TO_SUPER, LookupMode.COMMON_CONVERT);
214-
Set<LookupMode> smartConversions = EnumSet.of(LookupMode.SMART_CONVERT);
214+
Set<LookupMode> commonConversions = of(LookupMode.COMMON_CONVERT);
215+
Set<LookupMode> castConvert = of(LookupMode.CAST_TO_SUPER, LookupMode.CAST_TO_INTERFACE);
216+
Set<LookupMode> castThenCommonsConvert = of(LookupMode.CAST_TO_SUPER, LookupMode.COMMON_CONVERT);
217+
Set<LookupMode> smartConversions = of(LookupMode.SMART_CONVERT);
215218

216219
Method m = Math.class.getMethod("min", int.class, int.class);
217220
assertThat(MethodUtils.isMethodCompatible(m, noConversions, int.class, boolean.class)).isFalse();
@@ -334,4 +337,13 @@ public void testFirstParameterIndexByAnnotation() {
334337
assertThat(MethodUtils.firstParameterIndexByAnnotation(testMethod, Nonnull.class)).isEqualTo(2);
335338
assertThat(MethodUtils.firstParameterIndexByAnnotation(testMethod, Meta.class)).isEqualTo(-1);
336339
}
340+
341+
@Test
342+
public void testInvokeMethodSimple() {
343+
Set<InvokableObject<Method>> valueOf = MethodUtils.findSimpleCompatibleMethod(String.class, "valueOf", boolean.class);
344+
Method valueOfBoolean = Iterables.firstOf(valueOf).getMethod();
345+
346+
assertThat(MethodUtils.invokeMethodSimple(valueOfBoolean, null, true)).isEqualTo("true");
347+
assertThat(MethodUtils.invokeMethodSimple(valueOfBoolean, null, false)).isEqualTo("false");
348+
}
337349
}

0 commit comments

Comments
 (0)