public Marshaller generateMarshaller()

in lightning-core/src/main/java/org/apache/directmemory/lightning/internal/generator/BytecodeMarshallerGenerator.java [56:134]


    public Marshaller generateMarshaller( Class<?> type, List<PropertyDescriptor> propertyDescriptors,
                                          Map<java.lang.reflect.Type, Marshaller> marshallers,
                                          ClassDescriptorAwareSerializer serializer,
                                          SerializationStrategy serializationStrategy,
                                          ObjectInstantiatorFactory objectInstantiatorFactory,
                                          File debugCacheDirectory, Logger logger )
    {

        try
        {
            ClassWriter cw = new ClassWriter( 0 );

            // Copy properties and sort them by name
            List<PropertyDescriptor> propertyDescriptorsCopy = new ArrayList<PropertyDescriptor>( propertyDescriptors );
            Collections.sort( propertyDescriptorsCopy );

            // Build className e.g. "SomeTypeMarshaller$$X$$Lightning"
            String className =
                new StringBuilder( !type.isArray() ? type.getSimpleName() : type.getComponentType().getSimpleName()
                    + "Array" ).append( "Marshaller" ).append( GENEREATED_CLASS_ID.getAndIncrement() ).append( "Lightning" ).toString();

            // Build class
            cw.visit( V1_6, ACC_PUBLIC & ACC_SUPER, className, null, SUPER_CLASS_INTERNAL_TYPE, null );

            // Build marshaller fields
            createMarshallerFields( cw, propertyDescriptorsCopy );

            // Build constructor
            createConstructor( cw, className, propertyDescriptorsCopy );

            // Build Marshaller#marshall method
            createMarshallMethod( cw, className, type, serializationStrategy, propertyDescriptorsCopy );

            // Build Marshaller#unmarshall method
            createUnmarshallMethod( cw, className, type, propertyDescriptorsCopy );

            // Closing class visit
            cw.visitEnd();

            final byte[] bytecode = cw.toByteArray();

            if ( debugCacheDirectory != null )
            {
                File file = new File( debugCacheDirectory, className + ".class" );
                FileOutputStream out = new FileOutputStream( file );
                try
                {
                    out.write( bytecode );
                    out.flush();
                }
                finally
                {
                    try
                    {
                        out.close();
                    }
                    catch ( IOException e )
                    {
                        logger.warn( String.format( "Class %s could not be cached", className ), e );
                    }
                }
            }

            Class<? extends Marshaller> generatedClass = classloader.loadClass( bytecode );
            Constructor<? extends Marshaller> constructor =
                generatedClass.getConstructor( Class.class, Map.class, ClassDescriptorAwareSerializer.class,
                                               ObjectInstantiatorFactory.class, List.class, MarshallerStrategy.class );

            constructor.setAccessible( true );
            return constructor.newInstance( type, marshallers, serializer, objectInstantiatorFactory,
                                            propertyDescriptorsCopy, new InternalMarshallerStrategy() );
        }
        catch ( Exception e )
        {
            throw new SerializerMarshallerGeneratorException(
                                                              "Marshaller for type " + type + " could not be generated",
                                                              e );
        }
    }