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

David Kerekes April 2, 2013

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 votes
Answer accepted
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 2, 2013

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.

David Kerekes April 2, 2013

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

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 2, 2013

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

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 2, 2013

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

David Kerekes April 7, 2013

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?

David Kerekes April 7, 2013

Thanks Jamie, the method you suggested works (as described here: https://answers.atlassian.com/questions/113898/use-of-greenhopper-classes-in-groovy-script-classes)

Suggest an answer

Log in or Sign up to answer