Package org.mvel2

Examples of org.mvel2.ParserContext


                name = context.getPkg().getName() + ".Unknown";
            }
        } else {
            name = "Unknown";
        }
        final ParserContext parserContext = new ParserContext( this.imports,
                                                               null,
                                                               name );
        // getRuleDescr().getClassName() );

        for ( Iterator it = this.packageImports.values().iterator(); it.hasNext(); ) {
            String packageImport = (String) it.next();
            parserContext.addPackageImport( packageImport );
        }

        parserContext.setStrictTypeEnforcement( strictMode );

        if ( interceptors != null ) {
            parserContext.setInterceptors( interceptors );
        }

        if ( analysis != null ) {
            List list[] = analysis.getBoundIdentifiers();

            // @TODO yuck, we don't want conditions for configuration :(
            if ( context instanceof RuleBuildContext ) {
                // FIXME: analysis can be null, throws an NPE
                DeclarationScopeResolver resolver = ((RuleBuildContext) context).getDeclarationResolver();
                for ( Iterator it = list[0].iterator(); it.hasNext(); ) {
                    String identifier = (String) it.next();
                    Class cls = resolver.getDeclaration(null, identifier ).getExtractor().getExtractToClass();
                    parserContext.addInput( identifier,
                                            cls );
                }
            }

            Map globalTypes = context.getPackageBuilder().getGlobals();
            for ( Iterator it = list[1].iterator(); it.hasNext(); ) {
                String identifier = (String) it.next();
                parserContext.addInput( identifier,
                                        (Class) globalTypes.get( identifier ) );
            }

            if ( otherInputVariables != null ) {
                for ( Iterator it = otherInputVariables.entrySet().iterator(); it.hasNext(); ) {
                    Entry entry = (Entry) it.next();
                    parserContext.addInput( (String) entry.getKey(),
                                            (Class) entry.getValue() );
                }
            }

            if ( outerDeclarations != null ) {
                for ( Iterator it = outerDeclarations.entrySet().iterator(); it.hasNext(); ) {
                    Entry entry = (Entry) it.next();
                    parserContext.addInput( (String) entry.getKey(),
                                            ((Declaration) entry.getValue()).getExtractor().getExtractToClass() );
                }
            }

            parserContext.addInput( "drools",
                                    KnowledgeHelper.class );
        }

        return parserContext;
    }
