Skip to content

Commit

Permalink
[fixes #2990] Treat records and enums as places where static is all…
Browse files Browse the repository at this point in the history
…owed.
  • Loading branch information
rzwitserloot committed Mar 17, 2022
1 parent 3a4f0e0 commit 2eddba6
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 20 deletions.
10 changes: 1 addition & 9 deletions src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java
Expand Up @@ -2770,15 +2770,7 @@ static boolean isTypeAndDoesNotHaveFlags(EclipseNode typeNode, long flags) {
* Returns {@code true} if the provided node supports static methods and types (top level or static class)
*/
public static boolean isStaticAllowed(EclipseNode typeNode) {
boolean staticAllowed = true;

while (typeNode.getKind() != Kind.COMPILATION_UNIT) {
if (!staticAllowed) return false;

staticAllowed = typeNode.isStatic();
typeNode = typeNode.up();
}
return true;
return typeNode.isStatic() || typeNode.up() == null || typeNode.up().getKind() == Kind.COMPILATION_UNIT || isRecord(typeNode);
}

public static AbstractVariableDeclaration[] getRecordComponents(TypeDeclaration typeDeclaration) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/lombok/javac/handlers/HandleLog.java
Expand Up @@ -80,7 +80,7 @@ public static void processAnnotation(LoggingFramework framework, AnnotationValue
return;
}

if (!typeNode.isEnumType() && useStatic && !isStaticAllowed(typeNode)) {
if (useStatic && !isStaticAllowed(typeNode)) {
annotationNode.addError(framework.getAnnotationAsString() + " is not supported on non-static nested classes.");
return;
}
Expand Down
10 changes: 1 addition & 9 deletions src/core/lombok/javac/handlers/JavacHandlerUtil.java
Expand Up @@ -2096,15 +2096,7 @@ public static boolean isClassAndDoesNotHaveFlags(JavacNode typeNode, long flags)
* Returns {@code true} if the provided node supports static methods and types (top level or static class)
*/
public static boolean isStaticAllowed(JavacNode typeNode) {
boolean staticAllowed = true;

while (typeNode.getKind() != Kind.COMPILATION_UNIT) {
if (!staticAllowed) return false;

staticAllowed = typeNode.isStatic();
typeNode = typeNode.up();
}
return true;
return typeNode.isStatic() || typeNode.up() == null || typeNode.up().getKind() == Kind.COMPILATION_UNIT || isRecord(typeNode);
}

public static JavacNode upToTypeNode(JavacNode node) {
Expand Down
@@ -0,0 +1,6 @@
class LoggerFloggerRecord {
record Inner(String x) {
@java.lang.SuppressWarnings("all")
private static final com.google.common.flogger.FluentLogger log = com.google.common.flogger.FluentLogger.forEnclosingClass();
}
}
16 changes: 16 additions & 0 deletions test/transform/resource/after-ecj/LoggerFloggerRecord.java
@@ -0,0 +1,16 @@
import lombok.extern.flogger.Flogger;
class LoggerFloggerRecord {
static @Flogger record Inner(String x) {
private static final com.google.common.flogger.FluentLogger log = com.google.common.flogger.FluentLogger.forEnclosingClass();
/* Implicit */ private final String x;
<clinit>() {
}
public Inner(String x) {
super();
.x = x;
}
}
LoggerFloggerRecord() {
super();
}
}
2 changes: 1 addition & 1 deletion test/transform/resource/before/LoggerFlogger.java
Expand Up @@ -25,4 +25,4 @@ class LoggerFloggerWithInnerEnum {
enum Inner {
CONSTANT;
}
}
}
8 changes: 8 additions & 0 deletions test/transform/resource/before/LoggerFloggerRecord.java
@@ -0,0 +1,8 @@
// version 14:

import lombok.extern.flogger.Flogger;

class LoggerFloggerRecord {
@Flogger
record Inner(String x) {}
}

0 comments on commit 2eddba6

Please sign in to comment.