All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.test4j.mock.processor.MocksTreeTranslator Maven / Gradle / Ivy

//package org.test4j.mock.processor;
//
//import com.sun.source.tree.CompilationUnitTree;
//import com.sun.source.util.Trees;
//import com.sun.tools.javac.code.Flags;
//import com.sun.tools.javac.tree.JCTree;
//import com.sun.tools.javac.tree.TreeTranslator;
//import com.sun.tools.javac.util.List;
//
//import javax.lang.model.element.ElementKind;
//import javax.lang.model.element.TypeElement;
//import javax.tools.JavaFileObject;
//
//public class MocksTreeTranslator extends TreeTranslator {
//    public static final String MOCKS_FIELD_NAME = "mocks";
//
//    private final TypeElement typeElm;
//
//    private final BaseProcessor processor;
//
//    MocksTreeTranslator(BaseProcessor processor, TypeElement typeElm) {
//        this.processor = processor;
//        this.typeElm = typeElm;
//    }
//
//    @Override
//    public void visitClassDef(JCTree.JCClassDecl jcClassDecl) {
//        // 在原有类定义中增加mocks变量
//        JCTree.JCVariableDecl mocks = this.newMocksField();
//        jcClassDecl.defs = jcClassDecl.defs.prepend(mocks);
//        super.visitClassDef(jcClassDecl);
//    }
//
////    @Override
////    public List translateVarDefs(List list) {
////        list.add(this.newMocksField());
////        return super.translateVarDefs(list);
////    }
//
//    private JCTree.JCVariableDecl newMocksField() {
//        String type = typeElm.getSimpleName().toString() + "Mocks";
//        JCTree.JCExpression mocksType = processor.maker.Ident(processor.names.fromString(type));
//        JCTree.JCNewClass newMocks = processor.maker.NewClass(null, List.nil(), mocksType, List.nil(), null);
//        JCTree.JCVariableDecl mocksField = processor.maker.VarDef(
//            processor.maker.Modifiers(Flags.PUBLIC | Flags.STATIC | Flags.FINAL),
//            processor.names.fromString(MOCKS_FIELD_NAME), mocksType, newMocks);
//        return mocksField;
//    }
//
//    public static void addMocksField(BaseProcessor processor, Trees trees, TypeElement typeElm) {
//        if (hasMocksField(typeElm) || !isSourceFile(trees, typeElm)) {
//            return;
//        }
//        // 注入 mocks 成员变量
//        // 生成public static final XyzMocks mocks = new XyzMocks();
//        JCTree tree = (JCTree) trees.getTree(typeElm);
//        tree.accept(new MocksTreeTranslator(processor, typeElm));
//    }
//
//    /**
//     * 是否源码编译
//     *
//     * @param typeElm
//     * @return
//     */
//    public static boolean isSourceFile(Trees trees, TypeElement typeElm) {
//        CompilationUnitTree cuTree = trees.getPath(typeElm).getCompilationUnit();
//        if (cuTree instanceof JCTree.JCCompilationUnit) {
//            JCTree.JCCompilationUnit cu = (JCTree.JCCompilationUnit) cuTree;
//            // only process on files which have been compiled from source
//            if (cu.sourcefile.getKind() != JavaFileObject.Kind.SOURCE) {
//                return false;
//            }
//        }
//        return true;
//    }
//
//    /**
//     * 是否已经定义的 mocks 变量
//     *
//     * @param typeElm
//     * @return
//     */
//    public static boolean hasMocksField(TypeElement typeElm) {
//        long count = typeElm.getEnclosedElements().stream()
//            .filter(e -> e.getKind() == ElementKind.FIELD)
//            .filter(e -> MOCKS_FIELD_NAME.equals(e.getSimpleName().toString()))
//            .count();
//        return count > 0;
//    }
//}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy