How to get a loaded class's instance from ComponentAccessor?

I have a problem where I try to access my plugin component from Groovy-script.

As far as I know the Groovy Script Runner can't load custom classes without a workaround:

File jiraHome = ComponentAccessor.getComponent(JiraHome.class).getHome();
		def jardir = new File(jiraHome,"\\plugins\\installed-plugins\\" );
		def urlList = []
		def jars  = jardir.listFiles().findAll {".jar") }
		jars.each {
			def url = it.toURI().toURL()
		def urls = (URL[]) urlList.toArray()
		def loader = new RootLoader( urls, this.class.classLoader);

I use this to get a Class object with the Class.forName() method, but when I try to get a managed instance with ComponentAccessor.getComponentOfType(myClass), it returns a null value (I suspect something with the JVM not knowing the two classes are the same is the problem).

Does anyone knows a workaround for this? (besides adding the class to the groovy runner's POM)

1 answer

1 accepted

0 vote

ComponentAccessor.getComponentOfType(myClass) should work fine - is your component defined as a public <component> in the plugin xml? It works for other types of components from other plugins so should work fine for yours.

If you just want a class from another plugin that's not a component you should use: PluginAccessor.getClassLoader().findClass(str).

Your method is going to end badly... there is lots of problems with it.

Jamie you wrote: "If you just want a class from another plugin that's not a component you should use..." How should I get a class of a component then?

my plugin component has public="true" in the projet.xml

As I said: ComponentAccessor.getComponentOfType(myClass) should work fine

Sorry I normally use ComponentAccessor.getComponent()... try that.

Sorry, it looks like you misunderstood me. I was asking about how to get the Class for my component, since importing it doesn't work.
Should I use the PluginAccessor.getClassLoader().findClass("com.mypackage.MyPluginComp") function to get a Class object?
Or should importing it work if I set up the plugin correctly?

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,181 views 13 19
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot