[groovy-user] What is this in static context ?

14 views
Skip to first unread message

Ista Pouss

unread,
Dec 1, 2009, 3:54:48 AM12/1/09
to us...@groovy.codehaus.org
Hi,

i read at http://groovy.codehaus.org/Differences+from+Java "this" in a
static context refer to the class.

So if I do :

class Dev
{
public static void main(String[] args)
{
println this
}
}

... this print "class pack.Dev". I Understand. I suppose "this" is an
object of class Class here.

But if I write "println this.getClass()" -> syntax error ?
this.name -> syntax error ?
this.hashCode() -> syntax error ?

So what is this ?

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

http://xircles.codehaus.org/manage_email


Roshan Dawrani

unread,
Dec 1, 2009, 4:01:50 AM12/1/09
to us...@groovy.codehaus.org
Looks like these issues are not there anymore - I am currently able to try only on 1.7-rc-1 and see none of the issues you mention - this.name, this.hasCode(), etc work fine.

My guess is that it is something that would be fixed on latest 1.6.6 as well.

What version of groovy are you on?

rgds,
Roshan

Saager Mhatre

unread,
Dec 1, 2009, 4:11:39 AM12/1/09
to us...@groovy.codehaus.org
On 1.6.5 running on 32 bit JVM 1.6.0u15 I get ClassCastExceptions (cannot cast j.l.Class to g.l.GroovyObject) when accessing properties the groovy way, but can still invoke the getters directly.
hashCode works, though; sorry Ista! :P

groovy:000> class Dev { public static void main(String[] arg) { println this.hashCode() } }; Dev.main([] as String[]);
25563270
===> null
groovy:000> class Dev { public static void main(String[] arg) { println this.name } }; Dev.main([] as String[]);     
ERROR java.lang.ClassCastException: java.lang.Class cannot be cast to groovy.lang.GroovyObject
        at Dev.main (groovysh_evaluate:2)
        at groovysh_evaluate.run (groovysh_evaluate:2)
        ...
--
Saager Mhatre

Ista Pouss

unread,
Dec 1, 2009, 4:21:59 AM12/1/09
to us...@groovy.codehaus.org
2009/12/1 Roshan Dawrani <roshan...@codehaus.org>:

>
> What version of groovy are you on?
>

I use groovy 1.6.4 with JVM 1.6.0_14 and Netbeans 6.7.1

So it's a groovy 1.6.4 bug ?

I'm going to download the 1.6.6...

Thanks.

Roshan Dawrani

unread,
Dec 1, 2009, 4:24:34 AM12/1/09
to us...@groovy.codehaus.org
At this point I only know about 1.7-rc-1 - that the issues you mentioned are not there. That the problem is not there on 1.6.6 is just a guess at this point, as I don't have access to it. Maybe someone else can confirm...else, I will do it later..

Jochen Theodorou

unread,
Dec 1, 2009, 4:52:05 AM12/1/09
to us...@groovy.codehaus.org
Roshan Dawrani schrieb:

> At this point I only know about 1.7-rc-1 - that the issues you mentioned
> are not there. That the problem is not there on 1.6.6 is just a guess at
> this point, as I don't have access to it. Maybe someone else can
> confirm...else, I will do it later..

I fixed several bugs in that area a while back, not sure if I did for
1.6.6 as well

bye blackdrag

--
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/

Ista Pouss

unread,
Dec 1, 2009, 12:51:16 PM12/1/09
to us...@groovy.codehaus.org
I've downloaded 1.6.6 and it's always same.

During Netbeans code parsing there this an error

