diff --git a/src/core/lombok/javac/handlers/HandleDelegate.java b/src/core/lombok/javac/handlers/HandleDelegate.java index 4d72eea32c..a6520a9033 100644 --- a/src/core/lombok/javac/handlers/HandleDelegate.java +++ b/src/core/lombok/javac/handlers/HandleDelegate.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2021 The Project Lombok Authors. + * Copyright (C) 2010-2023 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -46,6 +46,7 @@ import com.sun.tools.javac.code.Attribute.Compound; import com.sun.tools.javac.code.Flags; import com.sun.tools.javac.code.Symbol; +import com.sun.tools.javac.code.Symbol.ClassSymbol; import com.sun.tools.javac.code.Symbol.MethodSymbol; import com.sun.tools.javac.code.Symbol.TypeSymbol; import com.sun.tools.javac.code.Type; @@ -177,9 +178,16 @@ public class HandleDelegate extends JavacAnnotationHandler { Set banList = new HashSet(); banList.addAll(METHODS_IN_OBJECT); + // Add already implemented methods to ban list JavacNode typeNode = upToTypeNode(annotationNode); - for (Symbol m : ((JCClassDecl)typeNode.get()).sym.getEnclosedElements()) { + JCClassDecl classDecl = ((JCClassDecl)typeNode.get()); + ClassSymbol targetSymbol = classDecl.sym; + if (targetSymbol == null) { + // Local classes and anonymous classes needs to be resolved + targetSymbol = (ClassSymbol) reso.resolveMethodMember(typeNode).get(classDecl).type.tsym; + } + for (Symbol m : targetSymbol.getEnclosedElements()) { if (m instanceof MethodSymbol) { banList.add(printSig((ExecutableType) m.asType(), m.name, annotationNode.getTypesUtil())); } diff --git a/test/transform/resource/after-delombok/DelegateInNestedClass.java b/test/transform/resource/after-delombok/DelegateInNestedClass.java new file mode 100644 index 0000000000..463e770cb9 --- /dev/null +++ b/test/transform/resource/after-delombok/DelegateInNestedClass.java @@ -0,0 +1,43 @@ +class DelegateInNestedClass { + void localClass() { + + class LocalClass { + private final java.lang.Runnable field = null; + + @java.lang.SuppressWarnings("all") + public void run() { + this.field.run(); + } + } + } + + void anonymousClass() { + Runnable r = new Runnable() { + private final java.lang.Runnable field = null; + @java.lang.SuppressWarnings("all") + public void run() { + this.field.run(); + } + }; + } + + + class InnerClass { + private final java.lang.Runnable field = null; + + @java.lang.SuppressWarnings("all") + public void run() { + this.field.run(); + } + } + + + static class StaticClass { + private final java.lang.Runnable field = null; + + @java.lang.SuppressWarnings("all") + public void run() { + this.field.run(); + } + } +} diff --git a/test/transform/resource/before/DelegateOnLocalClass.java b/test/transform/resource/before/DelegateInNestedClass.java similarity index 55% rename from test/transform/resource/before/DelegateOnLocalClass.java rename to test/transform/resource/before/DelegateInNestedClass.java index 7376c0871f..c27cc5f14f 100644 --- a/test/transform/resource/before/DelegateOnLocalClass.java +++ b/test/transform/resource/before/DelegateInNestedClass.java @@ -1,19 +1,24 @@ //platform !eclipse: Requires a 'full' eclipse with intialized workspace, and we don't (yet) have that set up properly in the test run. -//skip compare content -//ignore: crashed javac with NPE, should be enabled when that bug is fixed import lombok.experimental.Delegate; -import lombok.Getter; -interface DelegateOnLocalClass { - void test1() { - class DelegateOnStatic { +class DelegateInNestedClass { + void localClass() { + class LocalClass { @Delegate private final java.lang.Runnable field = null; } } - void test2() { + void anonymousClass() { Runnable r = new Runnable() { @Delegate private final java.lang.Runnable field = null; - } + }; + } + + class InnerClass { + @Delegate private final java.lang.Runnable field = null; + } + + static class StaticClass { + @Delegate private final java.lang.Runnable field = null; } }