private void findSuperTypes(Class<?> type, Set<Class<?>> types) { if (type == Object.class) { return; } types.add(type); for (Class<?> intf : type.getInterfaces()) { findSuperTypes(intf, types); } findSuperTypes(type.getSuperclass(), types); }
private FMLControlledNamespacedRegistry<?> findRegistry(Class<?> type) { BiMap<Class<?>, String> typeReg = registryTypes.inverse(); String name = typeReg.get(type); if (name == null) { Set<Class<?>> parents = Sets.newHashSet(); findSuperTypes(type, parents); SetView<Class<?>> foundType = Sets.intersection(parents, registryTypes.values()); if (foundType.isEmpty()) { FMLLog.severe("Unable to find registry for type %s", type.getName()); throw new IllegalArgumentException( "Attempt to register an object without an associated registry"); } Class<?> regtype = Iterables.getOnlyElement(foundType); name = typeReg.get(regtype); } return genericRegistries.get(name); }
@SuppressWarnings("unchecked") private <T> FMLControlledNamespacedRegistry<T> createGenericRegistry( String registryName, Class<T> type, int minId, int maxId) { Set<Class<?>> parents = Sets.newHashSet(); findSuperTypes(type, parents); SetView<Class<?>> overlappedTypes = Sets.intersection(parents, registryTypes.values()); if (!overlappedTypes.isEmpty()) { Class<?> foundType = overlappedTypes.iterator().next(); FMLLog.severe( "Found existing registry of type %1s named %2s, you cannot create a new registry (%3s) with type %4s, as %4s has a parent of that type", foundType, registryTypes.inverse().get(foundType), registryName, type); throw new IllegalArgumentException( "Duplicate registry parent type found - you can only have one registry for a particular super type"); } FMLControlledNamespacedRegistry<?> fmlControlledNamespacedRegistry = new FMLControlledNamespacedRegistry<T>(null, maxId, minId, type); genericRegistries.put(registryName, fmlControlledNamespacedRegistry); registryTypes.put(registryName, type); return (FMLControlledNamespacedRegistry<T>) fmlControlledNamespacedRegistry; }