- * // Static import all reflection methods to decrease verbosity
- * import static org.joor.Reflect.*;
- *
- * // Wrap an Object / Class / class name with the on() method:
- * on("java.lang.String")
- * // Invoke constructors using the create() method:
- * .create("Hello World")
- * // Invoke methods using the call() method:
- * .call("toString")
- * // Retrieve the wrapped object
- *
- * @author Lukas Eder
- */
-public class Reflect {
-
- // ---------------------------------------------------------------------
- // Static API used as entrance points to the fluent API
- // ---------------------------------------------------------------------
- /**
- * Wrap a class name.
- *
- * This is the same as calling on(Class.forName(name))
- *
- * @param name A fully qualified class name
- * @return A wrapped class object, to be used for further reflection.
- * @throws ReflectException If any reflection exception occurred.
- * @see #on(Class)
- */
- public static Reflect on(String name) throws ReflectException {
- return on(forName(name));
- }
-
- /**
- * Wrap a class.
- *
- * Use this when you want to access static fields and methods on a
- * {@link Class} object, or as a basis for constructing objects of that
- * class using {@link #create(Object...)}
- *
- * @param clazz The class to be wrapped
- * @return A wrapped class object, to be used for further reflection.
- */
- public static Reflect on(Class> clazz) {
- return new Reflect(clazz);
- }
-
- /**
- * Wrap an object.
- *
- * Use this when you want to access instance fields and methods on any
- * {@link Object}
- *
- * @param object The object to be wrapped
- * @return A wrapped object, to be used for further reflection.
- */
- public static Reflect on(Object object) {
- return new Reflect(object);
- }
- // ---------------------------------------------------------------------
- // Members
- // ---------------------------------------------------------------------
- /**
- * The wrapped object
- */
- private final Object object;
- /**
- * A flag indicating whether the wrapped object is a {@link Class} (for
- * accessing static fields and methods), or any other type of {@link Object}
- * (for accessing instance fields and methods).
- */
- private final boolean isClass;
-
- // ---------------------------------------------------------------------
- // Constructors
- // ---------------------------------------------------------------------
- private Reflect(Class> type) {
- this.object = type;
- this.isClass = true;
- }
-
- private Reflect(Object object) {
- this.object = object;
- this.isClass = false;
- }
-
- // ---------------------------------------------------------------------
- // Fluent Configuration API
- // ---------------------------------------------------------------------
- // TO_DO: Allow for accessing non-public members, methods, etc
- // ---------------------------------------------------------------------
- // Fluent Reflection API
- // ---------------------------------------------------------------------
- /**
- * Get the wrapped object
- *
- * @param A convenience generic parameter for automatic unsafe casting
- */
- @SuppressWarnings("unchecked")
- public T get() {
- return (T) object;
- }
-
- /**
- * Set a field value.
- *
- * This is roughly equivalent to {@link Field#set(Object, Object)}. If the
- * wrapped object is a {@link Class}, then this will set a value to a static
- * member field. If the wrapped object is any other {@link Object}, then
- * this will set a value to an instance member field.
- *
- * @param name The field name
- * @param value The new field value
- * @return The same wrapped object, to be used for further reflection.
- * @throws ReflectException If any reflection exception occurred.
- */
- public Reflect set(String name, Object value) throws ReflectException {
- try {
- Field field = type().getField(name);
- if (!field.isAccessible()) {
- field.setAccessible(true);
- }
- field.set(object, unwrap(value));
- return this;
- } catch (Exception e) {
- throw new ReflectException(e);
- }
- }
-
- /**
- * Get a field value.
- *
- * This is roughly equivalent to {@link Field#get(Object)}. If the wrapped
- * object is a {@link Class}, then this will get a value from a static
- * member field. If the wrapped object is any other {@link Object}, then
- * this will get a value from an instance member field.
- *
- * If you want to "navigate" to a wrapped version of the field, use
- * {@link #field(String)} instead.
- *
- * @param name The field name
- * @return The field value
- * @throws ReflectException If any reflection exception occurred.
- * @see #field(String)
- */
- public Object get(String name) throws ReflectException {
- return field(name).get();
- }
-
- /**
- * Get a wrapped field.
- *
- * This is roughly equivalent to {@link Field#get(Object)}. If the wrapped
- * object is a {@link Class}, then this will wrap a static member field. If
- * the wrapped object is any other {@link Object}, then this wrap an
- * instance member field.
- *
- * @param name The field name
- * @return The wrapped field
- * @throws ReflectException If any reflection exception occurred.
- */
- public Reflect field(String name) throws ReflectException {
- try {
- Field field = type().getField(name);
- if (!field.isAccessible()) {
- field.setAccessible(true);
- }
- return on(field.get(object));
- } catch (Exception e) {
- throw new ReflectException(e);
- }
- }
-
- /**
- * Get a Map containing field names and wrapped values for the fields'
- * values.
- *
- * If the wrapped object is a {@link Class}, then this will return static
- * fields. If the wrapped object is any other {@link Object}, then this will
- * return instance fields.
- *
- * These two calls are equivalent
- * on(object).field("myField");
- * on(object).fields().get("myField");
- *
- *
- * @return A map containing field names and wrapped values.
- */
- public Map fields() {
- Map result = new LinkedHashMap();
-
- for (Field field : type().getFields()) {
- if (!isClass ^ Modifier.isStatic(field.getModifiers())) {
- String name = field.getName();
- result.put(name, field(name));
- }
- }
-
- return result;
- }
-
- /**
- * Call a method by its name.
- *
- * This is a convenience method for calling
- * call(name, new Object[0])
- *
- * @param name The method name
- * @return The wrapped method result or the same wrapped object if the
- * method returns void, to be used for further
- * reflection.
- * @throws ReflectException If any reflection exception occurred.
- * @see #call(String, Object...)
- */
- public Reflect call(String name) throws ReflectException {
- return call(name, new Object[0]);
- }
-
- /**
- * Call a method by its name.
- *
- * This is roughly equivalent to {@link Method#invoke(Object, Object...)}.
- * If the wrapped object is a {@link Class}, then this will invoke a static
- * method. If the wrapped object is any other {@link Object}, then this will
- * invoke an instance method.
- *
- * Just like {@link Method#invoke(Object, Object...)}, this will try to wrap
- * primitive types or unwrap primitive type wrappers if applicable. If
- * several methods are applicable, by that rule, the first one encountered
- * is called. i.e. when calling
- * on(...).call("method", 1, 1);
- * The first of the following methods will be called:
- *
- * public void method(int param1, Integer param2);
- * public void method(Integer param1, int param2);
- * public void method(Number param1, Number param2);
- * public void method(Number param1, Object param2);
- * public void method(int param1, Object param2);
- *
- *
- * @param name The method name
- * @param args The method arguments
- * @return The wrapped method result or the same wrapped object if the
- * method returns void, to be used for further
- * reflection.
- * @throws ReflectException If any reflection exception occurred.
- */
- public Reflect call(String name, Object... args) throws ReflectException {
- Class>[] types = types(args);
-
- // Try invoking the "canonical" method, i.e. the one with exact
- // matching argument types
- try {
- Method method = type().getMethod(name, types);
- return on(method, object, args);
- } // If there is no exact match, try to find one that has a "similar"
- // signature if primitive argument types are converted to their wrappers
- catch (NoSuchMethodException e) {
- for (Method method : type().getMethods()) {
- if (method.getName().equals(name) && match(method.getParameterTypes(), types)) {
- return on(method, object, args);
- }
- }
-
- throw new ReflectException(e);
- }
- }
-
- /**
- * Call a constructor.
- *
- * This is a convenience method for calling
- * create(new Object[0])
- *
- * @return The wrapped new object, to be used for further reflection.
- * @throws ReflectException If any reflection exception occurred.
- * @see #create(Object...)
- */
- public Reflect create() throws ReflectException {
- return create(new Object[0]);
- }
-
- /**
- * Call a constructor.
- *
- * This is roughly equivalent to {@link Constructor#newInstance(Object...)}.
- * If the wrapped object is a {@link Class}, then this will create a new
- * object of that class. If the wrapped object is any other {@link Object},
- * then this will create a new object of the same type.
- *
- * Just like {@link Constructor#newInstance(Object...)}, this will try to
- * wrap primitive types or unwrap primitive type wrappers if applicable. If
- * several constructors are applicable, by that rule, the first one
- * encountered is called. i.e. when calling
- * on(C.class).create(1, 1);
- *
The first of the following constructors will be applied:
- *
- * public C(int param1, Integer param2);
- * public C(Integer param1, int param2);
- * public C(Number param1, Number param2);
- * public C(Number param1, Object param2);
- * public C(int param1, Object param2);
- *
- *
- * @param args The constructor arguments
- * @return The wrapped new object, to be used for further reflection.
- * @throws ReflectException If any reflection exception occurred.
- */
- public Reflect create(Object... args) throws ReflectException {
- Class>[] types = types(args);
-
- // Try invoking the "canonical" constructor, i.e. the one with exact
- // matching argument types
- try {
- Constructor> constructor = type().getConstructor(types);
- return on(constructor, args);
- } // If there is no exact match, try to find one that has a "similar"
- // signature if primitive argument types are converted to their wrappers
- catch (NoSuchMethodException e) {
- for (Constructor> constructor : type().getConstructors()) {
- if (match(constructor.getParameterTypes(), types)) {
- return on(constructor, args);
- }
- }
-
- throw new ReflectException(e);
- }
- }
-
- // ---------------------------------------------------------------------
- // Object API
- // ---------------------------------------------------------------------
- /**
- * Check whether two arrays of types match, converting primitive types to
- * their corresponding wrappers.
- */
- private boolean match(Class>[] declaredTypes, Class>[] actualTypes) {
- if (declaredTypes.length == actualTypes.length) {
- for (int i = 0; i < actualTypes.length; i++) {
- if (!wrapper(declaredTypes[i]).isAssignableFrom(wrapper(actualTypes[i]))) {
- return false;
- }
- }
-
- return true;
- } else {
- return false;
- }
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int hashCode() {
- return object.hashCode();
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean equals(Object obj) {
- if (obj instanceof Reflect) {
- return object.equals(((Reflect) obj).get());
- }
-
- return false;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String toString() {
- return object.toString();
- }
-
- // ---------------------------------------------------------------------
- // Utility methods
- // ---------------------------------------------------------------------
- /**
- * Wrap an object created from a constructor
- */
- private static Reflect on(Constructor> constructor, Object... args) throws ReflectException {
- try {
- return on(constructor.newInstance(args));
- } catch (Exception e) {
- throw new ReflectException(e);
- }
- }
-
- /**
- * Wrap an object returned from a method
- */
- private static Reflect on(Method method, Object object, Object... args) throws ReflectException {
- try {
- if (method.getReturnType() == void.class) {
- method.invoke(object, args);
- return on(object);
- } else {
- return on(method.invoke(object, args));
- }
- } catch (Exception e) {
- throw new ReflectException(e);
- }
- }
-
- /**
- * Unwrap an object
- */
- private static Object unwrap(Object object) {
- if (object instanceof Reflect) {
- return ((Reflect) object).get();
- }
-
- return object;
- }
-
- /**
- * Get an array of types for an array of objects
- *
- * @see Object#getClass()
- */
- private static Class>[] types(Object... values) {
- if (values == null) {
- return new Class[0];
- }
-
- Class>[] result = new Class[values.length];
-
- for (int i = 0; i < values.length; i++) {
- result[i] = values[i].getClass();
- }
-
- return result;
- }
-
- /**
- * Load a class
- *
- * @see Class#forName(String)
- */
- private static Class> forName(String name) throws ReflectException {
- try {
- return Class.forName(name);
- } catch (Exception e) {
- throw new ReflectException(e);
- }
- }
-
- /**
- * Get the type of the wrapped object.
- *
- * @see Object#getClass()
- */
- private Class> type() {
- if (isClass) {
- return (Class>) object;
- } else {
- return object.getClass();
- }
- }
-
- /**
- * Get a wrapper type for a primitive type, or the argument type itself, if
- * it is not a primitive type.
- */
- private static Class> wrapper(Class> type) {
- if (boolean.class == type) {
- return Boolean.class;
- } else if (int.class == type) {
- return Integer.class;
- } else if (long.class == type) {
- return Long.class;
- } else if (short.class == type) {
- return Short.class;
- } else if (byte.class == type) {
- return Byte.class;
- } else if (double.class == type) {
- return Double.class;
- } else if (float.class == type) {
- return Float.class;
- } else if (char.class == type) {
- return Character.class;
- } else {
- return type;
- }
- }
-}
\ No newline at end of file
diff --git a/src/main/resources/Configuration.yml b/src/main/resources/Configuration.yml
deleted file mode 100644
index b72df809..00000000
--- a/src/main/resources/Configuration.yml
+++ /dev/null
@@ -1,20 +0,0 @@
-colors:
- cuboidGrid: '#CC3333'
- cuboidEdge: '#CC4C4C'
- cuboidFirstPoint: '#33CC33'
- cuboidSecondPoint: '#3333CC'
-
- cylinderEdge: '#CC4C4C'
- cylinderGrid: '#CC3333'
- cylinderPoint: '#CC33CC'
-
- polyGrid: '#CC3333'
- polyEdge: '#CC4C4C'
- polyPoint: '#33CCCC'
-
- ellipsoidGrid: '#CC4C4C'
- ellipsoidPoint: '#CCCC33'
-
-debug: false
-
-ignoreUpdates: false
\ No newline at end of file
diff --git a/updates.yml b/updates.yml
deleted file mode 100644
index fd9109e5..00000000
--- a/updates.yml
+++ /dev/null
@@ -1,6 +0,0 @@
-updaterVersion1:
- current: 1.4.6
- supported:
- - 1.4.5
-# - 1.3.1
-