Package org.mvel2.asm

Examples of org.mvel2.asm.MethodVisitor


        }
    }


    protected void buildConstructors(ClassWriter cw, EnumClassDefinition classDef) throws IOException, ClassNotFoundException {
        MethodVisitor mv;
        String argTypes = "";
        int size = 0;
        for ( FieldDefinition fld : classDef.getFieldsDefinitions() ) {
            argTypes += BuildUtils.getTypeDescriptor( fld.getTypeName() );
            size += BuildUtils.sizeOf( fld.getTypeName() );
        }


        {
            int ofs = 3;

            mv = cw.visitMethod( ACC_PRIVATE,
                    "<init>",
                    "(Ljava/lang/String;I" + argTypes +")V",
                    "(" + argTypes + ")V",
                    null );
            mv.visitCode();
            mv.visitVarInsn( ALOAD, 0 );
            mv.visitVarInsn( ALOAD, 1 );
            mv.visitVarInsn( ILOAD, 2 );
            mv.visitMethodInsn( INVOKESPECIAL,
                    "java/lang/Enum",
                    "<init>",
                    "(Ljava/lang/String;I)V" );
            for ( FieldDefinition fld : classDef.getFieldsDefinitions() ) {
                mv.visitVarInsn( ALOAD, 0 );
                mv.visitVarInsn( BuildUtils.varType( fld.getTypeName() ), ofs );
                mv.visitFieldInsn( PUTFIELD,
                        BuildUtils.getInternalType( classDef.getName() ),
                        fld.getName(),
                        BuildUtils.getTypeDescriptor( fld.getTypeName() ) );
                ofs += BuildUtils.sizeOf( fld.getTypeName() );
            }
            mv.visitInsn( RETURN );
            mv.visitMaxs( 3, ofs );
            mv.visitEnd();
        }


        {


            mv = cw.visitMethod( ACC_STATIC,
                    "<clinit>",
                    "()V",
                    null,
                    null);
            mv.visitCode();

            int N = classDef.getEnumLiterals().size();

            mv.visitTypeInsn( NEW,
                    BuildUtils.getInternalType( classDef.getClassName() ) );

            for ( int j = 0; j < N; j++ ) {
                EnumLiteralDefinition lit = classDef.getEnumLiterals().get( j );
                mv.visitInsn( DUP );
                mv.visitLdcInsn( lit.getName() );
                BuildUtils.pushInt( mv, j );

                List<String> args = lit.getConstructorArgs();
                for ( int k = 0; k < args.size(); k++ ) {
                    String argType = classDef.getField( k ).getTypeName();

                    mv.visitLdcInsn( args.get( k ) );
                    mv.visitMethodInsn( INVOKESTATIC,
                            "org/mvel2/MVEL",
                            "eval",
                            "(Ljava/lang/String;)Ljava/lang/Object;");

                    if ( BuildUtils.isPrimitive( argType ) ) {
                        mv.visitTypeInsn( CHECKCAST,
                                BuildUtils.getInternalType( BuildUtils.box( argType ) ) );
                        mv.visitMethodInsn( INVOKEVIRTUAL,
                                BuildUtils.getInternalType( BuildUtils.box( argType ) ),
                                BuildUtils.numericMorph( BuildUtils.box( argType ) ),
                                "()" + BuildUtils.getTypeDescriptor( argType ) );
                    } else {
                        mv.visitTypeInsn( CHECKCAST,
                                BuildUtils.getInternalType( argType ) );
                    }
                }

                mv.visitMethodInsn( INVOKESPECIAL,
                        BuildUtils.getInternalType( classDef.getClassName() ),
                        "<init>",
                        "(Ljava/lang/String;I" + argTypes + ")V" );
                mv.visitFieldInsn(PUTSTATIC,
                        BuildUtils.getInternalType(classDef.getClassName()),
                        lit.getName(),
                        BuildUtils.getTypeDescriptor(classDef.getClassName()));
                mv.visitTypeInsn( NEW, BuildUtils.getInternalType( classDef.getClassName() ) );
            }


            BuildUtils.pushInt( mv, N );
            mv.visitTypeInsn( ANEWARRAY, BuildUtils.getInternalType( classDef.getClassName() ));

            for ( int j = 0; j < N; j++ ) {
                EnumLiteralDefinition lit = classDef.getEnumLiterals().get( j );
                mv.visitInsn(DUP);
                BuildUtils.pushInt( mv, j );
                mv.visitFieldInsn( GETSTATIC,
                        BuildUtils.getInternalType( classDef.getClassName() ),
                        lit.getName(),
                        BuildUtils.getTypeDescriptor( classDef.getClassName() ) );
                mv.visitInsn(AASTORE);
            }


            mv.visitFieldInsn( PUTSTATIC,
                    BuildUtils.getInternalType( classDef.getClassName() ),
                    "$VALUES",
                    "[" + BuildUtils.getTypeDescriptor( classDef.getClassName() ));

            mv.visitInsn( RETURN );
            mv.visitMaxs( 4 + size, 0 );
            mv.visitEnd();
        }



        {
            mv = cw.visitMethod( ACC_PUBLIC + ACC_STATIC,
                    "valueOf",
                    "(Ljava/lang/String;)" + BuildUtils.getTypeDescriptor( classDef.getClassName() ),
                    null,
                    null );
            mv.visitCode();
            mv.visitLdcInsn( Type.getType( BuildUtils.getTypeDescriptor( classDef.getClassName() ) ) );
            mv.visitVarInsn( ALOAD, 0 );
            mv.visitMethodInsn( INVOKESTATIC,
                    "java/lang/Enum",
                    "valueOf",
                    "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;" );
            mv.visitTypeInsn( CHECKCAST,
                    BuildUtils.getInternalType( classDef.getClassName() ) );
            mv.visitInsn( ARETURN );
            mv.visitMaxs( 2, 1 );
            mv.visitEnd();
        }
    }
