Skip to content

Commit 45164f6

Browse files
authored
Add condition to removeUnusedImports and add removeUnusedImports to VerboseRequestMappingCodemod (#336)
Add missing node type condition to removeUnusedImports Add removeUnusedImports for RequestMapping and RequestMethod to VerboseRequestMappingCodemod Issue #334
1 parent c669008 commit 45164f6

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed

core-codemods/src/main/java/io/codemodder/codemods/VerboseRequestMappingCodemod.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.codemodder.codemods;
22

33
import static io.codemodder.ast.ASTTransforms.addImportIfMissing;
4+
import static io.codemodder.ast.ASTTransforms.removeImportIfUnused;
45

56
import com.contrastsecurity.sarif.Result;
67
import com.github.javaparser.ast.CompilationUnit;
@@ -46,6 +47,8 @@ public ChangesResult onResultFound(
4647
annotationExpr.getPairs().remove(methodAttribute);
4748
annotationExpr.setName(newType.get());
4849
addImportIfMissing(cu, "org.springframework.web.bind.annotation." + newType.get());
50+
removeImportIfUnused(cu, "org.springframework.web.bind.annotation.RequestMapping");
51+
removeImportIfUnused(cu, "org.springframework.web.bind.annotation.RequestMethod");
4952
return ChangesResult.changesApplied;
5053
}
5154
}

