Search in sources :

Example 6 with JavaExprAnalyzer

use of org.drools.mvel.java.JavaExprAnalyzer in project drools by kiegroup.

the class JavaAccumulateBuilderTest method testFixInitCode.

@Test
public void testFixInitCode() throws Exception {
    JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
    JavaAccumulateBuilder builder = new JavaAccumulateBuilder();
    String code = "int x = 0;";
    String expected = "x = 0;";
    BoundIdentifiers bindings = new BoundIdentifiers(new HashMap<String, Class<?>>(), null);
    JavaAnalysisResult analysis = analyzer.analyzeBlock(code, bindings);
    String result = builder.fixInitCode(analysis, code);
    assertEquals(expected, result);
    code = "$anExternalVar.method(); \nint aVar = 0, anotherVar=10    ;Integer bla = new Integer( 25);functionCall();\n";
    expected = "$anExternalVar.method(); \naVar = 0;anotherVar=10;bla = new Integer( 25);functionCall();\n";
    ;
    analysis = analyzer.analyzeBlock(code, bindings);
    result = builder.fixInitCode(analysis, code);
    assertEquals(expected, result);
    code = "$anExternalVar.method(); String[] aVar = new String[] { \"a\", \"b\" }, anotherVar=new String[] { someStringVar }  ;final Integer bla = new Integer( 25);functionCall();\n";
    expected = "$anExternalVar.method(); aVar = new String[] { \"a\", \"b\" };anotherVar=new String[] { someStringVar };bla = new Integer( 25);functionCall();\n";
    analysis = analyzer.analyzeBlock(code, bindings);
    result = builder.fixInitCode(analysis, code);
    assertEquals(expected, result);
}
Also used : JavaAnalysisResult(org.drools.mvel.java.JavaAnalysisResult) JavaExprAnalyzer(org.drools.mvel.java.JavaExprAnalyzer) JavaAccumulateBuilder(org.drools.mvel.java.JavaAccumulateBuilder) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) Test(org.junit.Test)

Aggregations

BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)6 JavaAnalysisResult (org.drools.mvel.java.JavaAnalysisResult)6 JavaExprAnalyzer (org.drools.mvel.java.JavaExprAnalyzer)6 Test (org.junit.Test)6 RecognitionException (org.antlr.runtime.RecognitionException)5 Declaration (org.drools.core.rule.Declaration)4 ImportDeclaration (org.drools.core.rule.ImportDeclaration)4 HashMap (java.util.HashMap)1 JavaAccumulateBuilder (org.drools.mvel.java.JavaAccumulateBuilder)1