Skip to content

Commit

Permalink
Help ?
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedsamehsalah committed Jul 23, 2023
1 parent 38554be commit 7c3da88
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 2 deletions.
11 changes: 11 additions & 0 deletions error-prone-contrib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@
<artifactId>testng</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${version.lombok}</version>
<scope>import</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -241,6 +247,11 @@
<artifactId>refaster-support</artifactId>
<version>${project.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${version.lombok}</version>
</path>
</annotationProcessorPaths>
<compilerArgs combine.children="append">
<arg>-Xplugin:RefasterRuleCompiler</arg>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.AssignmentTree;
import com.sun.source.tree.BlockTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.ExpressionStatementTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.IdentifierTree;
Expand Down Expand Up @@ -76,6 +77,11 @@ public Description matchBlock(BlockTree tree, VisitorState state) {
return Description.NO_MATCH;
}

//TODO: Fix is => Maybe exclude Lombok @Data classes?
// if (isClassAnnotatedWithLombokData(state)) {
// return Description.NO_MATCH;
// }

Symbol variableSymbol = ASTHelpers.getSymbol(((ReturnTree) finalStatement).getExpression());
StatementTree precedingStatement = statements.get(statements.size() - 2);

Expand All @@ -91,12 +97,26 @@ public Description matchBlock(BlockTree tree, VisitorState state) {
SuggestedFix.builder()
.replace(
precedingStatement,
String.format("return %s;", SourceCode.treeToString(resultExpr, state)))
.delete(finalStatement)
// String.format("return %s;", SourceCode.treeToString(resultExpr, state)))
String.format("return %s;", SourceCode.treeToString(resultExpr)))
//Same case for trying to delete wrongly assumed "source code".
//Note 2: Seems we can only use ONE fix because of the way the errorprone replaces "source code"
// .delete(finalStatement)
// .replace(finalStatement, "")
.build()))
.orElse(Description.NO_MATCH);
}

private boolean isClassAnnotatedWithLombokData(VisitorState state) {
ClassTree enclosingNode = ASTHelpers.findEnclosingNode(state.getPath(), ClassTree.class);

return ASTHelpers.getAnnotations(enclosingNode)
.stream()
.anyMatch(annotation ->
annotation.toString().contains("Data")
);
}

private static Optional<ExpressionTree> tryMatchAssignment(Symbol targetSymbol, Tree tree) {
if (tree instanceof ExpressionStatementTree) {
return tryMatchAssignment(targetSymbol, ((ExpressionStatementTree) tree).getExpression());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,42 @@ private SourceCode() {}
* @return A non-{@code null} string.
*/
public static String treeToString(Tree tree, VisitorState state) {
/** The issue starts inside `state.getSourceForNode(tree)` statement because line:
* CharSequence source = getSourceCode()
*
* returns =>
* """
*import lombok.Data;
*
* @Data
* public class AnyClass {
* private String anything;
* }
* """
* Which is not the correct start & end position of the intended replacement statement.
if (end == -1) {
return null;
}
checkArgument(start >= 0, "invalid start position (%s) for: %s", start, tree);
checkArgument(start < end, "invalid source positions (%s, %s) for: %s", start, end, tree);
checkArgument(end <= source.length(), "invalid end position (%s) for: %s", end, tree);
return source.subSequence(start, end).toString();
*/
String src = state.getSourceForNode(tree);
return src != null ? src : tree.toString();
}



/**
* TODO: Call this conditionally from DirectReturn ??
*/
public static String treeToString(Tree tree) {
String src = tree.toString();
return src != null ? src : tree.toString();
}

/**
* Creates a {@link SuggestedFix} for the deletion of the given {@link Tree}, including any
* whitespace that follows it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ void identification() {
"import static org.mockito.Mockito.mock;",
"import static org.mockito.Mockito.spy;",
"",
"import lombok.Data;",
"import java.util.function.Supplier;",
"",
"@Data",
"class A {",
" private String field;",
"",
Expand Down Expand Up @@ -223,4 +225,26 @@ void replacement() {
"}")
.doTest(TestMode.TEXT_MATCH);
}

@Test
void lombokDataReplacement() {
BugCheckerRefactoringTestHelper.newInstance(DirectReturn.class, getClass())
.addInputLines(
"AnyClass.java",
"import lombok.Data;",
"",
"@Data",
"public class AnyClass {",
"private String anything;",
"}")
.addOutputLines(
"AnyClass.java",
"import lombok.Data;",
"",
"@Data",
"public class AnyClass {",
"private String anything;",
"}")
.doTest(TestMode.TEXT_MATCH);
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
<version.error-prone-slf4j>0.1.18</version.error-prone-slf4j>
<version.guava-beta-checker>1.0</version.guava-beta-checker>
<version.jdk>11</version.jdk>
<version.lombok>1.18.22</version.lombok>
<version.maven>3.8.7</version.maven>
<version.mockito>5.4.0</version.mockito>
<version.nopen-checker>1.0.1</version.nopen-checker>
Expand Down

0 comments on commit 7c3da88

Please sign in to comment.