Guest User

Untitled

a guest
May 26th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 27.26 KB | None | 0 0
  1. package me.flash110;
  2.  
  3. import java.lang.reflect.Constructor;
  4. import java.lang.reflect.Field;
  5. import java.lang.reflect.InvocationTargetException;
  6. import java.lang.reflect.Method;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9.  
  10. import org.bukkit.Bukkit;
  11.  
  12. /**
  13.  * <b>ReflectionUtils</b>
  14.  * <p>
  15.  * This class provides useful methods which makes dealing with reflection much easier, especially when working with Bukkit
  16.  * <p>
  17.  * You are welcome to use it, modify it and redistribute it under the following conditions:
  18.  * <ul>
  19.  * <li>Don't claim this class as your own
  20.  * <li>Don't remove this disclaimer
  21.  * </ul>
  22.  * <p>
  23.  * <i>It would be nice if you provide credit to me if you use this class in a published project</i>
  24.  *
  25.  * @author DarkBlade12
  26.  * @version 1.1
  27.  */
  28. public final class ReflectionUtils {
  29.     // Prevent accidental construction
  30.     private ReflectionUtils() {}
  31.  
  32.     /**
  33.      * Returns the constructor of a given class with the given parameter types
  34.      *
  35.      * @param clazz Target class
  36.      * @param parameterTypes Parameter types of the desired constructor
  37.      * @return The constructor of the target class with the specified parameter types
  38.      * @throws NoSuchMethodException If the desired constructor with the specified parameter types cannot be found
  39.      * @see DataType
  40.      * @see DataType#getPrimitive(Class[])
  41.      * @see DataType#compare(Class[], Class[])
  42.      */
  43.     public static Constructor<?> getConstructor(Class<?> clazz, Class<?>... parameterTypes) throws NoSuchMethodException {
  44.         Class<?>[] primitiveTypes = DataType.getPrimitive(parameterTypes);
  45.         for (Constructor<?> constructor : clazz.getConstructors()) {
  46.             if (!DataType.compare(DataType.getPrimitive(constructor.getParameterTypes()), primitiveTypes)) {
  47.                 continue;
  48.             }
  49.             return constructor;
  50.         }
  51.         throw new NoSuchMethodException("There is no such constructor in this class with the specified parameter types");
  52.     }
  53.  
  54.     /**
  55.      * Returns the constructor of a desired class with the given parameter types
  56.      *
  57.      * @param className Name of the desired target class
  58.      * @param packageType Package where the desired target class is located
  59.      * @param parameterTypes Parameter types of the desired constructor
  60.      * @return The constructor of the desired target class with the specified parameter types
  61.      * @throws NoSuchMethodException If the desired constructor with the specified parameter types cannot be found
  62.      * @throws ClassNotFoundException ClassNotFoundException If the desired target class with the specified name and package cannot be found
  63.      * @see #getClass(String, PackageType)
  64.      * @see #getConstructor(Class, Class...)
  65.      */
  66.     public static Constructor<?> getConstructor(String className, PackageType packageType, Class<?>... parameterTypes) throws NoSuchMethodException, ClassNotFoundException {
  67.         return getConstructor(packageType.getClass(className), parameterTypes);
  68.     }
  69.  
  70.     /**
  71.      * Returns an instance of a class with the given arguments
  72.      *
  73.      * @param clazz Target class
  74.      * @param arguments Arguments which are used to construct an object of the target class
  75.      * @return The instance of the target class with the specified arguments
  76.      * @throws InstantiationException If you cannot create an instance of the target class due to certain circumstances
  77.      * @throws IllegalAccessException If the desired constructor cannot be accessed due to certain circumstances
  78.      * @throws IllegalArgumentException If the types of the arguments do not match the parameter types of the constructor (this should not occur since it searches for a constructor with the types of the arguments)
  79.      * @throws InvocationTargetException If the desired constructor cannot be invoked
  80.      * @throws NoSuchMethodException If the desired constructor with the specified arguments cannot be found
  81.      */
  82.     public static Object instantiateObject(Class<?> clazz, Object... arguments) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException {
  83.         return getConstructor(clazz, DataType.getPrimitive(arguments)).newInstance(arguments);
  84.     }
  85.  
  86.     /**
  87.      * Returns an instance of a desired class with the given arguments
  88.      *
  89.      * @param className Name of the desired target class
  90.      * @param packageType Package where the desired target class is located
  91.      * @param arguments Arguments which are used to construct an object of the desired target class
  92.      * @return The instance of the desired target class with the specified arguments
  93.      * @throws InstantiationException If you cannot create an instance of the desired target class due to certain circumstances
  94.      * @throws IllegalAccessException If the desired constructor cannot be accessed due to certain circumstances
  95.      * @throws IllegalArgumentException If the types of the arguments do not match the parameter types of the constructor (this should not occur since it searches for a constructor with the types of the arguments)
  96.      * @throws InvocationTargetException If the desired constructor cannot be invoked
  97.      * @throws NoSuchMethodException If the desired constructor with the specified arguments cannot be found
  98.      * @throws ClassNotFoundException If the desired target class with the specified name and package cannot be found
  99.      * @see #getClass(String, PackageType)
  100.      * @see #instantiateObject(Class, Object...)
  101.      */
  102.     public static Object instantiateObject(String className, PackageType packageType, Object... arguments) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException {
  103.         return instantiateObject(packageType.getClass(className), arguments);
  104.     }
  105.  
  106.     /**
  107.      * Returns a method of a class with the given parameter types
  108.      *
  109.      * @param clazz Target class
  110.      * @param methodName Name of the desired method
  111.      * @param parameterTypes Parameter types of the desired method
  112.      * @return The method of the target class with the specified name and parameter types
  113.      * @throws NoSuchMethodException If the desired method of the target class with the specified name and parameter types cannot be found
  114.      * @see DataType#getPrimitive(Class[])
  115.      * @see DataType#compare(Class[], Class[])
  116.      */
  117.     public static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException {
  118.         Class<?>[] primitiveTypes = DataType.getPrimitive(parameterTypes);
  119.         for (Method method : clazz.getMethods()) {
  120.             if (!method.getName().equals(methodName) || !DataType.compare(DataType.getPrimitive(method.getParameterTypes()), primitiveTypes)) {
  121.                 continue;
  122.             }
  123.             return method;
  124.         }
  125.         throw new NoSuchMethodException("There is no such method in this class with the specified name and parameter types");
  126.     }
  127.  
  128.     /**
  129.      * Returns a method of a desired class with the given parameter types
  130.      *
  131.      * @param className Name of the desired target class
  132.      * @param packageType Package where the desired target class is located
  133.      * @param methodName Name of the desired method
  134.      * @param parameterTypes Parameter types of the desired method
  135.      * @return The method of the desired target class with the specified name and parameter types
  136.      * @throws NoSuchMethodException If the desired method of the desired target class with the specified name and parameter types cannot be found
  137.      * @throws ClassNotFoundException If the desired target class with the specified name and package cannot be found
  138.      * @see #getClass(String, PackageType)
  139.      * @see #getMethod(Class, String, Class...)
  140.      */
  141.     public static Method getMethod(String className, PackageType packageType, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException, ClassNotFoundException {
  142.         return getMethod(packageType.getClass(className), methodName, parameterTypes);
  143.     }
  144.  
  145.     /**
  146.      * Invokes a method on an object with the given arguments
  147.      *
  148.      * @param instance Target object
  149.      * @param methodName Name of the desired method
  150.      * @param arguments Arguments which are used to invoke the desired method
  151.      * @return The result of invoking the desired method on the target object
  152.      * @throws IllegalAccessException If the desired method cannot be accessed due to certain circumstances
  153.      * @throws IllegalArgumentException If the types of the arguments do not match the parameter types of the method (this should not occur since it searches for a method with the types of the arguments)
  154.      * @throws InvocationTargetException If the desired method cannot be invoked on the target object
  155.      * @throws NoSuchMethodException If the desired method of the class of the target object with the specified name and arguments cannot be found
  156.      * @see #getMethod(Class, String, Class...)
  157.      * @see DataType#getPrimitive(Object[])
  158.      */
  159.     public static Object invokeMethod(Object instance, String methodName, Object... arguments) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException {
  160.         return getMethod(instance.getClass(), methodName, DataType.getPrimitive(arguments)).invoke(instance, arguments);
  161.     }
  162.  
  163.     /**
  164.      * Invokes a method of the target class on an object with the given arguments
  165.      *
  166.      * @param instance Target object
  167.      * @param clazz Target class
  168.      * @param methodName Name of the desired method
  169.      * @param arguments Arguments which are used to invoke the desired method
  170.      * @return The result of invoking the desired method on the target object
  171.      * @throws IllegalAccessException If the desired method cannot be accessed due to certain circumstances
  172.      * @throws IllegalArgumentException If the types of the arguments do not match the parameter types of the method (this should not occur since it searches for a method with the types of the arguments)
  173.      * @throws InvocationTargetException If the desired method cannot be invoked on the target object
  174.      * @throws NoSuchMethodException If the desired method of the target class with the specified name and arguments cannot be found
  175.      * @see #getMethod(Class, String, Class...)
  176.      * @see DataType#getPrimitive(Object[])
  177.      */
  178.     public static Object invokeMethod(Object instance, Class<?> clazz, String methodName, Object... arguments) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException {
  179.         return getMethod(clazz, methodName, DataType.getPrimitive(arguments)).invoke(instance, arguments);
  180.     }
  181.  
  182.     /**
  183.      * Invokes a method of a desired class on an object with the given arguments
  184.      *
  185.      * @param instance Target object
  186.      * @param className Name of the desired target class
  187.      * @param packageType Package where the desired target class is located
  188.      * @param methodName Name of the desired method
  189.      * @param arguments Arguments which are used to invoke the desired method
  190.      * @return The result of invoking the desired method on the target object
  191.      * @throws IllegalAccessException If the desired method cannot be accessed due to certain circumstances
  192.      * @throws IllegalArgumentException If the types of the arguments do not match the parameter types of the method (this should not occur since it searches for a method with the types of the arguments)
  193.      * @throws InvocationTargetException If the desired method cannot be invoked on the target object
  194.      * @throws NoSuchMethodException If the desired method of the desired target class with the specified name and arguments cannot be found
  195.      * @throws ClassNotFoundException If the desired target class with the specified name and package cannot be found
  196.      * @see #getClass(String, PackageType)
  197.      * @see #invokeMethod(Object, Class, String, Object...)
  198.      */
  199.     public static Object invokeMethod(Object instance, String className, PackageType packageType, String methodName, Object... arguments) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException {
  200.         return invokeMethod(instance, packageType.getClass(className), methodName, arguments);
  201.     }
  202.  
  203.     /**
  204.      * Returns a field of the target class with the given name
  205.      *
  206.      * @param clazz Target class
  207.      * @param declared Whether the desired field is declared or not
  208.      * @param fieldName Name of the desired field
  209.      * @return The field of the target class with the specified name
  210.      * @throws NoSuchFieldException If the desired field of the given class cannot be found
  211.      * @throws SecurityException If the desired field cannot be made accessible
  212.      */
  213.     public static Field getField(Class<?> clazz, boolean declared, String fieldName) throws NoSuchFieldException, SecurityException {
  214.         Field field = declared ? clazz.getDeclaredField(fieldName) : clazz.getField(fieldName);
  215.         field.setAccessible(true);
  216.         return field;
  217.     }
  218.  
  219.     /**
  220.      * Returns a field of a desired class with the given name
  221.      *
  222.      * @param className Name of the desired target class
  223.      * @param packageType Package where the desired target class is located
  224.      * @param declared Whether the desired field is declared or not
  225.      * @param fieldName Name of the desired field
  226.      * @return The field of the desired target class with the specified name
  227.      * @throws NoSuchFieldException If the desired field of the desired class cannot be found
  228.      * @throws SecurityException If the desired field cannot be made accessible
  229.      * @throws ClassNotFoundException If the desired target class with the specified name and package cannot be found
  230.      * @see #getField(Class, boolean, String)
  231.      */
  232.     public static Field getField(String className, PackageType packageType, boolean declared, String fieldName) throws NoSuchFieldException, SecurityException, ClassNotFoundException {
  233.         return getField(packageType.getClass(className), declared, fieldName);
  234.     }
  235.  
  236.     /**
  237.      * Returns the value of a field of the given class of an object
  238.      *
  239.      * @param instance Target object
  240.      * @param clazz Target class
  241.      * @param declared Whether the desired field is declared or not
  242.      * @param fieldName Name of the desired field
  243.      * @return The value of field of the target object
  244.      * @throws IllegalArgumentException If the target object does not feature the desired field
  245.      * @throws IllegalAccessException If the desired field cannot be accessed
  246.      * @throws NoSuchFieldException If the desired field of the target class cannot be found
  247.      * @throws SecurityException If the desired field cannot be made accessible
  248.      * @see #getField(Class, boolean, String)
  249.      */
  250.     public static Object getValue(Object instance, Class<?> clazz, boolean declared, String fieldName) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
  251.         return getField(clazz, declared, fieldName).get(instance);
  252.     }
  253.  
  254.     /**
  255.      * Returns the value of a field of a desired class of an object
  256.      *
  257.      * @param instance Target object
  258.      * @param className Name of the desired target class
  259.      * @param packageType Package where the desired target class is located
  260.      * @param declared Whether the desired field is declared or not
  261.      * @param fieldName Name of the desired field
  262.      * @return The value of field of the target object
  263.      * @throws IllegalArgumentException If the target object does not feature the desired field
  264.      * @throws IllegalAccessException If the desired field cannot be accessed
  265.      * @throws NoSuchFieldException If the desired field of the desired class cannot be found
  266.      * @throws SecurityException If the desired field cannot be made accessible
  267.      * @throws ClassNotFoundException If the desired target class with the specified name and package cannot be found
  268.      * @see #getValue(Object, Class, boolean, String)
  269.      */
  270.     public static Object getValue(Object instance, String className, PackageType packageType, boolean declared, String fieldName) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, ClassNotFoundException {
  271.         return getValue(instance, packageType.getClass(className), declared, fieldName);
  272.     }
  273.  
  274.     /**
  275.      * Returns the value of a field with the given name of an object
  276.      *
  277.      * @param instance Target object
  278.      * @param declared Whether the desired field is declared or not
  279.      * @param fieldName Name of the desired field
  280.      * @return The value of field of the target object
  281.      * @throws IllegalArgumentException If the target object does not feature the desired field (should not occur since it searches for a field with the given name in the class of the object)
  282.      * @throws IllegalAccessException If the desired field cannot be accessed
  283.      * @throws NoSuchFieldException If the desired field of the target object cannot be found
  284.      * @throws SecurityException If the desired field cannot be made accessible
  285.      * @see #getValue(Object, Class, boolean, String)
  286.      */
  287.     public static Object getValue(Object instance, boolean declared, String fieldName) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
  288.         return getValue(instance, instance.getClass(), declared, fieldName);
  289.     }
  290.  
  291.     /**
  292.      * Sets the value of a field of the given class of an object
  293.      *
  294.      * @param instance Target object
  295.      * @param clazz Target class
  296.      * @param declared Whether the desired field is declared or not
  297.      * @param fieldName Name of the desired field
  298.      * @param value New value
  299.      * @throws IllegalArgumentException If the type of the value does not match the type of the desired field
  300.      * @throws IllegalAccessException If the desired field cannot be accessed
  301.      * @throws NoSuchFieldException If the desired field of the target class cannot be found
  302.      * @throws SecurityException If the desired field cannot be made accessible
  303.      * @see #getField(Class, boolean, String)
  304.      */
  305.     public static void setValue(Object instance, Class<?> clazz, boolean declared, String fieldName, Object value) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
  306.         getField(clazz, declared, fieldName).set(instance, value);
  307.     }
  308.  
  309.     /**
  310.      * Sets the value of a field of a desired class of an object
  311.      *
  312.      * @param instance Target object
  313.      * @param className Name of the desired target class
  314.      * @param packageType Package where the desired target class is located
  315.      * @param declared Whether the desired field is declared or not
  316.      * @param fieldName Name of the desired field
  317.      * @param value New value
  318.      * @throws IllegalArgumentException If the type of the value does not match the type of the desired field
  319.      * @throws IllegalAccessException If the desired field cannot be accessed
  320.      * @throws NoSuchFieldException If the desired field of the desired class cannot be found
  321.      * @throws SecurityException If the desired field cannot be made accessible
  322.      * @throws ClassNotFoundException If the desired target class with the specified name and package cannot be found
  323.      * @see #setValue(Object, Class, boolean, String, Object)
  324.      */
  325.     public static void setValue(Object instance, String className, PackageType packageType, boolean declared, String fieldName, Object value) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, ClassNotFoundException {
  326.         setValue(instance, packageType.getClass(className), declared, fieldName, value);
  327.     }
  328.  
  329.     /**
  330.      * Sets the value of a field with the given name of an object
  331.      *
  332.      * @param instance Target object
  333.      * @param declared Whether the desired field is declared or not
  334.      * @param fieldName Name of the desired field
  335.      * @param value New value
  336.      * @throws IllegalArgumentException If the type of the value does not match the type of the desired field
  337.      * @throws IllegalAccessException If the desired field cannot be accessed
  338.      * @throws NoSuchFieldException If the desired field of the target object cannot be found
  339.      * @throws SecurityException If the desired field cannot be made accessible
  340.      * @see #setValue(Object, Class, boolean, String, Object)
  341.      */
  342.     public static void setValue(Object instance, boolean declared, String fieldName, Object value) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
  343.         setValue(instance, instance.getClass(), declared, fieldName, value);
  344.     }
  345.  
  346.     /**
  347.      * Represents an enumeration of dynamic packages of NMS and CraftBukkit
  348.      * <p>
  349.      * This class is part of the <b>ReflectionUtils</b> and follows the same usage conditions
  350.      *
  351.      * @author DarkBlade12
  352.      * @since 1.0
  353.      */
  354.     public enum PackageType {
  355.         MINECRAFT_SERVER("net.minecraft.server." + getServerVersion()),
  356.         CRAFTBUKKIT("org.bukkit.craftbukkit." + getServerVersion()),
  357.         CRAFTBUKKIT_BLOCK(CRAFTBUKKIT, "block"),
  358.         CRAFTBUKKIT_CHUNKIO(CRAFTBUKKIT, "chunkio"),
  359.         CRAFTBUKKIT_COMMAND(CRAFTBUKKIT, "command"),
  360.         CRAFTBUKKIT_CONVERSATIONS(CRAFTBUKKIT, "conversations"),
  361.         CRAFTBUKKIT_ENCHANTMENS(CRAFTBUKKIT, "enchantments"),
  362.         CRAFTBUKKIT_ENTITY(CRAFTBUKKIT, "entity"),
  363.         CRAFTBUKKIT_EVENT(CRAFTBUKKIT, "event"),
  364.         CRAFTBUKKIT_GENERATOR(CRAFTBUKKIT, "generator"),
  365.         CRAFTBUKKIT_HELP(CRAFTBUKKIT, "help"),
  366.         CRAFTBUKKIT_INVENTORY(CRAFTBUKKIT, "inventory"),
  367.         CRAFTBUKKIT_MAP(CRAFTBUKKIT, "map"),
  368.         CRAFTBUKKIT_METADATA(CRAFTBUKKIT, "metadata"),
  369.         CRAFTBUKKIT_POTION(CRAFTBUKKIT, "potion"),
  370.         CRAFTBUKKIT_PROJECTILES(CRAFTBUKKIT, "projectiles"),
  371.         CRAFTBUKKIT_SCHEDULER(CRAFTBUKKIT, "scheduler"),
  372.         CRAFTBUKKIT_SCOREBOARD(CRAFTBUKKIT, "scoreboard"),
  373.         CRAFTBUKKIT_UPDATER(CRAFTBUKKIT, "updater"),
  374.         CRAFTBUKKIT_UTIL(CRAFTBUKKIT, "util");
  375.  
  376.         private final String path;
  377.  
  378.         /**
  379.          * Construct a new package type
  380.          *
  381.          * @param path Path of the package
  382.          */
  383.         private PackageType(String path) {
  384.             this.path = path;
  385.         }
  386.  
  387.         /**
  388.          * Construct a new package type
  389.          *
  390.          * @param parent Parent package of the package
  391.          * @param path Path of the package
  392.          */
  393.         private PackageType(PackageType parent, String path) {
  394.             this(parent + "." + path);
  395.         }
  396.  
  397.         /**
  398.          * Returns the path of this package type
  399.          *
  400.          * @return The path
  401.          */
  402.         public String getPath() {
  403.             return path;
  404.         }
  405.  
  406.         /**
  407.          * Returns the class with the given name
  408.          *
  409.          * @param className Name of the desired class
  410.          * @return The class with the specified name
  411.          * @throws ClassNotFoundException If the desired class with the specified name and package cannot be found
  412.          */
  413.         public Class<?> getClass(String className) throws ClassNotFoundException {
  414.             return Class.forName(this + "." + className);
  415.         }
  416.  
  417.         // Override for convenience
  418.         @Override
  419.         public String toString() {
  420.             return path;
  421.         }
  422.  
  423.         /**
  424.          * Returns the version of your server
  425.          *
  426.          * @return The server version
  427.          */
  428.         public static String getServerVersion() {
  429.             return Bukkit.getServer().getClass().getPackage().getName().substring(23);
  430.         }
  431.     }
  432.  
  433.     /**
  434.      * Represents an enumeration of Java data types with corresponding classes
  435.      * <p>
  436.      * This class is part of the <b>ReflectionUtils</b> and follows the same usage conditions
  437.      *
  438.      * @author DarkBlade12
  439.      * @since 1.0
  440.      */
  441.     public enum DataType {
  442.         BYTE(byte.class, Byte.class),
  443.         SHORT(short.class, Short.class),
  444.         INTEGER(int.class, Integer.class),
  445.         LONG(long.class, Long.class),
  446.         CHARACTER(char.class, Character.class),
  447.         FLOAT(float.class, Float.class),
  448.         DOUBLE(double.class, Double.class),
  449.         BOOLEAN(boolean.class, Boolean.class);
  450.  
  451.         private static final Map<Class<?>, DataType> CLASS_MAP = new HashMap<Class<?>, DataType>();
  452.         private final Class<?> primitive;
  453.         private final Class<?> reference;
  454.  
  455.         // Initialize map for quick class lookup
  456.         static {
  457.             for (DataType type : values()) {
  458.                 CLASS_MAP.put(type.primitive, type);
  459.                 CLASS_MAP.put(type.reference, type);
  460.             }
  461.         }
  462.  
  463.         /**
  464.          * Construct a new data type
  465.          *
  466.          * @param primitive Primitive class of this data type
  467.          * @param reference Reference class of this data type
  468.          */
  469.         private DataType(Class<?> primitive, Class<?> reference) {
  470.             this.primitive = primitive;
  471.             this.reference = reference;
  472.         }
  473.  
  474.         /**
  475.          * Returns the primitive class of this data type
  476.          *
  477.          * @return The primitive class
  478.          */
  479.         public Class<?> getPrimitive() {
  480.             return primitive;
  481.         }
  482.  
  483.         /**
  484.          * Returns the reference class of this data type
  485.          *
  486.          * @return The reference class
  487.          */
  488.         public Class<?> getReference() {
  489.             return reference;
  490.         }
  491.  
  492.         /**
  493.          * Returns the data type with the given primitive/reference class
  494.          *
  495.          * @param clazz Primitive/Reference class of the data type
  496.          * @return The data type
  497.          */
  498.         public static DataType fromClass(Class<?> clazz) {
  499.             return CLASS_MAP.get(clazz);
  500.         }
  501.  
  502.         /**
  503.          * Returns the primitive class of the data type with the given reference class
  504.          *
  505.          * @param clazz Reference class of the data type
  506.          * @return The primitive class
  507.          */
  508.         public static Class<?> getPrimitive(Class<?> clazz) {
  509.             DataType type = fromClass(clazz);
  510.             return type == null ? clazz : type.getPrimitive();
  511.         }
  512.  
  513.         /**
  514.          * Returns the reference class of the data type with the given primitive class
  515.          *
  516.          * @param clazz Primitive class of the data type
  517.          * @return The reference class
  518.          */
  519.         public static Class<?> getReference(Class<?> clazz) {
  520.             DataType type = fromClass(clazz);
  521.             return type == null ? clazz : type.getReference();
  522.         }
  523.  
  524.         /**
  525.          * Returns the primitive class array of the given class array
  526.          *
  527.          * @param classes Given class array
  528.          * @return The primitive class array
  529.          */
  530.         public static Class<?>[] getPrimitive(Class<?>[] classes) {
  531.             int length = classes == null ? 0 : classes.length;
  532.             Class<?>[] types = new Class<?>[length];
  533.             for (int index = 0; index < length; index++) {
  534.                 types[index] = getPrimitive(classes[index]);
  535.             }
  536.             return types;
  537.         }
  538.  
  539.         /**
  540.          * Returns the reference class array of the given class array
  541.          *
  542.          * @param classes Given class array
  543.          * @return The reference class array
  544.          */
  545.         public static Class<?>[] getReference(Class<?>[] classes) {
  546.             int length = classes == null ? 0 : classes.length;
  547.             Class<?>[] types = new Class<?>[length];
  548.             for (int index = 0; index < length; index++) {
  549.                 types[index] = getReference(classes[index]);
  550.             }
  551.             return types;
  552.         }
  553.  
  554.         /**
  555.          * Returns the primitive class array of the given object array
  556.          *
  557.          * @param object Given object array
  558.          * @return The primitive class array
  559.          */
  560.         public static Class<?>[] getPrimitive(Object[] objects) {
  561.             int length = objects == null ? 0 : objects.length;
  562.             Class<?>[] types = new Class<?>[length];
  563.             for (int index = 0; index < length; index++) {
  564.                 types[index] = getPrimitive(objects[index].getClass());
  565.             }
  566.             return types;
  567.         }
  568.  
  569.         /**
  570.          * Returns the reference class array of the given object array
  571.          *
  572.          * @param object Given object array
  573.          * @return The reference class array
  574.          */
  575.         public static Class<?>[] getReference(Object[] objects) {
  576.             int length = objects == null ? 0 : objects.length;
  577.             Class<?>[] types = new Class<?>[length];
  578.             for (int index = 0; index < length; index++) {
  579.                 types[index] = getReference(objects[index].getClass());
  580.             }
  581.             return types;
  582.         }
  583.  
  584.         /**
  585.          * Compares two class arrays on equivalence
  586.          *
  587.          * @param primary Primary class array
  588.          * @param secondary Class array which is compared to the primary array
  589.          * @return Whether these arrays are equal or not
  590.          */
  591.         public static boolean compare(Class<?>[] primary, Class<?>[] secondary) {
  592.             if (primary == null || secondary == null || primary.length != secondary.length) {
  593.                 return false;
  594.             }
  595.             for (int index = 0; index < primary.length; index++) {
  596.                 Class<?> primaryClass = primary[index];
  597.                 Class<?> secondaryClass = secondary[index];
  598.                 if (primaryClass.equals(secondaryClass) || primaryClass.isAssignableFrom(secondaryClass)) {
  599.                     continue;
  600.                 }
  601.                 return false;
  602.             }
  603.             return true;
  604.         }
  605.     }
  606. }
Add Comment
Please, Sign In to add comment