mardi 3 février 2015

Java - Force class to declare a static method or a static field

I know this question has been asked a few times now, but i have a special case.


I have a class called JavaPlugin, a class called PluginLoader and an interface called InitializeAble. The PluginLoader has a Method which loads all classes, which extend from JavaPlugin and are contained in the JarFiles, which are contained in the File/Folder, which you give the loadingmethod as parameter.


So after calling the loadmethod, you have a Set< Class< ? extends JavaPlugin>>.

Also the class PluginLoader has the method initializePlugins(), which runs through the Set and checks for every class if it implements InitializeAble. If the class implements the interface, a new instance of the class shall be created. And thats the point where i fail.

I would like to have a static method or a static field which has a method, which returns the JavaPlugin-instance i want to create



  • I cant call any constructor because the person who created the class, which extends from JavaPlugin, might overwrote the default one

  • I cant force the class to implement a static method or field of an interface

  • I cant make an Annotation, which forces a method to be static and to return a JavaPlugin

  • I dont want to define a method in JavaPlugin which must be overwritten, because i want to be able to make clear through an instance check, if the JavaPlugin is supposed to be initialized or not. For this i will check if PluginClass implements InitializeAble

  • I want the creator of the class, which extends JavaPlugin, to be able to write his own constructor, so he can decide what happens while the object is going to be created


As you can see, i tried it with constructor, annotation and interface. But i couldnt figure out a way which could work. The perfect thing would be an interface, which forces the class to declare public/protected/private static JavaPlugin initialize(). This method should be overwritten like initialize(){ return new CustomPluginClass(...); }


A public/protected/private static Field would also be okay. I would create an interface for this field which has a public method which returns a JavaPlugin. This way i could get the field, call its method and get the instance. The interface "Serializable" does something like that with a long field, but i think thats a special thing and it is not possible to create something like this on your own.


My last idea was to let the creator of the class, which extends JavaPlugin, the choice, which method in his pluginclass shall be his "constructor" by marking it with the annotation @PluginConstructor. But if i do so, i would have to make sure, that the marked method must return a JavaPlugin and must be static, because i can't call a not-static method without an instance. I am not sure if something like that is possible, because i dont know much about annotations. Also there should be just 1 of the @PluginConstructor annotations per class allowed


If the costumpluginclass does not implement InitializeAble or it has no PluginConstructor annotation or whatever... if the plugin is not made to be initialized, it wont be. i would like to be able to check this with an instance-check or an annotation. Thats why i dont want to make a method directly in JavaPlugin which can be overwritten


If there is another solution i would accept it, too. As long as i get this problem solved. I have no idea anymore how i could do this. Hope you can help me


thanks, basti

ps: yes, i looked over all similar questions and no, those didnt help me and sorry for bad english :P






Aucun commentaire:

Enregistrer un commentaire