//package com.gh.gamecenter.plugin.transform
//
//import javassist.ClassPool
//import javassist.CtClass
//import javassist.bytecode.ClassFile
//import javassist.expr.Expr
//import javassist.expr.ExprEditor
//import javassist.expr.MethodCall
//import javassist.expr.NewExpr
//
///**
// * 全局替换线程池
// *
// * 问题：
// * 1.由于全局替换会导致编译很慢，因此只替换okhttp3中的线程池创建
// * 2.全局替换okhttp3的线程池会导致闪退，原因是Http2Connection#close方法中会调用ThreadPoolExecutor#shutdown方法
// *
// * 因此最后只替换了会产生大量线程的ConnectionPool类中的线程池
// *
// * 参考：https://github.com/wei120698598/threadpool-shrink-plugin
// */
//class ThreadPoolShrinkTransformer implements Transformer {
//    private ClassPool classPool = ClassPool.getDefault()
//
//    private String ExecutorsClassName = java.util.concurrent.Executors.class.name
//    private String ThreadPoolExecutorClassName = java.util.concurrent.ThreadPoolExecutor.class.name
//    private String ScheduledThreadPoolExecutorClassName = java.util.concurrent.ScheduledThreadPoolExecutor.class.name
//
//    private ArrayList<String> executorServiceMethods = [
//            "newFixedThreadPool",
//            "newFixedThreadPool",
//            "newWorkStealingPool",
//            "newWorkStealingPool",
//            "newSingleThreadExecutor",
//            "newSingleThreadExecutor",
//            "newCachedThreadPool",
//            "newCachedThreadPool"
//    ]
//    private ArrayList<String> scheduledExecutorServiceMethods = [
//            "newSingleThreadScheduledExecutor",
//            "newSingleThreadScheduledExecutor",
//            "newScheduledThreadPool",
//            "newScheduledThreadPool",
//            "unconfigurableExecutorService",
//            "unconfigurableScheduledExecutorService"
//    ]
//
//    private ArrayList<String> defaultFilterClassSignaturePrefix = [
//            "androidx.core.content",
//            "com.gh.gamecenter"
//    ]
//
//    private String defaultExecutor = "com.gh.common.AppExecutor.getIoExecutor()"
//
//    @Override
//    String getModifyClassName() {
////        return "okhttp3"
//        return "ConnectionPool"
//    }
//
//    @Override
//    CtClass modifyClass(String filePath, InputStream inputStream) {
//        if (filePath.contains(getModifyClassName())){
//            def classFile = new ClassFile(new DataInputStream(new BufferedInputStream(inputStream)))
//            def ctClass = classPool.get(classFile.name)
//            //判断是否需要解冻
//            if (ctClass.isFrozen()) {
//                ctClass.defrost()
//            }
//            ctClass.instrument(new ThreadPoolExprEditor())
//
//            return ctClass
//        }
//        return null
//    }
//
//    class ThreadPoolExprEditor extends ExprEditor {
//        @Override
//        void edit(MethodCall methodCall) {
//            if (!checkEnclosingClass(methodCall)) return
//            if (methodCall.className == ExecutorsClassName) {
//                if (executorServiceMethods.contains(methodCall.methodName)) {
//                    println("Method:" + methodCall.enclosingClass.name + ":" + methodCall.lineNumber + "-->" + methodCall.className + "." + methodCall.methodName)
//                    methodCall.replace("{\$_ = " + defaultExecutor + ";}")
//                }
//            }
//        }
//
//        @Override
//        void edit(NewExpr expr) {
//            if (!checkEnclosingClass(expr)) return
//            if (expr.className == ThreadPoolExecutorClassName) {
//                println("NewInstance: " + expr.enclosingClass.name + ":" + expr.lineNumber + "-->" + expr.className)
//                expr.replace("{\$_ = " + defaultExecutor + ";}")
//            }
//        }
//
//        private boolean checkEnclosingClass(Expr methodCall) {
//            return methodCall != null &&
//                    !defaultExecutor.contains(methodCall.enclosingClass.name) &&
//                    ExecutorsClassName != methodCall.enclosingClass.name &&
//                    ThreadPoolExecutorClassName != methodCall.enclosingClass.name &&
//                    ScheduledThreadPoolExecutorClassName != methodCall.enclosingClass.name &&
//                    !matchFilterClass(methodCall.enclosingClass.name)
//        }
//
//        private boolean matchFilterClass(String className) {
//            boolean isContains = false
//            for (int i = 0; i < defaultFilterClassSignaturePrefix.size(); i++) {
//                String packageName = defaultFilterClassSignaturePrefix.get(i)
//                if (className.contains(packageName)) {
//                    isContains = true
//                    break
//                }
//            }
//            return isContains
//        }
//    }
//}