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 { it.name.endsWith(".jar") }
		jars.each {
			def url = it.toURI().toURL()
			urlList.add(url)
		}
		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 Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

351 views 1 18
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