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