This site requires JavaScript, please enable it in your browser!
Greenfoot back
MathManiac
MathManiac wrote ...

2012/8/18

[Help] Searching Classes

1
2
MathManiac MathManiac

2012/8/18

#
Hello, I want to have a class, when executed, search all the classes that implement an interface, then create an instance of it and run a command from it. What algorithm do I need to use in order for this to work, Or Is It Just a Fantasy? Thanks, - Math.
GregC246 GregC246

2012/8/18

#
1. You don't execute classes. 2. Create an instance of what? 3. What do you mean by "run a command from it"?
kiarocks kiarocks

2012/8/18

#
So, when the constructor of the class is called? If so, use this:
public ClassA()
{
    ArrayList<Interface> implements = new ArrayList<Interface>();
    //Use the method provided in the link
    for(Class c : <list of classes>)
    {
        if(Interface.class.isAssignableFrom(c))
            implements.add((Interface)c.newInstance());
    }
    for(Interface i : implements)
    {
        i.callMethod();
    }
}
Geting all classes in a package I think you need to call it with "" as the package, because Greenfoot keeps you in the default package.
kiarocks kiarocks

2012/8/18

#
Just tested, need another way.
MathManiac MathManiac

2012/8/18

#
@GregC246: 1. I meant when the method that does the stuff is executed. 2. An instance of the class 3. SomeSearchedClass.methodFromImplementedInterface(args);
SPower SPower

2012/8/18

#
With command in 3 he means to execute a method on it, but that was a Ready clear :)
kiarocks kiarocks

2012/8/30

#
Ok, I have made it! Note you currently need to download it. Just hit the button! InterfaceClassLoader
MathManiac MathManiac

2012/8/31

#
The thing is, I need it to be Online Compatable. All the stuff I saw searches through files, and might be blocked by certain Internet Browsers.
kiarocks kiarocks

2012/8/31

#
I'm working on that part
MathManiac MathManiac

2012/9/3

#
Builderboy said that he had tutorials on how he proccesses EventScripts, and it looks like it has Online Compatability. Maybe we should look at that.
nccb nccb

2012/9/3

#
Why do you want to be able to search through all classes like this from the files? The usual way to accomplish dynamic loading or plugins is that you explicitly list the classes you want, e.g.
Class<MyInterface> plugins = {Foo.class, Bar.class, Baz.class};
then use reflection from there. This explicit listing should work online, too.
MathManiac MathManiac

2012/9/3

#
I want to be able to make a Modifying API that allows people to do certain things at World initialization (or other events), but without modification of code. Instead, they would create classes that would automatically be searched.
kiarocks kiarocks

2012/9/3

#
I'm working on getting the list of classes so it works.
kiarocks kiarocks

2012/9/5

#
I just need to be able to get a list of classes. How can I do this?
danpost danpost

2012/9/5

#
I do not know if this can help, but can you not open the 'project' Greenfoot FILE for the classes listed? That would involve opening a file with a known location and name, and reading the data from the file (lines 2 through ?). I am not sure, but it probably shows what implements what somewhere else in the file, also.
There are more replies on the next page.
1
2