Main Page | Class Hierarchy | Compound List | File List | Compound Members | File Members | Related Pages

class.cpp

00001 /*
00002  * Copyright (c) 2003, Raymond Bosman
00003  * Copyright (c) 2003, Frederik Holljen
00004  * All Rights Reserved.
00005  *
00006  * See COPYING for licensing.
00007  */
00008 
00009 
00010 #include <fstream>
00011 
00012 #include "class.hpp"
00013 #include "defs.hpp"
00014 #include "method_info.hpp"
00015 
00016 namespace ClassEncoder
00017 {
00018 using namespace std;
00019 
00021 
00024 Class::Class( const std::string &className )
00025 {
00026     ClassName = className;
00027 }
00028 
00030 
00033 Class::~Class()
00034 {
00035 }
00036 
00038 
00042 void Class::write( )
00043 {
00044     string file = ClassName + ".class";
00045     ofstream classFile( file.c_str(), ios::out | ios::binary );
00046 
00047     // put some default stuff in the constant_pool
00048 
00049     // we do not do any inheritance at this point, tell JVM we inherit from java.lang.Object
00050     unsigned int utfPos = ConstantPoolEntries.count() + 2;
00051     u2 super_class = ConstantPoolEntries.add( new Class_info( utfPos ) );
00052     ConstantPoolEntries.add( new Utf8_info( "java/lang/Object" ) );
00053 
00054     // we're not going to use multiple classes either, just name this class output
00055     utfPos = ConstantPoolEntries.count() + 2;
00056     u2 this_class = ConstantPoolEntries.add( new Class_info( utfPos ) );
00057     ConstantPoolEntries.add( new Utf8_info( ClassName ) );
00058 
00059     //*** START THE REAL WRITING ***
00060     // u4 magic
00061     writeu4( classFile, JVM_MAGIC );
00062 
00063     // u2 minor_version
00064     writeu2( classFile, JVM_MINOR_VER );
00065     // u2 major_version
00066     writeu2( classFile, JVM_MAJOR_VER );
00067 
00068     // u2 constant_pool_count;
00069     writeu2( classFile, ConstantPoolEntries.count() + 1 );
00070     // cp_info constant_pool[constant_pool_count-1];
00071     ConstantPoolEntries.write( classFile );
00072 
00073 //  u2 access_flags; empty for now
00074     writeu2( classFile, ACC_SUPER );
00075 //  u2 this_class;
00076     writeu2( classFile, this_class );
00077 //  u2 super_class;
00078     writeu2( classFile, super_class );
00079 
00080 //  u2 interfaces_count;
00081 //  u2 interfaces[interfaces_count];
00082     writeu2( classFile, 0 );
00083 
00084 //  u2 fields_count;
00085 //  field_info fields[fields_count];
00086     writeu2( classFile, 0 );
00087 
00088 //  u2 methods_count;
00089 //  method_info methods[methods_count];
00090     writeu2( classFile, Methods.size() );
00091     for( vector<method_info*>::iterator it = Methods.begin(); it != Methods.end(); ++it )
00092     {
00093         (*it)->write( classFile );
00094     }
00095 
00096 //  u2 attributes_count;
00097 //  attribute_info attributes[attributes_count];
00098     writeu2( classFile, 0 );
00099 
00100     classFile.close();
00101 }
00102 
00104 
00109 method_info *Class::addMethod( const string &name )
00110 {
00111     method_info *method = new method_info( &ConstantPoolEntries, name );
00112     Methods.push_back( method );
00113     return method;
00114 }
00115 
00116 } // end namespace

Generated on Mon Dec 1 14:26:27 2003 for Ck by doxygen 1.3.3