com.github.javaparser.ast.visitor.VoidVisitorAdapter Java Examples

The following examples show how to use com.github.javaparser.ast.visitor.VoidVisitorAdapter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PackageDocScanParser.java    From joyqueue with Apache License 2.0 6 votes vote down vote up
private Map<String, JavadocComment> parseDoc(File classFile) {
    Map<String, JavadocComment> classDoc = new HashMap<>();
    try {
        CompilationUnit cu = StaticJavaParser.parse(classFile, StandardCharsets.UTF_8);
        new VoidVisitorAdapter<Object>() {
            @Override
            public void visit(JavadocComment comment, Object arg) {
                super.visit(comment, arg);
                if (comment.getCommentedNode().get() instanceof MethodDeclaration) {
                    MethodDeclaration node = (MethodDeclaration) comment.getCommentedNode().get();
                    classDoc.put(methodName(node), comment);
                }
            }
        }.visit(cu, null);
    } catch (Exception e) {
        logger.info("ERROR PROCESSING ", e);
    }
    return classDoc;
}
 
Example #2
Source File: EntitySourceAnalyzer.java    From enkan with Eclipse Public License 1.0 6 votes vote down vote up
public List<EntityField> analyze(File source) throws IOException, ParseException {
    List<EntityField> entityFields = new ArrayList<>();
    CompilationUnit cu = JavaParser.parse(source);
    cu.accept(new VoidVisitorAdapter<List<EntityField>>() {
        @Override
        public void visit(FieldDeclaration fd, List<EntityField> f) {
            if (fd.getAnnotations().stream().anyMatch(anno -> anno.getName().getName().equals("Column"))) {
                Class<?> type = null;
                switch (fd.getType().toString()) {
                    case "String": type = String.class; break;
                    case "Long": type = Long.class; break;
                    case "Integer": type = Integer.class; break;
                    case "boolean": type = boolean.class; break;
                }
                if (type == null) return;
                f.add(new EntityField(
                        fd.getVariables().get(0).getId().getName(),
                        type,
                        fd.getAnnotations().stream().anyMatch(anno -> anno.getName().getName().equals("Id"))));
            }
        }
    }, entityFields);

    return entityFields;
}
 
Example #3
Source File: A_Nodes.java    From deadcode4j with Apache License 2.0 5 votes vote down vote up
@Test
public void calculatesTypeNames() throws IOException, ParseException {
    CompilationUnit compilationUnit = JavaParser.parse(
            FileLoader.getFile("de/is24/javaparser/TypeNameTestClass.java"));

    compilationUnit.accept(new VoidVisitorAdapter<Void>() {
        @Override
        public void visit(StringLiteralExpr n, Void arg) {
            assertThat("Name of anonymous class is invalid!", Nodes.getTypeName(n), is(n.getValue()));
        }
    }, null);
}
 
Example #4
Source File: JavaFileUtils.java    From xDoc with MIT License 4 votes vote down vote up
public static Map<String, String> analysisFieldComments(Class<?> classz) {

        final Map<String, String> commentMap = new HashMap(10);

        List<Class> classes = new LinkedList<>();

        Class nowClass = classz;

        //获取所有的属性注释(包括父类的)
        while (true) {
            classes.add(0, nowClass);
            if (Object.class.equals(nowClass) || Object.class.equals(nowClass.getSuperclass())) {
                break;
            }
            nowClass = nowClass.getSuperclass();
        }

        //反方向循环,子类属性注释覆盖父类属性
        for (Class clz : classes) {
            String path = JavaSourceFileManager.getInstance().getPath(clz.getSimpleName());
            if (StringUtils.isBlank(path)) {
                continue;
            }
            try (FileInputStream in = new FileInputStream(path)) {
                CompilationUnit cu = javaParser.parse(in).getResult().get();

                new VoidVisitorAdapter<Void>() {
                    @Override
                    public void visit(FieldDeclaration n, Void arg) {
                        String name = n.getVariable(0).getName().asString();

                        String comment = "";
                        if (n.getComment().isPresent()) {
                            comment = n.getComment().get().getContent();
                        }

                        if (name.contains("=")) {
                            name = name.substring(0, name.indexOf("=")).trim();
                        }

                        commentMap.put(name, CommentUtils.parseCommentText(comment));
                    }
                }.visit(cu, null);

            } catch (Exception e) {
                logger.warn("读取java原文件失败:{}", path, e.getMessage(), e);
            }
        }

        return commentMap;
    }