Skip to content

Commit

Permalink
[fixes projectlombok#3240] Resolve delegate target class to find impl…
Browse files Browse the repository at this point in the history
…emented methods
  • Loading branch information
Rawi01 committed Jan 21, 2023
1 parent 930cc24 commit 78b1370
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
12 changes: 10 additions & 2 deletions src/core/lombok/javac/handlers/HandleDelegate.java
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 78b1370

Please sign in to comment.