SHARE
TWEET

EnumUtil.java

ie9 May 10th, 2019 (edited) 27 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Copyright 2019 Ilya Egorov <ie9@null.net>. All rights reserved.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16.  
  17. package li.cock.ie.enumerate;
  18.  
  19. import li.cock.ie.reflect.*;
  20. import java.lang.reflect.*;
  21. import java.util.*;
  22.  
  23. public class EnumUtil {
  24.     protected static boolean isSetup = false;
  25.     private static Map<Class<? extends Enum<?>>, EnumList> enums;
  26.     private static DuckBypass bypass;
  27.     private static EnumHacker hack;
  28.  
  29.     protected static Class<?>[][] commonTypes = new Class<?>[][]{};
  30.     public static void setCommonTypes(Class<?>[][] commonTypes) {
  31.         EnumUtil.commonTypes = commonTypes;
  32.     }
  33.  
  34.     static {
  35.         if (!isSetup) {
  36.             setup();
  37.         }
  38.     }
  39.  
  40.     protected static void setup() {
  41.         if(isSetup) {
  42.             return;
  43.         }
  44.  
  45.         enums = new HashMap<Class<? extends Enum<?>>, EnumList>();
  46.         bypass = new DuckBypass();
  47.         hack = new EnumHacker(bypass);
  48.  
  49.         isSetup = true;
  50.     }
  51.  
  52.     public static void setFailsafeFieldValue(Field field, Object target, Object value) {
  53.         bypass.setValue(field, target, value);
  54.     }
  55.  
  56.     public static <E extends Enum<? >> E addEnum(Class<?>[][] map, Class<E> enumType, String enumName, Object... paramValues) {
  57.         for (Class<?>[] lookup : map) {
  58.             if (lookup[0] == enumType) {
  59.                 Class<?>[] paramTypes = new Class<?>[lookup.length - 1];
  60.                 if (paramTypes.length > 0) {
  61.                     System.arraycopy(lookup, 1, paramTypes, 0, paramTypes.length);
  62.                 }
  63.  
  64.                 return addEnum(enumType, enumName, paramTypes, paramValues);
  65.             }
  66.         }
  67.  
  68.         return null;
  69.     }
  70.  
  71.     public static void testEnum(Class<? extends Enum<?>> enumType, Class<?>[] paramTypes) {
  72.         addEnum(true, enumType, null, paramTypes, (Object[])null);
  73.     }
  74.  
  75.     public static <E extends Enum<?>> E addEnum(Class<E> enumType, String enumName, Object... paramValues) {
  76.         return addEnum(commonTypes, enumType, enumName, paramValues);
  77.     }
  78.  
  79.     public static <E extends Enum<?>> E addEnum(Class<E> enumType, String enumName, Class<?>[] paramTypes, Object... paramValues) {
  80.         return addEnum(false, enumType, enumName, paramTypes, paramValues);
  81.     }
  82.  
  83.     @SuppressWarnings("unchecked")
  84.     public static <E extends Enum<?>> E addEnum(boolean test, final Class<E> enumType, String enumName, final Class<?>[] paramTypes, Object[] paramValues) {
  85.         if (!isSetup) {
  86.             setup();
  87.         }
  88.  
  89.         if(!enums.containsKey(enumType)) {
  90.             enums.put(enumType, new EnumList(hack, enumType));
  91.         }
  92.  
  93.         EnumList editor = enums.get(enumType);
  94.         return (E) editor.add(enumName, paramTypes, paramValues);
  95.     }
  96. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top