1818 */
1919public class JavaFileDataListener extends Java20ParserBaseListener implements CommonFileDataListener {
2020
21- public static final String FILE_SIZE = "size" ;
22- public static final String LOC = "loc" ;
23- public static final String CLOC = "cloc" ;
24-
2521 private static final Logger LOGGER = LoggerFactory .getLogger (JavaFileDataListener .class );
2622
2723 private final JavaFileDataHandler fileDataHandler ;
@@ -30,6 +26,8 @@ public class JavaFileDataListener extends Java20ParserBaseListener implements Co
3026 private String wildcardImport ;
3127 private String currentPackage = "" ;
3228 private final org .antlr .v4 .runtime .CommonTokenStream tokens ;
29+ private int functionCount = 0 ;
30+ private int variableCount = 0 ;
3331
3432 public JavaFileDataListener (final JavaFileDataHandler fileDataHandler ,
3533 final boolean wildcardImportProperty ,
@@ -43,17 +41,23 @@ public JavaFileDataListener(final JavaFileDataHandler fileDataHandler,
4341
4442 @ Override
4543 public void enterCompilationUnit (final Java20Parser .CompilationUnitContext ctx ) {
46- // Calculate LOC and CLOC
47- final int loc = getLoc ( ctx );
44+ // Calculate total source SLOC and CLOC
45+ final int sloc = getSloc ( tokens );
4846 final int cloc = getCloc (ctx );
4947
50- fileDataHandler .addMetric (LOC , String .valueOf (loc ));
48+ fileDataHandler .addMetric (SLOC , String .valueOf (sloc ));
5149 fileDataHandler .addMetric (CLOC , String .valueOf (cloc ));
5250
5351 LOGGER .atTrace ()
5452 .addArgument (fileDataHandler .getFileName ())
55- .addArgument (loc )
56- .log ("{} - LOC: {}" );
53+ .addArgument (sloc )
54+ .log ("{} - SLOC: {}" );
55+ }
56+
57+ @ Override
58+ public void exitCompilationUnit (final Java20Parser .CompilationUnitContext ctx ) {
59+ fileDataHandler .addMetric (FUNCTION_COUNT , String .valueOf (functionCount ));
60+ fileDataHandler .addMetric (VARIABLE_COUNT , String .valueOf (variableCount ));
5761 }
5862
5963 @ Override
@@ -111,7 +115,8 @@ public void enterNormalClassDeclaration(final Java20Parser.NormalClassDeclaratio
111115 // Add modifiers
112116 addModifiers (ctx .classModifier ());
113117
114- // Add LOC
118+ // Add SLOC and LOC
119+ fileDataHandler .getCurrentClassData ().addMetric (SLOC , String .valueOf (getSloc (ctx , tokens )));
115120 fileDataHandler .getCurrentClassData ().addMetric (LOC , String .valueOf (getLoc (ctx )));
116121
117122 // Handle extends
@@ -149,7 +154,8 @@ public void enterNormalInterfaceDeclaration(
149154 // Add modifiers
150155 addModifiers (ctx .interfaceModifier ());
151156
152- // Add LOC
157+ // Add SLOC and LOC
158+ fileDataHandler .getCurrentClassData ().addMetric (SLOC , String .valueOf (getSloc (ctx , tokens )));
153159 fileDataHandler .getCurrentClassData ().addMetric (LOC , String .valueOf (getLoc (ctx )));
154160
155161 // Handle extends
@@ -179,7 +185,8 @@ public void enterEnumDeclaration(final Java20Parser.EnumDeclarationContext ctx)
179185 // Add modifiers
180186 addModifiers (ctx .classModifier ());
181187
182- // Add LOC
188+ // Add SLOC and LOC
189+ fileDataHandler .getCurrentClassData ().addMetric (SLOC , String .valueOf (getSloc (ctx , tokens )));
183190 fileDataHandler .getCurrentClassData ().addMetric (LOC , String .valueOf (getLoc (ctx )));
184191 }
185192
@@ -207,6 +214,7 @@ public void enterFieldDeclaration(final Java20Parser.FieldDeclarationContext ctx
207214 fileDataHandler .enterMethod (fieldFqn );
208215 fileDataHandler .getCurrentClassData ().addField (fieldName , fieldType , modifiers );
209216 fileDataHandler .leaveMethod ();
217+ variableCount ++;
210218 }
211219 }
212220 }
@@ -231,6 +239,7 @@ public void enterMethodDeclaration(final Java20Parser.MethodDeclarationContext c
231239 + "#" + parameterHash ;
232240
233241 fileDataHandler .enterMethod (methodFqn );
242+ functionCount ++;
234243
235244 // Get return type
236245 String returnType = "void" ;
@@ -256,7 +265,8 @@ public void enterMethodDeclaration(final Java20Parser.MethodDeclarationContext c
256265 methodData .setLines (ctx .start .getLine (), ctx .stop .getLine ());
257266 }
258267
259- // Add LOC
268+ // Add SLOC and LOC
269+ methodData .addMetric (SLOC , String .valueOf (getSloc (ctx , tokens )));
260270 methodData .addMetric (LOC , String .valueOf (getLoc (ctx )));
261271 }
262272
@@ -286,6 +296,7 @@ public void enterInterfaceMethodDeclaration(
286296 + "#" + parameterHash ;
287297
288298 fileDataHandler .enterMethod (methodFqn );
299+ functionCount ++;
289300
290301 // Get return type
291302 String returnType = "void" ;
@@ -311,7 +322,8 @@ public void enterInterfaceMethodDeclaration(
311322 methodData .setLines (ctx .start .getLine (), ctx .stop .getLine ());
312323 }
313324
314- // Add LOC
325+ // Add SLOC and LOC
326+ methodData .addMetric (SLOC , String .valueOf (getSloc (ctx , tokens )));
315327 methodData .addMetric (LOC , String .valueOf (getLoc (ctx )));
316328 }
317329
@@ -336,6 +348,7 @@ public void enterConstructorDeclaration(final Java20Parser.ConstructorDeclaratio
336348 + "#" + parameterHash ;
337349
338350 fileDataHandler .enterMethod (constructorFqn );
351+ functionCount ++;
339352
340353 final MethodDataHandler constructor = fileDataHandler .getCurrentClassData ()
341354 .addConstructor (constructorName , constructorFqn );
@@ -353,7 +366,8 @@ public void enterConstructorDeclaration(final Java20Parser.ConstructorDeclaratio
353366 constructor .setLines (ctx .start .getLine (), ctx .stop .getLine ());
354367 }
355368
356- // Add LOC
369+ // Add SLOC and LOC
370+ constructor .addMetric (SLOC , String .valueOf (getSloc (ctx , tokens )));
357371 constructor .addMetric (LOC , String .valueOf (getLoc (ctx )));
358372 }
359373
@@ -635,6 +649,13 @@ private String resolveTypeName(final String typeName) {
635649 return typeName ;
636650 }
637651
652+ @ Override
653+ public void enterLocalVariableDeclaration (final Java20Parser .LocalVariableDeclarationContext ctx ) {
654+ if (ctx .variableDeclaratorList () != null ) {
655+ variableCount += ctx .variableDeclaratorList ().variableDeclarator ().size ();
656+ }
657+ }
658+
638659 private boolean isPrimitiveType (final String type ) {
639660 return Arrays .asList ("byte" , "short" , "int" , "long" , "float" , "double" ,
640661 "boolean" , "char" , "void" ).contains (type );
0 commit comments