Skip to content

Commit

Permalink
[fixes projectlombok#3370] Support Helper in method
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawi01 committed May 5, 2023
1 parent 250bc06 commit 37ac1bf
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/core/lombok/javac/handlers/HandleHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCIdent;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
import com.sun.tools.javac.tree.JCTree.JCStatement;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
Expand All @@ -61,12 +62,14 @@ public class HandleHelper extends JavacAnnotationHandler<Helper> {
private List<JCStatement> getStatementsFromJcNode(JCTree tree) {
if (tree instanceof JCBlock) return ((JCBlock) tree).stats;
if (tree instanceof JCCase) return ((JCCase) tree).stats;
if (tree instanceof JCMethodDecl) return ((JCMethodDecl) tree).body.stats;
return null;
}

private void setStatementsOfJcNode(JCTree tree, List<JCStatement> statements) {
if (tree instanceof JCBlock) ((JCBlock) tree).stats = statements;
else if (tree instanceof JCCase) ((JCCase) tree).stats = statements;
else if (tree instanceof JCMethodDecl) ((JCMethodDecl) tree).body.stats = statements;
else throw new IllegalArgumentException("Can't set statements on node type: " + tree.getClass());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class HelperTest {
public class HelperInInitializationBlock {
{
final int z = 5;
if (Boolean.TRUE) {
Expand Down
13 changes: 13 additions & 0 deletions test/transform/resource/after-delombok/HelperInMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class HelperInMethod {
int someMethod(int arg1) {
int localVar = 5;

class Helpers {
int helperMethod(int arg) {
return arg + localVar;
}
}
final Helpers $Helpers = new Helpers();
return $Helpers.helperMethod(10);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import lombok.experimental.Helper;
class HelperTest {
public class HelperInInitializationBlock {
{
final int z = 5;
if (Boolean.TRUE)
Expand All @@ -24,7 +24,7 @@ void bar() {
}
}
}
HelperTest() {
public HelperInInitializationBlock() {
super();
}
}
19 changes: 19 additions & 0 deletions test/transform/resource/after-ecj/HelperInMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import lombok.experimental.Helper;
public class HelperInMethod {
public HelperInMethod() {
super();
}
int someMethod(int arg1) {
int localVar = 5;
@Helper class Helpers {
Helpers() {
super();
}
int helperMethod(int arg) {
return (arg + localVar);
}
}
final Helpers $Helpers = new Helpers();
return $Helpers.helperMethod(10);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import lombok.experimental.Helper;

class HelperTest {
public class HelperInInitializationBlock {
{
final int z = 5;
if (Boolean.TRUE) {
Expand Down
16 changes: 16 additions & 0 deletions test/transform/resource/before/HelperInMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import lombok.experimental.Helper;

public class HelperInMethod {
int someMethod(int arg1) {
int localVar = 5;

@Helper
class Helpers {
int helperMethod(int arg) {
return arg + localVar;
}
}

return helperMethod(10);
}
}

0 comments on commit 37ac1bf

Please sign in to comment.