Search in sources :

Example 41 with TypeDeclaration

use of com.github.javaparser.ast.body.TypeDeclaration in project Hidoc by yuzhengyang.

the class Jptest method main.

public static void main(String[] args) throws FileNotFoundException {
    // parse() 参数可以是 String, File, InputStream等
    String filepath = "D:\\code\\projects\\haixin-projects\\hicmp-utils\\hicmp-utils\\hicmp-common-hiutils\\src\\main\\java\\com\\histone\\hiutils\\hiDbUtils\\hiDbAssistUtils\\HiDbModel.java";
    CompilationUnit cu = StaticJavaParser.parse(new File(filepath));
    List<TypeDeclaration> cds = cu.findAll(TypeDeclaration.class);
    List<MethodDeclaration> mds = cu.findAll(MethodDeclaration.class);
    mds.forEach(md -> System.out.println(md.toString() + "\n------------------------------\n"));
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) File(java.io.File) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration)

Example 42 with TypeDeclaration

use of com.github.javaparser.ast.body.TypeDeclaration in project peass by DaGeRe.

the class TestSourceDetection method testDirectoryWalker.

@Test
public void testDirectoryWalker() throws FileNotFoundException {
    final File file = new File(SOURCE, "DirectoryWalkerTestCase.java");
    final CompilationUnit cu = JavaParserProvider.parse(file);
    final Map<String, TypeDeclaration<?>> named = TraceReadUtils.getNamedClasses(cu, "");
    Assert.assertEquals(4, named.size());
    MatcherAssert.assertThat(named.get("DirectoryWalkerTestCase$TestFileFinder").toString(), Matchers.containsString("List results"));
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) File(java.io.File) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) Test(org.junit.jupiter.api.Test)

Example 43 with TypeDeclaration

use of com.github.javaparser.ast.body.TypeDeclaration in project cornelius by bkushigian.

the class PegTranslator method translate.

public PegNode translate(final CompilationUnit cu, final String canonical) {
    if (canonical == null)
        return PegNode.unit();
    final NodeList<TypeDeclaration<?>> types = cu.getTypes();
    types.sort(Comparator.comparing(TypeDeclaration::getNameAsString));
    for (TypeDeclaration<?> type : types) {
        if (type.isClassOrInterfaceDeclaration()) {
            final ClassOrInterfaceDeclaration ctype = type.asClassOrInterfaceDeclaration();
            final PegClassVisitor.ClassVisitorResult classVisitorResult = classVisitor.visit(ctype);
            if (ctype.isInterface())
                continue;
            for (MethodDeclaration method : ctype.getMethods()) {
                if (canonical.equals(Util.canonicalizeDeclarationName(method))) {
                    return translate(method, classVisitorResult);
                }
            }
        }
    }
    return PegNode.unit();
}
Also used : ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration)

Example 44 with TypeDeclaration

use of com.github.javaparser.ast.body.TypeDeclaration in project Matcher by sfPlayer1.

the class TypeResolver method getCls.

public ClassInstance getCls(Node node) {
    StringBuilder sb = new StringBuilder();
    if (pkg != null) {
        sb.append(pkg);
        sb.append('/');
    }
    int pkgEnd = sb.length();
    do {
        if (node instanceof ClassOrInterfaceDeclaration || node instanceof EnumDeclaration) {
            TypeDeclaration<?> typeDecl = (TypeDeclaration<?>) node;
            String namePart = typeDecl.getName().getIdentifier();
            if (sb.length() > pkgEnd) {
                sb.insert(pkgEnd, '$');
                sb.insert(pkgEnd, namePart);
            } else {
                sb.append(namePart);
            }
        }
    } while ((node = node.getParentNode().orElse(null)) != null);
    ClassInstance cls = getClsByName(sb.toString());
    return cls;
}
Also used : ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) ClassInstance(matcher.type.ClassInstance) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration)

Aggregations

TypeDeclaration (com.github.javaparser.ast.body.TypeDeclaration)44 CompilationUnit (com.github.javaparser.ast.CompilationUnit)22 Node (com.github.javaparser.ast.Node)14 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)13 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)12 BodyDeclaration (com.github.javaparser.ast.body.BodyDeclaration)10 ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)8 List (java.util.List)8 Optional (java.util.Optional)8 ResolvedTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)6 SymbolReference (com.github.javaparser.symbolsolver.model.resolution.SymbolReference)6 Collectors (java.util.stream.Collectors)6 ImportDeclaration (com.github.javaparser.ast.ImportDeclaration)5 NodeList (com.github.javaparser.ast.NodeList)5 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)5 EnumDeclaration (com.github.javaparser.ast.body.EnumDeclaration)5 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)5 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)5 ResolvedValueDeclaration (com.github.javaparser.resolution.declarations.ResolvedValueDeclaration)5 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)5