Search in sources :

Example 1 with SwitchStmt

use of com.github.javaparser.ast.stmt.SwitchStmt in project javaparser by javaparser.

the class TokenKindGenerator method generate.

@Override
public void generate() {
    Log.info("Running %s", getClass().getSimpleName());
    final CompilationUnit javaTokenCu = sourceRoot.parse("com.github.javaparser", "JavaToken.java");
    final ClassOrInterfaceDeclaration javaToken = javaTokenCu.getClassByName("JavaToken").orElseThrow(() -> new AssertionError("Can't find class in java file."));
    final EnumDeclaration kindEnum = javaToken.findFirst(EnumDeclaration.class, e -> e.getNameAsString().equals("Kind")).orElseThrow(() -> new AssertionError("Can't find class in java file."));
    kindEnum.getEntries().clear();
    annotateGenerated(kindEnum);
    final SwitchStmt valueOfSwitch = kindEnum.findFirst(SwitchStmt.class).orElseThrow(() -> new AssertionError("Can't find valueOf switch."));
    valueOfSwitch.findAll(SwitchEntryStmt.class).stream().filter(e -> e.getLabel().isPresent()).forEach(Node::remove);
    final CompilationUnit constantsCu = generatedJavaCcSourceRoot.parse("com.github.javaparser", "GeneratedJavaParserConstants.java");
    final ClassOrInterfaceDeclaration constants = constantsCu.getInterfaceByName("GeneratedJavaParserConstants").orElseThrow(() -> new AssertionError("Can't find class in java file."));
    for (BodyDeclaration<?> member : constants.getMembers()) {
        member.toFieldDeclaration().filter(field -> {
            String javadoc = field.getJavadocComment().get().getContent();
            return javadoc.contains("RegularExpression Id") || javadoc.contains("End of File");
        }).map(field -> field.getVariable(0)).ifPresent(var -> {
            final String name = var.getNameAsString();
            final IntegerLiteralExpr kind = var.getInitializer().get().asIntegerLiteralExpr();
            generateEnumEntry(kindEnum, name, kind);
            generateValueOfEntry(valueOfSwitch, name, kind);
        });
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) NodeList(com.github.javaparser.ast.NodeList) Generator(com.github.javaparser.generator.Generator) SourceRoot(com.github.javaparser.utils.SourceRoot) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) EnumConstantDeclaration(com.github.javaparser.ast.body.EnumConstantDeclaration) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration) Log(com.github.javaparser.utils.Log) SwitchEntryStmt(com.github.javaparser.ast.stmt.SwitchEntryStmt) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Node(com.github.javaparser.ast.Node) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Node(com.github.javaparser.ast.Node) SwitchEntryStmt(com.github.javaparser.ast.stmt.SwitchEntryStmt) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration)

Example 2 with SwitchStmt

use of com.github.javaparser.ast.stmt.SwitchStmt in project javaparser by javaparser.

the class EnumResolutionTest method switchOnEnum.

@Test
public void switchOnEnum() {
    CompilationUnit cu = parseSample("SwitchOnEnum");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "SwitchOnEnum");
    MethodDeclaration method = Navigator.demandMethod(clazz, "foo");
    SwitchStmt switchStmt = Navigator.findSwitch(method);
    Expression expression = switchStmt.getEntries().get(0).getLabel().get();
    SymbolReference<? extends ResolvedValueDeclaration> ref = JavaParserFacade.get(new ReflectionTypeSolver()).solve(expression);
    assertTrue(ref.isSolved());
    assertEquals("SwitchOnEnum.MyEnum", ref.getCorrespondingDeclaration().getType().asReferenceType().getQualifiedName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) Test(org.junit.Test)

Example 3 with SwitchStmt

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

the class FEELPropertyAccessibleImplementation method getFeelPropertyDefinition.

private MethodDefinition getFeelPropertyDefinition() {
    MethodDeclaration getFEELProperty = cloneMethodTemplate("getFEELProperty");
    SwitchStmt firstSwitch = getFEELProperty.findFirst(SwitchStmt.class).orElseThrow(() -> new InvalidTemplateException("Missing Switch Statement in getFEELProperty template"));
    firstSwitch.setComment(null);
    List<SwitchEntry> collect = fields.stream().map(this::toGetPropertySwitchEntry).collect(Collectors.toList());
    SwitchEntry defaultSwitchStmt = firstSwitch.findFirst(SwitchEntry.class, sw -> sw.getLabels().isEmpty()).orElseThrow(() -> new InvalidTemplateException("Missing Default Switch Statement in getFEELProperty template"));
    collect.add(defaultSwitchStmt);
    firstSwitch.setEntries(nodeList(collect));
    String body = getFEELProperty.getBody().orElseThrow(() -> new InvalidTemplateException("Empty body in getFeelProperty clone")).toString();
    MethodWithStringBody getFeelPropertyDefinition = new MethodWithStringBody("getFEELProperty", EvalHelper.PropertyValueResult.class.getCanonicalName(), body).addParameter(String.class.getCanonicalName(), "property");
    addOverrideAnnotation(getFeelPropertyDefinition);
    return getFeelPropertyDefinition;
}
Also used : MethodWithStringBody(org.drools.modelcompiler.builder.generator.declaredtype.api.MethodWithStringBody) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) NodeList.nodeList(com.github.javaparser.ast.NodeList.nodeList) CastExpr(com.github.javaparser.ast.expr.CastExpr) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) ArrayList(java.util.ArrayList) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) NodeList(com.github.javaparser.ast.NodeList) FieldDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) TypeDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.TypeDefinition) MethodDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.MethodDefinition) StringUtils.ucFirst(org.drools.core.util.StringUtils.ucFirst) NameExpr(com.github.javaparser.ast.expr.NameExpr) Statement(com.github.javaparser.ast.stmt.Statement) Collectors(java.util.stream.Collectors) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) List(java.util.List) StaticJavaParser(com.github.javaparser.StaticJavaParser) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) InputStream(java.io.InputStream) EvalHelper(org.kie.dmn.feel.util.EvalHelper) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) EvalHelper(org.kie.dmn.feel.util.EvalHelper) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) MethodWithStringBody(org.drools.modelcompiler.builder.generator.declaredtype.api.MethodWithStringBody) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry)

