@Test
  public void testDebugSymbolCount() {
    String expr =
        "System.out.println( \"a1\" );\n"
            + "System.out.println( \"a2\" );\n"
            + "System.out.println( \"a3\" );\n"
            + "System.out.println( \"a4\" );\n";

    ExpressionCompiler compiler = new ExpressionCompiler(expr);

    ParserContext context = new ParserContext();
    context.setDebugSymbols(true);
    context.addImport("System", System.class);
    context.setStrictTypeEnforcement(true);
    // context.setDebugSymbols( true );
    context.setSourceFile("mysource");

    Serializable compiledExpression = compiler.compile(context);

    String s = DebugTools.decompile(compiledExpression);

    System.out.println("s " + s);

    int fromIndex = 0;
    int count = 0;
    while ((fromIndex = s.indexOf("DEBUG_SYMBOL", fromIndex + 1)) > -1) {
      count++;
    }
    assertEquals(4, count);
  }
Ejemplo n.º 2
0
 public void testParameterizedTypeInStrictMode() {
   ParserContext ctx = new ParserContext();
   ctx.setStrongTyping(true);
   ctx.addInput("foo", HashMap.class, new Class[] {String.class, String.class});
   ExpressionCompiler compiler = new ExpressionCompiler("foo.get('bar').toUpperCase()");
   compiler.compile(ctx);
 }
Ejemplo n.º 3
0
  public void testSetCoercion2() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("sampleBean", SampleBean.class);

    Serializable s = compileSetExpression("sampleBean.map2['bleh']", ctx);

    Foo foo = new Foo();
    executeSetExpression(s, foo, "12");

    assertEquals(12, foo.getSampleBean().getMap2().get("bleh").intValue());

    foo = new Foo();
    executeSetExpression(s, foo, "13");

    assertEquals(13, foo.getSampleBean().getMap2().get("bleh").intValue());

    OptimizerFactory.setDefaultOptimizer("ASM");

    ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("sampleBean", SampleBean.class);

    s = compileSetExpression("sampleBean.map2['bleh']", ctx);

    foo = new Foo();
    executeSetExpression(s, foo, "12");

    assertEquals(12, foo.getSampleBean().getMap2().get("bleh").intValue());

    executeSetExpression(s, foo, new Integer(12));

    assertEquals(12, foo.getSampleBean().getMap2().get("bleh").intValue());
  }
Ejemplo n.º 4
0
  public void testEgressType() {
    ExpressionCompiler compiler = new ExpressionCompiler("( $cheese )");
    ParserContext context = new ParserContext();
    context.addInput("$cheese", Cheese.class);

    assertEquals(Cheese.class, compiler.compile(context).getKnownEgressType());
  }
Ejemplo n.º 5
0
  public static Class getClassReference(ParserContext ctx, TypeDescriptor tDescr)
      throws ClassNotFoundException {
    Class cls;
    if (ctx != null && ctx.hasImport(tDescr.className)) {
      cls = ctx.getImport(tDescr.className);
      if (tDescr.isArray()) {
        cls =
            findClass(
                null, repeatChar('[', tDescr.arraySize.length) + "L" + cls.getName() + ";", ctx);
      }
    } else if (ctx == null && hasContextFreeImport(tDescr.className)) {
      cls = getContextFreeImport(tDescr.className);
      if (tDescr.isArray()) {
        cls =
            findClass(
                null, repeatChar('[', tDescr.arraySize.length) + "L" + cls.getName() + ";", ctx);
      }
    } else {
      cls = createClass(tDescr.getClassName(), ctx);
      if (tDescr.isArray()) {
        cls =
            findClass(
                null, repeatChar('[', tDescr.arraySize.length) + "L" + cls.getName() + ";", ctx);
      }
    }

    return cls;
  }
