Skip to content

Commit e1b552a

Browse files
committed
add support for kotlin classes
1 parent bb8edec commit e1b552a

2 files changed

Lines changed: 24 additions & 25 deletions

File tree

core/src/main/java/net/j4c0b3y/api/config/StaticConfig.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@
3232
*/
3333
@Getter
3434
public abstract class StaticConfig {
35-
/**
36-
* The modifiers for static config fields, this should never be changed.
37-
*/
38-
private final static int MODIFIERS = Modifier.PUBLIC | Modifier.STATIC;
39-
4035
/**
4136
* The file the config document is associated with.
4237
*/
@@ -233,10 +228,16 @@ public void save() {
233228
private void step(Class<?> parent, String path, boolean initialize) throws ReflectiveOperationException {
234229
for (Field field : parent.getDeclaredFields()) {
235230
// Skip field if its modifiers are invalid, or it is marked @Ignore.
236-
if (!ClassUtils.hasModifiers(field, MODIFIERS) || field.isAnnotationPresent(Ignore.class)) {
231+
if (!Modifier.isStatic(field.getModifiers()) || field.isAnnotationPresent(Ignore.class)) {
232+
continue;
233+
}
234+
235+
if (ClassUtils.isCompanionField(field)) {
237236
continue;
238237
}
239238

239+
field.setAccessible(true);
240+
240241
// Get the route by combining the current path and the formatted key.
241242
String route = path + getRoute(field.getAnnotation(Key.class), field.getName());
242243

@@ -272,7 +273,11 @@ private void step(Class<?> parent, String path, boolean initialize) throws Refle
272273
Class<?> clazz = classes[i];
273274

274275
// Skip field if its modifiers are invalid, or it is marked @Ignore.
275-
if (!ClassUtils.hasModifiers(clazz, MODIFIERS) || clazz.isAnnotationPresent(Ignore.class)) {
276+
if (!Modifier.isStatic(clazz.getModifiers()) || clazz.isAnnotationPresent(Ignore.class)) {
277+
continue;
278+
}
279+
280+
if (ClassUtils.isCompanionClass(clazz)) {
276281
continue;
277282
}
278283

core/src/main/java/net/j4c0b3y/api/config/utils/ClassUtils.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.lang.invoke.MethodType;
66
import java.lang.reflect.Field;
7+
import java.lang.reflect.Modifier;
78
import java.lang.reflect.ParameterizedType;
89
import java.lang.reflect.Type;
910

@@ -36,25 +37,18 @@ public <T> Class<T> wrap(Class<T> type) {
3637
return (Class<T>) MethodType.methodType(type).wrap().returnType();
3738
}
3839

39-
/**
40-
* Checks if a field contains certain modifiers.
41-
*
42-
* @param field The field to check.
43-
* @param contains The modifiers.
44-
* @return If the field contains the modifiers.
45-
*/
46-
public boolean hasModifiers(Field field, int contains) {
47-
return (field.getModifiers() & contains) == contains;
40+
public boolean isCompanionClass(Class<?> clazz) {
41+
if (!Modifier.isPublic(clazz.getModifiers())) return false;
42+
if (!Modifier.isStatic(clazz.getModifiers())) return false;
43+
44+
return clazz.getSimpleName().equals("Companion");
4845
}
4946

50-
/**
51-
* Checks if a class contains certain modifiers.
52-
*
53-
* @param clazz The class to check.
54-
* @param contains The modifiers.
55-
* @return If the class contains the modifiers.
56-
*/
57-
public boolean hasModifiers(Class<?> clazz, int contains) {
58-
return (clazz.getModifiers() & contains) == contains;
47+
public boolean isCompanionField(Field field) {
48+
if (!isCompanionClass(field.getType())) return false;
49+
if (!Modifier.isStatic(field.getModifiers())) return false;
50+
if (!Modifier.isFinal(field.getModifiers())) return false;
51+
52+
return field.getName().equals("Companion");
5953
}
6054
}

0 commit comments

Comments
 (0)