Search in sources :

Example 81 with NodeList

use of com.github.javaparser.ast.NodeList in project mybatis-generator-gui-extension by spawpaw.

the class MyShellCallback method mergeCompilationUnit.

/**
 * 合并可编译单元(即合并两个Java文件)
 */
private CompilationUnit mergeCompilationUnit(CompilationUnit oldCompilationUnit, CompilationUnit newCompilationUnit) {
    CompilationUnit finalCompilationUnit = new CompilationUnit();
    // 修改包名为新类的包名
    if (newCompilationUnit.getPackageDeclaration().isPresent())
        finalCompilationUnit.setPackageDeclaration(newCompilationUnit.getPackageDeclaration().get());
    // 合并import
    Set<ImportDeclaration> importSet = new HashSet<>();
    importSet.addAll(oldCompilationUnit.getImports());
    importSet.addAll(newCompilationUnit.getImports());
    NodeList<ImportDeclaration> imports = new NodeList<>();
    imports.addAll(importSet);
    finalCompilationUnit.setImports(imports);
    // 合并topLevelClass
    finalCompilationUnit.setTypes(mergeTypes(oldCompilationUnit.getTypes(), newCompilationUnit.getTypes()));
    return finalCompilationUnit;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) NodeList(com.github.javaparser.ast.NodeList) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration)

Example 82 with NodeList

use of com.github.javaparser.ast.NodeList in project flow by vaadin.

the class GeneratorUtilsTest method should_Not_BeConsideredAsHavingAnAnnotation_When_GivenClassHavsAnnotationDeclarationButWithDifferentImport.

@Test
public void should_Not_BeConsideredAsHavingAnAnnotation_When_GivenClassHavsAnnotationDeclarationButWithDifferentImport() {
    NodeWithAnnotations<?> declarationWithEndpointAnnotation = Mockito.mock(NodeWithAnnotations.class);
    CompilationUnit compilationUnitWithNonVaadinEndpointImport = Mockito.mock(CompilationUnit.class);
    AnnotationExpr endpointAnnotation = Mockito.mock(AnnotationExpr.class);
    Mockito.doReturn(Optional.of(endpointAnnotation)).when(declarationWithEndpointAnnotation).getAnnotationByClass(Endpoint.class);
    NodeList<ImportDeclaration> imports = new NodeList<>();
    ImportDeclaration importDeclaration = Mockito.mock(ImportDeclaration.class);
    Mockito.doReturn("some.non.vaadin.Endpoint").when(importDeclaration).getNameAsString();
    imports.add(importDeclaration);
    Mockito.doReturn(imports).when(compilationUnitWithNonVaadinEndpointImport).getImports();
    Assert.assertFalse("A class with a non Vaadin Endpoint should not be considered as an Endpoint", GeneratorUtils.hasAnnotation(declarationWithEndpointAnnotation, compilationUnitWithNonVaadinEndpointImport, Endpoint.class));
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) Endpoint(dev.hilla.Endpoint) NodeList(com.github.javaparser.ast.NodeList) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) Test(org.junit.Test)

Example 83 with NodeList

use of com.github.javaparser.ast.NodeList in project flow by vaadin.

the class OpenAPIObjectGenerator method toMappedType.

ResolvedType toMappedType(Type type) {
    ResolvedType resolvedType;
    try {
        resolvedType = type.resolve();
    } catch (UnsupportedOperationException e) {
        // This is called for T
        return null;
    }
    if (!resolvedType.isReferenceType()) {
        return null;
    }
    String className = getFullyQualifiedName(new GeneratorType(type));
    String mappedClassName = endpointTransferMapper.getTransferType(className);
    if (mappedClassName == null) {
        return null;
    }
    List<ResolvedType> typeArguments = new ArrayList<>();
    if (type.isClassOrInterfaceType()) {
        Optional<NodeList<Type>> maybeTypeArgs = type.asClassOrInterfaceType().getTypeArguments();
        if (maybeTypeArgs.isPresent()) {
            NodeList<Type> typeArgs = maybeTypeArgs.get();
            for (Type typeArg : typeArgs) {
                typeArguments.add(typeArg.resolve());
            }
        }
    }
    ResolvedReferenceTypeDeclaration solved = typeSolver.solveType(mappedClassName);
    return new ReferenceTypeImpl(solved, typeArguments, typeSolver);
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) MediaType(io.swagger.v3.oas.models.media.MediaType) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) NodeList(com.github.javaparser.ast.NodeList) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ArrayList(java.util.ArrayList) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Aggregations

NodeList (com.github.javaparser.ast.NodeList)83 Expression (com.github.javaparser.ast.expr.Expression)48 NameExpr (com.github.javaparser.ast.expr.NameExpr)40 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)37 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)36 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)29 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)25 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)24 List (java.util.List)22 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)18 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)18 CompilationUnit (com.github.javaparser.ast.CompilationUnit)17 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)17 ArrayList (java.util.ArrayList)17 Collectors (java.util.stream.Collectors)17 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)16 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)16 Parameter (com.github.javaparser.ast.body.Parameter)15 Test (org.junit.Test)15 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)14