1

I am writing plugin for nexus oss. I am having the privilege to call my class by any name while creating plugin. I am new to java so forgive me if my question is weird.

If name can be anything, how are they instantiating my class?

In my plugin the class starts like:

public class HelloWorldPlexusResource
    extends AbstractPlexusResource
    implements PlexusResource

Even when I was writing plugin for jenkins I had the same doubt. the class started just like:

public class HelloWorldBuilder extends Builder

Example: jenkins plugin

Again how will they call my class in runtime when my class name can be anything? Is the system scanning for classes that extends a particular class? if so how is it done? But this too needs instantiation right? (not sure).

If possible please also direct me some java reading on this.

Magnilex
  • 11,584
  • 9
  • 62
  • 84
Harish Kayarohanam
  • 3,886
  • 4
  • 31
  • 55
  • 1
    What you are looking for is called `Reflections`. Have a look at http://en.wikipedia.org/wiki/Reflection_(computer_programming) Of course you need some kind of convention (e.g. your class must have a default constructor) then it is very easy to instantiate your class during runtime (e.g. scan a specific folder, load all classes and instantiate each class once). On details I suggest reading the Plugin Documentation. They should provide such information. – zip Aug 22 '13 at 07:25
  • @zip Thanks . Yes I went through tutorials for for reflection and also this particular link https://code.google.com/p/reflections/ where they get all classes in a package but also saw this [reflcetion](http://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection) where they say "no" . after learning reflection i am also with the same question why its not possible – Harish Kayarohanam Aug 26 '13 at 07:32
  • if you only use build-in Java features, it may be possible not to load all classes from your package (e.g. your application is packaged in a jar-archive). With jenkins that is different. Jenkins is a Webarchive (war-file), which is unpacked before start. Therefore you can load all classes/resources in a package (which is basically a directory). It is also possible with a 3rd part library as you mentioned already. And as soon as you have the class name instantiation is very easy: `Class.forName("com.stackoverflow.examples.MyClass").newInstance();` – zip Aug 26 '13 at 08:21

1 Answers1

0

I cant give you a concrete answer, but in other such plugin supporting system, all valid files within say a plugins/ folder are essentially loaded as plugins.

And since it is Java, I guess filenname == main class name

Karthik T
  • 31,456
  • 5
  • 68
  • 87