PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ReflectionUtils.java

845 lines
24 KiB
Java
Raw Normal View History

2014-11-08 20:27:09 +01:00
////////////////////////////////////////////////////////////////////////////////////////////////////
// PlotSquared - A plot manager and world generator for the Bukkit API /
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
// /
// This program is free software; you can redistribute it and/or modify /
// it under the terms of the GNU General Public License as published by /
// the Free Software Foundation; either version 3 of the License, or /
// (at your option) any later version. /
// /
// This program is distributed in the hope that it will be useful, /
// but WITHOUT ANY WARRANTY; without even the implied warranty of /
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /
// GNU General Public License for more details. /
// /
// You should have received a copy of the GNU General Public License /
// along with this program; if not, write to the Free Software Foundation, /
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
// /
// You can contact us via: support@intellectualsites.com /
////////////////////////////////////////////////////////////////////////////////////////////////////
2014-11-16 10:48:18 +01:00
package com.intellectualcrafters.plot.util;
2014-09-22 13:02:14 +02:00
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
2015-06-08 12:30:46 +02:00
import java.lang.reflect.InvocationTargetException;
2014-09-22 13:02:14 +02:00
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
2014-09-22 13:02:14 +02:00
import java.util.Collections;
import java.util.List;
2015-07-30 16:25:16 +02:00
import org.bukkit.Bukkit;
2014-09-22 13:02:14 +02:00
/**
* @author DPOH-VAR
* @version 1.0
*/
2015-09-11 12:09:22 +02:00
public class ReflectionUtils
{
public ReflectionUtils(final String version)
{
2015-08-03 20:20:04 +02:00
preClassB += "." + version;
preClassM += "." + version;
}
2015-09-11 12:09:22 +02:00
2014-11-08 20:27:09 +01:00
/**
* prefix of bukkit classes
*/
2014-12-18 03:15:11 +01:00
private static String preClassB = "org.bukkit.craftbukkit";
2014-11-08 20:27:09 +01:00
/**
* prefix of minecraft classes
*/
2014-12-18 03:15:11 +01:00
private static String preClassM = "net.minecraft.server";
2015-09-11 12:09:22 +02:00
public static Class<?> getNmsClass(final String name)
{
2015-06-08 12:30:46 +02:00
final String className = "net.minecraft.server." + getVersion() + "." + name;
return getClass(className);
}
2015-09-11 12:09:22 +02:00
public static Class<?> getCbClass(final String name)
{
2015-06-08 12:30:46 +02:00
final String className = "org.bukkit.craftbukkit." + getVersion() + "." + name;
return getClass(className);
}
2015-09-11 12:09:22 +02:00
public static Class<?> getUtilClass(final String name)
{
try
{
2015-06-08 12:30:46 +02:00
return Class.forName(name); //Try before 1.8 first
2015-09-11 12:09:22 +02:00
}
catch (final ClassNotFoundException ex)
{
try
{
2015-06-08 12:30:46 +02:00
return Class.forName("net.minecraft.util." + name); //Not 1.8
2015-09-11 12:09:22 +02:00
}
catch (final ClassNotFoundException ex2)
{
2015-06-08 12:30:46 +02:00
return null;
}
}
}
2015-09-11 12:09:22 +02:00
public static String getVersion()
{
2015-06-08 12:30:46 +02:00
final String packageName = Bukkit.getServer().getClass().getPackage().getName();
return packageName.substring(packageName.lastIndexOf('.') + 1);
}
2015-09-11 12:09:22 +02:00
public static Object getHandle(final Object wrapper)
{
2015-06-08 12:30:46 +02:00
final Method getHandle = makeMethod(wrapper.getClass(), "getHandle");
return callMethod(getHandle, wrapper);
}
//Utils
2015-09-11 12:09:22 +02:00
public static Method makeMethod(final Class<?> clazz, final String methodName, final Class<?>... paramaters)
{
try
{
2015-06-08 12:30:46 +02:00
return clazz.getDeclaredMethod(methodName, paramaters);
2015-09-11 12:09:22 +02:00
}
catch (final NoSuchMethodException ex)
{
2015-06-08 12:30:46 +02:00
return null;
2015-09-11 12:09:22 +02:00
}
catch (final Exception ex)
{
2015-06-08 12:30:46 +02:00
throw new RuntimeException(ex);
}
}
@SuppressWarnings("unchecked")
2015-09-11 12:09:22 +02:00
public static <T> T callMethod(final Method method, final Object instance, final Object... paramaters)
{
if (method == null) { throw new RuntimeException("No such method"); }
2015-06-08 12:30:46 +02:00
method.setAccessible(true);
2015-09-11 12:09:22 +02:00
try
{
2015-06-08 12:30:46 +02:00
return (T) method.invoke(instance, paramaters);
2015-09-11 12:09:22 +02:00
}
catch (final InvocationTargetException ex)
{
2015-06-08 12:30:46 +02:00
throw new RuntimeException(ex.getCause());
2015-09-11 12:09:22 +02:00
}
catch (final Exception ex)
{
2015-06-08 12:30:46 +02:00
throw new RuntimeException(ex);
}
}
@SuppressWarnings("unchecked")
2015-09-11 12:09:22 +02:00
public static <T> Constructor<T> makeConstructor(final Class<?> clazz, final Class<?>... paramaterTypes)
{
try
{
2015-06-08 12:30:46 +02:00
return (Constructor<T>) clazz.getConstructor(paramaterTypes);
2015-09-11 12:09:22 +02:00
}
catch (final NoSuchMethodException ex)
{
2015-06-08 12:30:46 +02:00
return null;
2015-09-11 12:09:22 +02:00
}
catch (final Exception ex)
{
2015-06-08 12:30:46 +02:00
throw new RuntimeException(ex);
}
}
2015-09-11 12:09:22 +02:00
public static <T> T callConstructor(final Constructor<T> constructor, final Object... paramaters)
{
if (constructor == null) { throw new RuntimeException("No such constructor"); }
2015-06-08 12:30:46 +02:00
constructor.setAccessible(true);
2015-09-11 12:09:22 +02:00
try
{
2015-06-08 12:30:46 +02:00
return constructor.newInstance(paramaters);
2015-09-11 12:09:22 +02:00
}
catch (final InvocationTargetException ex)
{
2015-06-08 12:30:46 +02:00
throw new RuntimeException(ex.getCause());
2015-09-11 12:09:22 +02:00
}
catch (final Exception ex)
{
2015-06-08 12:30:46 +02:00
throw new RuntimeException(ex);
}
}
2015-09-11 12:09:22 +02:00
public static Field makeField(final Class<?> clazz, final String name)
{
try
{
2015-06-08 12:30:46 +02:00
return clazz.getDeclaredField(name);
2015-09-11 12:09:22 +02:00
}
catch (final NoSuchFieldException ex)
{
2015-06-08 12:30:46 +02:00
return null;
2015-09-11 12:09:22 +02:00
}
catch (final Exception ex)
{
2015-06-08 12:30:46 +02:00
throw new RuntimeException(ex);
}
}
@SuppressWarnings("unchecked")
2015-09-11 12:09:22 +02:00
public static <T> T getField(final Field field, final Object instance)
{
if (field == null) { throw new RuntimeException("No such field"); }
2015-06-08 12:30:46 +02:00
field.setAccessible(true);
2015-09-11 12:09:22 +02:00
try
{
2015-06-08 12:30:46 +02:00
return (T) field.get(instance);
2015-09-11 12:09:22 +02:00
}
catch (final Exception ex)
{
2015-06-08 12:30:46 +02:00
throw new RuntimeException(ex);
}
}
2015-09-11 12:09:22 +02:00
public static void setField(final Field field, final Object instance, final Object value)
{
if (field == null) { throw new RuntimeException("No such field"); }
2015-06-08 12:30:46 +02:00
field.setAccessible(true);
2015-09-11 12:09:22 +02:00
try
{
2015-06-08 12:30:46 +02:00
field.set(instance, value);
2015-09-11 12:09:22 +02:00
}
catch (final Exception ex)
{
2015-06-08 12:30:46 +02:00
throw new RuntimeException(ex);
}
}
2015-09-11 12:09:22 +02:00
public static Class<?> getClass(final String name)
{
try
{
2015-06-08 12:30:46 +02:00
return Class.forName(name);
2015-09-11 12:09:22 +02:00
}
catch (final ClassNotFoundException ex)
{
2015-06-08 12:30:46 +02:00
return null;
}
}
2015-09-11 12:09:22 +02:00
public static <T> Class<? extends T> getClass(final String name, final Class<T> superClass)
{
try
{
2015-06-08 12:30:46 +02:00
return Class.forName(name).asSubclass(superClass);
2015-09-11 12:09:22 +02:00
}
catch (ClassCastException | ClassNotFoundException ex)
{
2015-06-08 12:30:46 +02:00
return null;
}
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
2014-12-18 03:15:11 +01:00
* Get class for name. Replace {nms} to net.minecraft.server.V*. Replace {cb} to org.bukkit.craftbukkit.V*. Replace
* {nm} to net.minecraft
*
* @param classes possible class paths
2014-11-05 04:42:08 +01:00
*
* @return RefClass object
2014-12-18 03:15:11 +01:00
*
* @throws RuntimeException if no class found
2014-11-05 04:42:08 +01:00
*/
2015-09-11 12:09:22 +02:00
public static RefClass getRefClass(final String... classes) throws RuntimeException
{
for (String className : classes)
{
try
{
2014-11-05 04:42:08 +01:00
className = className.replace("{cb}", preClassB).replace("{nms}", preClassM).replace("{nm}", "net.minecraft");
return getRefClass(Class.forName(className));
}
2015-09-11 12:09:22 +02:00
catch (final ClassNotFoundException ignored)
{}
2014-11-05 04:42:08 +01:00
}
throw new RuntimeException("no class found");
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* get RefClass object by real class
*
2014-12-18 03:15:11 +01:00
* @param clazz class
*
2014-11-05 04:42:08 +01:00
* @return RefClass based on passed class
*/
2015-09-11 12:09:22 +02:00
public static RefClass getRefClass(final Class clazz)
{
2014-11-05 04:42:08 +01:00
return new RefClass(clazz);
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* RefClass - utility to simplify work with reflections.
*/
2015-09-11 12:09:22 +02:00
public static class RefClass
{
2014-11-05 04:42:08 +01:00
private final Class<?> clazz;
2015-02-23 02:32:27 +01:00
2015-09-11 12:09:22 +02:00
private RefClass(final Class<?> clazz)
{
2014-11-16 10:48:18 +01:00
this.clazz = clazz;
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* get passed class
*
* @return class
*/
2015-09-11 12:09:22 +02:00
public Class<?> getRealClass()
{
return clazz;
2014-11-05 04:42:08 +01:00
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* see {@link Class#isInstance(Object)}
*
2014-12-18 03:15:11 +01:00
* @param object the object to check
*
2014-11-05 04:42:08 +01:00
* @return true if object is an instance of this class
*/
2015-09-11 12:09:22 +02:00
public boolean isInstance(final Object object)
{
return clazz.isInstance(object);
2014-11-05 04:42:08 +01:00
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* get existing method by name and types
*
2014-12-18 03:15:11 +01:00
* @param name name
* @param types method parameters. can be Class or RefClass
*
2014-11-05 04:42:08 +01:00
* @return RefMethod object
2014-12-18 03:15:11 +01:00
*
* @throws RuntimeException if method not found
2014-11-05 04:42:08 +01:00
*/
2015-09-11 12:09:22 +02:00
public RefMethod getMethod(final String name, final Object... types) throws NoSuchMethodException
{
try
{
2014-11-05 04:42:08 +01:00
final Class[] classes = new Class[types.length];
int i = 0;
2015-09-11 12:09:22 +02:00
for (final Object e : types)
{
if (e instanceof Class)
{
2014-11-05 04:42:08 +01:00
classes[i++] = (Class) e;
2015-09-11 12:09:22 +02:00
}
else if (e instanceof RefClass)
{
2014-11-05 04:42:08 +01:00
classes[i++] = ((RefClass) e).getRealClass();
2015-09-11 12:09:22 +02:00
}
else
{
2014-11-05 04:42:08 +01:00
classes[i++] = e.getClass();
}
}
2015-09-11 12:09:22 +02:00
try
{
return new RefMethod(clazz.getMethod(name, classes));
}
catch (final NoSuchMethodException ignored)
{
return new RefMethod(clazz.getDeclaredMethod(name, classes));
2014-11-05 04:42:08 +01:00
}
2015-09-11 12:09:22 +02:00
}
catch (final Exception e)
{
2014-11-05 04:42:08 +01:00
throw new RuntimeException(e);
}
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* get existing constructor by types
*
2014-12-18 03:15:11 +01:00
* @param types parameters. can be Class or RefClass
*
2014-11-05 04:42:08 +01:00
* @return RefMethod object
2014-12-18 03:15:11 +01:00
*
* @throws RuntimeException if constructor not found
2014-11-05 04:42:08 +01:00
*/
2015-09-11 12:09:22 +02:00
public RefConstructor getConstructor(final Object... types)
{
try
{
2014-11-05 04:42:08 +01:00
final Class[] classes = new Class[types.length];
int i = 0;
2015-09-11 12:09:22 +02:00
for (final Object e : types)
{
if (e instanceof Class)
{
2014-11-05 04:42:08 +01:00
classes[i++] = (Class) e;
2015-09-11 12:09:22 +02:00
}
else if (e instanceof RefClass)
{
2014-11-05 04:42:08 +01:00
classes[i++] = ((RefClass) e).getRealClass();
2015-09-11 12:09:22 +02:00
}
else
{
2014-11-05 04:42:08 +01:00
classes[i++] = e.getClass();
}
}
2015-09-11 12:09:22 +02:00
try
{
return new RefConstructor(clazz.getConstructor(classes));
2014-11-05 04:42:08 +01:00
}
2015-09-11 12:09:22 +02:00
catch (final NoSuchMethodException ignored)
{
return new RefConstructor(clazz.getDeclaredConstructor(classes));
}
}
catch (final Exception e)
{
2014-11-05 04:42:08 +01:00
throw new RuntimeException(e);
}
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* find method by type parameters
*
2014-12-18 03:15:11 +01:00
* @param types parameters. can be Class or RefClass
*
2014-11-05 04:42:08 +01:00
* @return RefMethod object
2014-12-18 03:15:11 +01:00
*
* @throws RuntimeException if method not found
2014-11-05 04:42:08 +01:00
*/
2015-09-11 12:09:22 +02:00
public RefMethod findMethod(final Object... types)
{
2014-11-05 04:42:08 +01:00
final Class[] classes = new Class[types.length];
int t = 0;
2015-09-11 12:09:22 +02:00
for (final Object e : types)
{
if (e instanceof Class)
{
2014-11-05 04:42:08 +01:00
classes[t++] = (Class) e;
2015-09-11 12:09:22 +02:00
}
else if (e instanceof RefClass)
{
2014-11-05 04:42:08 +01:00
classes[t++] = ((RefClass) e).getRealClass();
2015-09-11 12:09:22 +02:00
}
else
{
2014-11-05 04:42:08 +01:00
classes[t++] = e.getClass();
}
}
final List<Method> methods = new ArrayList<>();
2015-09-11 12:09:22 +02:00
Collections.addAll(methods, clazz.getMethods());
Collections.addAll(methods, clazz.getDeclaredMethods());
findMethod: for (final Method m : methods)
{
2015-02-23 02:32:27 +01:00
final Class<?>[] methodTypes = m.getParameterTypes();
2015-09-11 12:09:22 +02:00
if (methodTypes.length != classes.length)
{
2015-02-23 02:32:27 +01:00
continue;
}
2015-09-11 12:09:22 +02:00
for (final Class aClass : classes)
{
if (!Arrays.equals(classes, methodTypes))
{
2015-02-23 02:32:27 +01:00
continue findMethod;
2014-11-05 04:42:08 +01:00
}
2015-02-23 02:32:27 +01:00
return new RefMethod(m);
2014-11-05 04:42:08 +01:00
}
2015-02-23 02:32:27 +01:00
}
2014-11-05 04:42:08 +01:00
throw new RuntimeException("no such method");
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* find method by name
*
2014-12-18 03:15:11 +01:00
* @param names possible names of method
*
2014-11-05 04:42:08 +01:00
* @return RefMethod object
2014-12-18 03:15:11 +01:00
*
* @throws RuntimeException if method not found
2014-11-05 04:42:08 +01:00
*/
2015-09-11 12:09:22 +02:00
public RefMethod findMethodByName(final String... names)
{
2014-11-05 04:42:08 +01:00
final List<Method> methods = new ArrayList<>();
2015-09-11 12:09:22 +02:00
Collections.addAll(methods, clazz.getMethods());
Collections.addAll(methods, clazz.getDeclaredMethods());
for (final Method m : methods)
{
for (final String name : names)
{
if (m.getName().equals(name)) { return new RefMethod(m); }
2014-11-05 04:42:08 +01:00
}
}
throw new RuntimeException("no such method");
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* find method by return value
*
2014-12-18 03:15:11 +01:00
* @param type type of returned value
*
2014-11-05 04:42:08 +01:00
* @return RefMethod
2014-12-18 03:15:11 +01:00
*
* @throws RuntimeException if method not found
2014-11-05 04:42:08 +01:00
*/
2015-09-11 12:09:22 +02:00
public RefMethod findMethodByReturnType(final RefClass type)
{
2014-11-05 04:42:08 +01:00
return findMethodByReturnType(type.clazz);
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* find method by return value
*
2014-12-18 03:15:11 +01:00
* @param type type of returned value
*
2014-11-05 04:42:08 +01:00
* @return RefMethod
2014-12-18 03:15:11 +01:00
*
* @throws RuntimeException if method not found
2014-11-05 04:42:08 +01:00
*/
2015-09-11 12:09:22 +02:00
public RefMethod findMethodByReturnType(Class type)
{
if (type == null)
{
2014-11-05 04:42:08 +01:00
type = void.class;
}
final List<Method> methods = new ArrayList<>();
2015-09-11 12:09:22 +02:00
Collections.addAll(methods, clazz.getMethods());
Collections.addAll(methods, clazz.getDeclaredMethods());
for (final Method m : methods)
{
if (type.equals(m.getReturnType())) { return new RefMethod(m); }
2014-11-05 04:42:08 +01:00
}
throw new RuntimeException("no such method");
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* find constructor by number of arguments
*
2014-12-18 03:15:11 +01:00
* @param number number of arguments
*
2014-11-05 04:42:08 +01:00
* @return RefConstructor
2014-12-18 03:15:11 +01:00
*
* @throws RuntimeException if constructor not found
2014-11-05 04:42:08 +01:00
*/
2015-09-11 12:09:22 +02:00
public RefConstructor findConstructor(final int number)
{
2014-11-05 04:42:08 +01:00
final List<Constructor> constructors = new ArrayList<>();
2015-09-11 12:09:22 +02:00
Collections.addAll(constructors, clazz.getConstructors());
Collections.addAll(constructors, clazz.getDeclaredConstructors());
for (final Constructor m : constructors)
{
if (m.getParameterTypes().length == number) { return new RefConstructor(m); }
2014-11-05 04:42:08 +01:00
}
throw new RuntimeException("no such constructor");
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* get field by name
*
2014-12-18 03:15:11 +01:00
* @param name field name
*
2014-11-05 04:42:08 +01:00
* @return RefField
2014-12-18 03:15:11 +01:00
*
* @throws RuntimeException if field not found
2014-11-05 04:42:08 +01:00
*/
2015-09-11 12:09:22 +02:00
public RefField getField(final String name)
{
try
{
try
{
return new RefField(clazz.getField(name));
2014-11-05 04:42:08 +01:00
}
2015-09-11 12:09:22 +02:00
catch (final NoSuchFieldException ignored)
{
return new RefField(clazz.getDeclaredField(name));
}
}
catch (final Exception e)
{
2014-11-05 04:42:08 +01:00
throw new RuntimeException(e);
}
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* find field by type
*
2014-12-18 03:15:11 +01:00
* @param type field type
*
2014-11-05 04:42:08 +01:00
* @return RefField
2014-12-18 03:15:11 +01:00
*
* @throws RuntimeException if field not found
2014-11-05 04:42:08 +01:00
*/
2015-09-11 12:09:22 +02:00
public RefField findField(final RefClass type)
{
2014-11-05 04:42:08 +01:00
return findField(type.clazz);
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* find field by type
*
2014-12-18 03:15:11 +01:00
* @param type field type
*
2014-11-05 04:42:08 +01:00
* @return RefField
2014-12-18 03:15:11 +01:00
*
* @throws RuntimeException if field not found
2014-11-05 04:42:08 +01:00
*/
2015-09-11 12:09:22 +02:00
public RefField findField(Class type)
{
if (type == null)
{
2014-11-05 04:42:08 +01:00
type = void.class;
}
final List<Field> fields = new ArrayList<>();
2015-09-11 12:09:22 +02:00
Collections.addAll(fields, clazz.getFields());
Collections.addAll(fields, clazz.getDeclaredFields());
for (final Field f : fields)
{
if (type.equals(f.getType())) { return new RefField(f); }
2014-11-05 04:42:08 +01:00
}
throw new RuntimeException("no such field");
}
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* Method wrapper
*/
2015-09-11 12:09:22 +02:00
public static class RefMethod
{
2014-11-05 04:42:08 +01:00
private final Method method;
2015-02-23 02:32:27 +01:00
2015-09-11 12:09:22 +02:00
private RefMethod(final Method method)
{
2014-11-16 10:48:18 +01:00
this.method = method;
method.setAccessible(true);
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* @return passed method
*/
2015-09-11 12:09:22 +02:00
public Method getRealMethod()
{
return method;
2014-11-05 04:42:08 +01:00
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* @return owner class of method
*/
2015-09-11 12:09:22 +02:00
public RefClass getRefClass()
{
return new RefClass(method.getDeclaringClass());
2014-11-05 04:42:08 +01:00
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* @return class of method return type
*/
2015-09-11 12:09:22 +02:00
public RefClass getReturnRefClass()
{
return new RefClass(method.getReturnType());
2014-11-05 04:42:08 +01:00
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* apply method to object
*
2014-12-18 03:15:11 +01:00
* @param e object to which the method is applied
*
2014-11-05 04:42:08 +01:00
* @return RefExecutor with method call(...)
*/
2015-09-11 12:09:22 +02:00
public RefExecutor of(final Object e)
{
2014-11-05 04:42:08 +01:00
return new RefExecutor(e);
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* call static method
*
2014-12-18 03:15:11 +01:00
* @param params sent parameters
*
2014-11-05 04:42:08 +01:00
* @return return value
*/
2015-09-11 12:09:22 +02:00
public Object call(final Object... params)
{
try
{
return method.invoke(null, params);
}
catch (final Exception e)
{
2014-11-05 04:42:08 +01:00
throw new RuntimeException(e);
}
}
2015-02-23 02:32:27 +01:00
2015-09-11 12:09:22 +02:00
public class RefExecutor
{
2014-11-21 23:45:46 +01:00
final Object e;
2015-02-23 02:32:27 +01:00
2015-09-11 12:09:22 +02:00
public RefExecutor(final Object e)
{
2014-11-05 04:42:08 +01:00
this.e = e;
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* apply method for selected object
*
2014-12-18 03:15:11 +01:00
* @param params sent parameters
*
2014-11-05 04:42:08 +01:00
* @return return value
2014-12-18 03:15:11 +01:00
*
* @throws RuntimeException if something went wrong
2014-11-05 04:42:08 +01:00
*/
2015-09-11 12:09:22 +02:00
public Object call(final Object... params)
{
try
{
return method.invoke(e, params);
}
catch (final Exception e)
{
2014-11-05 04:42:08 +01:00
throw new RuntimeException(e);
}
}
}
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* Constructor wrapper
*/
2015-09-11 12:09:22 +02:00
public static class RefConstructor
{
2014-11-05 04:42:08 +01:00
private final Constructor constructor;
2015-02-23 02:32:27 +01:00
2015-09-11 12:09:22 +02:00
private RefConstructor(final Constructor constructor)
{
2014-11-16 10:48:18 +01:00
this.constructor = constructor;
constructor.setAccessible(true);
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* @return passed constructor
*/
2015-09-11 12:09:22 +02:00
public Constructor getRealConstructor()
{
return constructor;
2014-11-05 04:42:08 +01:00
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* @return owner class of method
*/
2015-09-11 12:09:22 +02:00
public RefClass getRefClass()
{
return new RefClass(constructor.getDeclaringClass());
2014-11-05 04:42:08 +01:00
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* create new instance with constructor
*
2014-12-18 03:15:11 +01:00
* @param params parameters for constructor
*
2014-11-05 04:42:08 +01:00
* @return new object
2014-12-18 03:15:11 +01:00
*
* @throws RuntimeException if something went wrong
2014-11-05 04:42:08 +01:00
*/
2015-09-11 12:09:22 +02:00
public Object create(final Object... params)
{
try
{
return constructor.newInstance(params);
}
catch (final Exception e)
{
2014-11-05 04:42:08 +01:00
throw new RuntimeException(e);
}
}
}
2015-02-23 02:32:27 +01:00
2015-09-11 12:09:22 +02:00
public static class RefField
{
2014-11-05 04:42:08 +01:00
private final Field field;
2015-02-23 02:32:27 +01:00
2015-09-11 12:09:22 +02:00
private RefField(final Field field)
{
2014-11-16 10:48:18 +01:00
this.field = field;
field.setAccessible(true);
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* @return passed field
*/
2015-09-11 12:09:22 +02:00
public Field getRealField()
{
return field;
2014-11-05 04:42:08 +01:00
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* @return owner class of field
*/
2015-09-11 12:09:22 +02:00
public RefClass getRefClass()
{
return new RefClass(field.getDeclaringClass());
2014-11-05 04:42:08 +01:00
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* @return type of field
*/
2015-09-11 12:09:22 +02:00
public RefClass getFieldRefClass()
{
return new RefClass(field.getType());
2014-11-05 04:42:08 +01:00
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* apply fiend for object
*
2014-12-18 03:15:11 +01:00
* @param e applied object
*
2014-11-05 04:42:08 +01:00
* @return RefExecutor with getter and setter
*/
2015-09-11 12:09:22 +02:00
public RefExecutor of(final Object e)
{
2014-11-05 04:42:08 +01:00
return new RefExecutor(e);
}
2015-02-23 02:32:27 +01:00
2015-09-11 12:09:22 +02:00
public class RefExecutor
{
2014-11-21 23:45:46 +01:00
final Object e;
2015-02-23 02:32:27 +01:00
2015-09-11 12:09:22 +02:00
public RefExecutor(final Object e)
{
2014-11-05 04:42:08 +01:00
this.e = e;
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* set field value for applied object
*
2014-12-18 03:15:11 +01:00
* @param param value
2014-11-05 04:42:08 +01:00
*/
2015-09-11 12:09:22 +02:00
public void set(final Object param)
{
try
{
field.set(e, param);
}
catch (final Exception e)
{
2014-11-05 04:42:08 +01:00
throw new RuntimeException(e);
}
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* get field value for applied object
*
* @return value of field
*/
2015-09-11 12:09:22 +02:00
public Object get()
{
try
{
return field.get(e);
}
catch (final Exception e)
{
2014-11-05 04:42:08 +01:00
throw new RuntimeException(e);
}
}
}
}
2014-09-22 13:02:14 +02:00
}