00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <iostream>
00011 #include "method_info.hpp"
00012 #include "code_attribute.hpp"
00013 #include "constantpool.hpp"
00014
00015 namespace ClassEncoder
00016 {
00017 using namespace std;
00018
00020
00024 method_info::method_info( ConstantPool *constantPool, const string &name )
00025 {
00026 ConstantPoolEntries = constantPool;
00027 CodeAttribute = new code_attribute( ConstantPoolEntries );
00028
00029 access_flags = 0;
00030 FunctionName = name;
00031 ReturnType = "V";
00032
00033 if( name == "<init>" )
00034 {
00035 CodeAttribute->addLocal();
00036 }
00037 }
00038
00040
00043 method_info::~method_info()
00044 {
00045 if( CodeAttribute )
00046 delete CodeAttribute;
00047 }
00048
00050
00053 void method_info::write( ofstream &stream )
00054 {
00055
00056 writeu2( stream, access_flags );
00057
00058 writeu2( stream, name_index );
00059
00060 writeu2( stream, descriptor_index );
00061
00062
00063 writeu2( stream, 1 );
00064
00065 CodeAttribute->write( stream );
00066 }
00067
00069
00084 void method_info::setAccess( u2 flags )
00085 {
00086
00087 access_flags = flags;
00088 }
00089
00091
00095 void method_info::addParameter( const string &objectType, bool addLocal )
00096 {
00097 Parameters.push_back( objectType );
00098 if( addLocal )
00099 CodeAttribute->addLocal();
00100 }
00101
00103
00107 void method_info::setReturnType( const string &type )
00108 {
00109 ReturnType = type;
00110 }
00111
00113
00119 unsigned int method_info::addLocal()
00120 {
00121 return CodeAttribute->addLocal();
00122 }
00123
00125
00133 string method_info::buildMethodDescriptor()
00134 {
00135 string result = "(";
00136 for( vector<string>::const_iterator it = Parameters.begin();
00137 it != Parameters.end(); it++ )
00138 {
00139 result += (*it);
00140 }
00141 result += ")";
00142 result += ReturnType;
00143 return result;
00144 }
00145
00147
00150 ByteCode *method_info::code()
00151 {
00152 return CodeAttribute->code();
00153 }
00154
00155
00157
00163 void method_info::finish()
00164 {
00165 name_index = ConstantPoolEntries->add( new Utf8_info( FunctionName ) );
00166 descriptor_index = ConstantPoolEntries->add( new Utf8_info( buildMethodDescriptor() ) );
00167 }
00168
00169 }