Jump to content

Using Reflections doesn't work outside deobfuscated environments


MostafaSabry55

Recommended Posts

I know this isn't forge's fault but I actually have no idea why this happens, so basically I'm trying to get all classes in a package that extends a class, I've used reflections8 and and older verison of org.reflections since it seems bugged, but this issue stayed as is in obfuscated environments, and only worked inside IDE, here's the code :

public static Set<Class<? extends PluginHelper>> getAllPlugins() {
		Reflections reflections = new Reflections("ms55.moreplates.common.plugin");

		return reflections.getSubTypesOf(PluginHelper.class);
	}

How do I make it obfuscation friendly?

Link to comment
Share on other sites

If you want to look at type heirarchy or annotation data, Forge has already searched the classpath for annotations and types for mod loading and event bus subscription. Here's what it would look like to use it for something:
 

public static Set<Class<? extends ParentClass>> init() {
  return ModList.get()
    .getAllScanData()
    .stream()
    .map(ModFileScanData::getClasses)
    .flatMap(Collection::stream)
    .filter(data -> "your.ParentClass".equals(data.parent.getClassName()))
    .map(Type::getClassName)
    .map(Class::forName)
    .map(ParentClass.class::asSubclass)
    .collect(Collectors.toSet());
}

This is very simplistic, because the `parent` field is private, so you would have to use reflection, and `Class.forName(String)` throws some things, so it can't be used in a method reference like that, but otherwise, this kind of thing has worked for me before.

Edited by Vinyarion

Have you ever want the new operator to return a type that you didn't ask for? Well, now you can!

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now


×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.