#Help with reflection

1 messages · Page 1 of 1 (latest)

quartz harbor
#
ClassLoader cl = Thread.currentThread().getContextClassLoader();
String path = pkg.getName().replace('.', '/');
Enumeration<URL> resources = cl.getResources(path);
List<File> dirs = new ArrayList<>();
while(resources.hasMoreElements()) {
    URL url = resources.nextElement();
    dirs.add(new File(url.getFile()));
}

pkg is a parameter of type Package, however, this seems to not get ANY files, why's that?

smoky crest
#

You need to search files from inside the jar resource

quartz harbor
#

hm?

#

Also fyi this is how it's called

smoky crest
#

Ie you need to open the jar as some type of zip input or input stream using the ClassLoader you have

#

I can show you my method when im home

#

Cause it takes a little bit of work

#

In fact I didn't even write mine lmao its from stack overflow with a few modifications

quartz harbor
#

lol

smoky crest
#

Yeah that answer never worked for me

#

I went through about 20 stack overflow posts

#

Before finding one that works

quartz harbor
#

that one?

smoky crest
quartz harbor
#

tyvm

#

Error
java.lang.NullPointerException: Cannot invoke "java.net.URL.getFile()" because "root" is null

Code
URL root = Thread.currentThread().getContextClassLoader().getResource(packageName.replace(".", "/"));

Path (Before replace)
me.ikevoodoo.testplugin

smoky crest
#

sec

#

this was one of the modifications i used

#

did*

#

I changed the class loader in that method

#

to the main class file of the running plugin

quartz harbor
#

LMAO

#

I implemented that

#

then scrapped it

smoky crest
smoky crest
quartz harbor
#

This is what I implemented

smoky crest
quartz harbor
#

yeah ik

#

FYI

#

you can replace it with getClassLoader() if you are in a non static method

#

no need for getClass()

smoky crest
#

thats a static referance

#

hence why it needed getClass 😛

quartz harbor
#

oh wait

#

nvm

#

getClassLoader returns the spigot class loader

smoky crest
#

i mean

#

that might work too?

quartz harbor
#

Ill just stick with getClass

smoky crest
#

but youll scan waaay more files than you need to

#

ALSO

#

this is important

#

cache the files from the package your scanning

#

thats a pretty intensive method to call tbf

quartz harbor
#

its called once at startup for every plugin implementing this interface

#

so 1 plugin = 1 run

#

no need to cache, because I don't reuse it again

smoky crest
#

fair enough, i saw you had something for commands too thats why i said that

quartz harbor
#

Yeah the two separate calls are temp so see if they all work

#

it will be replaced with a single VarArgs

#

so I can run multiple stuff at once lmao

quartz harbor
#

lets go

#

@smoky crest I found a problem

#

You need to use getDeclaredConstructor

#

wait nothin

#

nvm

smoky crest
#

You can provide dependency injection for classes too with the correct constructor btw

quartz harbor
#

Yeah I am doing that

#

I just make stuff like subcommands protected so they don't get instantiated by the auto register

#

which is neat

#

LETS GO