View Full Code Here





    protected void buildGettersAndSetters(ClassWriter cw, EnumClassDefinition classDef) {
        MethodVisitor mv;
        {
            mv = cw.visitMethod( ACC_PUBLIC + ACC_STATIC,
                    "values",
                    "()[" + BuildUtils.getTypeDescriptor( classDef.getClassName() ),
                    null,
                    null);
            mv.visitCode();
            mv.visitFieldInsn( GETSTATIC,
                    BuildUtils.getInternalType( classDef.getClassName() ),
                    "$VALUES",
                    "[" + BuildUtils.getTypeDescriptor( classDef.getClassName() ) );
            mv.visitMethodInsn( INVOKEVIRTUAL,
                    "[" + BuildUtils.getTypeDescriptor( classDef.getClassName() ),
                    "clone",
                    "()Ljava/lang/Object;" );
            mv.visitTypeInsn( CHECKCAST,
                    "[" + BuildUtils.getTypeDescriptor( classDef.getClassName() ) );
            mv.visitInsn( ARETURN );
            mv.visitMaxs( 1, 0 );
            mv.visitEnd();
        }

        for ( FieldDefinition fld : classDef.getFieldsDefinitions() ) {
            mv = cw.visitMethod( ACC_PUBLIC,
                    BuildUtils.getterName( fld.getName(), fld.getTypeName() ),
                    "()" + BuildUtils.getTypeDescriptor( fld.getTypeName() ),
                    null,
                    null );
            mv.visitCode();
            mv.visitVarInsn( ALOAD, 0 );
            mv.visitFieldInsn( GETFIELD,
                    BuildUtils.getInternalType( classDef.getName() ),
                    fld.getName(),
                    BuildUtils.getTypeDescriptor( fld.getTypeName() ) );
            mv.visitInsn( BuildUtils.returnType( fld.getTypeName() ) );
            mv.visitMaxs( BuildUtils.sizeOf( fld.getTypeName() ), 1 );
            mv.visitEnd();


            mv = cw.visitMethod( ACC_PUBLIC,
                    BuildUtils.setterName( fld.getName(), fld.getTypeName() ),
                    "(" + BuildUtils.getTypeDescriptor( fld.getTypeName() ) + ")V",
                    null,
                    null );
            mv.visitCode();
            mv.visitInsn( RETURN );
            mv.visitMaxs( 0, 1 + BuildUtils.sizeOf( fld.getTypeName() ) );
            mv.visitEnd();
        }



        {
            mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "get$context", "()Lorg/mvel2/ParserContext;", null, null);
            mv.visitCode();
            mv.visitFieldInsn(GETSTATIC, BuildUtils.getInternalType( classDef.getName() ), "$context", "Lorg/mvel2/ParserContext;");
            mv.visitInsn(ARETURN);
            mv.visitMaxs(1, 0);
            mv.visitEnd();
        }
        {
            mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "set$context", "(Lorg/mvel2/ParserContext;)V", null, null);
            mv.visitCode();
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(PUTSTATIC, BuildUtils.getInternalType( classDef.getName() ), "$context", "Lorg/mvel2/ParserContext;");
            mv.visitInsn(RETURN);
            mv.visitMaxs(1, 1);
            mv.visitEnd();
        }
    }
