Fwd: GroovyCompilationUnit Question

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Fwd: GroovyCompilationUnit Question

Jonathan Ozik
A colleague has not been able to successfully subscribe to the mailing list so I'm forwarding the question below. (Are others experiencing subscription difficulties as well?)

Thank you,

Jonathan

Begin forwarded message:

From: Nick Collier <[hidden email]>
Subject: GroovyCompilationUnit Question
Date: January 18, 2013 9:17:26 AM EST

Hi,

We are developing an eclipse plugin that can modify a .groovy file. We get the user's file selection as an IJavaElement with this:

IJavaElement jelem = null;
 if (selection != null && !selection.isEmpty()) {
   Object selectedElement = selection.getFirstElement();
   if (selectedElement instanceof IAdaptable) {
     IAdaptable adaptable = (IAdaptable) selectedElement;
     jelem = (IJavaElement) adaptable.getAdapter(IJavaElement.class);
   }
 }

and that works fine. The IJavaElement instance is a GroovyCompilationUnit. However, when we try to add an import to that, we get an NPE.

jelem.createImport(modelPropsPage.getPackage() + "." + modelPropsPage.getClassName(),
           null, monitor);

java.lang.NullPointerException
at org.eclipse.jdt.internal.core.CreateElementInCUOperation.insertASTNode(CreateElementInCUOperation.java:246)
at org.eclipse.jdt.internal.core.CreateElementInCUOperation.generateNewCompilationUnitAST(CreateElementInCUOperation.java:173)
at org.eclipse.jdt.internal.core.CreateElementInCUOperation.executeOperation(CreateElementInCUOperation.java:127)
at org.eclipse.jdt.internal.core.JavaModelOperation.run(JavaModelOperation.java:728)
at org.eclipse.core.internal.resources.Workspace.run(Workspace.java:2344)
at org.eclipse.jdt.internal.core.JavaModelOperation.runOperation(JavaModelOperation.java:793)
at org.eclipse.jdt.internal.core.CompilationUnit.createImport(CompilationUnit.java:432)
at org.eclipse.jdt.internal.core.CompilationUnit.createImport(CompilationUnit.java:420)


This works fine when we select a .java file. Are we doing something wrong here, or is this just not possible with a GroovyCompilationUnit.

thanks,

Nick


Reply | Threaded
Open this post in threaded view
|

Re: Fwd: GroovyCompilationUnit Question

Andrew Eisenberg
This is not possible with groovy files since we don't implement all of
the underlying JDT apis.  The GroovyCompilationUnit is mostly a
wrapper around a groovy ModuleNode.

I would recommend that you take a look at
org.codehaus.groovy.eclipse.refactoring.actions.OrganizeGroovyImports
and how it uses
greclipse.org.eclipse.jdt.core.dom.rewrite.ImportRewrite.  This is the
most straightforward way to augment the imports.



On Mon, Jan 21, 2013 at 8:59 AM, Jonathan Ozik <[hidden email]> wrote:

> A colleague has not been able to successfully subscribe to the mailing list
> so I'm forwarding the question below. (Are others experiencing subscription
> difficulties as well?)
>
> Thank you,
>
> Jonathan
>
> Begin forwarded message:
>
> From: Nick Collier <[hidden email]>
> Subject: GroovyCompilationUnit Question
> Date: January 18, 2013 9:17:26 AM EST
> To: [hidden email]
>
> Hi,
>
> We are developing an eclipse plugin that can modify a .groovy file. We get
> the user's file selection as an IJavaElement with this:
>
> IJavaElement jelem = null;
>  if (selection != null && !selection.isEmpty()) {
>    Object selectedElement = selection.getFirstElement();
>    if (selectedElement instanceof IAdaptable) {
>      IAdaptable adaptable = (IAdaptable) selectedElement;
>      jelem = (IJavaElement) adaptable.getAdapter(IJavaElement.class);
>    }
>  }
>
> and that works fine. The IJavaElement instance is a GroovyCompilationUnit.
> However, when we try to add an import to that, we get an NPE.
>
> jelem.createImport(modelPropsPage.getPackage() + "." +
> modelPropsPage.getClassName(),
>            null, monitor);
>
> java.lang.NullPointerException
> at
> org.eclipse.jdt.internal.core.CreateElementInCUOperation.insertASTNode(CreateElementInCUOperation.java:246)
> at
> org.eclipse.jdt.internal.core.CreateElementInCUOperation.generateNewCompilationUnitAST(CreateElementInCUOperation.java:173)
> at
> org.eclipse.jdt.internal.core.CreateElementInCUOperation.executeOperation(CreateElementInCUOperation.java:127)
> at
> org.eclipse.jdt.internal.core.JavaModelOperation.run(JavaModelOperation.java:728)
> at org.eclipse.core.internal.resources.Workspace.run(Workspace.java:2344)
> at
> org.eclipse.jdt.internal.core.JavaModelOperation.runOperation(JavaModelOperation.java:793)
> at
> org.eclipse.jdt.internal.core.CompilationUnit.createImport(CompilationUnit.java:432)
> at
> org.eclipse.jdt.internal.core.CompilationUnit.createImport(CompilationUnit.java:420)
>
>
> This works fine when we select a .java file. Are we doing something wrong
> here, or is this just not possible with a GroovyCompilationUnit.
>
> thanks,
>
> Nick
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: GroovyCompilationUnit Question

