Skip to content

Commit

Permalink
more tooling in ReflectionHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jun 12, 2021
1 parent e994c68 commit 5ffed8a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
Expand Up @@ -1587,7 +1587,7 @@ else if (CoreUtilities.toLowerCase(element).contains(CoreUtilities.toLowerCase(c
});

// <--[tag]
// @attribute <ElementTag.split[(regex:)<string>]>
// @attribute <ElementTag.split[((regex:)<string>)]>
// @returns ListTag
// @group element manipulation
// @description
Expand All @@ -1608,7 +1608,7 @@ else if (CoreUtilities.toLowerCase(element).contains(CoreUtilities.toLowerCase(c
String[] split;

// <--[tag]
// @attribute <ElementTag.split[(regex:)<string>].limit[<#>]>
// @attribute <ElementTag.split[((regex:)<string>)].limit[<#>]>
// @returns ListTag
// @group element manipulation
// @description
Expand Down
Expand Up @@ -7,12 +7,13 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class ReflectionHelper {

private static final Map<Class, Map<String, Field>> cachedFields = new HashMap<>();
private static final Map<Class, CheckingFieldMap> cachedFields = new HashMap<>();

private static final Map<Class, Map<String, MethodHandle>> cachedFieldSetters = new HashMap<>();

Expand Down Expand Up @@ -49,6 +50,18 @@ public CheckingFieldMap(Class<?> clazz) {
this.clazz = clazz;
}

public Field getFirstOfType(Class fieldClazz) {
for (Field f : super.values()) {
if (f.getType().equals(fieldClazz)) {
return f;
}
}
String err = "Reflection field missing - Tried to find field of type '" + fieldClazz.getCanonicalName() + "' of class '" + clazz.getCanonicalName() + "'.";
System.err.println("[Denizen] [ReflectionHelper]: " + err);
Debug.echoError(err);
return null;
}

@Override
public Field get(Object name) {
Field f = super.get(name);
Expand All @@ -65,8 +78,8 @@ public Field getNoCheck(String name) {
}
}

public static Map<String, Field> getFields(Class clazz) {
Map<String, Field> fields = cachedFields.get(clazz);
public static CheckingFieldMap getFields(Class clazz) {
CheckingFieldMap fields = cachedFields.get(clazz);
if (fields != null) {
return fields;
}
Expand All @@ -82,8 +95,18 @@ public static Map<String, Field> getFields(Class clazz) {
public static Method getMethod(Class<?> clazz, String method, Class<?>... params) {
Method f = null;
try {
f = clazz.getDeclaredMethod(method, params);
f.setAccessible(true);
if (method == null) {
for (Method possible : clazz.getDeclaredMethods()) {
if (possible.getParameterCount() == params.length && Arrays.equals(possible.getParameterTypes(), params)) {
f = possible;
break;
}
}
}
else {
f = clazz.getDeclaredMethod(method, params);
f.setAccessible(true);
}
}
catch (Exception ex) {
Debug.echoError(ex);
Expand All @@ -106,6 +129,14 @@ public static MethodHandle getMethodHandle(Class<?> clazz, String method, Class<
return null;
}

public static MethodHandle getFinalSetterForFirstOfType(Class<?> clazz, Class<?> fieldType) {
Field field = getFields(clazz).getFirstOfType(fieldType);
if (field == null) {
return null;
}
return getFinalSetter(clazz, field.getName());
}

public static MethodHandle getFinalSetter(Class<?> clazz, String field) {
Map<String, MethodHandle> map = cachedFieldSetters.computeIfAbsent(clazz, k -> new HashMap<>());
MethodHandle result = map.get(field);
Expand Down

0 comments on commit 5ffed8a

Please sign in to comment.