Skip to content

Commit

Permalink
Fixed support for Java 14+
Browse files Browse the repository at this point in the history
  • Loading branch information
mikigal committed Feb 17, 2021
1 parent aa6856c commit 39b8df1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
3 changes: 1 addition & 2 deletions src/main/java/pl/mikigal/config/ConfigInvocationHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ private void prepareMethods() {
*/
private boolean updateConfigFile() {
boolean modified = false;
Object proxy = ReflectionUtils.createHelperProxy(this.clazz);
for (Method method : this.clazz.getDeclaredMethods()) {
String name = method.getName();
if (!name.startsWith("get") && !name.startsWith("set")) {
Expand All @@ -230,7 +229,7 @@ private boolean updateConfigFile() {
continue;
}

Object defaultValue = ReflectionUtils.getDefaultValue(proxy, method);
Object defaultValue = ReflectionUtils.getDefaultValue(method);
if (defaultValue == null && !method.isAnnotationPresent(ConfigOptional.class)) {
throw new InvalidConfigException("Method " + method.getName() + " is not optional, but it's default value is null");
}
Expand Down
28 changes: 15 additions & 13 deletions src/main/java/pl/mikigal/config/util/ReflectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pl.mikigal.config.exception.InvalidConfigException;

import java.lang.invoke.MethodHandles;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

Expand All @@ -15,30 +15,31 @@
*/
public class ReflectionUtils {

private static final Constructor<MethodHandles.Lookup> lookupConstructor;
private static final MethodHandles.Lookup lookup;

static {
try {
lookupConstructor = MethodHandles.Lookup.class.getDeclaredConstructor(Class.class, Integer.TYPE);
lookupConstructor.setAccessible(true);
} catch (NoSuchMethodException e) {
throw new InvalidConfigException("Could not get MethodHandles.Lookup constructor", e);
Field field = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP");
field.setAccessible(true);

lookup = (MethodHandles.Lookup) field.get(null);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new InvalidConfigException("Could not get MethodHandles.Lookup", e);
}
}

/**
* Allows to get default value of method from interface
* @param proxy instance of proxied object
* @param method method of which you want to get defalt value
* @param method method of which you want to get default value
* @return default value of method from interface
*/
public static Object getDefaultValue(Object proxy, Method method) {
public static Object getDefaultValue(Method method) {
try {
Class<?> clazz = method.getDeclaringClass();
return lookupConstructor.newInstance(clazz, MethodHandles.Lookup.PRIVATE)
return lookup
.in(clazz)
.unreflectSpecial(method, clazz)
.bindTo(proxy)
.bindTo(createHelperProxy(method.getDeclaringClass()))
.invoke();
} catch (Throwable throwable) {
throw new InvalidConfigException(throwable);
Expand All @@ -50,8 +51,9 @@ public static Object getDefaultValue(Object proxy, Method method) {
* @param clazz class which you want to get instance of
* @return instance of proxy
*/
public static Object createHelperProxy(Class<?> clazz) {
return Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, (Object object, Method method, Object[] args) -> null);
private static Object createHelperProxy(Class<?> clazz) {
return Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz},
(Object object, Method method, Object[] args) -> null);
}

/**
Expand Down

0 comments on commit 39b8df1

Please sign in to comment.