Ejemplo n.º 6
0
  public void testTypeCalculation() {
    ParserContext ctx = ParserContext.create().stronglyTyped();
    ctx.addInput("foo", Foo.class);

    Class cls = MVEL.analyze("foo.bar.testList.get(0)", ctx);

    assertTrue(Integer.class.isAssignableFrom(cls));
  }
Ejemplo n.º 7
0
  public void testGenericInference2() {
    ParserContext ctx;
    MVEL.analysisCompile(
        "$result = person.maptributes['fooey'].name",
        ctx = ParserContext.create().stronglyTyped().withInput("person", Person.class));

    assertEquals(String.class, ctx.getVarOrInputTypeOrNull("$result"));
  }
Ejemplo n.º 8
0
  public void testProvidedExternalTypes() {
    ExpressionCompiler compiler = new ExpressionCompiler("foo.bar");
    ParserContext ctx = new ParserContext();
    ctx.setStrictTypeEnforcement(true);
    ctx.addInput("foo", Foo.class);

    compiler.compile(ctx);
  }
Ejemplo n.º 9
0
  public void testParameterizedTypeInStrictMode2() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("ctx", Object.class);

    ExpressionCompiler compiler =
        new ExpressionCompiler("org.mvel2.DataConversion.convert(ctx, String).toUpperCase()");
    assertEquals(String.class, compiler.compile(ctx).getKnownEgressType());
  }
Ejemplo n.º 10
0
  public void testStrictStaticMethodCall() {
    ExpressionCompiler compiler = new ExpressionCompiler("Bar.staticMethod()");
    ParserContext ctx = new ParserContext();
    ctx.addImport("Bar", Bar.class);
    ctx.setStrictTypeEnforcement(true);

    Serializable s = compiler.compile(ctx);

    assertEquals(1, executeExpression(s));
  }
Ejemplo n.º 11
0
  public void testVarDeclr() {
    String ex = "var a";

    ParserContext ctx = new ParserContext();
    ExpressionCompiler compiler = new ExpressionCompiler(ex);
    compiler.setVerifyOnly(true);
    compiler.compile(ctx);

    assertEquals(1, ctx.getVariables().size());
  }
Ejemplo n.º 12
0
  public void testParameterizedTypeInStrictMode3() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("base", Base.class);

    ExpressionCompiler compiler = new ExpressionCompiler("base.list");

    assertTrue(
        compiler.compile(ctx).getParserContext().getLastTypeParameters()[0].equals(String.class));
  }
Ejemplo n.º 13
0
  public void testParameterizedTypeInStrictMode4() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("base", Base.class);

    ExpressionCompiler compiler = new ExpressionCompiler("base.list.get(1).toUpperCase()");
    CompiledExpression ce = compiler.compile(ctx);

    assertEquals(String.class, ce.getKnownEgressType());
  }
Ejemplo n.º 14
0
  public void testCompileTimeCoercion() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("foo", Foo.class);

    assertEquals(
        true,
        executeExpression(
            new ExpressionCompiler("foo.bar.woof == 'true'").compile(ctx), createTestMap()));
  }
Ejemplo n.º 15
0
  public void testVarInputs2() {
    ExpressionCompiler compiler =
        new ExpressionCompiler(
            "test != foo && bo.addSomething(trouble); String bleh = foo; twa = bleh;");

    ParserContext ctx = new ParserContext();

    compiler.compile(ctx);

    System.out.println(ctx.getVarOrInputType("bleh"));
  }
Ejemplo n.º 16
0
  public void testAnalysisCompile() {
    ParserContext pCtx = new ParserContext();
    ExpressionCompiler e = new ExpressionCompiler("foo.aValue = 'bar'");
    e.setVerifyOnly(true);

    e.compile(pCtx);

    assertTrue(pCtx.getInputs().keySet().contains("foo"));
    assertEquals(1, pCtx.getInputs().size());
    assertEquals(0, pCtx.getVariables().size());
  }
