{"id":3075,"date":"2015-02-14T13:14:40","date_gmt":"2015-02-14T11:14:40","guid":{"rendered":"http:\/\/www.tutego.de\/blog\/javainsel\/?p=3075"},"modified":"2015-03-02T16:06:09","modified_gmt":"2015-03-02T14:06:09","slug":"average-number-lines-methods-jdk","status":"publish","type":"post","link":"https:\/\/www.tutego.de\/blog\/javainsel\/2015\/02\/average-number-lines-methods-jdk\/","title":{"rendered":"Average number of lines per method in the JDK"},"content":{"rendered":"<p>The JDK itself can help us to count the lines of code, we just need to parse the source and get the method bodies in a String representation&#8211;then we can count the lines. A quick statistic from the new Java 8 Stream API will give us the numbers.<\/p>\n<p>Code first:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.io.IOException;\r\nimport java.nio.file.FileVisitResult;\r\nimport java.nio.file.Files;\r\nimport java.nio.file.Path;\r\nimport java.nio.file.Paths;\r\nimport java.nio.file.SimpleFileVisitor;\r\nimport java.nio.file.attribute.BasicFileAttributes;\r\nimport java.util.*;\r\nimport javax.tools.JavaCompiler;\r\nimport javax.tools.JavaCompiler.CompilationTask;\r\nimport javax.tools.StandardJavaFileManager;\r\nimport javax.tools.ToolProvider;\r\nimport com.sun.source.tree.CompilationUnitTree;\r\nimport com.sun.source.tree.MethodTree;\r\nimport com.sun.source.util.JavacTask;\r\nimport com.sun.source.util.TreeScanner;\r\n\r\npublic class AverageNumberOfLinesInJDKFinder {\r\n  public static void main( String&#x5B;] args ) throws IOException {\r\n\r\n    String&#x5B;] files = findAllJavaSourceFiles( &quot;C:\/Program Files\/Java\/jdk1.8.0\/src\/&quot; );\r\n    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();\r\n\r\n    try ( StandardJavaFileManager fileManager = compiler.getStandardFileManager( null, null, null ) ) {\r\n      CompilationTask task = compiler.getTask( null, fileManager, null, null, null,\r\n                                               fileManager.getJavaFileObjects( files ) );\r\n\r\n      JavacTask javacTask = (JavacTask) task;\r\n      Iterable&lt;? extends CompilationUnitTree&gt; trees = javacTask.parse();\r\n\r\n      LineCountingVisitor lineCountingVisitor = new LineCountingVisitor();\r\n      for ( CompilationUnitTree compilationUnitTree : trees )\r\n        compilationUnitTree.accept( lineCountingVisitor, null );\r\n\r\n      DoubleSummaryStatistics stats = lineCountingVisitor.numberOfLines.stream().mapToDouble( d -&gt; d ).summaryStatistics();\r\n      System.out.println( stats );\r\n    }\r\n  }\r\n\r\n  static String&#x5B;] findAllJavaSourceFiles( String base ) throws IOException {\r\n    final List&lt;String&gt; result = new ArrayList&lt;String&gt;();\r\n\r\n    Files.walkFileTree( Paths.get( base ), new SimpleFileVisitor&lt;Path&gt;() {\r\n      @Override\r\n      public FileVisitResult visitFile( Path path, BasicFileAttributes attribs ) {\r\n        if ( path.toString().endsWith( &quot;.java&quot; ) )\r\n          result.add( path.toString() );\r\n        return FileVisitResult.CONTINUE;\r\n      }\r\n    } );\r\n    return result.toArray( new String&#x5B;result.size()] );\r\n  }\r\n}\r\n\r\nclass LineCountingVisitor extends TreeScanner&lt;Void, Void&gt; {\r\n  final List&lt;Integer&gt; numberOfLines = new ArrayList&lt;Integer&gt;( 2048 );\r\n\r\n  @Override\r\n  public Void visitMethod( MethodTree node, Void p ) {\r\n    if ( node.getBody() != null ) {\r\n      int lines = new StringTokenizer( node.getBody().toString(), &quot;\\n&quot; ).countTokens() - 1 \/* { *\/ - 1 \/* } *\/;\r\n      if ( lines != 0 ) \/\/ ignore empty bodies\r\n        numberOfLines.add( lines );\r\n    }\r\n    return super.visitMethod( node, p );\r\n  }\r\n}\r\n<\/pre>\n<p>Result:<\/p>\n<p>DoubleSummaryStatistics{count=84695, sum=576427,000000, min=1,000000, average=6,805915, max=1716,000000}<\/p>\n<p>Exercise for the readers:<\/p>\n<ul>\n<li>Analyse different open source products and post the results here.<\/li>\n<li>Generate a SVG with the lines of code.<\/li>\n<li>Change the calculation\u00a0so\u00a0we\u00a0not only get the arithmetic mean but also the\u00a0geometric and harmonic mean.<\/li>\n<li>Sort the result by package, are there differences?<\/li>\n<li>Recognize the @since Javadoc tag and try to find out if the number of lines change over time.<\/li>\n<li>How about the line size?<\/li>\n<\/ul>\n<p><a href=\"http:\/\/www.tutego.de\/blog\/javainsel\/wp-content\/uploads\/number-of-lines-jdk.png\"><img loading=\"lazy\" decoding=\"async\" class=\"  aligncenter wp-image-3079\" src=\"http:\/\/www.tutego.de\/blog\/javainsel\/wp-content\/uploads\/number-of-lines-jdk.png\" alt=\"number-of-lines-jdk\" width=\"781\" height=\"438\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The JDK itself can help us to count the lines of code, we just need to parse the source and get the method bodies in a String representation&#8211;then we can count the lines. A quick statistic from the new Java 8 Stream API will give us the numbers. Code first: import java.io.IOException; import java.nio.file.FileVisitResult; import [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","_links_to":"","_links_to_target":""},"categories":[1],"tags":[],"class_list":["post-3075","post","type-post","status-publish","format-standard","hentry","category-allgemein"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts\/3075","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/comments?post=3075"}],"version-history":[{"count":9,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts\/3075\/revisions"}],"predecessor-version":[{"id":3090,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts\/3075\/revisions\/3090"}],"wp:attachment":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/media?parent=3075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/categories?post=3075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/tags?post=3075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}