Example 4 with SwitchStmt

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

the class AccessibleMethod method switchOnFieldName.

private SwitchStmt switchOnFieldName() {
    SwitchStmt switchStmt = new SwitchStmt();
    switchStmt.setSelector(new NameExpr(FIELD_NAME));
    return switchStmt;
}
Also used : SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr)

Example 5 with SwitchStmt

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

the class StatementVisitor method visit.

@Override
public TypedExpression visit(SwitchStmt n, Void arg) {
    TypedExpression typedSelector = new RHSPhase(mvelCompilerContext).invoke(n.getSelector());
    List<TypedExpression> typedEntries = n.getEntries().stream().map(e -> e.accept(this, arg)).collect(Collectors.toList());
    return new SwitchStmtT(typedSelector, typedEntries);
}
Also used : ForEachStmt(com.github.javaparser.ast.stmt.ForEachStmt) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) BlockStmtT(org.drools.mvelcompiler.ast.BlockStmtT) WhileStmt(com.github.javaparser.ast.stmt.WhileStmt) Optional.of(java.util.Optional.of) DoStmtT(org.drools.mvelcompiler.ast.DoStmtT) ForEachDowncastStmtT(org.drools.mvelcompiler.ast.ForEachDowncastStmtT) DoStmt(com.github.javaparser.ast.stmt.DoStmt) Declaration(org.drools.mvelcompiler.context.Declaration) WhileStmtT(org.drools.mvelcompiler.ast.WhileStmtT) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) Expression(com.github.javaparser.ast.expr.Expression) ForEachStmtT(org.drools.mvelcompiler.ast.ForEachStmtT) Node(com.github.javaparser.ast.Node) IfStmtT(org.drools.mvelcompiler.ast.IfStmtT) SwitchStmtT(org.drools.mvelcompiler.ast.SwitchStmtT) Optional.ofNullable(java.util.Optional.ofNullable) IfStmt(com.github.javaparser.ast.stmt.IfStmt) Collectors(java.util.stream.Collectors) PrintUtil(org.drools.mvel.parser.printer.PrintUtil) ForStmt(com.github.javaparser.ast.stmt.ForStmt) List(java.util.List) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression) DrlGenericVisitor(org.drools.mvel.parser.ast.visitor.DrlGenericVisitor) ForStmtT(org.drools.mvelcompiler.ast.ForStmtT) Optional(java.util.Optional) MvelCompilerContext(org.drools.mvelcompiler.context.MvelCompilerContext) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) SwitchEntryT(org.drools.mvelcompiler.ast.SwitchEntryT) SwitchStmtT(org.drools.mvelcompiler.ast.SwitchStmtT) UnalteredTypedExpression(org.drools.mvelcompiler.ast.UnalteredTypedExpression) TypedExpression(org.drools.mvelcompiler.ast.TypedExpression)

Aggregations

SwitchStmt (com.github.javaparser.ast.stmt.SwitchStmt)17 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)9 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)6 SwitchEntry (com.github.javaparser.ast.stmt.SwitchEntry)6 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)4 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)4 NameExpr (com.github.javaparser.ast.expr.NameExpr)4 Statement (com.github.javaparser.ast.stmt.Statement)4 CompilationUnit (com.github.javaparser.ast.CompilationUnit)3 NodeList (com.github.javaparser.ast.NodeList)3 Expression (com.github.javaparser.ast.expr.Expression)3 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)3 StaticJavaParser.parseStatement (com.github.javaparser.StaticJavaParser.parseStatement)2 Node (com.github.javaparser.ast.Node)2 IntegerLiteralExpr (com.github.javaparser.ast.expr.IntegerLiteralExpr)2 ForEachStmt (com.github.javaparser.ast.stmt.ForEachStmt)2 IfStmt (com.github.javaparser.ast.stmt.IfStmt)2 SwitchEntryStmt (com.github.javaparser.ast.stmt.SwitchEntryStmt)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2