java.lang.NullPointerException
at org.codehaus.groovy.ast.CodeVisitorSupport.visitListOfExpressions(CodeVisitorSupport.java:264)
at org.codehaus.groovy.ast.CodeVisitorSupport.visitTupleExpression(CodeVisitorSupport.java:165)
at org.netbeans.modules.groovy.editor.api.PathFinderVisitor.visitTupleExpression(PathFinderVisitor.java:358)
at org.codehaus.groovy.ast.CodeVisitorSupport.visitArgumentlistExpression(CodeVisitorSupport.java:274)
at org.netbeans.modules.groovy.editor.api.PathFinderVisitor.visitArgumentlistExpression(PathFinderVisitor.java:542)
at org.codehaus.groovy.ast.expr.ArgumentListExpression.visit(ArgumentListExpression.java:73)
at org.netbeans.modules.groovy.editor.api.PathFinderVisitor.visitMethodCallExpression(PathFinderVisitor.java:285)
at org.codehaus.groovy.ast.expr.MethodCallExpression.visit(MethodCallExpression.java:63)
at org.codehaus.groovy.ast.CodeVisitorSupport.visitExpressionStatement(CodeVisitorSupport.java:63)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitExpressionStatement(ClassCodeVisitorSupport.java:159)
at org.netbeans.modules.groovy.editor.api.PathFinderVisitor.visitExpressionStatement(PathFinderVisitor.java:205)
at org.codehaus.groovy.ast.stmt.ExpressionStatement.visit(ExpressionStatement.java:40)
at org.netbeans.modules.groovy.editor.api.PathFinderVisitor.visitBlockStatement(PathFinderVisitor.java:170)
at org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:52)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClassCodeContainer(ClassCodeVisitorSupport.java:73)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitConstructorOrMethod(ClassCodeVisitorSupport.java:80)
at org.netbeans.modules.groovy.editor.api.PathFinderVisitor.visitConstructorOrMethod(PathFinderVisitor.java:145)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitMethod(ClassCodeVisitorSupport.java:88)
at org.netbeans.modules.groovy.editor.api.PathFinderVisitor.visitMethod(PathFinderVisitor.java:571)
at org.codehaus.groovy.ast.ClassNode.visitContents(ClassNode.java:907)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java:48)
at org.netbeans.modules.groovy.editor.api.PathFinderVisitor.visitClass(PathFinderVisitor.java:556)
at org.netbeans.modules.groovy.editor.api.AstPath.find(AstPath.java:182)
at org.netbeans.modules.groovy.editor.api.AstPath.findPathTo(AstPath.java:139)
at org.netbeans.modules.groovy.editor.api.AstPath.<init>(AstPath.java:91)
at org.netbeans.modules.groovy.editor.api.parser.GroovyOccurrencesFinder.run(GroovyOccurrencesFinder.java:142)
at org.netbeans.modules.groovy.editor.api.parser.GroovyOccurrencesFinder.run(GroovyOccurrencesFinder.java:72)
at org.netbeans.modules.csl.editor.semantic.MarkOccurrencesHighlighter.processImpl(MarkOccurrencesHighlighter.java:170)
at org.netbeans.modules.csl.editor.semantic.MarkOccurrencesHighlighter.run(MarkOccurrencesHighlighter.java:122)
at org.netbeans.modules.csl.editor.semantic.MarkOccurrencesHighlighter.run(MarkOccurrencesHighlighter.java:81)
at org.netbeans.modules.parsing.impl.TaskProcessor$CompilationJob.run(TaskProcessor.java:668)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
[catch] at java.lang.Thread.run(Thread.java:619)

I suppose it's on :

public static void main(String[] args)
{

println this.getClass() // here.
}

Same with .hashCode(), .equals(), .name, .getName()...

2009/12/1 Jochen Theodorou <blac...@gmx.org>:

Jochen Theodorou

unread,
Dec 1, 2009, 1:40:54 PM12/1/09
to us...@groovy.codehaus.org
Ista Pouss schrieb:

> I've downloaded 1.6.6 and it's always same.
>
> During Netbeans code parsing there this an error
>
> java.lang.NullPointerException
> at org.codehaus.groovy.ast.CodeVisitorSupport.visitListOfExpressions(CodeVisitorSupport.java:264)
> at org.codehaus.groovy.ast.CodeVisitorSupport.visitTupleExpression(CodeVisitorSupport.java:165)
> at org.netbeans.modules.groovy.editor.api.PathFinderVisitor.visitTupleExpression(PathFinderVisitor.java:358)
[...]

> [catch] at java.lang.Thread.run(Thread.java:619)
>
> I suppose it's on :
>
> public static void main(String[] args)
> {
> println this.getClass() // here.
> }
>
> Same with .hashCode(), .equals(), .name, .getName()...

netbeans is messing around with the compiler, as it shows up in the
trace at positions were only the groovy internals are t be expected. SO
you need to try it outside of the IDE, since it is possible the netbeans
guys missed a possible NPE here

Reply all
Reply to author
Forward
0 new messages