tterrag1098

Untitled

Jan 1st, 2019
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1.   @SuppressWarnings("unchecked")
  2.   private IForgeRegistry findRegistry(Type type) {
  3.     Class<?> cls = TypeUtil.toClass(type);
  4.     return registryCache.computeIfAbsent(cls, $ -> {
  5.       Iterator<@NonnullType Type> iter = walkHierarchy(type);
  6.       while (iter.hasNext()) {
  7.         Type t = iter.next();
  8.         Class<?> c = TypeUtil.toClass(t);
  9.         if (c == IForgeRegistryEntry.class || c == IForgeRegistryEntry.Impl.class) {
  10.           Type[] types = ((ParameterizedType)t).getActualTypeArguments();
  11.           return GameRegistry.findRegistry((Class) TypeUtil.toClass(types[0]));
  12.         }
  13.       }
  14.       throw new IllegalArgumentException("Not a registry object subtype");
  15.     });
  16.   }
  17.  
  18.   private Iterator<Type> walkHierarchy(Type cls) {
  19.     return new Iterator<Type>() {
  20.  
  21.       Set<Class<?>>                 seenInterfaces = new HashSet<>();
  22.       ArrayDeque<@NonnullType Type> queue          = new ArrayDeque<>();
  23.       {
  24.         queue.add(cls);
  25.       }
  26.  
  27.       @Override
  28.       public boolean hasNext() {
  29.         return !queue.isEmpty();
  30.       }
  31.  
  32.       @Override
  33.       public Type next() {
  34.         if (hasNext()) {
  35.           Type ret = queue.pop();
  36.           Class<?> cls = TypeUtil.toClass(ret);
  37.           if (cls != Object.class && !cls.isInterface()) {
  38.             queue.push(cls.getGenericSuperclass());
  39.             for (Type itf : cls.getGenericInterfaces()) {
  40.               if (seenInterfaces.add(TypeUtil.toClass(itf))) {
  41.                 queue.push(itf);
  42.               }
  43.             }
  44.           }
  45.           return ret;
  46.         }
  47.         throw new NoSuchElementException();
  48.       }
  49.     };
  50.   }
Advertisement
Add Comment
Please, Sign In to add comment