Search in sources :

Example 21 with ParserConfiguration

use of com.github.javaparser.ParserConfiguration in project drools by kiegroup.

the class DroolsMvelParserTest method testLineBreakAtTheEndOfStatementWithoutSemicolon.

@Test
public void testLineBreakAtTheEndOfStatementWithoutSemicolon() {
    String expr = "{  Person p2 = new Person(\"John\");\n" + // a line break at the end of the statement without a semicolon
    "  p2.age = 30\n" + "insert(p2);\n }";
    MvelParser mvelParser = new MvelParser(new ParserConfiguration(), true);
    ParseResult<BlockStmt> r = mvelParser.parse(GeneratedMvelParser::BlockParseStart, new StringProvider(expr));
    BlockStmt blockStmt = r.getResult().get();
    assertEquals("Should parse 3 statements", 3, blockStmt.getStatements().size());
}
Also used : BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ParserConfiguration(com.github.javaparser.ParserConfiguration) Test(org.junit.Test)

Example 22 with ParserConfiguration

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

the class JavaParserUtil method parseExpression.

/**
 * Parses the {@code expression} and returns an {@code Expression} that represents it.
 *
 * <p>This is like {@code StaticJavaParser.parseExpression}, but it does not lead to memory leaks
 * because it creates a new instance of JavaParser each time it is invoked. Re-using {@code
 * StaticJavaParser} causes memory problems because it retains too much memory.
 *
 * @param expression the expression string
 * @param languageLevel the language level to use when parsing the Java source
 * @return the parsed expression
 * @throws ParseProblemException if the expression has parser errors
 */
public static Expression parseExpression(String expression, LanguageLevel languageLevel) {
    // The ParserConfiguration accumulates data each time parse is called, so create a new one each
    // time.  There's no method to set the ParserConfiguration used by a JavaParser, so a JavaParser
    // has to be created each time.
    ParserConfiguration configuration = new ParserConfiguration();
    configuration.setLanguageLevel(languageLevel);
    configuration.setStoreTokens(false);
    configuration.setLexicalPreservationEnabled(false);
    configuration.setAttributeComments(false);
    configuration.setDetectOriginalLineSeparator(false);
    JavaParser javaParser = new JavaParser(configuration);
    ParseResult<Expression> parseResult = javaParser.parseExpression(expression);
    if (parseResult.isSuccessful() && parseResult.getResult().isPresent()) {
        return parseResult.getResult().get();
    } else {
        throw new ParseProblemException(parseResult.getProblems());
    }
}
Also used : JavaParser(com.github.javaparser.JavaParser) Expression(com.github.javaparser.ast.expr.Expression) ParserConfiguration(com.github.javaparser.ParserConfiguration) ParseProblemException(com.github.javaparser.ParseProblemException)

Example 23 with ParserConfiguration

use of com.github.javaparser.ParserConfiguration in project flow by vaadin.

the class OpenAPIObjectGenerator method createParserConfiguration.

private ParserConfiguration createParserConfiguration() {
    typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(false));
    if (typeResolverClassLoader != null) {
        typeSolver.add(new ClassLoaderTypeSolver(typeResolverClassLoader));
    }
    JavaSymbolSolver symbolResolver = new JavaSymbolSolver(typeSolver);
    return new ParserConfiguration().setSymbolResolver(symbolResolver);
}
Also used : JavaSymbolSolver(com.github.javaparser.symbolsolver.JavaSymbolSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) ClassLoaderTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ClassLoaderTypeSolver) ParserConfiguration(com.github.javaparser.ParserConfiguration)

Example 24 with ParserConfiguration

use of com.github.javaparser.ParserConfiguration in project flow by vaadin.

the class OpenAPIObjectGenerator method init.

