|
10 | 10 | import com.github.javaparser.ast.body.*;
|
11 | 11 | import com.github.javaparser.ast.expr.*;
|
12 | 12 | import com.github.javaparser.ast.nodeTypes.NodeWithName;
|
13 |
| -import com.github.javaparser.ast.stmt.BlockStmt; |
| 13 | +import com.github.javaparser.ast.stmt.*; |
14 | 14 | import com.github.javaparser.ast.type.ReferenceType;
|
15 | 15 | import com.github.javaparser.ast.type.Type;
|
16 | 16 | import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
|
@@ -273,7 +273,9 @@ private static Pair<String, Callable> processCallableDeclaration(CallableDeclara
|
273 | 273 |
|
274 | 274 | // add the complete declaration string, including modifiers, throws, and
|
275 | 275 | // parameter names
|
276 |
| - callableNode.setDeclaration(callableDecl.getDeclarationAsString(true, true, true).strip()); |
| 276 | + callableNode.setDeclaration(callableDecl |
| 277 | + .getDeclarationAsString(true, true, true) |
| 278 | + .strip().replaceAll("//.*\n", "")); |
277 | 279 |
|
278 | 280 | // add information about callable parameters: for each parameter, type, name,
|
279 | 281 | // annotations,
|
@@ -301,11 +303,32 @@ private static Pair<String, Callable> processCallableDeclaration(CallableDeclara
|
301 | 303 | callableNode.setAccessedFields(getAccessedFields(body, classFields, typeName));
|
302 | 304 | callableNode.setCallSites(getCallSites(body));
|
303 | 305 | callableNode.setVariableDeclarations(getVariableDeclarations(body));
|
| 306 | + callableNode.setCyclomaticComplexity(getCyclomaticComplexity(callableDecl)); |
304 | 307 |
|
305 | 308 | String callableSignature = (callableDecl instanceof MethodDeclaration) ? callableDecl.getSignature().asString() : callableDecl.getSignature().asString().replace(callableDecl.getSignature().getName(), "<init>");
|
306 | 309 | return Pair.of(callableSignature, callableNode);
|
307 | 310 | }
|
308 | 311 |
|
| 312 | + /** |
| 313 | + * Computes cyclomatic complexity for the given callable. |
| 314 | + * |
| 315 | + * @param callableDeclaration Callable to compute cyclomatic complexity for |
| 316 | + * @return cyclomatic complexity |
| 317 | + */ |
| 318 | + private static int getCyclomaticComplexity(CallableDeclaration callableDeclaration) { |
| 319 | + int ifStmtCount = callableDeclaration.findAll(IfStmt.class).size(); |
| 320 | + int loopStmtCount = callableDeclaration.findAll(DoStmt.class).size() + |
| 321 | + callableDeclaration.findAll(ForStmt.class).size() + |
| 322 | + callableDeclaration.findAll(ForEachStmt.class).size() + |
| 323 | + callableDeclaration.findAll(WhileStmt.class).size(); |
| 324 | + int switchCaseCount = callableDeclaration.findAll(SwitchStmt.class).stream() |
| 325 | + .map(stmt -> stmt.getEntries().size()) |
| 326 | + .reduce(0, Integer::sum); |
| 327 | + int conditionalExprCount = callableDeclaration.findAll(ConditionalExpr.class).size(); |
| 328 | + int catchClauseCount = callableDeclaration.findAll(CatchClause.class).size(); |
| 329 | + return ifStmtCount + loopStmtCount + switchCaseCount + conditionalExprCount + catchClauseCount + 1; |
| 330 | + } |
| 331 | + |
309 | 332 | /**
|
310 | 333 | * Processes the given field declaration to extract information about the
|
311 | 334 | * declared field and
|
|
0 commit comments