Skip to content

Commit f6f0f8b

Browse files
authored
Merge pull request #106 from TsukimiRini/main
feat: add patterns to parsers and extractors
2 parents 7ccd42b + 37da069 commit f6f0f8b

15 files changed

Lines changed: 444 additions & 246 deletions

File tree

client/src/main/java/edu/pku/code2graph/client/CacheHandler.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ public static void initCache(String framework, String projectDir, String cacheDi
4747
GraphUtil.clearGraph();
4848

4949
switchFramework(framework, projectDir);
50-
if (framework.equals("mybatis")) {
51-
MybatisPreprocesser.preprocessMapperXmlFile(projectDir);
52-
}
5350

5451
FileUtil.setRootPath(projectDir);
5552
Map<String, List<String>> ext2FilePaths =

client/src/main/java/edu/pku/code2graph/client/Rename.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ public class Rename {
3535
private static String projectPath = null;
3636
private static String cachePath = null;
3737

38-
static {
39-
initLogger();
40-
}
41-
4238
public static void initCache(String projectDir, String cacheDir)
4339
throws IOException, ParserConfigurationException, SAXException {
4440
projectPath = projectDir;

client/src/main/java/edu/pku/code2graph/client/extractor/SpringExpressionVisitor.java

Lines changed: 133 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -217,174 +217,174 @@ private void parseBodyBlockForLayout(Block body) {
217217
private void parseStatementForLayout(Statement stmt) {
218218
switch (stmt.getNodeType()) {
219219
case ASTNode.BLOCK:
220-
{
221-
Block block = (Block) stmt;
222-
if (block.statements().isEmpty()) {
223-
return;
224-
} else {
225-
for (Object st : block.statements()) {
226-
parseStatementForLayout((Statement) st);
220+
{
221+
Block block = (Block) stmt;
222+
if (block.statements().isEmpty()) {
223+
return;
224+
} else {
225+
for (Object st : block.statements()) {
226+
parseStatementForLayout((Statement) st);
227+
}
228+
return;
227229
}
228-
return;
229230
}
230-
}
231231
case ASTNode.RETURN_STATEMENT:
232-
{
233-
Expression expression = ((ReturnStatement) stmt).getExpression();
234-
if (expression != null && expression.getNodeType() == ASTNode.STRING_LITERAL) {
235-
Pattern pathRegex = Pattern.compile("[\\w-/]+");
236-
String returnExpr = expression.toString();
237-
if (returnExpr != null
238-
&& !returnExpr.trim().isEmpty()
239-
&& pathRegex.matcher(returnExpr).find()) {
240-
currentTemplate = returnExpr.substring(1, returnExpr.length() - 1);
241-
currentReturnLiteral = true;
242-
return;
243-
}
244-
} else if (expression != null
245-
&& expression.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) {
246-
// For specific repos
247-
if (!((ClassInstanceCreation) expression).arguments().isEmpty()
248-
&& ((Expression) ((ClassInstanceCreation) expression).arguments().get(0))
249-
.getNodeType()
250-
== ASTNode.STRING_LITERAL) {
232+
{
233+
Expression expression = ((ReturnStatement) stmt).getExpression();
234+
if (expression != null && expression.getNodeType() == ASTNode.STRING_LITERAL) {
251235
Pattern pathRegex = Pattern.compile("[\\w-/]+");
252-
String returnExpr =
253-
((Expression) ((ClassInstanceCreation) expression).arguments().get(0)).toString();
236+
String returnExpr = expression.toString();
254237
if (returnExpr != null
255-
&& !returnExpr.trim().isEmpty()
256-
&& pathRegex.matcher(returnExpr).find()) {
238+
&& !returnExpr.trim().isEmpty()
239+
&& pathRegex.matcher(returnExpr).find()) {
257240
currentTemplate = returnExpr.substring(1, returnExpr.length() - 1);
258241
currentReturnLiteral = true;
259242
return;
260243
}
261-
}
262-
} else if (expression != null && expression.getNodeType() == ASTNode.METHOD_INVOCATION) {
263-
// For specific repos
264-
if (!((MethodInvocation) expression).arguments().isEmpty()
265-
&& ((Expression) ((MethodInvocation) expression).arguments().get(0)).getNodeType()
266-
== ASTNode.STRING_LITERAL) {
267-
Pattern pathRegex = Pattern.compile("[\\w-/]+");
268-
String returnExpr =
269-
((Expression) ((MethodInvocation) expression).arguments().get(0)).toString();
270-
if (returnExpr != null
271-
&& !returnExpr.trim().isEmpty()
272-
&& pathRegex.matcher(returnExpr).find()
273-
&& !returnExpr.equals("\".html\"")
274-
&& !returnExpr.equals("\".jsp\"")) {
275-
currentTemplate = returnExpr.substring(1, returnExpr.length() - 1);
276-
currentReturnLiteral = true;
277-
return;
244+
} else if (expression != null
245+
&& expression.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) {
246+
// For specific repos
247+
if (!((ClassInstanceCreation) expression).arguments().isEmpty()
248+
&& ((Expression) ((ClassInstanceCreation) expression).arguments().get(0))
249+
.getNodeType()
250+
== ASTNode.STRING_LITERAL) {
251+
Pattern pathRegex = Pattern.compile("[\\w-/]+");
252+
String returnExpr =
253+
((Expression) ((ClassInstanceCreation) expression).arguments().get(0)).toString();
254+
if (returnExpr != null
255+
&& !returnExpr.trim().isEmpty()
256+
&& pathRegex.matcher(returnExpr).find()) {
257+
currentTemplate = returnExpr.substring(1, returnExpr.length() - 1);
258+
currentReturnLiteral = true;
259+
return;
260+
}
261+
}
262+
} else if (expression != null && expression.getNodeType() == ASTNode.METHOD_INVOCATION) {
263+
// For specific repos
264+
if (!((MethodInvocation) expression).arguments().isEmpty()
265+
&& ((Expression) ((MethodInvocation) expression).arguments().get(0)).getNodeType()
266+
== ASTNode.STRING_LITERAL) {
267+
Pattern pathRegex = Pattern.compile("[\\w-/]+");
268+
String returnExpr =
269+
((Expression) ((MethodInvocation) expression).arguments().get(0)).toString();
270+
if (returnExpr != null
271+
&& !returnExpr.trim().isEmpty()
272+
&& pathRegex.matcher(returnExpr).find()
273+
&& !returnExpr.equals("\".html\"")
274+
&& !returnExpr.equals("\".jsp\"")) {
275+
currentTemplate = returnExpr.substring(1, returnExpr.length() - 1);
276+
currentReturnLiteral = true;
277+
return;
278+
}
278279
}
279280
}
281+
break;
280282
}
281-
break;
282-
}
283-
// control
283+
// control
284284
case ASTNode.IF_STATEMENT:
285-
{
286-
IfStatement ifStatement = (IfStatement) stmt;
285+
{
286+
IfStatement ifStatement = (IfStatement) stmt;
287287

288-
if (ifStatement.getThenStatement() != null) {
289-
parseStatementForLayout(ifStatement.getThenStatement());
290-
}
291-
if (ifStatement.getElseStatement() != null) {
292-
parseStatementForLayout(ifStatement.getElseStatement());
288+
if (ifStatement.getThenStatement() != null) {
289+
parseStatementForLayout(ifStatement.getThenStatement());
290+
}
291+
if (ifStatement.getElseStatement() != null) {
292+
parseStatementForLayout(ifStatement.getElseStatement());
293+
}
294+
return;
293295
}
294-
return;
295-
}
296296
case ASTNode.FOR_STATEMENT:
297-
{
298-
ForStatement forStatement = (ForStatement) stmt;
299-
parseStatementForLayout(forStatement.getBody());
300-
return;
301-
}
297+
{
298+
ForStatement forStatement = (ForStatement) stmt;
299+
parseStatementForLayout(forStatement.getBody());
300+
return;
301+
}
302302

303303
case ASTNode.ENHANCED_FOR_STATEMENT:
304-
{
305-
EnhancedForStatement eForStatement = (EnhancedForStatement) stmt;
304+
{
305+
EnhancedForStatement eForStatement = (EnhancedForStatement) stmt;
306306

307-
SingleVariableDeclaration p = eForStatement.getParameter();
308-
String para_name = p.getName().getFullyQualifiedName();
309-
String para_qname = para_name;
310-
IVariableBinding b = p.resolveBinding();
311-
if (b != null && b.getVariableDeclaration() != null) {
312-
para_qname =
313-
JdtService.getMethodQNameFromBinding(b.getDeclaringMethod()) + "." + para_name;
314-
}
307+
SingleVariableDeclaration p = eForStatement.getParameter();
308+
String para_name = p.getName().getFullyQualifiedName();
309+
String para_qname = para_name;
310+
IVariableBinding b = p.resolveBinding();
311+
if (b != null && b.getVariableDeclaration() != null) {
312+
para_qname =
313+
JdtService.getMethodQNameFromBinding(b.getDeclaringMethod()) + "." + para_name;
314+
}
315315

316-
parseStatementForLayout(eForStatement.getBody());
316+
parseStatementForLayout(eForStatement.getBody());
317317

318-
return;
319-
}
318+
return;
319+
}
320320
case ASTNode.DO_STATEMENT:
321-
{
322-
DoStatement doStatement = ((DoStatement) stmt);
321+
{
322+
DoStatement doStatement = ((DoStatement) stmt);
323323

324-
Statement doBody = doStatement.getBody();
325-
if (doBody != null) {
326-
parseStatementForLayout(doBody);
324+
Statement doBody = doStatement.getBody();
325+
if (doBody != null) {
326+
parseStatementForLayout(doBody);
327+
}
328+
return;
327329
}
328-
return;
329-
}
330330
case ASTNode.WHILE_STATEMENT:
331-
{
332-
WhileStatement whileStatement = (WhileStatement) stmt;
331+
{
332+
WhileStatement whileStatement = (WhileStatement) stmt;
333333

334-
Statement whileBody = whileStatement.getBody();
335-
if (whileBody != null) {
336-
parseStatementForLayout(whileBody);
334+
Statement whileBody = whileStatement.getBody();
335+
if (whileBody != null) {
336+
parseStatementForLayout(whileBody);
337+
}
338+
return;
337339
}
338-
return;
339-
}
340340
case ASTNode.TRY_STATEMENT:
341-
{
342-
TryStatement tryStatement = (TryStatement) stmt;
341+
{
342+
TryStatement tryStatement = (TryStatement) stmt;
343343

344-
Statement tryBody = tryStatement.getBody();
345-
if (tryBody != null) {
346-
parseStatementForLayout(tryBody);
344+
Statement tryBody = tryStatement.getBody();
345+
if (tryBody != null) {
346+
parseStatementForLayout(tryBody);
347347

348-
List<CatchClause> catchClauses = tryStatement.catchClauses();
349-
if (catchClauses != null && !catchClauses.isEmpty()) {
350-
for (CatchClause catchClause : catchClauses) {
351-
ITypeBinding binding = catchClause.getException().getType().resolveBinding();
348+
List<CatchClause> catchClauses = tryStatement.catchClauses();
349+
if (catchClauses != null && !catchClauses.isEmpty()) {
350+
for (CatchClause catchClause : catchClauses) {
351+
ITypeBinding binding = catchClause.getException().getType().resolveBinding();
352352

353-
if (catchClause.getBody() != null) {
354-
parseBodyBlockForLayout(catchClause.getBody());
353+
if (catchClause.getBody() != null) {
354+
parseBodyBlockForLayout(catchClause.getBody());
355+
}
355356
}
356357
}
358+
if (tryStatement.getFinally() != null) {
359+
parseBodyBlockForLayout(tryStatement.getFinally());
360+
}
357361
}
358-
if (tryStatement.getFinally() != null) {
359-
parseBodyBlockForLayout(tryStatement.getFinally());
360-
}
362+
return;
361363
}
362-
return;
363-
}
364364
case ASTNode.SWITCH_STATEMENT:
365-
{
366-
SwitchStatement switchStatement = (SwitchStatement) stmt;
367-
368-
Expression expression = switchStatement.getExpression();
369-
// treat case as an implicit block of statements
370-
for (int i = 0; i < switchStatement.statements().size(); ++i) {
371-
Object nxt = switchStatement.statements().get(i);
372-
if (nxt instanceof SwitchCase) {
373-
while (i + 1 < switchStatement.statements().size()) {
374-
Object nxxt = switchStatement.statements().get(++i);
375-
if (nxxt instanceof BreakStatement) {
376-
break;
377-
} else if (nxxt instanceof SwitchCase) {
378-
i -= 1;
379-
break;
380-
} else if (nxxt instanceof Statement) {
381-
parseStatementForLayout((Statement) nxxt);
365+
{
366+
SwitchStatement switchStatement = (SwitchStatement) stmt;
367+
368+
Expression expression = switchStatement.getExpression();
369+
// treat case as an implicit block of statements
370+
for (int i = 0; i < switchStatement.statements().size(); ++i) {
371+
Object nxt = switchStatement.statements().get(i);
372+
if (nxt instanceof SwitchCase) {
373+
while (i + 1 < switchStatement.statements().size()) {
374+
Object nxxt = switchStatement.statements().get(++i);
375+
if (nxxt instanceof BreakStatement) {
376+
break;
377+
} else if (nxxt instanceof SwitchCase) {
378+
i -= 1;
379+
break;
380+
} else if (nxxt instanceof Statement) {
381+
parseStatementForLayout((Statement) nxxt);
382+
}
382383
}
383384
}
384385
}
386+
return;
385387
}
386-
return;
387-
}
388388
}
389389

390390
return;
@@ -688,7 +688,11 @@ protected RelationNode parseExpression(Expression exp) {
688688
parseArguments(root, mi.arguments());
689689
popScope();
690690

691-
if (exp.toString().contains("addAttribute") || exp.toString().contains("setAttribute")) {
691+
if (exp.toString().contains("addAttribute(")
692+
|| exp.toString().contains("setAttribute(")
693+
|| exp.toString().contains("addObject(")
694+
|| exp.toString().contains("getParameter(")
695+
|| exp.toString().startsWith("model") && exp.toString().contains("put(")) {
692696
// parseArguments(root, mi.arguments(), addedToMap);
693697
String identifier = root.getUri().getIdentifier();
694698
String arg = mi.arguments().get(0).toString();

0 commit comments

Comments
 (0)