Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @SuppressWarnings("unchecked")
- private IForgeRegistry findRegistry(Type type) {
- Class<?> cls = TypeUtil.toClass(type);
- return registryCache.computeIfAbsent(cls, $ -> {
- Iterator<@NonnullType Type> iter = walkHierarchy(type);
- while (iter.hasNext()) {
- Type t = iter.next();
- Class<?> c = TypeUtil.toClass(t);
- if (c == IForgeRegistryEntry.class || c == IForgeRegistryEntry.Impl.class) {
- Type[] types = ((ParameterizedType)t).getActualTypeArguments();
- return GameRegistry.findRegistry((Class) TypeUtil.toClass(types[0]));
- }
- }
- throw new IllegalArgumentException("Not a registry object subtype");
- });
- }
- private Iterator<Type> walkHierarchy(Type cls) {
- return new Iterator<Type>() {
- Set<Class<?>> seenInterfaces = new HashSet<>();
- ArrayDeque<@NonnullType Type> queue = new ArrayDeque<>();
- {
- queue.add(cls);
- }
- @Override
- public boolean hasNext() {
- return !queue.isEmpty();
- }
- @Override
- public Type next() {
- if (hasNext()) {
- Type ret = queue.pop();
- Class<?> cls = TypeUtil.toClass(ret);
- if (cls != Object.class && !cls.isInterface()) {
- queue.push(cls.getGenericSuperclass());
- for (Type itf : cls.getGenericInterfaces()) {
- if (seenInterfaces.add(TypeUtil.toClass(itf))) {
- queue.push(itf);
- }
- }
- }
- return ret;
- }
- throw new NoSuchElementException();
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment