public class BetterReflectionUtils extends Object
| Modifier and Type | Field and Description |
|---|---|
static BetterReflectionClass |
CLASS |
static File |
LAUNCH_JAR_FILE
Caching the jar file on launch to make sure it's not null later.
|
| Constructor and Description |
|---|
BetterReflectionUtils() |
| Modifier and Type | Method and Description |
|---|---|
static boolean |
doParametersMatch(Class<?>[] parameters1,
Class<?>[] parameters2) |
static void |
dumpMethodHeader(Method method,
boolean includeModifiers,
boolean includeReturnType,
boolean includeParameterNames) |
static String |
generateMethodHeader(Method method,
boolean includeModifiers,
boolean includeReturnType,
boolean includeParameterNames)
* @param method the method to dump
* @param includeModifiers whether the method's modifiers should be dumped
* @param includeReturnType whether the method's return type should be dumped
* @param includeParameterNames whether the method's parameter names should be dumped
|
static Class<?>[] |
getClasses(Map<Integer,Class<?>> primitives,
Object... parameterTypes)
Deprecated.
(renamed) use
getTypes(Map, Object...) |
static Class<?>[] |
getClasses(Object... parameterTypes)
Deprecated.
(renamed) use
getTypes(Object...) |
static List<BetterReflectionClass> |
getClassesFromNameBeginning(String packageName,
String beginning)
This method will use
getClassesInPackage(String) to find all the
classes in the specified package, it will then return a list of all the
classes that start with beginning
For example:
getClassesFromNameBeginning("me.wavelength.betterreflection", "BetterReflection")
Will return a list with the following classes: [ "BetterReflection", "BetterReflectionClass", "BetterReflectionUtils" ] |
static List<BetterReflectionClass> |
getClassesInPackage(String packageName) |
static Constructor<?> |
getConstructor(Class<?>[] parameterTypes,
Constructor<?>[] constructors) |
static JarFile |
getCurrentJar() |
static JarFile |
getCurrentJar(BetterReflectionClass clasz) |
static File |
getCurrentJarFile() |
static File |
getCurrentJarFile(BetterReflectionClass clasz) |
static List<File> |
getDirectoriesFromPackageName(String packageName)
This should only be invoked when launching on an IDE
|
static Field |
getField(String name,
Field[] fields) |
static int |
getJavaVersion()
Gets the System property "java.version"
|
static Method |
getMethod(String name,
Class<?>[] parameterTypes,
Method[] methods) |
static Class<?>[] |
getTypes(Map<Integer,Class<?>> primitives,
Object... parameterTypes) |
static Class<?>[] |
getTypes(Object... parameterTypes) |
static boolean |
isRunningFromJar() |
static boolean |
isRunningFromJar(BetterReflectionClass clasz) |
static boolean |
isRunningOnAndroid() |
static void |
setFinal(Field field,
boolean setFinal)
Version is greater than or equal to 11
Provided by this StackOverflow post:
|
public static final BetterReflectionClass CLASS
public static final File LAUNCH_JAR_FILE
public static File getCurrentJarFile(BetterReflectionClass clasz)
clasz - the class to get the jar forpublic static File getCurrentJarFile()
public static JarFile getCurrentJar(BetterReflectionClass clasz)
clasz - the class to get the jar forgetCurrentJar()public static JarFile getCurrentJar()
getCurrentJar()public static boolean isRunningFromJar(BetterReflectionClass clasz)
clasz - the class to checkpublic static boolean isRunningFromJar()
public static boolean isRunningOnAndroid()
This can easily be fooled and should not be trusted on its own.
public static Constructor<?> getConstructor(Class<?>[] parameterTypes, Constructor<?>[] constructors)
public static boolean doParametersMatch(Class<?>[] parameters1, Class<?>[] parameters2)
@Deprecated public static Class<?>[] getClasses(Object... parameterTypes)
getTypes(Object...)parameterTypes - the parameter types@Deprecated public static Class<?>[] getClasses(Map<Integer,Class<?>> primitives, Object... parameterTypes)
getTypes(Map, Object...)primitives - a map of primitives to use for lookupparameterTypes - the parameter typespublic static Class<?>[] getTypes(Object... parameterTypes)
parameterTypes - the parameter typesgetClasses(Object...)public static Class<?>[] getTypes(Map<Integer,Class<?>> primitives, Object... parameterTypes)
primitives - a map of primitives to use for lookupparameterTypes - the parameter typesgetClasses(Map, Object...)public static List<File> getDirectoriesFromPackageName(String packageName) throws IOException, URISyntaxException
packageName - the package nameIOException - If I/O errors occurURISyntaxException - If the resource's URL cannot be converted to URIpublic static List<BetterReflectionClass> getClassesInPackage(String packageName) throws IOException, URISyntaxException, CannotReadJarException
packageName - the package name to scanIOException - If I/O errors occurURISyntaxException - If the resource's URL cannot be converted to URICannotReadJarException - If this method is invoked from a
Jar and the Jar file cannot be found, or it
cannot be read.public static List<BetterReflectionClass> getClassesFromNameBeginning(String packageName, String beginning) throws IOException, URISyntaxException, CannotReadJarException
getClassesInPackage(String) to find all the
classes in the specified package, it will then return a list of all the
classes that start with beginning
For example:
getClassesFromNameBeginning("me.wavelength.betterreflection", "BetterReflection")
Will return a list with the following classes: [ "BetterReflection", "BetterReflectionClass", "BetterReflectionUtils" ]packageName - The package name to search inside ofbeginning - the beginning of the class nameIOException - If I/O errors occurURISyntaxException - If the resource's URL cannot be converted to URICannotReadJarException - this is thrown if this method is run from a
Jar and the Jar file cannot be found or it
cannot be read.public static int getJavaVersion()
public static void setFinal(Field field, boolean setFinal) throws Exception
field - the field to change the modifier tosetFinal - whether the field should be set to final or not
Java 16 or higher requires the following launch arguments to be added:
--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED
Removes (or adds) the final modifier to a field.
Exceptionpublic static String generateMethodHeader(Method method, boolean includeModifiers, boolean includeReturnType, boolean includeParameterNames)
public static void dumpMethodHeader(Method method, boolean includeModifiers, boolean includeReturnType, boolean includeParameterNames)
method - the method to dumpincludeModifiers - whether the method's modifiers should be dumpedincludeReturnType - whether the method's return type should be dumpedincludeParameterNames - whether the method's parameter names should be dumpedgenerateMethodHeader(Method, boolean, boolean, boolean)} to stoutCopyright © 2024. All rights reserved.