MCPcopy Create free account
hub / github.com/boxbeam/RedLib / getExtendingClasses

Method getExtendingClasses

src/redempt/redlib/RedLib.java:114–145  ·  view source on GitHub ↗

Gets all non-abstract, non-interface classes which extend a certain class within a plugin @param plugin The plugin @param clazz The class @param The type of the class @return The list of matching classes

(Plugin plugin, Class<T> clazz)

Source from the content-addressed store, hash-verified

112 * @return The list of matching classes
113 */
114 public static <T> List<Class<? extends T>> getExtendingClasses(Plugin plugin, Class<T> clazz) {
115 List<Class<? extends T>> list = new ArrayList<>();
116 try {
117 ClassLoader loader = plugin.getClass().getClassLoader();
118 JarFile file = new JarFile(new File(plugin.getClass().getProtectionDomain().getCodeSource().getLocation().toURI()));
119 Enumeration<JarEntry> entries = file.entries();
120 while (entries.hasMoreElements()) {
121 JarEntry entry = entries.nextElement();
122 if (entry.isDirectory()) {
123 continue;
124 }
125 String name = entry.getName();
126 if (!name.endsWith(".class")) {
127 continue;
128 }
129 name = name.substring(0, name.length() - 6).replace("/", ".");
130 Class<?> c;
131 try {
132 c = Class.forName(name, true, loader);
133 } catch (ClassNotFoundException | NoClassDefFoundError ex) {
134 continue;
135 }
136 if (!clazz.isAssignableFrom(c) || Modifier.isAbstract(c.getModifiers()) || c.isInterface()) {
137 continue;
138 }
139 list.add((Class<? extends T>) c);
140 }
141 } catch (Exception e) {
142 throw new IllegalStateException(e);
143 }
144 return list;
145 }
146
147}

Callers 2

registerAllMethod · 0.95
getAllMethod · 0.95

Calls 5

getClassMethod · 0.80
getLocationMethod · 0.45
getNameMethod · 0.45
lengthMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected