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);
}
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;
}
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);
}
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);
}
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));
}
Aggregations