View Full Code Here



    protected void buildClearSoftFields( ClassWriter cw, String wrapperName, ClassDefinition trait, BitSet mask ) {

        MethodVisitor mv = cw.visitMethod( ACC_PRIVATE, "clearSoftFields", "()V", null, null );
        mv.visitCode();

        int j = 0;
        int stackSize = 0;
        for ( FieldDefinition field : trait.getFieldsDefinitions() ) {
            boolean isSoftField = TraitRegistry.isSoftField( field, j++, mask );
            if ( isSoftField ) {
                int size = clearSoftField(mv, wrapperName, field);
                stackSize = Math.max( stackSize, size );
            }
        }
        mv.visitInsn( RETURN );
//        mv.visitMaxs( 4 + stackSize, 2 );
        mv.visitMaxs( 0, 0 );
        mv.visitEnd();
    }
View Full Code Here



    protected void buildContainsValue( ClassWriter cw, String wrapperName, String coreName, ClassDefinition trait, ClassDefinition core, BitSet mask ) {

        MethodVisitor mv = cw.visitMethod( ACC_PUBLIC, "containsValue", "(" + Type.getDescriptor( Object.class ) + ")Z", null, null );
        mv.visitCode();

        boolean hasNillable = false;
        for ( FieldDefinition field : core.getFieldsDefinitions() ) {
            if ( ! BuildUtils.isPrimitive( field.getTypeName() ) ) {
                hasNillable = true;
            }
        }
        Label l99 = null;
        if ( hasNillable ) {
            mv.visitVarInsn(ALOAD, 1);
            l99 = new Label();
            mv.visitJumpInsn(IFNONNULL, l99);
        }

        int j = 0;
        int N = core.getFieldsDefinitions().size();
        for ( FieldDefinition field : core.getFieldsDefinitions() ) {
            j++;
            if ( ! BuildUtils.isPrimitive( field.getTypeName() ) ) {
                TraitFactory.invokeExtractor( mv, wrapperName, trait, core, field );
                if ( j != N ) {
                    Label l1 = new Label();
                    mv.visitJumpInsn( IFNONNULL, l1 );
                    mv.visitInsn( ICONST_1 );
                    mv.visitInsn( IRETURN );
                    mv.visitLabel( l1 );
                } else {
                    mv.visitJumpInsn( IFNONNULL, l99 );
                    mv.visitInsn( ICONST_1 );
                    mv.visitInsn( IRETURN );
                    mv.visitLabel( l99 );
                }

            }
        }

        mv.visitVarInsn( ALOAD, 0 );
        mv.visitVarInsn( ALOAD, 1 );
        mv.visitMethodInsn( INVOKESPECIAL,
                            Type.getInternalName( TripleBasedStruct.class ),
                            "containsValue",
                            "(" + Type.getDescriptor( Object.class ) + ")Z" );

        mv.visitInsn( IRETURN );
//        mv.visitMaxs( core.getFieldsDefinitions().size() > 0 ? 3 : 2, 2 );
        mv.visitMaxs( 0, 0 );
        mv.visitEnd();

    }
