---
title: 'Average number of lines per method in the JDK'
url: 'https://tutego.de/blog/javainsel/2015/02/average-number-lines-methods-jdk'
markdown: 'https://tutego.de/blog/javainsel/2015/02/average-number-lines-methods-jdk.md'
date: '2015-02-14'
description: '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–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.…'
taxonomy:
  category:
    - Allgemein
---

# Average number of lines per method in the JDK

![Illustration einer tropischen Insel mit Palmen und einem hohen Berg](https://tutego.de/blog/javainsel/user/themes/tutego/images/insel-cover.jpg)# Java Blog für Programmierer

[Java Blog für Programmierer](https://tutego.de/blog/javainsel/)

## Average number of lines per method in the JDK

14.02.2015 · [Christian Ullenboom](https://tutego.de/blog/javainsel/author/admin/)

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–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 java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.util.JavacTask;
import com.sun.source.util.TreeScanner;

public class AverageNumberOfLinesInJDKFinder {
  public static void main( String[] args ) throws IOException {

    String[] files = findAllJavaSourceFiles( "C:/Program Files/Java/jdk1.8.0/src/" );
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

    try ( StandardJavaFileManager fileManager = compiler.getStandardFileManager( null, null, null ) ) {
      CompilationTask task = compiler.getTask( null, fileManager, null, null, null,
                                               fileManager.getJavaFileObjects( files ) );

      JavacTask javacTask = (JavacTask) task;
      Iterable
```

Kategorien: [Allgemein](https://tutego.de/blog/javainsel/category/allgemein/)

## 1 Kommentare

**Hartmut** · [14.02.2015 15:47](#comment-82294)

Das ist ja mal eine nette Fingerübung :-) Sehr schön gemacht.

Die bisherigen Kommentare wurden aus WordPress übernommen. Neue Kommentare sind derzeit geschlossen.

[← Alle Beiträge](https://tutego.de/blog/javainsel/)

---

## Navigation

- Previous: [Avatar verabschiedet sich](https://tutego.de/blog/javainsel/2015/02/avatar-verabschiedet-sich.md)
