Package org.drools.core.util.asm

Examples of org.drools.core.util.asm.ClassFieldInspector


     * @param fieldName
     */
    public BaseClassFieldWriter(final Class< ? > clazz,
                                final String fieldName) {
        try {
            final ClassFieldInspector inspector = new ClassFieldInspector( clazz );
            this.index = ((Integer) inspector.getFieldNames().get( fieldName )).intValue();
            this.fieldType = (Class< ? >) inspector.getFieldTypes().get( fieldName );
            this.valueType = ValueType.determineValueType( this.fieldType );
        } catch ( final Exception e ) {
            throw new RuntimeDroolsException( e );
        }
    }
View Full Code Here


     * @param fieldName
     */
    public BaseClassFieldReader(final Class< ? > clazz,
                                final String fieldName) {
        try {
            final ClassFieldInspector inspector = new ClassFieldInspector( clazz );
            this.index = ((Integer) inspector.getFieldNames().get( fieldName )).intValue();
            this.fieldType = (Class< ? >) inspector.getFieldTypes().get( fieldName );
            this.valueType = ValueType.determineValueType( this.fieldType );
        } catch ( final Exception e ) {
            throw new RuntimeDroolsException( e );
        }
    }
View Full Code Here

            if ( trait.getDefinedClass() != null ) {
                Trait annTrait = trait.getDefinedClass().getAnnotation( Trait.class );
                if ( annTrait != null && ! annTrait.impl().equals(Trait.NullMixin.class) ) {
                    mixinClass = annTrait.impl();
                    mixin = mixinClass.getSimpleName().substring(0,1).toLowerCase() + mixinClass.getSimpleName().substring(1);
                    ClassFieldInspector cfi = new ClassFieldInspector( mixinClass );

                    for ( Method m : mixinClass.getMethods() ) {
                        try {
                            trait.getDefinedClass().getMethod(m.getName(), m.getParameterTypes() );
                            if ( cfi.getGetterMethods().containsValue( m )
                                    || cfi.getSetterMethods().containsValue( m )) {
                                mixinGetSet.put( m.getName(), m );
                            } else {
                                mixinMethods.add( m );
                            }
                        } catch (NoSuchMethodException e) {
View Full Code Here

            if ( trait.getDefinedClass() != null ) {
                Trait annTrait = trait.getDefinedClass().getAnnotation( Trait.class );
                if ( annTrait != null && ! annTrait.impl().equals(Trait.NullMixin.class) ) {
                    mixinClass = annTrait.impl();
                    mixin = mixinClass.getSimpleName().substring(0,1).toLowerCase() + mixinClass.getSimpleName().substring(1);
                    ClassFieldInspector cfi = new ClassFieldInspector( mixinClass );

                    for ( Method m : mixinClass.getMethods() ) {
                        try {
                            trait.getDefinedClass().getMethod(m.getName(), m.getParameterTypes() );
                            if ( cfi.getGetterMethods().containsValue( m )
                                    || cfi.getSetterMethods().containsValue( m )) {
                                mixinGetSet.put( m.getName(), m );
                            } else {
                                mixinMethods.add( m );
                            }
                        } catch (NoSuchMethodException e) {
View Full Code Here

                return new MVELClassFieldReader( clazz,
                                                 fieldName,
                                                 cache );
            } else {
                // otherwise, bytecode generate a specific extractor
                ClassFieldInspector inspector = inspectors.get( clazz );
                if ( inspector == null ) {
                    inspector = new ClassFieldInspector( clazz );
                    inspectors.put( clazz,
                                    inspector );
                }
                final Class< ? > fieldType = (Class< ? >) inspector.getFieldTypes().get( fieldName );
                final Method getterMethod = (Method) inspector.getGetterMethods().get( fieldName );
                if ( fieldType != null && getterMethod != null ) {
                    final String className = ClassFieldAccessorFactory.BASE_PACKAGE + "/" + Type.getInternalName( clazz ) + Math.abs( System.identityHashCode( clazz ) ) + "$" + getterMethod.getName();

                    // generating byte array to create target class
                    final byte[] bytes = dumpReader( clazz,
                                                     className,
                                                     getterMethod,
                                                     fieldType,
                                                     clazz.isInterface() );
                    // use bytes to get a class

                    final Class< ? > newClass = byteArrayClassLoader.defineClass( className.replace( '/',
                                                                                                     '.' ),
                                                                                  bytes,
                                                                                  PROTECTION_DOMAIN );
                    // instantiating target class
                    final Integer index = (Integer) inspector.getFieldNames().get( fieldName );
                    final ValueType valueType = ValueType.determineValueType( fieldType );
                    final Object[] params = {index, fieldType, valueType};
                    return (BaseClassFieldReader) newClass.getConstructors()[0].newInstance( params );
                } else {
                    throw new RuntimeDroolsException( "Field/method '" + fieldName + "' not found for class '" + clazz.getName() + "'\n" );
View Full Code Here

        ByteArrayClassLoader byteArrayClassLoader = cache.getByteArrayClassLoader();
        Map<Class< ? >, ClassFieldInspector> inspectors = cache.getInspectors();
       
        try {
            // otherwise, bytecode generate a specific extractor
            ClassFieldInspector inspector = inspectors.get( clazz );
            if ( inspector == null ) {
                inspector = new ClassFieldInspector( clazz );
                inspectors.put( clazz,
                                inspector );
            }
            final Method setterMethod = (Method) inspector.getSetterMethods().get( fieldName );
            if ( setterMethod != null ) {
                final Class< ? > fieldType = setterMethod.getParameterTypes()[0];
                final String className = ClassFieldAccessorFactory.BASE_PACKAGE + "/" + Type.getInternalName( clazz ) + Math.abs( System.identityHashCode( clazz ) ) + "$" + setterMethod.getName();

                // generating byte array to create target class
                final byte[] bytes = dumpWriter( clazz,
                                                 className,
                                                 setterMethod,
                                                 fieldType,
                                                 clazz.isInterface() );
                // use bytes to get a class

                final Class< ? > newClass = byteArrayClassLoader.defineClass( className.replace( '/',
                                                                                                 '.' ),
                                                                              bytes,
                                                                              PROTECTION_DOMAIN );
                // instantiating target class
                final Integer index = (Integer) inspector.getFieldNames().get( fieldName );
                final ValueType valueType = ValueType.determineValueType( fieldType );
                final Object[] params = {index, fieldType, valueType};
                return (BaseClassFieldWriter) newClass.getConstructors()[0].newInstance( params );
            } else {
                throw new RuntimeDroolsException( "Field/method '" + fieldName + "' not found for class '" + clazz.getName() + "'" );
View Full Code Here

public class ClassFieldInspectorTest {

    @Test
    public void testIt() throws Exception {
        final ClassFieldInspector ext = new ClassFieldInspector( Person.class );
        assertEquals( 7,
                      ext.getFieldNames().size() );
        assertEquals( "getAge" ,
                      ext.getGetterMethods().get( "age" ).getName() );
        assertEquals( "isHappy" ,
                      ext.getGetterMethods().get( "happy" ).getName() );
        assertEquals( "getName" ,
                      ext.getGetterMethods().get( "name" ).getName() );

        final Map<String, Integer> names = ext.getFieldNames();
        assertNotNull( names );
        assertEquals( 7,
                      names.size() );
        assertNull( names.get( "nAme" ) );
View Full Code Here

    }

    @Test
    public void testInterface() throws Exception {
        final ClassFieldInspector ext = new ClassFieldInspector( TestInterface.class );
        assertEquals( 2,
                      ext.getFieldNames().size() );
        assertEquals( "getSomething" ,
                      ext.getGetterMethods().get( "something" ).getName() );
        assertEquals( "getAnother" ,
                      ext.getGetterMethods().get( "another" ).getName() );

        final Map<String, Integer> names = ext.getFieldNames();
        assertNotNull( names );
        assertEquals( 2,
                      names.size() );

    }
View Full Code Here

    }

    @Test
    public void testAbstract() throws Exception {
        final ClassFieldInspector ext = new ClassFieldInspector( TestAbstract.class );
        assertEquals( 5,
                      ext.getFieldNames().size() );
        assertEquals( "getSomething" ,
                      ext.getGetterMethods().get( "something" ).getName() );
        assertEquals( "getAnother" ,
                      ext.getGetterMethods().get( "another" ).getName() );

        final Map<String, Integer> names = ext.getFieldNames();
        assertNotNull( names );
        assertEquals( 5,
                      names.size() );

    }
View Full Code Here

    }

    @Test
    public void testInheritedFields() throws Exception {
        ClassFieldInspector ext = new ClassFieldInspector( BeanInherit.class );
        assertEquals( 5,
                      ext.getFieldNames().size() );

        ext = new ClassFieldInspector( InterfaceChildImpl.class );
        assertEquals( 8,
                      ext.getFieldNames().size() );

        // test inheritence from abstract class
        assertNotNull( ext.getFieldNames().get( "HTML" ) );

        // check normal field on child class
        assertNotNull( ext.getFieldNames().get( "baz" ) );

        // test inheritence from an interface
        assertNotNull( ext.getFieldNames().get( "URI" ) );
    }
View Full Code Here

TOP

Related Classes of org.drools.core.util.asm.ClassFieldInspector

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.