Package cn.shenyanchao.builder

Examples of cn.shenyanchao.builder.CompilationUnitBuilder


    private void convertJavaFile2Test(File javaFile) {

        CompilationUnit compilationUnit = JavaParserFactory.getCompilationUnit(javaFile, sourceEncode);

        CompilationUnitBuilder compilationUnitBuilder = new CompilationUnitBuilder();
        compilationUnitBuilder.buildComment(FileComments.GENERATOR_COMMENT);
        //process package
        PackageDeclaration packageDeclaration = compilationUnit.getPackage();
        String testPackageName = PackageUtils.getTestPackageNameFrom(packageDeclaration);
        compilationUnitBuilder.buildPackage(testPackageName);
        //process import

        //process type
        List typeList = compilationUnit.getTypes();
        for (Object type : typeList) {
            TypeDeclaration typeDeclaration = (TypeDeclaration) type;
            String className = typeDeclaration.getName();
            getLog().info("className:" + className);
            ClassTypeBuilder classTypeBuilder = new ClassTypeBuilder(className + Consts.TEST_SUFFIX);
            //process methods
            List<MethodDeclaration> methodDeclarations = MembersFilter.findMethodsFrom(typeDeclaration);
            for (MethodDeclaration methodDeclaration : methodDeclarations) {
                //has method and add import
                compilationUnitBuilder.buildImports(null);

                String methodName = methodDeclaration.getName();
                getLog().info("methodName:" + methodDeclaration.getName());
                classTypeBuilder.buildMethod(methodName + Consts.TEST_SUFFIX, methodDeclaration);
            }

            compilationUnitBuilder.buildClass(classTypeBuilder.build());
        }

        CompilationUnit testCompilationUnit = compilationUnitBuilder.build();

        //写入测试代码文件
        TestGenerator.writeJavaTest(testDir, testCompilationUnit);
    }
View Full Code Here

TOP

Related Classes of cn.shenyanchao.builder.CompilationUnitBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.