SpringBoot 源码解析——源码各模块代码统计

点击上方 "Java指南者"关注, 星标或置顶一起成长

免费送 1024GB 精品学习资源 

统计结果


柿子要挑软的捏,自然代码要先看少的 module。

统计了一下 (spring-boot-project) 各个模块源码行数:

spring-boot-test : 13608
spring-boot-starters : 0
spring-boot-autoconfigure : 69450
spring-boot-docs : 1131
spring-boot-properties-migrator : 548
spring-boot-tools : 33627
spring-boot-cli : 10278
spring-boot-dependencies : 0
spring-boot-test-autoconfigure : 8439
spring-boot-actuator-autoconfigure : 26997
spring-boot-actuator : 27745
spring-boot-parent : 0
spring-boot-devtools : 10912
spring-boot : 61107

总的代码行数:263842


统计了一下 (spring-boot-tests) 各个模块源码行数:

spring-boot-deployment-tests : 253
spring-boot-smoke-tests-invoker : 0
spring-boot-integration-tests : 1955
spring-boot-smoke-tests : 12866
总的代码行数:15074

统计工具


授之以鱼不如授之以渔,统计代码的工具类代码如下:

package com.zhisheng.common.utils;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * Desc: 统计项目代码行数
 * Created by Java指南者 on 2020-06-25 14:49
 */
public class ProjectCodeCount {
    public static <T> Consumer<T> cof(UncheckedConsumer<T> mapper) {
        Objects.requireNonNull(mapper);
        return t -> {
            try {
                mapper.accept(t);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        };
    }

    public static <T> Consumer<T> ncof(UncheckedConsumer<T> mapper) {
        Objects.requireNonNull(mapper);
        return t -> {
            try {
                mapper.accept(t);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        };
    }

    public static <T, R> Function<T, R> of(UncheckedFunction<T, R> mapper) {
        Objects.requireNonNull(mapper);
        return t -> {
            try {
                return mapper.apply(t);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        };
    }

    public static <T, R> Function<T, R> eof(UncheckedFunction<T, R> mapper, Exception cex) {
        Objects.requireNonNull(mapper);
        return t -> {
            try {
                return mapper.apply(t);
            } catch (Exception ex) {
                if (cex != null && cex.getClass() == ex.getClass()) {
                    throw new RuntimeException(cex);
                } else {
                    throw new RuntimeException(ex);
                }
            }
        };
    }

    public static <T, R> Function<T, R> of(UncheckedFunction<T, R> mapper, R defaultR) {
        Objects.requireNonNull(mapper);
        return t -> {
            try {
                return mapper.apply(t);
            } catch (Exception ex) {
                ex.printStackTrace(System.err);
                return defaultR;
            }
        };
    }

    @FunctionalInterface
    public static interface UncheckedFunction<T, R> {
        R apply(T t) throws Exception;
    }

    @FunctionalInterface
    public static interface UncheckedConsumer<T> {
        void accept(T t) throws Exception;
    }

    /**
     * 查看项目文件夹下的代码行数
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        String path = "/Users/zhisheng/Documents/github/spring-boot/spring-boot-project";
        File f = new File(path);
        File[] files = f.listFiles();
        if (files == null || files.length < 1) {
            return;
        }
        int allCount = 0;
        for (File fi : files) {
            String[] split = fi.toString().split("/");
            if (fi.isDirectory() && !split[split.length - 1].startsWith(".")) {
                long count = Files.walk(Paths.get(fi.toString()))    // 递归获得项目目录下的所有文件
                        .filter(file -> !Files.isDirectory(file))   // 筛选出文件
                        .filter(file -> file.toString().endsWith(".java"))  // 筛选出 java 文件
                        .flatMap(ProjectCodeCount.of(file -> Files.lines(file), Stream.empty()))     // 将会抛出受检异常的 Lambda 包装为 抛出非受检异常的 Lambda
                        .filter(line -> !line.trim().isEmpty())         // 过滤掉空行
                        .filter(line -> !line.trim().startsWith("//"))  //过滤掉 //之类的注释
                        .filter(line -> !(line.trim().startsWith("/*") && line.trim().endsWith("*/")))  //过滤掉/* */之类的注释
                        .filter(line -> !(line.trim().startsWith("/*") && !line.trim().endsWith("*/")))     //过滤掉以 /* 开头的注释(去除空格后的开头)
                        .filter(line -> !(!line.trim().startsWith("/*") && line.trim().endsWith("*/")))     //过滤掉已 */ 结尾的注释
                        .filter(line -> !line.trim().startsWith("*"))   //过滤掉 javadoc 中的文字注释
                        .filter(line -> !line.trim().startsWith("@Override"))   //过滤掉方法上含 @Override 的
                        .count();
                System.out.println(split[split.length - 1] + " : " + count);
                allCount += count;
            }
        }
        System.out.println("总的代码行数:" + allCount);
    }
}

关注我

关注我,Java 学习不迷路!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值