private void init() {
    if (javaSourcePaths == null || configuration == null) {
        throw new IllegalStateException("Java source path and configuration should not be null");
    }
    openApiModel = createBasicModel();
    nonEndpointMap = new HashMap<>();
    endpointExposedMap = new HashMap<>();
    qualifiedNameToPath = new HashMap<>();
    pathItems = new TreeMap<>();
    usedTypes = new HashMap<>();
    generatedSchema = new HashSet<>();
    endpointsJavadoc = new HashMap<>();
    schemaGenerator = new SchemaGenerator(this);
    needsDeferrableImport = false;
    ParserConfiguration parserConfiguration = createParserConfiguration();
    javaSourcePaths.stream().map(path -> new SourceRoot(path, parserConfiguration)).forEach(sourceRoot -> parseSourceRoot(sourceRoot, this::findEndpointExposed));
    javaSourcePaths.stream().map(path -> new SourceRoot(path, parserConfiguration)).forEach(sourceRoot -> parseSourceRoot(sourceRoot, this::process));
    for (Map.Entry<String, GeneratorType> entry : new ArrayList<>(usedTypes.entrySet())) {
        List<Schema> schemas = createSchemasFromQualifiedNameAndType(entry.getKey(), entry.getValue());
        schemas.forEach(schema -> {
            if (qualifiedNameToPath.get(schema.getName()) != null) {
                schema.addExtension(EXTENSION_VAADIN_FILE_PATH, qualifiedNameToPath.get(schema.getName()));
            }
            openApiModel.getComponents().addSchemas(schema.getName(), schema);
        });
    }
    addTagsInformation();
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) RolesAllowed(javax.annotation.security.RolesAllowed) LoggerFactory(org.slf4j.LoggerFactory) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement) Operation(io.swagger.v3.oas.models.Operation) LiteralStringValueExpr(com.github.javaparser.ast.expr.LiteralStringValueExpr) Pair(com.github.javaparser.utils.Pair) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) NodeWithSimpleName(com.github.javaparser.ast.nodeTypes.NodeWithSimpleName) Type(com.github.javaparser.ast.type.Type) Scopes(io.swagger.v3.oas.models.security.Scopes) Map(java.util.Map) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Tag(io.swagger.v3.oas.models.tags.Tag) Path(java.nio.file.Path) JavaSymbolSolver(com.github.javaparser.symbolsolver.JavaSymbolSolver) NodeList(com.github.javaparser.ast.NodeList) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) Content(io.swagger.v3.oas.models.media.Content) PermitAll(javax.annotation.security.PermitAll) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) Collection(java.util.Collection) MediaType(io.swagger.v3.oas.models.media.MediaType) Set(java.util.Set) ParserConfiguration(com.github.javaparser.ParserConfiguration) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) EndpointTransferMapper(dev.hilla.endpointransfermapper.EndpointTransferMapper) Paths(io.swagger.v3.oas.models.Paths) Endpoint(dev.hilla.Endpoint) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) EndpointNameChecker(dev.hilla.EndpointNameChecker) Server(io.swagger.v3.oas.models.servers.Server) Optional(java.util.Optional) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) EndpointExposed(dev.hilla.EndpointExposed) ParseResult(com.github.javaparser.ParseResult) Callback(com.github.javaparser.utils.SourceRoot.Callback) Parameter(com.github.javaparser.ast.body.Parameter) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) HashMap(java.util.HashMap) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) TreeSet(java.util.TreeSet) OAuthFlows(io.swagger.v3.oas.models.security.OAuthFlows) ArrayList(java.util.ArrayList) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) JavadocBlockTag(com.github.javaparser.javadoc.JavadocBlockTag) Schema(io.swagger.v3.oas.models.media.Schema) DenyAll(javax.annotation.security.DenyAll) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) SourceRoot(com.github.javaparser.utils.SourceRoot) Logger(org.slf4j.Logger) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) PathItem(io.swagger.v3.oas.models.PathItem) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) Info(io.swagger.v3.oas.models.info.Info) MapSchema(io.swagger.v3.oas.models.media.MapSchema) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) TreeMap(java.util.TreeMap) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) OAuthFlow(io.swagger.v3.oas.models.security.OAuthFlow) Javadoc(com.github.javaparser.javadoc.Javadoc) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) Components(io.swagger.v3.oas.models.Components) AnonymousAllowed(com.vaadin.flow.server.auth.AnonymousAllowed) ClassLoaderTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ClassLoaderTypeSolver) Comparator(java.util.Comparator) Collections(java.util.Collections) ResolvedTypeParametersMap(com.github.javaparser.resolution.types.parametrization.ResolvedTypeParametersMap) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) ArrayList(java.util.ArrayList) SourceRoot(com.github.javaparser.utils.SourceRoot) ParserConfiguration(com.github.javaparser.ParserConfiguration) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) ResolvedTypeParametersMap(com.github.javaparser.resolution.types.parametrization.ResolvedTypeParametersMap)

Aggregations

ParserConfiguration (com.github.javaparser.ParserConfiguration)24 JavaParser (com.github.javaparser.JavaParser)14 CompilationUnit (com.github.javaparser.ast.CompilationUnit)11 Test (org.junit.Test)8 ParseProblemException (com.github.javaparser.ParseProblemException)6 Expression (com.github.javaparser.ast.expr.Expression)4 Path (java.nio.file.Path)4 StreamProvider (com.github.javaparser.StreamProvider)3 ParseResult (com.github.javaparser.ParseResult)2 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)2 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)2 JavaSymbolSolver (com.github.javaparser.symbolsolver.JavaSymbolSolver)2 ClassLoaderTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ClassLoaderTypeSolver)2 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)2 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)2 SourceZip (com.github.javaparser.utils.SourceZip)2 FileInputStream (java.io.FileInputStream)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2