Search in sources :

Example 11 with SwitchStmt

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

the class SwitchEntryContext method solveSymbol.

@Override
public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String name, TypeSolver typeSolver) {
    SwitchStmt switchStmt = (SwitchStmt) requireParentNode(wrappedNode);
    ResolvedType type = JavaParserFacade.get(typeSolver).getType(switchStmt.getSelector());
    if (type.isReferenceType() && type.asReferenceType().getTypeDeclaration().isEnum()) {
        if (type instanceof ReferenceTypeImpl) {
            ReferenceTypeImpl typeUsageOfTypeDeclaration = (ReferenceTypeImpl) type;
            if (typeUsageOfTypeDeclaration.getTypeDeclaration().hasField(name)) {
                return SymbolReference.solved(typeUsageOfTypeDeclaration.getTypeDeclaration().getField(name));
            }
        } else {
            throw new UnsupportedOperationException();
        }
    }
    // look for declaration in other switch statements
    for (SwitchEntryStmt seStmt : switchStmt.getEntries()) {
        if (!seStmt.equals(wrappedNode)) {
            for (Statement stmt : seStmt.getStatements()) {
                SymbolDeclarator symbolDeclarator = JavaParserFactory.getSymbolDeclarator(stmt, typeSolver);
                SymbolReference<? extends ResolvedValueDeclaration> symbolReference = solveWith(symbolDeclarator, name);
                if (symbolReference.isSolved()) {
                    return symbolReference;
                }
            }
        }
    }
    return getParent().solveSymbol(name, typeSolver);
}
Also used : SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) SymbolDeclarator(com.github.javaparser.symbolsolver.resolution.SymbolDeclarator) Statement(com.github.javaparser.ast.stmt.Statement) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) SwitchEntryStmt(com.github.javaparser.ast.stmt.SwitchEntryStmt) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Example 12 with SwitchStmt

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

the class JointJavacJavaParserVisitor method visitSwitch.

@Override
public Void visitSwitch(SwitchTree javacTree, Node javaParserNode) {
    SwitchStmt node = castNode(SwitchStmt.class, javaParserNode, javacTree);
    processSwitch(javacTree, node);
    // Switch expressions are always parenthesized in javac but never in JavaParser.
    ExpressionTree expression = ((ParenthesizedTree) javacTree.getExpression()).getExpression();
    expression.accept(this, node.getSelector());
    visitLists(javacTree.getCases(), node.getEntries());
    return null;
}
Also used : SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 13 with SwitchStmt

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

the class PropagatorCompilerHandler method addNewSwitchEntryToStack.

private void addNewSwitchEntryToStack(SwitchEntry switchEntry) {
    SwitchStmt currentSwitch = (SwitchStmt) currentStatement.getFirst();
    BlockStmt block = new BlockStmt();
    block.setParentNode(switchEntry);
    this.currentStatement.push(block);
    switchEntry.setStatements(nodeList(block));
    currentSwitch.getEntries().add(switchEntry);
}
Also used : SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt)

Example 14 with SwitchStmt

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

the class PropagatorCompilerHandler method startHashedAlphaNodes.

@Override
public void startHashedAlphaNodes(IndexableConstraint indexableConstraint) {
    final InternalReadAccessor fieldExtractor = indexableConstraint.getFieldExtractor();
    fieldType = fieldExtractor.getExtractToClass();
    final SwitchStmt switchStmt;
    final Statement nullCheck;
    if (canInlineValue(fieldType)) {
        String switchVariableName = "switchVar";
        ExpressionStmt switchVariable = localVariableWithCastInitializer(toJPType(fieldType), switchVariableName, parseExpression("readAccessor.getValue(fact)"));
        this.allStatements.addStatement(switchVariable);
        switchStmt = new SwitchStmt().setSelector(new NameExpr(switchVariableName));
        if (fieldType.isPrimitive()) {
            nullCheck = new BlockStmt().addStatement(switchStmt);
        } else {
            nullCheck = new IfStmt().setCondition(new BinaryExpr(new NameExpr(switchVariableName), new NullLiteralExpr(), BinaryExpr.Operator.NOT_EQUALS)).setThenStmt(switchStmt);
        }
    } else {
        // Hashable but not inlinable
        String localVariableName = "NodeId";
        ExpressionStmt expressionStmt = localVariableWithCastInitializer(parseType("java.lang.Integer"), localVariableName, parseExpression("ToNodeId.get(readAccessor.getValue(fact))"));
        this.allStatements.addStatement(expressionStmt);
        switchStmt = new SwitchStmt().setSelector(new MethodCallExpr(new NameExpr(localVariableName), "intValue", nodeList()));
        // ensure that the value is present in the node map
        nullCheck = new IfStmt().setCondition(new BinaryExpr(new NameExpr(localVariableName), new NullLiteralExpr(), BinaryExpr.Operator.NOT_EQUALS)).setThenStmt(switchStmt);
    }
    this.allStatements.addStatement(nullCheck);
    this.currentStatement.push(switchStmt);
}
Also used : NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) IfStmt(com.github.javaparser.ast.stmt.IfStmt) StaticJavaParser.parseStatement(com.github.javaparser.StaticJavaParser.parseStatement) Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) NameExpr(com.github.javaparser.ast.expr.NameExpr) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 15 with SwitchStmt

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

the class AccessibleMethod method getterSwitchStatement.

private Statement getterSwitchStatement() {
    SwitchStmt switchStmt = switchOnFieldName();
    NodeList<SwitchEntry> switchEntries = switchStmt.getEntries();
    for (DescrFieldDefinition field : fields) {
        if (!field.isOverride()) {
            switchEntries.add(getValueFromField(field));
        }
    }
    Optional<Class<?>> abstractResolvedClass = descrTypeDefinition.getAbstractResolvedClass();
    if (abstractResolvedClass.isPresent()) {
        switchEntries.addAll(superClassGetterEntries(abstractResolvedClass.get()).collect(toList()));
    } else if (descrTypeDefinition.getDeclaredAbstractClass().isPresent()) {
        switchEntries.add(switchEntry(SUPER_GET_VALUE));
    } else {
        switchEntries.add(switchEntry(RETURN_NULL));
    }
    return new BlockStmt(nodeList(switchStmt));
}
Also used : SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry)

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