core-codemods/src/test/java/io/codemodder/codemods/VerboseRequestMappingCodemodTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@
88
testResourceDir = "verbose-request-mapping",
99
dependencies = {})
1010
final class VerboseRequestMappingCodemodTest implements CodemodTestMixin {}
11+
12+
@Metadata(
13+
codemodType = VerboseRequestMappingCodemod.class,
14+
testResourceDir = "verbose-request-mapping-remove-imports",
15+
dependencies = {})
16+
final class VerboseRequestMappingRemoveUnusedImportsCodemodTest implements CodemodTestMixin {}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.owasp.benchmark.helpers;
2+
import java.io.IOException;
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import javax.servlet.ServletException;
6+
import javax.servlet.http.HttpServletRequest;
7+
import javax.servlet.http.HttpServletResponse;
8+
import org.owasp.benchmark.service.pojo.Person;
9+
import org.owasp.benchmark.service.pojo.XMLMessage;
10+
import org.springframework.http.HttpStatus;
11+
import org.springframework.http.ResponseEntity;
12+
import org.springframework.web.bind.annotation.GetMapping;
13+
import org.springframework.web.bind.annotation.PostMapping;
14+
import org.springframework.web.bind.annotation.RequestBody;
15+
import org.springframework.web.bind.annotation.RestController;
16+
@RestController
17+
public class DataBaseServer {
18+
19+
@GetMapping(value = "/resetdb")
20+
public ResponseEntity<List<XMLMessage>> getOtherOrder(
21+
@RequestBody Person model, HttpServletRequest request, HttpServletResponse response)
22+
throws ServletException, IOException {
23+
ArrayList<XMLMessage> resp = new ArrayList<XMLMessage>();
24+
resp.add(new XMLMessage("Not Implemented."));
25+
return new ResponseEntity<List<XMLMessage>>(resp, HttpStatus.OK);
26+
}
27+
28+
@PostMapping(value = "/testdb")
29+
public ResponseEntity<List<XMLMessage>> createOrder2(
30+
@RequestBody Person model, HttpServletRequest request, HttpServletResponse response)
31+
throws ServletException, IOException {
32+
List<XMLMessage> resp = new ArrayList<XMLMessage>();
33+
resp.add(new XMLMessage("Not Implemented."));
34+
return new ResponseEntity<List<XMLMessage>>(resp, HttpStatus.OK);
35+
}
36+
37+
@GetMapping(value = "/getall")
38+
public ResponseEntity<List<XMLMessage>> getAll(
39+
HttpServletRequest request, HttpServletResponse response)
40+
throws ServletException, IOException {
41+
List<XMLMessage> resp = new ArrayList<XMLMessage>();
42+
String sql = "SELECT * from USERS";
43+
try {
44+
java.sql.Connection connection =
45+
org.owasp.benchmark.helpers.DatabaseHelper.getSqlConnection();
46+
java.sql.PreparedStatement statement = connection.prepareStatement(sql);
47+
statement.execute();
48+
org.owasp.benchmark.helpers.DatabaseHelper.printResults(statement, sql, resp);
49+
} catch (java.sql.SQLException e) {
50+
if (org.owasp.benchmark.helpers.DatabaseHelper.hideSQLErrors) {
51+
e.printStackTrace();
52+
resp.add(new XMLMessage("Error processing request: " + e.getMessage()));
53+
return new ResponseEntity<List<XMLMessage>>(resp, HttpStatus.OK);
54+
} else throw new ServletException(e);
55+
}
56+
return new ResponseEntity<List<XMLMessage>>(resp, HttpStatus.OK);
57+
}
58+
public static void main(String[] args) {
59+
// This empty main() method is required to be able to start the Database. Otherwise you get
60+
// the error:
61+
/*
62+
[java] Error: Main method not found in class org.owasp.benchmark.helpers.DataBaseServer, please define the main method as:
63+
[java] public static void main(String[] args)
64+
[java] or a JavaFX application class must extend javafx.application.Application
65+
*/
66+
}
67+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.owasp.benchmark.helpers;
2+
import java.io.IOException;
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import javax.servlet.ServletException;
6+
import javax.servlet.http.HttpServletRequest;
7+
import javax.servlet.http.HttpServletResponse;
8+
import org.owasp.benchmark.service.pojo.Person;
9+
import org.owasp.benchmark.service.pojo.XMLMessage;
10+
import org.springframework.http.HttpStatus;
11+
import org.springframework.http.ResponseEntity;
12+
import org.springframework.web.bind.annotation.RequestBody;
13+
import org.springframework.web.bind.annotation.RequestMapping;
14+
import org.springframework.web.bind.annotation.RequestMethod;
15+
import org.springframework.web.bind.annotation.RestController;
16+
@RestController
17+
public class DataBaseServer {
18+
19+
@RequestMapping(value = "/resetdb", method = RequestMethod.GET)
20+
public ResponseEntity<List<XMLMessage>> getOtherOrder(
21+
@RequestBody Person model, HttpServletRequest request, HttpServletResponse response)
22+
throws ServletException, IOException {
23+
ArrayList<XMLMessage> resp = new ArrayList<XMLMessage>();
24+
resp.add(new XMLMessage("Not Implemented."));
25+
return new ResponseEntity<List<XMLMessage>>(resp, HttpStatus.OK);
26+
}
27+
28+
@RequestMapping(value = "/testdb", method = RequestMethod.POST)
29+
public ResponseEntity<List<XMLMessage>> createOrder2(
30+
@RequestBody Person model, HttpServletRequest request, HttpServletResponse response)
31+
throws ServletException, IOException {
32+
List<XMLMessage> resp = new ArrayList<XMLMessage>();
33+
resp.add(new XMLMessage("Not Implemented."));
34+
return new ResponseEntity<List<XMLMessage>>(resp, HttpStatus.OK);
35+
}
36+
37+
@RequestMapping(value = "/getall", method = RequestMethod.GET)
38+
public ResponseEntity<List<XMLMessage>> getAll(
39+
HttpServletRequest request, HttpServletResponse response)
40+
throws ServletException, IOException {
41+
List<XMLMessage> resp = new ArrayList<XMLMessage>();
42+
String sql = "SELECT * from USERS";
43+
try {
44+
java.sql.Connection connection =
45+
org.owasp.benchmark.helpers.DatabaseHelper.getSqlConnection();
46+
java.sql.PreparedStatement statement = connection.prepareStatement(sql);
47+
statement.execute();
48+
org.owasp.benchmark.helpers.DatabaseHelper.printResults(statement, sql, resp);
49+
} catch (java.sql.SQLException e) {
50+
if (org.owasp.benchmark.helpers.DatabaseHelper.hideSQLErrors) {
51+
e.printStackTrace();
52+
resp.add(new XMLMessage("Error processing request: " + e.getMessage()));
53+
return new ResponseEntity<List<XMLMessage>>(resp, HttpStatus.OK);
54+
} else throw new ServletException(e);
55+
}
56+
return new ResponseEntity<List<XMLMessage>>(resp, HttpStatus.OK);
57+
}
58+
public static void main(String[] args) {
59+
// This empty main() method is required to be able to start the Database. Otherwise you get
60+
// the error:
61+
/*
62+
[java] Error: Main method not found in class org.owasp.benchmark.helpers.DataBaseServer, please define the main method as:
63+
[java] public static void main(String[] args)
64+
[java] or a JavaFX application class must extend javafx.application.Application
65+
*/
66+
}
67+
}

framework/codemodder-base/src/main/java/io/codemodder/ast/ASTTransforms.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.github.javaparser.ast.expr.StringLiteralExpr;
1414
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
1515
import com.github.javaparser.ast.nodeTypes.NodeWithBody;
16+
import com.github.javaparser.ast.nodeTypes.NodeWithName;
1617
import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName;
1718
import com.github.javaparser.ast.stmt.BlockStmt;
1819
import com.github.javaparser.ast.stmt.ExpressionStmt;
@@ -215,6 +216,12 @@ public static void removeImportIfUnused(final CompilationUnit cu, final String c
215216
.anyMatch(n -> n.getNameAsString().equals(simpleName))) {
216217
return;
217218
}
219+
if (cu.findAll(Node.class).stream()
220+
.filter(n -> n instanceof NodeWithName<?>)
221+
.map(n -> (NodeWithName<?>) n)
222+
.anyMatch(n -> n.getNameAsString().equals(simpleName))) {
223+
return;
224+
}
218225
cu.remove(importToRemove.get());
219226
}
220227

0 commit comments

Comments
 (0)