use of com.github.javaparser.ast.expr.CastExpr in project vue-gwt by Axellience.
the class TemplateParser method processEventParameter.
/**
* Process the $event variable passed on v-on. This variable must have a valid cast in front.
* @param expression The currently processed expression
* @param nameExpr The variable we are processing
* @param parameters The parameters this expression depends on
*/
private void processEventParameter(Expression expression, NameExpr nameExpr, List<VariableInfo> parameters) {
if (nameExpr.getParentNode().isPresent() && nameExpr.getParentNode().get() instanceof CastExpr) {
CastExpr castExpr = (CastExpr) nameExpr.getParentNode().get();
parameters.add(new VariableInfo(castExpr.getType().toString(), "$event"));
} else {
logger.error("\"$event\" should always be casted to it's intended type. Example: @click=\"doSomething((NativeEvent) $event)\".", expression.toString());
}
}
use of com.github.javaparser.ast.expr.CastExpr in project javaparser by javaparser.
the class JavaParserTest method rangeOfLambdaBody.
@Test
public void rangeOfLambdaBody() {
String code = "class A {" + EOL + " Object f() {" + EOL + " return (Comparator<Map.Entry<K, V>> & Serializable)(c1, c2) -> c1.getKey().compareTo(c2.getKey()); " + EOL + "}}";
CompilationUnit cu = JavaParser.parse(code);
MethodDeclaration methodDeclaration = cu.getClassByName("A").get().getMember(0).asMethodDeclaration();
ReturnStmt returnStmt = methodDeclaration.getBody().get().getStatement(0).asReturnStmt();
CastExpr castExpr = returnStmt.getExpression().get().asCastExpr();
LambdaExpr lambdaExpr = castExpr.getExpression().asLambdaExpr();
Statement lambdaBody = lambdaExpr.getBody();
assertEquals(range(3, 68, 3, 101), lambdaBody.getRange().get());
}
use of com.github.javaparser.ast.expr.CastExpr in project javaparser by javaparser.
the class JavaParserTest method rangeOfCastNonIntersection.
@Test
public void rangeOfCastNonIntersection() {
String code = "class A {" + EOL + " Object f() {" + EOL + " return (Comparator<Map.Entry<K, V>> )(c1, c2) -> c1.getKey().compareTo(c2.getKey()); " + EOL + "}}";
CompilationUnit cu = JavaParser.parse(code);
MethodDeclaration methodDeclaration = cu.getClassByName("A").get().getMember(0).asMethodDeclaration();
ReturnStmt returnStmt = methodDeclaration.getBody().get().getStatement(0).asReturnStmt();
CastExpr castExpr = returnStmt.getExpression().get().asCastExpr();
assertEquals(range(3, 12, 3, 101), castExpr.getRange().get());
}
use of com.github.javaparser.ast.expr.CastExpr in project javaparser by javaparser.
the class JavaParserTest method rangeOfCast.
@Test
public void rangeOfCast() {
String code = "class A {" + EOL + " Object f() {" + EOL + " return (Comparator<Map.Entry<K, V>> & Serializable)(c1, c2) -> c1.getKey().compareTo(c2.getKey()); " + EOL + "}}";
CompilationUnit cu = JavaParser.parse(code);
MethodDeclaration methodDeclaration = cu.getClassByName("A").get().getMember(0).asMethodDeclaration();
ReturnStmt returnStmt = methodDeclaration.getBody().get().getStatement(0).asReturnStmt();
CastExpr castExpr = returnStmt.getExpression().get().asCastExpr();
assertEquals(range(3, 12, 3, 101), castExpr.getRange().get());
}
use of com.github.javaparser.ast.expr.CastExpr in project drools by kiegroup.
the class CompilerBytecodeLoader method getCompilationUnit.
public <T> CompilationUnit getCompilationUnit(Class<T> clazz, String templateResourcePath, String cuPackage, String cuClass, String feelExpression, Expression theExpression, Set<FieldDeclaration> fieldDeclarations) {
CompilationUnit cu = parse(CompilerBytecodeLoader.class.getResourceAsStream(templateResourcePath));
cu.setPackageDeclaration(cuPackage);
final String className = templateResourcePath.substring(1, templateResourcePath.length() - 5);
ClassOrInterfaceDeclaration classSource = cu.getClassByName(className).orElseThrow(() -> new IllegalArgumentException("Cannot find class by name " + className));
classSource.setName(cuClass);
MethodDeclaration lookupMethod = cu.findFirst(MethodDeclaration.class).orElseThrow(() -> new RuntimeException("Something unexpected changed in the template."));
lookupMethod.setComment(new JavadocComment(" FEEL: " + feelExpression + " "));
ReturnStmt returnStmt = lookupMethod.findFirst(ReturnStmt.class).orElseThrow(() -> new RuntimeException("Something unexpected changed in the template."));
Expression expr;
if (clazz.equals(CompiledFEELUnaryTests.class)) {
expr = new CastExpr(StaticJavaParser.parseType("java.util.List"), new EnclosedExpr(theExpression));
} else {
expr = theExpression;
}
returnStmt.setExpression(expr);
List<ClassOrInterfaceDeclaration> classDecls = cu.findAll(ClassOrInterfaceDeclaration.class);
if (classDecls.size() != 1) {
throw new RuntimeException("Something unexpected changed in the template.");
}
ClassOrInterfaceDeclaration classDecl = classDecls.get(0);
fieldDeclarations.stream().filter(fd -> !isUnaryTest(fd)).sorted(new SortFieldDeclarationStrategy()).forEach(classDecl::addMember);
fieldDeclarations.stream().filter(fd -> fd.getVariable(0).getName().asString().startsWith("UT")).sorted(new SortFieldDeclarationStrategy()).forEach(classDecl::addMember);
if (generateClassListener != null) {
generateClassListener.generatedClass(cu);
}
LOG.debug("{}", cu);
return cu;
}
Aggregations