-
-
Notifications
You must be signed in to change notification settings - Fork 340
Add ReflectHelper class to better facilitate various reflection actions. #2586
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
140 changes: 140 additions & 0 deletions
140
src/main/java/com/onarandombox/MultiverseCore/utils/ReflectHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| package com.onarandombox.MultiverseCore.utils; | ||
|
|
||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.lang.reflect.Method; | ||
|
|
||
| /** | ||
| * Utility class used to help in doing various reflection actions. | ||
| */ | ||
| public class ReflectHelper { | ||
|
|
||
| /** | ||
| * Try to get the {@link Class} based on its classpath. | ||
| * | ||
| * @param classPath The target classpath. | ||
| * @return A {@link Class} if found, else null. | ||
| */ | ||
| @Nullable | ||
| public static Class<?> getClass(String classPath) { | ||
| try { | ||
| return Class.forName(classPath); | ||
| } catch (ClassNotFoundException e) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Check if the {@link Class} for a give classpath is present/valid. | ||
| * | ||
| * @param classPath Target classpath. | ||
| * @return True if class path is a valid class, else false. | ||
| */ | ||
| public static boolean hasClass(String classPath) { | ||
| return getClass(classPath) != null; | ||
| } | ||
|
|
||
| /** | ||
| * Try to get a {@link Method} from a given class. | ||
| * | ||
| * @param clazz The class to search the method on. | ||
| * @param methodName Name of the method to get. | ||
| * @param parameterTypes Parameters present for that method. | ||
| * @param <C> The class type. | ||
| * @return A {@link Method} if found, else null. | ||
| */ | ||
| @Nullable | ||
| public static <C> Method getMethod(Class<C> clazz, String methodName, Class<?>... parameterTypes) { | ||
| try { | ||
| Method method = clazz.getDeclaredMethod(methodName, parameterTypes); | ||
| method.setAccessible(true); | ||
| return method; | ||
| } catch (NoSuchMethodException e) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Try to get a {@link Method} from a given class. | ||
| * | ||
| * @param classInstance Instance of the class to search the method on. | ||
| * @param methodName Name of the method to get. | ||
| * @param parameterTypes Parameters present for that method. | ||
| * @param <C> The class type. | ||
| * @return A {@link Method} if found, else null. | ||
| */ | ||
| @Nullable | ||
| public static <C> Method getMethod(C classInstance, String methodName, Class<?>... parameterTypes) { | ||
| return getMethod(classInstance.getClass(), methodName, parameterTypes); | ||
| } | ||
|
|
||
| /** | ||
| * Calls a {@link Method}. | ||
| * | ||
| * @param classInstance Instance of the class responsible for the method. | ||
| * @param method The method to call. | ||
| * @param parameters Parameters needed when calling the method. | ||
| * @param <C> The class type. | ||
| * @param <R> The return type. | ||
| * @return Return value of the method call if any, else null. | ||
| */ | ||
| @Nullable | ||
| public static <C, R> R invokeMethod(C classInstance, Method method, Object...parameters) { | ||
| try { | ||
| return (R) method.invoke(classInstance, parameters); | ||
| } catch (Exception e) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Try to get a {@link Field} from a given class. | ||
| * | ||
| * @param clazz The class to search the field on. | ||
| * @param fieldName Name of the field to get. | ||
| * @param <C> The class type. | ||
| * @return A {@link Field} if found, else null. | ||
| */ | ||
| @Nullable | ||
| public static <C> Field getField(Class<C> clazz, String fieldName) { | ||
| try { | ||
| Field field = clazz.getDeclaredField(fieldName); | ||
| field.setAccessible(true); | ||
| return field; | ||
| } catch (NoSuchFieldException e) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Try to get a {@link Field} from a given class. | ||
| * | ||
| * @param classInstance Instance of the class to search the field on. | ||
| * @param fieldName Name of the field to get. | ||
| * @param <C> The class type. | ||
| * @return A {@link Field} if found, else null. | ||
| */ | ||
| @Nullable | ||
| public static <C> Field getField(C classInstance, String fieldName) { | ||
| return getField(classInstance.getClass(), fieldName); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the value of an {@link Field} from an instance of the class responsible. | ||
| * | ||
| * @param classInstance Instance of the class to get the field value from. | ||
| * @param field The field to get value from. | ||
| * @param <C> The class type. | ||
| * @param <V> The field value type. | ||
| * @return The field value if any, else null. | ||
| */ | ||
| @Nullable | ||
| public static <C, V> V getFieldValue(C classInstance, Field field) { | ||
| try { | ||
| return (V) field.get(classInstance); | ||
| } catch (IllegalAccessException e) { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hopefully we're not using this all the time as it is a relatively costly operation. Class.forName is "slow" and should an exception be thrown that will also slow this down quite a bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might want to consider a caching strategy here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea we do store the result, it should only be used on started, then the results are stored.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can see #2580 on implementation on it.