Ejemplo n.º 17
0
  public void testSetAccessorOverloadedEqualsStrictMode2() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("foo", Foo.class);

    try {
      CompiledExpression expr = new ExpressionCompiler("foo.aValue = 'bar'").compile(ctx);
    } catch (CompileException e) {
      assertTrue(false);
    }
  }
Ejemplo n.º 18
0
  public void testAnalyzer() {
    ParserContext ctx = new ParserContext();
    MVEL.compileExpression("order.id == 10", ctx);

    for (String input : ctx.getInputs().keySet()) {
      System.out.println("input>" + input);
    }

    assertEquals(1, ctx.getInputs().size());
    assertTrue(ctx.getInputs().containsKey("order"));
  }
Ejemplo n.º 19
0
 @Test
 public void test1() {
   ParserContext pc = new ParserContext();
   pc.addInput("x", String.class);
   pc.setStrongTyping(true);
   Object o = MVEL.compileExpression("x.startsWith('d')", pc);
   Map vars = new HashMap();
   vars.put("x", "d");
   MVEL.executeExpression(o, vars);
   System.out.println(o);
 }
Ejemplo n.º 20
0
  public void testContextMethodCallsInStrongMode() {
    ParserContext context = new ParserContext();
    context.setStrongTyping(true);
    context.addInput("this", EchoContext.class);

    ExecutableStatement stmt =
        (ExecutableStatement) MVEL.compileExpression("this.echo( 'Mac')", context);
    stmt = (ExecutableStatement) MVEL.compileExpression("echo( 'Mac')", context);

    assertEquals("Mac", MVEL.executeExpression(stmt, new EchoContext()));
  }
Ejemplo n.º 21
0
  public void testTypeVarDeclr() {
    String ex = "String a;";
    ParserContext ctx = new ParserContext();
    ExpressionCompiler compiler = new ExpressionCompiler(ex);
    compiler.compile(ctx);

    assertNotNull(ctx.getVariables());
    assertEquals(1, ctx.getVariables().entrySet().size());
    for (Map.Entry<String, Class> entry : ctx.getVariables().entrySet()) {
      assertEquals(String.class, entry.getValue());
    }
  }
Ejemplo n.º 22
0
  public void testStrictTypingCompilation4() throws NoSuchMethodException {
    ParserContext ctx = new ParserContext();

    ctx.addImport(Foo.class);
    ctx.setStrictTypeEnforcement(true);

    ExpressionCompiler compiler = new ExpressionCompiler("x_a = new Foo()");

    compiler.compile(ctx);

    assertEquals(Foo.class, ctx.getVariables().get("x_a"));
  }