View Full Code Here


        MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData( "mvel" );
        ParserConfiguration conf = data.getParserConfiguration();

        conf.setClassLoader( context.getPackageBuilder().getRootClassLoader() );

        final ParserContext pctx = new ParserContext( conf );
        pctx.setStrictTypeEnforcement( false );
        pctx.setStrongTyping( false );
        pctx.addInput( "this",
                       thisClass );
        pctx.addInput( "empty",
                       boolean.class ); // overrides the mvel empty label
        MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = true;
        MVEL.analysisCompile( expr,
                              pctx );

        if ( !pctx.getInputs().isEmpty() ) {
            for ( String v : pctx.getInputs().keySet() ) {
                if ( "this".equals( v ) || PropertyTools.getFieldOrAccessor( thisClass, v ) != null ) {
                    descrBranch.getFieldAccessors().add( v );
                } else if ( "empty".equals( v ) ) {
                    // do nothing
                } else if ( !context.getPkg().getGlobals().containsKey( v ) ) {
View Full Code Here

        try {
            String value = literalRestrictionDescr.getText().trim();

            MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData( "mvel" );
            ParserConfiguration pconf = data.getParserConfiguration();
            ParserContext pctx = new ParserContext( pconf );

            Object o = MVEL.executeExpression( MVEL.compileExpression( value,
                                                                       pctx ) );
            if ( o != null && vtype == null ) {
                // was a compilation problem else where, so guess valuetype so we can continue
View Full Code Here

    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 );
View Full Code Here

            conf.setClassLoader( context.getKnowledgeBuilder().getRootClassLoader() );

            // first compilation is for verification only
            // @todo proper source file name
            final ParserContext parserContext1 = new ParserContext( conf );
            if ( localTypes != null ) {
                for ( Entry entry : localTypes.entrySet() ) {
                    parserContext1.addInput( (String) entry.getKey(),
                                             (Class) entry.getValue() );
                }
            }
            if ( availableIdentifiers.getThisClass() != null ) {
                parserContext1.addInput( "this",
                                         availableIdentifiers.getThisClass() );
            }

            if ( availableIdentifiers.getOperators() != null ) {
                for ( Entry<String, EvaluatorWrapper> opEntry : availableIdentifiers.getOperators().entrySet() ) {
                    parserContext1.addInput( opEntry.getKey(), opEntry.getValue().getClass() );
                }
            }

            parserContext1.setStrictTypeEnforcement( false );
            parserContext1.setStrongTyping( false );
            parserContext1.setInterceptors( dialect.getInterceptors() );
            Class< ? > returnType;

            try {
                returnType = MVEL.analyze( expr,
                                           parserContext1 );
            } catch ( Exception e ) {
                BaseDescr base = (context instanceof RuleBuildContext) ? ((RuleBuildContext)context).getRuleDescr() : context.getParentDescr();
                DialectUtil.copyErrorLocation(e, context.getParentDescr());
                context.addError( new DescrBuildError( base,
                                                              context.getParentDescr(),
                                                              null,
                                                              "Unable to Analyse Expression " + expr + ":\n" + e.getMessage() ) );
                return null;
            }

            Set<String> requiredInputs = new HashSet<String>();
            requiredInputs.addAll( parserContext1.getInputs().keySet() );
            HashMap<String, Class< ? >> variables = (HashMap<String, Class< ? >>) ((Map) parserContext1.getVariables());
            if ( localTypes != null ) {
                for ( String str : localTypes.keySet() ) {
                    // we have to do this due to mvel regressions on detecting true local vars
                    variables.remove( str );
                }
            }

            // MVEL includes direct fields of context object in non-strict mode. so we need to strip those
            if ( availableIdentifiers.getThisClass() != null ) {
                for ( Iterator<String> it = requiredInputs.iterator(); it.hasNext(); ) {
                    if ( PropertyTools.getFieldOrAccessor( availableIdentifiers.getThisClass(),
                                                           it.next() ) != null ) {
                        it.remove();
                    }
                }
            }

            // now, set the required input types and compile again
            final ParserContext parserContext2 = new ParserContext( conf );
            parserContext2.setStrictTypeEnforcement( true );
            parserContext2.setStrongTyping( true );
            parserContext2.setInterceptors( dialect.getInterceptors() );

            for ( String str : requiredInputs ) {
                Class< ? > cls = availableIdentifiers.getDeclrClasses().get( str );
                if ( cls != null ) {
                    parserContext2.addInput( str,
                                             cls );
                    continue;
                }

                cls = availableIdentifiers.getGlobals().get( str );
                if ( cls != null ) {
                    parserContext2.addInput( str,
                                             cls );
                    continue;
                }

                cls = availableIdentifiers.getOperators().keySet().contains( str ) ? context.getConfiguration().getComponentFactory().getExpressionProcessor().getEvaluatorWrapperClass() : null;
                if ( cls != null ) {
                    parserContext2.addInput( str,
                                             cls );
                    continue;
                }

                if ( str.equals( contextIndeifier ) ) {
                    parserContext2.addInput( contextIndeifier,
                                             kcontextClass );
                } else if ( str.equals( "kcontext" ) ) {
                    parserContext2.addInput( "kcontext",
                                             kcontextClass );
                }
                if ( str.equals( "rule" ) ) {
                    parserContext2.addInput( "rule",
                                             Rule.class );
                }

                if ( localTypes != null ) {
                    cls = localTypes.get( str );
                    if ( cls != null ) { 
                        parserContext2.addInput( str,
                                                 cls );
                    }
                }
            }  
           
            if ( availableIdentifiers.getThisClass() != null ) {
                parserContext2.addInput( "this",
                                         availableIdentifiers.getThisClass() );
            }

            boolean typesafe = context.isTypesafe();
           
            try {
                returnType = MVEL.analyze( expr,
                                           parserContext2 );
                typesafe = true;
            } catch ( Exception e ) {
                // is this an error, or can we fall back to non-typesafe mode?
                if ( typesafe ) {
                    BaseDescr base = (context instanceof RuleBuildContext) ? ((RuleBuildContext)context).getRuleDescr() : context.getParentDescr();
                    DialectUtil.copyErrorLocation(e, context.getParentDescr());
                    context.addError( new DescrBuildError( base,
                                                           context.getParentDescr(),
                                                           null,
                                                           "Unable to Analyse Expression " + expr + ":\n" + e.getMessage() ) );
                    return null;                   
                }
            }

            if ( typesafe ) {
                requiredInputs = new HashSet<String>();
                requiredInputs.addAll( parserContext2.getInputs().keySet() );
                requiredInputs.addAll( variables.keySet() );
                variables = (HashMap<String, Class< ? >>) ((Map) parserContext2.getVariables());
                if ( localTypes != null ) {
                    for ( String str : localTypes.keySet() ) {
                        // we have to do this due to mvel regressions on detecting true local vars
                        variables.remove( str );
                    }               
View Full Code Here

                    MVELDialectRuntimeData data = ( MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData( "mvel" );
                    ParserConfiguration conf = data.getParserConfiguration();
                    conf.setClassLoader( context.getKnowledgeBuilder().getRootClassLoader() );

                    arguments.set( pos,
                    MVELSafeHelper.getEvaluator().executeExpression( MVEL.compileExpression( expr, new ParserContext( conf ) ) ) );
                } catch ( Exception e ) {
                    context.addError( new DescrBuildError( context.getParentDescr(),
                                                                  descr,
                                                                  null,
                                                                  "Unable to compile expression:\n" + expr ) );
View Full Code Here

            try {
                MVELDialectRuntimeData data = ( MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData( "mvel" );
                ParserConfiguration conf = data.getParserConfiguration();
                conf.setClassLoader( context.getKnowledgeBuilder().getRootClassLoader() );

                arguments.set( position, MVELSafeHelper.getEvaluator().executeExpression( MVEL.compileExpression( rewrittenExpr, new ParserContext( conf ) ) ) );
            } catch ( Exception e ) {
                context.addError( new DescrBuildError( context.getParentDescr(), base, null, "Unable to compile expression:\n" + rewrittenExpr ) );
            }
        }
    }
View Full Code Here

                                       new HashMap() );
    }

    @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 );
View Full Code Here

        MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData( "mvel" );
        ParserConfiguration conf = data.getParserConfiguration();

        conf.setClassLoader( context.getKnowledgeBuilder().getRootClassLoader() );

        final ParserContext pctx = new ParserContext( conf );
        pctx.setStrictTypeEnforcement(false);
        pctx.setStrongTyping( false );
        pctx.addInput( "this",
                       thisClass );
        pctx.addInput( "empty",
                       boolean.class ); // overrides the mvel empty label
        MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = true;
        MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING = true;
        MVEL.COMPILER_OPT_ALLOW_RESOLVE_INNERCLASSES_WITH_DOTNOTATION = true;
        MVEL.COMPILER_OPT_SUPPORT_JAVA_STYLE_CLASS_LITERALS = true;

        try {
            MVEL.analysisCompile( expr, pctx );
        } catch (Exception e) {
            // There is a problem in setting the inputs for this expression, but it will be
            // reported during expression analysis, so swallow it at the moment
            return;
        }

        if ( !pctx.getInputs().isEmpty() ) {
            for ( String v : pctx.getInputs().keySet() ) {
                // in the following if, we need to check that the expr actually contains a reference
                // to an "empty" property, or the if will evaluate to true even if it doesn't
                if ( "this".equals( v ) || (PropertyTools.getFieldOrAccessor( thisClass,
                                                                               v ) != null && expr.matches( "(^|.*\\W)empty($|\\W.*)" )) ) {
                    descrBranch.getFieldAccessors().add( v );
View Full Code Here

            MVEL.COMPILER_OPT_ALLOW_RESOLVE_INNERCLASSES_WITH_DOTNOTATION = true;
            MVEL.COMPILER_OPT_SUPPORT_JAVA_STYLE_CLASS_LITERALS = true;

            MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData( "mvel" );
            ParserConfiguration pconf = data.getParserConfiguration();
            ParserContext pctx = new ParserContext( pconf );

            Object o = MVELSafeHelper.getEvaluator().executeExpression( MVEL.compileExpression( value,
                    pctx ) );
            if ( o != null && vtype == null ) {
                // was a compilation problem else where, so guess valuetype so we can continue
View Full Code Here

TOP

Related Classes of org.mvel2.ParserContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.