Search in sources :

Example 21 with ImportDeclaration

use of com.github.javaparser.ast.ImportDeclaration in project javaparser by javaparser.

the class NameTest method importName.

@Test
public void importName() {
    ImportDeclaration importDeclaration = parseImport("import java.@Abc util.List;");
    assertThat(importDeclaration.getName().getQualifier().get().getAnnotations()).containsExactly(new MarkerAnnotationExpr("Abc"));
    assertEquals("import java.@Abc util.List;" + EOL, importDeclaration.toString());
    assertEquals("import java.@Abc util.List;", ConcreteSyntaxModel.genericPrettyPrint(importDeclaration));
}
Also used : ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) Test(org.junit.Test)

Example 22 with ImportDeclaration

use of com.github.javaparser.ast.ImportDeclaration in project drools by kiegroup.

the class DrlxVisitor method visit.

public void visit(CompilationUnit u, Void arg) {
    PackageDeclaration packageDeclaration = u.getPackageDeclaration().orElseThrow(() -> new ParseException("Expected package declaration.", -1));
    String pkgName = packageDeclaration.getNameAsString();
    builder.name(pkgName);
    for (ImportDeclaration i : u.getImports()) {
        this.visit(i, null);
    }
    ModuleDeclaration moduleDeclaration = u.getModule().orElseThrow(() -> new ParseException("Expected unit declaration.", -1));
    builder.newUnit().target(String.format("%s.%s", pkgName, moduleDeclaration.getNameAsString()));
    for (TypeDeclaration<?> typeDeclaration : u.getTypes()) {
        RuleDeclaration rd = (RuleDeclaration) typeDeclaration;
        this.visit(rd, null);
    }
}
Also used : ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) ModuleDeclaration(com.github.javaparser.ast.modules.ModuleDeclaration) ParseException(org.drools.drl.parser.lang.ParseException) PackageDeclaration(com.github.javaparser.ast.PackageDeclaration)

Example 23 with ImportDeclaration

use of com.github.javaparser.ast.ImportDeclaration in project drools by kiegroup.

the class JavaParserCompiler method toPojoSource.

public static String toPojoSource(String pkgName, Collection<String> imports, Collection<String> staticImports, TypeDeclaration pojo) {
    CompilationUnit cu = new CompilationUnit();
    cu.setPackageDeclaration(pkgName);
    for (String i : imports) {
        cu.getImports().add(new ImportDeclaration(new Name(i), false, false));
    }
    for (String i : staticImports) {
        cu.getImports().add(new ImportDeclaration(new Name(i), true, false));
    }
    cu.addType(pojo);
    return getPrettyPrinter().print(cu);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) Name(com.github.javaparser.ast.expr.Name)

Example 24 with ImportDeclaration

use of com.github.javaparser.ast.ImportDeclaration in project checker-framework by typetools.

the class JointJavacJavaParserVisitor method visitImport.

@Override
public Void visitImport(ImportTree javacTree, Node javaParserNode) {
    ImportDeclaration node = castNode(ImportDeclaration.class, javaParserNode, javacTree);
    processImport(javacTree, node);
    // stores "a" and records that the name ends in an asterisk.
    if (node.isAsterisk()) {
        assert javacTree.getQualifiedIdentifier().getKind() == Tree.Kind.MEMBER_SELECT;
        MemberSelectTree identifier = (MemberSelectTree) javacTree.getQualifiedIdentifier();
        identifier.getExpression().accept(this, node.getName());
    } else {
        javacTree.getQualifiedIdentifier().accept(this, node.getName());
    }
    return null;
}
Also used : MemberSelectTree(com.sun.source.tree.MemberSelectTree) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration)

Example 25 with ImportDeclaration

use of com.github.javaparser.ast.ImportDeclaration in project checker-framework by typetools.

the class ToIndexFileConverter method extractScene.

/**
 * Entry point of recursive-descent IndexUnit to AScene transformer. It operates by visiting the
 * stub and scene in parallel, descending into them in the same way. It augments the existing
 * scene (it does not create a new scene).
 *
 * @param iu {@link StubUnit} representing stubfile
 */
private static void extractScene(StubUnit iu, AScene scene) {
    for (CompilationUnit cu : iu.getCompilationUnits()) {
        NodeList<TypeDeclaration<?>> typeDecls = cu.getTypes();
        if (typeDecls != null && cu.getPackageDeclaration().isPresent()) {
            List<ImportDeclaration> impDecls = cu.getImports();
            PackageDeclaration pkgDecl = cu.getPackageDeclaration().get();
            for (TypeDeclaration<?> typeDecl : typeDecls) {
                ToIndexFileConverter converter = new ToIndexFileConverter(pkgDecl, impDecls, scene);
                String pkgName = converter.pkgName;
                String name = typeDecl.getNameAsString();
                if (pkgName != null) {
                    name = pkgName + "." + name;
                }
                typeDecl.accept(converter, scene.classes.getVivify(name));
            }
        }
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) PackageDeclaration(com.github.javaparser.ast.PackageDeclaration)

Aggregations

ImportDeclaration (com.github.javaparser.ast.ImportDeclaration)29 CompilationUnit (com.github.javaparser.ast.CompilationUnit)11 Name (com.github.javaparser.ast.expr.Name)6 Test (org.junit.Test)6 NodeList (com.github.javaparser.ast.NodeList)5 TypeDeclaration (com.github.javaparser.ast.body.TypeDeclaration)5 PackageDeclaration (com.github.javaparser.ast.PackageDeclaration)4 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)4 SimpleName (com.github.javaparser.ast.expr.SimpleName)3 HashMap (java.util.HashMap)3 ClassUtil.asJavaSourceName (org.drools.modelcompiler.util.ClassUtil.asJavaSourceName)3 AnnotationDeclaration (com.github.javaparser.ast.body.AnnotationDeclaration)2 EmptyTypeDeclaration (com.github.javaparser.ast.body.EmptyTypeDeclaration)2 EnumDeclaration (com.github.javaparser.ast.body.EnumDeclaration)2 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)2 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)2 MemberValuePair (com.github.javaparser.ast.expr.MemberValuePair)2 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)2 ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)2 ResolvedTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)2