use of com.github.javaparser.ast.expr.CastExpr in project drools by kiegroup.
the class KiePMMLRegressionTableFactoryTest method getNumericPredictorExpressionWithoutExponent.
@Test
public void getNumericPredictorExpressionWithoutExponent() throws IOException {
String predictorName = "predictorName";
int exponent = 1;
double coefficient = 1.23;
NumericPredictor numericPredictor = PMMLModelTestUtils.getNumericPredictor(predictorName, exponent, coefficient);
CastExpr retrieved = KiePMMLRegressionTableFactory.getNumericPredictorExpression(numericPredictor);
String text = getFileContent(TEST_02_SOURCE);
Expression expected = JavaParserUtils.parseExpression(String.format(text, coefficient));
assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
}
use of com.github.javaparser.ast.expr.CastExpr in project drools by kiegroup.
the class FunctionDefs method asMethodCall.
public static Expression asMethodCall(String className, String methodSignature, List<String> params) {
// creating a simple algorithm to find the method in java
// without using any external libraries in this initial implementation
// might need to explicitly use a classloader here
String[] mp = FunctionDefNode.parseMethod(methodSignature);
try {
String methodName = mp[0];
String[] paramTypeNames = FunctionDefNode.parseParams(mp[1]);
ArrayList<Expression> paramExprs = new ArrayList<>();
if (paramTypeNames.length == params.size()) {
for (int i = 0; i < params.size(); i++) {
String paramName = params.get(i);
String paramTypeName = paramTypeNames[i];
Type paramTypeCanonicalName = parseType(FunctionDefNode.getType(paramTypeName, null).getCanonicalName());
Expression param = new CastExpr(paramTypeCanonicalName, new MethodCallExpr(null, "coerceTo", new NodeList<>(new ClassExpr(paramTypeCanonicalName), new MethodCallExpr(new NameExpr("feelExprCtx"), "getValue", new NodeList<>(new StringLiteralExpr(paramName))))));
paramExprs.add(param);
}
return new MethodCallExpr(new NameExpr(className), methodName, new NodeList<>(paramExprs));
} else {
throw new FEELCompilationError(Msg.createMessage(Msg.ERROR_RESOLVING_EXTERNAL_FUNCTION_AS_DEFINED_BY, methodSignature));
}
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
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;
}
use of com.github.javaparser.ast.expr.CastExpr in project checker-framework by typetools.
the class DoubleJavaParserVisitor method visit.
@Override
public void visit(final CastExpr node1, final Node other) {
CastExpr node2 = (CastExpr) other;
defaultAction(node1, node2);
node1.getExpression().accept(this, node2.getExpression());
node1.getType().accept(this, node2.getType());
}
use of com.github.javaparser.ast.expr.CastExpr in project javaparser by javaparser.
the class JavaParserTest method rangeOfLambda.
@Test
public void rangeOfLambda() {
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();
assertEquals(range(3, 56, 3, 101), lambdaExpr.getRange().get());
assertEquals(GeneratedJavaParserConstants.LPAREN, lambdaExpr.getTokenRange().get().getBegin().getKind());
assertEquals(GeneratedJavaParserConstants.RPAREN, lambdaExpr.getTokenRange().get().getEnd().getKind());
}
Aggregations