<s> from . . import flags <EOL> from . . byteio import Writer <EOL> from . import constantpool , writebytecode , error <EOL> from . optimization import options <EOL> def writeField ( pool , stream , field ) : <EOL> stream . u16 ( field . access & flags . FIELD_FLAGS ) <EOL> stream . u16 ( pool . utf8 ( field . id . name ) ) <EOL> stream . u16 ( pool . utf8 ( field . id . desc ) ) <EOL> if field . constant_value is not None : <EOL> stream . u16 ( <NUM_LIT:1> ) <EOL> stream . u16 ( pool . utf8 ( b"<STR_LIT>" ) ) <EOL> stream . u32 ( <NUM_LIT:2> ) <EOL> ctype , val = field . constant_value <EOL> index = { <EOL> b'<STR_LIT>' : pool . int , <EOL> b'<STR_LIT:B>' : pool . int , <EOL> b'<STR_LIT:S>' : pool . int , <EOL> b'<STR_LIT:C>' : pool . int , <EOL> b'<STR_LIT:I>' : pool . int , <EOL> b'<STR_LIT:F>' : pool . float , <EOL> b'<STR_LIT>' : pool . long , <EOL> b'<STR_LIT:D>' : pool . double , <EOL> b'<STR_LIT>' : pool . string , <EOL> b'<STR_LIT>' : pool . class_ , <EOL> } [ field . id . desc ] ( val ) <EOL> stream . u16 ( index ) <EOL> else : <EOL> stream . u16 ( <NUM_LIT:0> ) <EOL> def writeMethod ( pool , stream , method , code_attr_data ) : <EOL> stream . u16 ( method . access & flags . METHOD_FLAGS ) <EOL> stream . u16 ( pool . utf8 ( method . id . name ) ) <EOL> stream . u16 ( pool . utf8 ( method . id . desc ) ) <EOL> if code_attr_data is not None : <EOL> code_attr_data = code_attr_data . toBytes ( ) <EOL> stream . u16 ( <NUM_LIT:1> ) <EOL> stream . u16 ( pool . utf8 ( b"<STR_LIT>" ) ) <EOL> stream . u32 ( len ( code_attr_data ) ) <EOL> stream . write ( code_attr_data ) <EOL> else : <EOL> stream . u16 ( <NUM_LIT:0> ) <EOL> def writeMethods ( pool , stream , methods , opts ) : <EOL> code_irs = [ ] <EOL> for method in methods : <EOL> code_irs . append ( writebytecode . getCodeIR ( pool , method , opts = opts ) ) <EOL> code_attrs = writebytecode . finishCodeAttrs ( pool , code_irs , opts = opts ) <EOL> stream . u16 ( len ( methods ) ) <EOL> for method in methods : <EOL> writeMethod ( pool , stream , method , code_attrs . get ( method ) ) <EOL> def classFileAfterPool ( cls , opts ) : <EOL> stream = Writer ( ) <EOL> if opts . split_pool : <EOL> pool = constantpool . SplitConstantPool ( ) <EOL> else : <EOL> pool = constantpool . SimpleConstantPool ( ) <EOL> cls . parseData ( ) <EOL> stream . u16 ( cls . access & flags . CLASS_FLAGS ) <EOL> stream . u16 ( pool . class_ ( cls . name ) ) <EOL> super_ = pool . class_ ( cls . super ) if cls . super is not None else <NUM_LIT:0> <EOL> stream . u16 ( super_ ) <EOL> stream . u16 ( len ( cls . interfaces ) ) <EOL> for interface in cls . interfaces : <EOL> stream . u16 ( pool . class_ ( interface ) ) <EOL> stream . u16 ( len ( cls . data . fields ) ) <EOL> for field in cls . data . fields : <EOL> writeField ( pool , stream , field ) <EOL> writeMethods ( pool , stream , cls . data . methods , opts = opts ) <EOL> stream . u16 ( <NUM_LIT:0> ) <EOL> return pool , stream <EOL> def toClassFile ( cls , opts ) : <EOL> stream = Writer ( ) <EOL> stream . u32 ( <NUM_LIT> ) <EOL> stream . u16 ( <NUM_LIT:0> ) <EOL> stream . u16 ( <NUM_LIT> ) <EOL> try : <EOL> pool , rest_stream = classFileAfterPool ( cls , opts = opts ) <EOL> except error . ClassfileLimitExceeded : <EOL> pool , rest_stream = classFileAfterPool ( cls , opts = options . ALL ) <EOL> pool . write ( stream ) <EOL> stream . write ( rest_stream . toBytes ( ) ) <EOL> return stream . toBytes ( ) </s>
