Is @Getter not supposed to work on static fields?

2,624 views
Skip to first unread message

Brian Skaarup

unread,
Jul 28, 2010, 5:23:38 AM7/28/10
to Project Lombok
I just upgraded to 0.9.3 from 0.9.2 and at the few places I have
"@Getter private static boolean someBoolean;" I now get "Cannot use
this in a static context"

Why has this changed?

I use Eclipse 3.6 and Java 6u21

Roel Spilker

unread,
Jul 28, 2010, 8:23:43 AM7/28/10
to project...@googlegroups.com
I presume the value assigned is a compile time constant. Is that correct? If so, the reason is really fantastic...

Please try to compile the following three examples separately:

class Foo {
class Bar {
static final String baz = null;
}
}

class Foo {
class Bar {
static final String baz = "baz";
}
}

class Foo {
static class Bar {
static final String baz = null;
}
}


As you might notice, the first one will give an error message and the second and third are ok. You cannot declare a static variable in a non-static inner class. Unless the value is a compile time constant (and null isn't).

That said, I must say that I didn't expect this error message. And I'm not sure we're going to fix it. I recommend making either the inner class static, move the declaration to an outer class. Or not use @Getter :-(

Can you confirm that this is the problem?

Roel

> -----Oorspronkelijk bericht-----
> Van: bska...@gmail.com
> [mailto:project...@googlegroups.com] Namens Brian Skaarup
> Verzonden: 28 July 2010 11:24
> Aan: Project Lombok
> Onderwerp: [project lombok] Is @Getter not supposed to work
> on static fields?

> --
> You received this message because you are subscribed to the
> Google Groups group for http://projectlombok.org/
>
> To post to this group, send email to
> project...@googlegroups.com To unsubscribe from this
> group, send email to
> project-lombo...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/project-lombok?hl=en
>
>

Roel Spilker

unread,
Jul 28, 2010, 8:26:37 AM7/28/10
to project...@googlegroups.com
If that's not the case, we might have created a bug in the static-context analysis. Than a bug report would be appreciated: http://code.google.com/p/projectlombok/issues/list

> -----Oorspronkelijk bericht-----
> Van: bska...@gmail.com
> [mailto:project...@googlegroups.com] Namens Brian Skaarup
> Verzonden: 28 July 2010 11:24
> Aan: Project Lombok
> Onderwerp: [project lombok] Is @Getter not supposed to work
> on static fields?
>

Reinier Zwitserloot

unread,
Jul 28, 2010, 12:29:59 PM7/28/10
to Project Lombok
I know exactly what this is; it's a bug we programmed in during one of
the fixes for 0.9.2 to 0.9.3: We've qualified all our field accesses
with "this." but evidently we haven't coded in a check to see if the
method-to-be-generated is in fact referring to a static field, in
which case, that obviously won't fly.

As far as I can tell, setters do work (we've always had "this.x"
there), so does @Synchronized (same thing: I know for sure both the
tests and the code itself has been designed to handle static methods),
but for the rest it isn't relevant; an equals or hashCode method we
generate can't be static, and the same goes for toString. Which leaves
Getter as the only one out. In 0.9.2 because we generated "return x;",
by sheer serendipity that did in fact work on static fields. In 0.9.3
it doesn't.

I'll make a regression test, fix the bug, and create an edge release
right now.

On Jul 28, 2:26 pm, Roel Spilker <R.Spil...@topdesk.com> wrote:
> If that's not the case, we might have created a bug in the static-context analysis. Than a bug report would be appreciated:http://code.google.com/p/projectlombok/issues/list
>
>
>
> > -----Oorspronkelijk bericht-----
> > Van: bskaa...@gmail.com
> > [mailto:project...@googlegroups.com] Namens Brian Skaarup
> > Verzonden: 28 July 2010 11:24
> > Aan: Project Lombok
> > Onderwerp: [project lombok] Is @Getter not supposed to work
> > on static fields?
>
> > I just upgraded to 0.9.3 from 0.9.2 and at the few places I
> > have "@Getter private static boolean someBoolean;" I now get
> > "Cannot use this in a static context"
>
> > Why has this changed?
>
> > I use Eclipse 3.6 and Java 6u21
>
> > --
> > You received this message because you are subscribed to the
> > Google Groups group forhttp://projectlombok.org/

Reinier Zwitserloot

unread,
Jul 28, 2010, 12:37:15 PM7/28/10
to Project Lombok
Yes, it's a bug. I've created an issue for it, where further progress
on this bug will be tracked from here on out:

http://code.google.com/p/projectlombok/issues/detail?id=136
Reply all
Reply to author
Forward
0 new messages