View Full Code Here

        mv.visitLabel( l0 );
    }

    protected void buildContainsKey(ClassWriter cw, String name, String className, ClassDefinition trait, ClassDefinition core, BitSet mask) {

        MethodVisitor mv = cw.visitMethod( ACC_PUBLIC,
                                           "containsKey",
                                           "(" + Type.getDescriptor( Object.class ) + ")Z",
                                           null,
                                           null );
        mv.visitCode();

        for ( FieldDefinition field : core.getFieldsDefinitions() ) {
            invokeContainsKey( mv, field.getName() );
        }

        mv.visitVarInsn( ALOAD, 0 );
        mv.visitVarInsn( ALOAD, 1 );
        mv.visitMethodInsn( INVOKESPECIAL,
                            Type.getInternalName( TripleBasedStruct.class ),
                            "containsKey",
                            "(" + Type.getDescriptor( Object.class ) + ")Z" );

        mv.visitInsn( IRETURN );
//        mv.visitMaxs( 2, 2 );
        mv.visitMaxs( 0, 0 );
        mv.visitEnd();
    }
View Full Code Here

    }


    protected void buildSize( ClassVisitor cw, String wrapperName, String coreName, ClassDefinition trait, ClassDefinition core, BitSet mask ) {

        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "size", "()I", null, null);
        mv.visitCode();
        mv.visitVarInsn( ALOAD, 0 );
        mv.visitMethodInsn( INVOKESPECIAL,
                            Type.getInternalName( TripleBasedStruct.class ),
                            "size",
                            "()I" );

        int n = core.getFieldsDefinitions().size();
        for ( int j = 0; j < n; j++ ) {
            mv.visitInsn( ICONST_1 );
            mv.visitInsn( IADD );
        }

        mv.visitInsn(IRETURN);
//        mv.visitMaxs( core.getFieldsDefinitions().size() > 0 ? 2 : 1,
//                1 );
        mv.visitMaxs( 0, 0 );
        mv.visitEnd();

    }
View Full Code Here

    protected void buildIsEmpty( ClassVisitor cw, String wrapperName, String coreName, ClassDefinition trait, ClassDefinition core, BitSet mask ) {

        boolean hasHardFields = core.getFieldsDefinitions().size() > 0;

        MethodVisitor mv = cw.visitMethod( ACC_PUBLIC, "isEmpty", "()Z", null, null );
        mv.visitCode();

        if ( ! hasHardFields ) {
            mv.visitVarInsn( ALOAD, 0 );
            mv.visitMethodInsn( INVOKESPECIAL,
                                Type.getInternalName( TripleBasedStruct.class ),
                                "isEmpty",
                                "()Z" );
        } else {
            mv.visitInsn( ICONST_0 );
        }
        mv.visitInsn( IRETURN );
//        mv.visitMaxs( 1, 1 );
        mv.visitMaxs( 0, 0 );
        mv.visitEnd();
    }
View Full Code Here



    protected void buildGet( ClassVisitor cw, String wrapperName, String coreName, ClassDefinition trait, ClassDefinition core, BitSet mask ) {

        MethodVisitor mv = cw.visitMethod( ACC_PUBLIC,
                                           "get",
                                            "(" + Type.getDescriptor( Object.class ) + ")" + Type.getDescriptor( Object.class ),
                                            null,
                                            null );
        mv.visitCode();

        if ( core.getFieldsDefinitions().size() > 0 ) {
            for ( FieldDefinition field : core.getFieldsDefinitions() ) {
                invokeGet( mv, wrapperName, core, field.getName(), field );
            }
        }

        mv.visitVarInsn( ALOAD, 0 );
        mv.visitVarInsn( ALOAD, 1 );
        mv.visitMethodInsn( INVOKESPECIAL,
                            Type.getInternalName( TripleBasedStruct.class ),
                            "get",
                            "(" + Type.getDescriptor( Object.class ) + ")" + Type.getDescriptor( Object.class ) );

        mv.visitInsn( ARETURN );
//        mv.visitMaxs( 2, 2 );
        mv.visitMaxs( 0, 0 );
        mv.visitEnd();
    }
