Skip to content

Commit 39b8df1

Browse files
committed
Fixed support for Java 14+
1 parent aa6856c commit 39b8df1

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

src/main/java/pl/mikigal/config/ConfigInvocationHandler.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ private void prepareMethods() {
219219
*/
220220
private boolean updateConfigFile() {
221221
boolean modified = false;
222-
Object proxy = ReflectionUtils.createHelperProxy(this.clazz);
223222
for (Method method : this.clazz.getDeclaredMethods()) {
224223
String name = method.getName();
225224
if (!name.startsWith("get") && !name.startsWith("set")) {
@@ -230,7 +229,7 @@ private boolean updateConfigFile() {
230229
continue;
231230
}
232231

233-
Object defaultValue = ReflectionUtils.getDefaultValue(proxy, method);
232+
Object defaultValue = ReflectionUtils.getDefaultValue(method);
234233
if (defaultValue == null && !method.isAnnotationPresent(ConfigOptional.class)) {
235234
throw new InvalidConfigException("Method " + method.getName() + " is not optional, but it's default value is null");
236235
}

src/main/java/pl/mikigal/config/util/ReflectionUtils.java

+15-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pl.mikigal.config.exception.InvalidConfigException;
55

66
import java.lang.invoke.MethodHandles;
7-
import java.lang.reflect.Constructor;
7+
import java.lang.reflect.Field;
88
import java.lang.reflect.Method;
99
import java.lang.reflect.Proxy;
1010

@@ -15,30 +15,31 @@
1515
*/
1616
public class ReflectionUtils {
1717

18-
private static final Constructor<MethodHandles.Lookup> lookupConstructor;
18+
private static final MethodHandles.Lookup lookup;
1919

2020
static {
2121
try {
22-
lookupConstructor = MethodHandles.Lookup.class.getDeclaredConstructor(Class.class, Integer.TYPE);
23-
lookupConstructor.setAccessible(true);
24-
} catch (NoSuchMethodException e) {
25-
throw new InvalidConfigException("Could not get MethodHandles.Lookup constructor", e);
22+
Field field = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP");
23+
field.setAccessible(true);
24+
25+
lookup = (MethodHandles.Lookup) field.get(null);
26+
} catch (NoSuchFieldException | IllegalAccessException e) {
27+
throw new InvalidConfigException("Could not get MethodHandles.Lookup", e);
2628
}
2729
}
2830

2931
/**
3032
* Allows to get default value of method from interface
31-
* @param proxy instance of proxied object
32-
* @param method method of which you want to get defalt value
33+
* @param method method of which you want to get default value
3334
* @return default value of method from interface
3435
*/
35-
public static Object getDefaultValue(Object proxy, Method method) {
36+
public static Object getDefaultValue(Method method) {
3637
try {
3738
Class<?> clazz = method.getDeclaringClass();
38-
return lookupConstructor.newInstance(clazz, MethodHandles.Lookup.PRIVATE)
39+
return lookup
3940
.in(clazz)
4041
.unreflectSpecial(method, clazz)
41-
.bindTo(proxy)
42+
.bindTo(createHelperProxy(method.getDeclaringClass()))
4243
.invoke();
4344
} catch (Throwable throwable) {
4445
throw new InvalidConfigException(throwable);
@@ -50,8 +51,9 @@ public static Object getDefaultValue(Object proxy, Method method) {
5051
* @param clazz class which you want to get instance of
5152
* @return instance of proxy
5253
*/
53-
public static Object createHelperProxy(Class<?> clazz) {
54-
return Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, (Object object, Method method, Object[] args) -> null);
54+
private static Object createHelperProxy(Class<?> clazz) {
55+
return Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz},
56+
(Object object, Method method, Object[] args) -> null);
5557
}
5658

5759
/**

0 commit comments

Comments
 (0)