Ejemplo n.º 23
0
  public void lispFormHandler(LispForm lispForm) {
    StringBuilderAppendable appendable = new StringBuilderAppendable();
    FunctionHandlers.dump(lispForm, appendable, true);

    ClassLoader tempClassLoader = Thread.currentThread().getContextClassLoader();
    try {
      Thread.currentThread()
          .setContextClassLoader(((InternalRuleBase) ruleBase).getRootClassLoader());

      ParserContext context = new ParserContext();

      String namespace = this.session.getAgenda().getFocusName();

      Package pkg = this.ruleBase.getPackage(namespace);
      if (pkg == null) {
        this.packageBuilder.addPackage(createPackageDescr(namespace));
        pkg = this.ruleBase.getPackage(namespace);
      }

      if (pkg != null) {
        // only time this will be null is if we have yet to do any packagedescr work
        try {

          for (Iterator it = pkg.getImports().entrySet().iterator(); it.hasNext(); ) {
            Entry entry = (Entry) it.next();
            String importName = ((ImportDeclaration) entry.getValue()).getTarget();
            if (importName.endsWith("*")) {
              context.addPackageImport(importName.substring(0, importName.length() - 2));
            } else {

              Class cls = ((InternalRuleBase) ruleBase).getRootClassLoader().loadClass(importName);
              context.addImport(cls.getSimpleName(), (Class) cls);
            }
          }

        } catch (Exception e) {
          e.printStackTrace();
        }

        MVELDialectRuntimeData data =
            (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData("clips");
        this.factory.setNextFactory(data.getFunctionFactory());
      }

      ExpressionCompiler expr = new ExpressionCompiler(appendable.toString());
      Serializable executable = expr.compile(context);

      MVEL.executeExpression(executable, this, this.factory);
    } finally {
      Thread.currentThread().setContextClassLoader(tempClassLoader);
    }
  }
Ejemplo n.º 24
0
  public void testEgressTypeCorrect2() {

    ParserContext context = new ParserContext();
    context.setStrongTyping(true);
    context.addInput("this", SampleBean.class);
    ExecutableStatement stmt =
        (ExecutableStatement) MVEL.compileExpression("( map2[ 'yyy' ] )", context);

    SampleBean s = new SampleBean();
    s.getMap2().put("yyy", 1);

    assertEquals(new Integer(1), MVEL.executeExpression(stmt, s));
  }
Ejemplo n.º 25
0
  public void testStrictTypingCompilation3() throws NoSuchMethodException {
    ParserContext ctx = new ParserContext();

    ctx.setStrictTypeEnforcement(true);

    ExpressionCompiler compiler =
        new ExpressionCompiler(
            "message='Hello';b=7;\nSystem.out.println(message + ';' + b);\n"
                + "System.out.println(message + ';' + b); b");

    assertEquals(
        7, executeExpression(compiler.compile(ctx), new DefaultLocalVariableResolverFactory()));
  }
Ejemplo n.º 26
0
  public void testStrongTyping() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);

    try {
      new ExpressionCompiler("blah").compile(ctx);
    } catch (Exception e) {
      // should fail
      return;
    }

    assertTrue(false);
  }
Ejemplo n.º 27
0
  public final void testDetermineEgressParametricType2() {
    final ParserContext parserContext = new ParserContext();
    parserContext.setStrongTyping(true);
    parserContext.addInput("strings", List.class, new Class[] {String.class});

    final CompiledExpression expr = new ExpressionCompiler("strings", parserContext).compile();

    assertTrue(STRINGS.equals(executeExpression(expr, new A())));

    final Type[] typeParameters = expr.getParserContext().getLastTypeParameters();

    assertTrue(null != typeParameters);
    assertTrue(String.class.equals(typeParameters[0]));
  }
Ejemplo n.º 28
0
  public void testDetermineRequiredInputsInConstructor() throws Exception {
    ParserContext ctx = new ParserContext();
    ctx.setStrictTypeEnforcement(false);
    ctx.setStrongTyping(false);
    ctx.addImport(Foo.class);

    ExpressionCompiler compiler = new ExpressionCompiler("new Foo244( $bar,  $bar.age );");

    Serializable compiled = compiler.compile(ctx);

    Set<String> requiredInputs = compiler.getParserContextState().getInputs().keySet();
    assertEquals(1, requiredInputs.size());
    assertTrue(requiredInputs.contains("$bar"));
  }
Ejemplo n.º 29
0
  public void testAutoBoxing() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    // ctx.addInput("base", Base.class);

    Serializable s = compileExpression("(list = new java.util.ArrayList()).add( 5 ); list", ctx);

    Map vars = new HashMap();
    // vars.put("base", new Base());

    List list = (List) executeExpression(s, vars);

    assertEquals(1, list.size());
  }
Ejemplo n.º 30
0
  public void testStrictStrongTypingCompilationErrors2() throws Exception {
    ParserContext ctx = new ParserContext();
    ctx.setStrictTypeEnforcement(true);
    ctx.setStrongTyping(true);
    ctx.addImport(Foo.class);
    ctx.addInput("$bar", Bar.class);

    try {
      MVEL.compileExpression("x_a = new Foo244( $ba ); x_a.equals($ba);", ctx);
      fail("This should not compileShared");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }