Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow different mapping for reflection #3470

Merged
merged 8 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions src/main/java/org/spongepowered/common/util/ReflectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,23 @@
*/
package org.spongepowered.common.util;

import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.google.common.base.Preconditions.checkArgument;
import static org.apache.commons.lang3.ClassUtils.isAssignable;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
Expand All @@ -38,13 +52,6 @@
import org.spongepowered.common.SpongeCommon;
import org.spongepowered.common.launch.Launch;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* A handy utility for doing some neat things with generics and reflection.
* This is primarily used for {@link ImmutableDataCachingUtil} to create
Expand Down Expand Up @@ -77,6 +84,28 @@ public final class ReflectionUtil {
Entity.class
};

public static final String REFLECTION_PROPERTY_FILE = "reflection_mappings.json";
LogicFan marked this conversation as resolved.
Show resolved Hide resolved
private static Map<String, String> mappingCache;

private static String getMethodNameForEnvironment(final String methodName) {
if (mappingCache == null) {
LogicFan marked this conversation as resolved.
Show resolved Hide resolved
try (InputStreamReader reader = new InputStreamReader(Objects.requireNonNull(ReflectionUtil.class.getClassLoader().getResourceAsStream(REFLECTION_PROPERTY_FILE)))) {
mappingCache = new Gson().fromJson(reader, new TypeToken<Map<String, String>>(){}.getType());
} catch (IOException | NullPointerException e) {
mappingCache = Collections.emptyMap();
}
}

String methodNameForEnvironment = mappingCache.get(methodName);
LogicFan marked this conversation as resolved.
Show resolved Hide resolved

if (methodNameForEnvironment == null) {
SpongeCommon.logger().fatal(ReflectionUtil.STUPID_REFLECTION, "Could not find reflection mapping for method {} in reflection property file {}", methodName, REFLECTION_PROPERTY_FILE);
LogicFan marked this conversation as resolved.
Show resolved Hide resolved
return methodName;
} else {
return methodNameForEnvironment;
}
}

public static boolean isNeighborChangedDeclared(final Class<?> targetClass) {
return ReflectionUtil.doesMethodExist(
targetClass,
Expand Down Expand Up @@ -110,7 +139,7 @@ public static boolean doesMethodExist(
final String methodName,
final Class<?>[] methodParameters
) {
final String targetMethodForEnvironment = Launch.instance().developerEnvironment() ? methodName : methodName;
final String targetMethodForEnvironment = Launch.instance().developerEnvironment() ? methodName : getMethodNameForEnvironment(methodName);
LogicFan marked this conversation as resolved.
Show resolved Hide resolved
try {
final Class<?> declaringClass = targetClass.getMethod(targetMethodForEnvironment, methodParameters).getDeclaringClass();
return !ignoredClass.equals(declaringClass);
Expand Down
5 changes: 5 additions & 0 deletions vanilla/src/main/resources/reflection_mappings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"neighborChanged" : "neighborChanged",
"entityInside": "entityInside",
"stepOn": "stepOn"
}