Jonathan Ozik
Thanks, that worked great for the imports. For injecting fields, the standard method of using the ICompilationUnit seems to have worked.

Jonathan

On Jan 21, 2013, at 11:10 AM, Andrew Eisenberg <[hidden email]> wrote:

> This is not possible with groovy files since we don't implement all of
> the underlying JDT apis.  The GroovyCompilationUnit is mostly a
> wrapper around a groovy ModuleNode.
>
> I would recommend that you take a look at
> org.codehaus.groovy.eclipse.refactoring.actions.OrganizeGroovyImports
> and how it uses
> greclipse.org.eclipse.jdt.core.dom.rewrite.ImportRewrite.  This is the
> most straightforward way to augment the imports.
>
>
>
> On Mon, Jan 21, 2013 at 8:59 AM, Jonathan Ozik <[hidden email]> wrote:
>> A colleague has not been able to successfully subscribe to the mailing list
>> so I'm forwarding the question below. (Are others experiencing subscription
>> difficulties as well?)
>>
>> Thank you,
>>
>> Jonathan
>>
>> Begin forwarded message:
>>
>> From: Nick Collier <[hidden email]>
>> Subject: GroovyCompilationUnit Question
>> Date: January 18, 2013 9:17:26 AM EST
>> To: [hidden email]
>>
>> Hi,
>>
>> We are developing an eclipse plugin that can modify a .groovy file. We get
>> the user's file selection as an IJavaElement with this:
>>
>> IJavaElement jelem = null;
>> if (selection != null && !selection.isEmpty()) {
>>   Object selectedElement = selection.getFirstElement();
>>   if (selectedElement instanceof IAdaptable) {
>>     IAdaptable adaptable = (IAdaptable) selectedElement;
>>     jelem = (IJavaElement) adaptable.getAdapter(IJavaElement.class);
>>   }
>> }
>>
>> and that works fine. The IJavaElement instance is a GroovyCompilationUnit.
>> However, when we try to add an import to that, we get an NPE.
>>
>> jelem.createImport(modelPropsPage.getPackage() + "." +
>> modelPropsPage.getClassName(),
>>           null, monitor);
>>
>> java.lang.NullPointerException
>> at
>> org.eclipse.jdt.internal.core.CreateElementInCUOperation.insertASTNode(CreateElementInCUOperation.java:246)
>> at
>> org.eclipse.jdt.internal.core.CreateElementInCUOperation.generateNewCompilationUnitAST(CreateElementInCUOperation.java:173)
>> at
>> org.eclipse.jdt.internal.core.CreateElementInCUOperation.executeOperation(CreateElementInCUOperation.java:127)
>> at
>> org.eclipse.jdt.internal.core.JavaModelOperation.run(JavaModelOperation.java:728)
>> at org.eclipse.core.internal.resources.Workspace.run(Workspace.java:2344)
>> at
>> org.eclipse.jdt.internal.core.JavaModelOperation.runOperation(JavaModelOperation.java:793)
>> at
>> org.eclipse.jdt.internal.core.CompilationUnit.createImport(CompilationUnit.java:432)
>> at
>> org.eclipse.jdt.internal.core.CompilationUnit.createImport(CompilationUnit.java:420)
>>
>>
>> This works fine when we select a .java file. Are we doing something wrong
>> here, or is this just not possible with a GroovyCompilationUnit.
>>
>> thanks,
>>
>> Nick
>>
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email