Implicit imports

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

Implicit imports

hans_d
Administrator
Gradle is adding a set of import statements to any gradlefile before  
it is evaluated. This saves the user typing something like  
org.gradle.api.X and she can just type X. In particular for non  
IntelliJ users this is an important feature ;)

Right now the set of import statements is just a String we add to the  
actual gradlefile text before we evaluate it. This has one big  
disadvantage. The line numbers are changed. In case of an exception  
we parse the stacktrace for line number information and add an offset  
to it before we report it. This is tedious and does not work when the  
line number is part of the exception message (e.g. Groovy's compile  
exception). I think AST transformations could do a much better job  
here. I don't have any experience here.

The relevant class is BuildScriptProcessor and here is the snippet  
where the evaluation takes place:

             buildScript = buildScriptWithImports(project)
             GroovyShell groovyShell = new GroovyShell(classLoader,  
binding, conf)
             Script script = groovyShell.parse(buildScript.text,  
project.buildScriptFinder.buildFileName)
             replaceMetaclass(script, project)
             project.projectScript = script
             script.run()

Of course I could start to do my own experiments or post to the  
Groovy list. But if someone has a clear idea of how to do this that  
would be a great time saver. And although this is a public mail I  
have a one person in mind for whom this might be very easy to do :)

- Hans

--
Hans Dockter
Gradle Project lead
http://www.gradle.org





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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Implicit imports

Peter Niederwieser
OK, give me a day. :-)

hdockter wrote
Of course I could start to do my own experiments or post to the  
Groovy list. But if someone has a clear idea of how to do this that  
would be a great time saver. And although this is a public mail I  
have a one person in mind for whom this might be very easy to do :)
Reply | Threaded
Open this post in threaded view
|

Re: Implicit imports

Peter Niederwieser
In reply to this post by hans_d
Before I continue on this, how about adding the imports at the end of the script? The following works for me:

def foo = new AtomicBoolean()

import java.util.concurrent.atomic.AtomicBoolean

Cheers,
Peter

Reply | Threaded
Open this post in threaded view
|

Re: Implicit imports

hans_d
Administrator
Hi Peter,

On May 15, 2008, at 1:05 AM, Peter Niederwieser wrote:

>
> Before I continue on this, how about adding the imports at the end  
> of the
> script? The following works for me:
>
> def foo = new AtomicBoolean()
>
> import java.util.concurrent.atomic.AtomicBoolean

Wonderful. What a nice simple solution. This works perfectly. The  
issue is solved.

Thanks a lot :)

- Hans

>
> Cheers,
> Peter
>
>
> --
> View this message in context: http://www.nabble.com/Implicit- 
> imports-tp17239611p17242854.html
> Sent from the gradle-dev mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>

--
Hans Dockter
Gradle Project lead
http://www.gradle.org





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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Implicit imports

Russel Winder-2
In reply to this post by hans_d
Hans,

On Wed, 2008-05-14 at 21:58 +0200, Hans Dockter wrote:

> Gradle is adding a set of import statements to any gradlefile before  
> it is evaluated. This saves the user typing something like  
> org.gradle.api.X and she can just type X. In particular for non  
> IntelliJ users this is an important feature ;)
>
> Right now the set of import statements is just a String we add to the  
> actual gradlefile text before we evaluate it. This has one big  
> disadvantage. The line numbers are changed. In case of an exception  
> we parse the stacktrace for line number information and add an offset  
> to it before we report it. This is tedious and does not work when the  
> line number is part of the exception message (e.g. Groovy's compile  
> exception). I think AST transformations could do a much better job  
> here. I don't have any experience here.
I came across the same problem with Gant -- not surprising as this is
idiomatic of what both Gant and Gradle are doing (and any other system
with a similar process).  If I remember correctly I added the set of
imports as a single semicolon separated statement sequence with no new
lines prepended to the beginning of the first line of the users script.
This means all the line numbers are correct but it has the disadvantage
that if there is an error in line 1 of the script then the user gets a
very bizarre character position for the error.  I took the view that
errors in line 1  of a script are generally rare.  To date no-one has
complained!

> The relevant class is BuildScriptProcessor and here is the snippet  
> where the evaluation takes place:
>
>              buildScript = buildScriptWithImports(project)
>              GroovyShell groovyShell = new GroovyShell(classLoader,  
> binding, conf)
>              Script script = groovyShell.parse(buildScript.text,  
> project.buildScriptFinder.buildFileName)
>              replaceMetaclass(script, project)
>              project.projectScript = script
>              script.run()
>
> Of course I could start to do my own experiments or post to the  
> Groovy list. But if someone has a clear idea of how to do this that  
> would be a great time saver. And although this is a public mail I  
> have a one person in mind for whom this might be very easy to do :)
>
> - Hans
--
Russel.
====================================================
Dr Russel Winder                 Partner

Concertant LLP                   t: +44 20 7585 2200, +44 20 7193 9203
41 Buckmaster Road,              f: +44 8700 516 084
London SW11 1EN, UK.             m: +44 7770 465 077

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Implicit imports

Russel Winder-2
In reply to this post by hans_d
Hans,

On Thu, 2008-05-15 at 06:51 +0200, Hans Dockter wrote:

> Hi Peter,
>
> On May 15, 2008, at 1:05 AM, Peter Niederwieser wrote:
>
> >
> > Before I continue on this, how about adding the imports at the end  
> > of the
> > script? The following works for me:
> >
> > def foo = new AtomicBoolean()
> >
> > import java.util.concurrent.atomic.AtomicBoolean
>
> Wonderful. What a nice simple solution. This works perfectly. The  
> issue is solved.
>
> Thanks a lot :)
OK, this is weird.  Is there an explanation of why this works?  If there
is and it is reliable then I will change Gant to use this trick as
well :-)

--
Russel.
====================================================
Dr Russel Winder                 Partner

Concertant LLP                   t: +44 20 7585 2200, +44 20 7193 9203
41 Buckmaster Road,              f: +44 8700 516 084
London SW11 1EN, UK.             m: +44 7770 465 077

signature.asc (196 bytes) Download Attachment