View Full Code Here

        mv.visitLabel( l1 );
    }
   
    protected void buildPut( ClassVisitor cw, String wrapperName, String coreName, ClassDefinition trait, ClassDefinition core, BitSet mask ) {

        MethodVisitor mv = cw.visitMethod( ACC_PUBLIC,
                                           "put",
                                            "(" + Type.getDescriptor( String.class) + Type.getDescriptor( Object.class ) + ")" + Type.getDescriptor( Object.class ),
                                            null,
                                            null );
        mv.visitCode();

        if ( core.getFieldsDefinitions().size() > 0) {
            int j = 0;
            for ( FieldDefinition field : core.getFieldsDefinitions() ) {
                invokePut( mv, wrapperName, core, field.getName(), field );  
            }
        }


        mv.visitVarInsn( ALOAD, 0 );
        mv.visitVarInsn( ALOAD, 1 );
        mv.visitVarInsn( ALOAD, 2 );
        mv.visitMethodInsn( INVOKESPECIAL,
                            Type.getInternalName( TripleBasedStruct.class ),
                            "put",
                            "(" + Type.getDescriptor( String.class) + Type.getDescriptor( Object.class ) + ")" + Type.getDescriptor( Object.class ) );
        mv.visitInsn( ARETURN );
//        mv.visitMaxs( 4, 5 );
        mv.visitMaxs( 0, 0 );
        mv.visitEnd();
    }
View Full Code Here

   


    protected void buildEntryset( ClassVisitor cw, String wrapperName, String coreName, ClassDefinition trait, ClassDefinition core, BitSet mask ) {

        MethodVisitor mv = cw.visitMethod( ACC_PUBLIC,
                                           "entrySet",
                                           "()" + Type.getDescriptor( Set.class ),
                                           "()Ljava/util/Set<Ljava/util/Map$Entry<Ljava/lang/String;Ljava/lang/Object;>;>;",
                                           null );
        mv.visitCode();
        mv.visitTypeInsn( NEW, Type.getInternalName( HashSet.class ) );
        mv.visitInsn( DUP );
        mv.visitMethodInsn( INVOKESPECIAL, Type.getInternalName( HashSet.class ), "<init>", "()V" );
        mv.visitVarInsn( ASTORE, 1 );

        for ( FieldDefinition field : core.getFieldsDefinitions() ) {
            mv.visitVarInsn( ALOAD, 1 );
            mv.visitLdcInsn( field.getName() );

            TraitFactory.invokeExtractor( mv, wrapperName, trait, core, field );

            if ( BuildUtils.isPrimitive( field.getTypeName() ) ) {
                TraitFactory.valueOf( mv, field.getTypeName() );
            }

            mv.visitMethodInsn( INVOKESTATIC,
                                Type.getInternalName( TraitProxy.class ),
                                "buildEntry",
                                "(" + Type.getDescriptor( String.class ) + Type.getDescriptor( Object.class ) + ")" + Type.getDescriptor( Map.Entry.class ) );
            mv.visitMethodInsn( INVOKEINTERFACE,
                                Type.getInternalName( Set.class ),
                                "add",
                                "(" + Type.getDescriptor( Object.class ) + ")Z" );
            mv.visitInsn(POP);
        }

        mv.visitVarInsn( ALOAD, 1 );
        mv.visitVarInsn( ALOAD, 0 );
        mv.visitMethodInsn( INVOKESPECIAL,
                            Type.getInternalName( TripleBasedStruct.class ),
                            "entrySet",
                            "()" + Type.getDescriptor( Set.class ) );
        mv.visitMethodInsn( INVOKEINTERFACE,
                            Type.getInternalName( Set.class ),
                            "addAll",
                            "(" + Type.getDescriptor( Collection.class ) + ")Z" );                      
        mv.visitInsn( POP );

        mv.visitVarInsn( ALOAD, 1 );
        mv.visitInsn( ARETURN );
//        mv.visitMaxs( core.getFieldsDefinitions().size() > 0 ?  4 : 2, 2 );
        mv.visitMaxs( 0, 0 );
        mv.visitEnd();

    }
View Full Code Here

TOP

Related Classes of org.mvel2.asm.MethodVisitor

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.