package lombok.core; import com.adjust.sdk.Constants; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.annotation.Annotation; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.net.URL; import java.util.ArrayList; import java.util.Collection; import java.util.Enumeration; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; /* loaded from: com.discord-118108.apk:lombok/core/SpiLoadUtil.SCL.lombok */ public class SpiLoadUtil { /* renamed from: lombok.core.SpiLoadUtil$1 reason: invalid class name */ /* loaded from: com.discord-118108.apk:lombok/core/SpiLoadUtil$1.SCL.lombok */ class AnonymousClass1 implements Iterable { private final /* synthetic */ Iterator val$names; private final /* synthetic */ Class val$target; private final /* synthetic */ ClassLoader val$fLoader; /* renamed from: lombok.core.SpiLoadUtil$1$1 reason: invalid class name and collision with other inner class name */ /* loaded from: com.discord-118108.apk:lombok/core/SpiLoadUtil$1$1.SCL.lombok */ class C04141 implements Iterator { private final /* synthetic */ Iterator val$names; private final /* synthetic */ Class val$target; private final /* synthetic */ ClassLoader val$fLoader; C04141(Iterator it, Class cls, ClassLoader classLoader) { this.val$names = it; this.val$target = cls; this.val$fLoader = classLoader; } @Override // java.util.Iterator public boolean hasNext() { return this.val$names.hasNext(); } @Override // java.util.Iterator public C next() { try { return this.val$target.cast(Class.forName((String) this.val$names.next(), true, this.val$fLoader).getConstructor(new Class[0]).newInstance(new Object[0])); } catch (Exception e) { e = e; if (e instanceof InvocationTargetException) { e = e.getCause(); } if (e instanceof RuntimeException) { throw ((RuntimeException) e); } else if (e instanceof Error) { throw ((Error) e); } else { throw new RuntimeException(e); } } } @Override // java.util.Iterator public void remove() { throw new UnsupportedOperationException(); } } AnonymousClass1(Iterator it, Class cls, ClassLoader classLoader) { this.val$names = it; this.val$target = cls; this.val$fLoader = classLoader; } @Override // java.lang.Iterable public Iterator iterator() { return new C04141(this.val$names, this.val$target, this.val$fLoader); } } private SpiLoadUtil() { } public static List readAllFromIterator(Iterable iterable) { ArrayList arrayList = new ArrayList(); Iterator it = iterable.iterator(); while (it.hasNext()) { arrayList.add(it.next()); } return arrayList; } public static Iterable findServices(Class cls) throws IOException { return findServices(cls, Thread.currentThread().getContextClassLoader()); } public static Iterable findServices(Class cls, ClassLoader classLoader) throws IOException { if (classLoader == null) { classLoader = ClassLoader.getSystemClassLoader(); } Enumeration resources = classLoader.getResources("META-INF/services/" + cls.getName()); LinkedHashSet linkedHashSet = new LinkedHashSet(); while (resources.hasMoreElements()) { readServicesFromUrl(linkedHashSet, resources.nextElement()); } return new AnonymousClass1(linkedHashSet.iterator(), cls, classLoader); } private static void readServicesFromUrl(Collection collection, URL url) throws IOException { InputStream openStream = url.openStream(); BufferedReader bufferedReader = null; if (openStream == null) { if (0 != 0) { try { bufferedReader.close(); } catch (Throwable unused) { return; } } if (openStream != null) { openStream.close(); return; } return; } try { bufferedReader = new BufferedReader(new InputStreamReader(openStream, Constants.ENCODING)); while (true) { String readLine = bufferedReader.readLine(); if (readLine == null) { break; } int indexOf = readLine.indexOf(35); if (indexOf != -1) { readLine = readLine.substring(0, indexOf); } String trim = readLine.trim(); if (trim.length() != 0) { collection.add(trim); } } if (bufferedReader != null) { try { bufferedReader.close(); } catch (Throwable unused2) { return; } } if (openStream != null) { openStream.close(); } } catch (Throwable th) { if (bufferedReader != null) { try { bufferedReader.close(); } catch (Throwable unused3) { throw th; } } if (openStream != null) { openStream.close(); } throw th; } } public static Class findAnnotationClass(Class cls, Class cls2) { if (cls == Object.class || cls == null) { return null; } Class findAnnotationHelper = findAnnotationHelper(cls2, cls.getGenericSuperclass()); if (findAnnotationHelper != null) { return findAnnotationHelper; } for (Type type : cls.getGenericInterfaces()) { Class findAnnotationHelper2 = findAnnotationHelper(cls2, type); if (findAnnotationHelper2 != null) { return findAnnotationHelper2; } } Class findAnnotationClass = findAnnotationClass(cls.getSuperclass(), cls2); if (findAnnotationClass != null) { return findAnnotationClass; } for (Class cls3 : cls.getInterfaces()) { Class findAnnotationClass2 = findAnnotationClass(cls3, cls2); if (findAnnotationClass2 != null) { return findAnnotationClass2; } } return null; } private static Class findAnnotationHelper(Class cls, Type type) { if (!(type instanceof ParameterizedType)) { return null; } ParameterizedType parameterizedType = (ParameterizedType) type; if (!cls.equals(parameterizedType.getRawType())) { return null; } Type type2 = parameterizedType.getActualTypeArguments()[0]; if ((type2 instanceof Class) && Annotation.class.isAssignableFrom((Class) type2)) { return (Class) type2; } throw new ClassCastException("Not an annotation type: " + type2); } }