Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve delegate target class to find implemented methods #3336

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions 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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -177,9 +178,16 @@ public class HandleDelegate extends JavacAnnotationHandler<Delegate> {
Set<String> banList = new HashSet<String>();
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()));
}
Expand Down
43 changes: 43 additions & 0 deletions 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();
}
}
}
@@ -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;
}
}