Lombok: @Slf4j doesn't work if class has an `org` field

Created on 24 May 2017  路  4Comments  路  Source: projectlombok/lombok

@Slf4j
class Foo {
    private Org org;
}

This produces a compiler error "non-static variable org cannot be referenced from a static context". It makes sense; the = org.slf4j.LoggerFactory.getLogger(... is ambiguous. Is there any way to make @Slf4j work?

Most helpful comment

BTW, you are likely to run into this same problem if you import both org.example.foo.Bar and com.example.moo.Bar and needs to reference somewhere in your code the org.example.foo.Bar class - You'd have no way to do that! This is a handicap in the java language that uses the same notation for two completely different concepts. Don't know what could be done in lombok to solve this issue without creating some other issue somewhere else.

All 4 comments

BTW, you are likely to run into this same problem if you import both org.example.foo.Bar and com.example.moo.Bar and needs to reference somewhere in your code the org.example.foo.Bar class - You'd have no way to do that! This is a handicap in the java language that uses the same notation for two completely different concepts. Don't know what could be done in lombok to solve this issue without creating some other issue somewhere else.

I didn't find any.

so, had to replace @Slf4j with direct private static final Logger log: stackoverflow

Actually you might maybe get away with just adding the missing import to make this compile.

There is a problem in the java language where using fully qualified names and the presence of a field with a name that is the same as the first part of the fully qualified names gives a compiler error.

Also, in our generated we use fully qualified names for several important reasons:

  • To prevent collisions with local types
  • To prevent collisions with imported types
  • We can suppress warning on generated fields and methonds, not on imports
  • It is easier to code and maintain. We don't have to add code in two different locations, or check for the existence of the import or a wildcard variant of it
Was this page helpful?
0 / 5 - 0 ratings