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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Wednesday in Jira

Join our webinar: How 1B+ feature flag events helped us build the new Jira

Every time you release software, there's a bit of risk – that there's a bug, that something breaks, or that the feature doesn't resonate with customers. Feature flagging helps make high stakes s...

111 views 0 3
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