com.svincent.smalljava
Class SmallClass

java.lang.Object
  |
  +--com.svincent.util.BaseObject
        |
        +--com.svincent.smalljava.SmallClass

public class SmallClass
extends BaseObject

Represents a class in SmallJava.

To generate any code with the SmallJava library, you must create an instance of SmallClass, add a number of methods and fields to it, call finalize (), and finally, either generate Java code or JVM bytecodes using an appropriate method.

An example follows.

   {
     // --- make a new SmallClass: by default, is public and subclasses
     //   - java.lang.Object
     SmallClass myClass = new SmallClass ("MyClass");

     // --- add a protected instance field, (int age)
     myClass.instanceField ("I", "age");

     // --- add a method (single int parameter, named myInt
     SmallMethod foo = myClass.method ("foo(I)V", new String[] {"myInt"})
     
     // --- add a println (using a convenience method)
     foo.add (Expr.println ("Hello, world!"));

     // --- finalize the class
     myClass.finalize ();

     // --- write the class as java to MyClass.java
     myClass.writeAsJava ("MyClass.java");
     
     // --- write the class as JVM bytecodes to MyClass.class
     myClass.writeAsBytecodes ("MyClass.class");
   }
 

Note the use of JVM type descriptors. One can also use the Type class to create type descriptors. See that class's documentation also on documentation on how to write type descriptors.

Author:
Shawn Vincent

Constructor Summary
SmallClass(java.lang.String className)
          Make a new SmallClass given just a classname.
SmallClass(java.lang.String className, java.lang.String superClassName)
          Make a new SmallClass with a specified name and superclass.
SmallClass(java.lang.String className, java.lang.String superClassName, int accessFlags, java.lang.String[] interfaces)
          Make a new SmallClass with a specified name, superclass, permissions (as defined on java.lang.reflect.Modifier), and interfaces.
SmallClass(java.lang.String _className, java.lang.String _superClassName, int _accessFlags, java.lang.String[] _interfaces, java.lang.String _sourceFileName)
          The mother of all constructors: specifies everything.
 
Method Summary
 SmallConstructor addConstructor(SmallConstructor constructor)
          Adds a new constructor to this class.
 SmallField addField(SmallField field)
          Add the given field to this class.
 SmallMethod addMethod(SmallMethod method)
          Adds a new method to this class, returning the parameter for convenience.
 void addNullArgConstructor()
          Adds a default public nullarg constructor to this class.
 SmallConstructor constructor(java.lang.String descriptor, java.lang.String[] argNames)
          Adds a new constructor with the given descriptor and argnames to this class.
 java.util.Iterator constructors()
          Returns iterator over this class's constructors (type SmallConstructor)
 SmallField field(java.lang.String signature, java.lang.String name)
          Creates a new protected instance field, and adds it to this class.
 java.util.Iterator fields()
          Returns an iterator over all fields (type SmallField) in this class
 void finalize()
          Does a final typecheck pass, and fills in required missing information with defaults (add null-arg constructors, etc)
 int getConstructorCount()
          Returns the number of construtors this class has defined.
 int getFieldCount()
          Returns the number of construtors this class has defined.
 int getMethodCount()
          Returns the number of construtors this class has defined.
 java.lang.String getName()
          Returns the name of this class.
 java.lang.String getPackageName()
          Returns the package naem of this class, or null if there is none
 java.lang.String getRelativeName()
          Returns the name of this class, stripped of package information.
 java.lang.String getSuperClassName()
          Returns the name of this class's superclass.
 boolean isFinalized()
          Returns true iff this class has been finalized.
static void main(java.lang.String[] args)
          Test: generate a class called 'Person', which has some methods and code within.
 SmallMethod method(java.lang.String signature, java.lang.String[] argNames)
          Adds a new public instance method to this class.
 java.util.Iterator methods()
          Returns an iterator over all methods (type SmallMethod) in this class
 SmallMethod staticMethod(java.lang.String signature, java.lang.String[] argNames)
          Adds a new public static method to this class.
 byte[] toByteArray()
          Returns this class as a byte array.
 void writeAsBytecodes(java.io.OutputStream out)
          Writes the compiled JVM classfile corresponding to this SmallJava class to the given output stream.
 void writeAsBytecodes(java.lang.String fileName)
          Writes a standard Java classfile to a new file with the given name.
 void writeAsJava(java.lang.String fileName)
          Writes a new text file with the given filename, containing the generated Java code from this SmallJava class.
 void writeAsJava(java.io.Writer _out)
          Writes the generated Java code from this SmallJava class to the given IndentPrintWriter.
 
Methods inherited from class com.svincent.util.BaseObject
dump, dump, dumpToString, tag
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SmallClass

public SmallClass(java.lang.String className)
Make a new SmallClass given just a classname. By default, the SmallClass is public, is a subclass of java.lang.Object, and has the source file name "[generated]"
See Also:
SmallClass(String,String,int,String[],String)

SmallClass

public SmallClass(java.lang.String className,
                  java.lang.String superClassName)
Make a new SmallClass with a specified name and superclass.
See Also:
SmallClass(String,String,int,String[],String)

SmallClass

public SmallClass(java.lang.String className,
                  java.lang.String superClassName,
                  int accessFlags,
                  java.lang.String[] interfaces)

Make a new SmallClass with a specified name, superclass, permissions (as defined on java.lang.reflect.Modifier), and interfaces.

'interfaces' can be specified as null or an empty string array, if there are no interfaces implemented by the generated class.

See Also:
SmallClass(String,String,int,String[],String)

SmallClass

public SmallClass(java.lang.String _className,
                  java.lang.String _superClassName,
                  int _accessFlags,
                  java.lang.String[] _interfaces,
                  java.lang.String _sourceFileName)

The mother of all constructors: specifies everything.

The parameters are largely self-explanatory

'interfaces' can be specified as null or an empty string array, if there are no interfaces implemented by the generated class.

Method Detail

isFinalized

public boolean isFinalized()
Returns true iff this class has been finalized.

getName

public java.lang.String getName()
Returns the name of this class.

getRelativeName

public java.lang.String getRelativeName()
Returns the name of this class, stripped of package information.

getPackageName

public java.lang.String getPackageName()
Returns the package naem of this class, or null if there is none

getSuperClassName

public java.lang.String getSuperClassName()
Returns the name of this class's superclass.

addConstructor

public SmallConstructor addConstructor(SmallConstructor constructor)
                                throws SmallJavaBuildingException
Adds a new constructor to this class.
See Also:
SmallConstructor

constructor

public SmallConstructor constructor(java.lang.String descriptor,
                                    java.lang.String[] argNames)
                             throws SmallJavaBuildingException

Adds a new constructor with the given descriptor and argnames to this class. Note that the descriptor of a constructor is simply its type. For example, the constructor

   public MyClass (int i) { ... }
 

would have the descriptor

   (I)V
 

Note that constructors ALWAYS have a void return type

See Also:
SmallConstructor

addNullArgConstructor

public void addNullArgConstructor()
                           throws SmallJavaBuildingException
Adds a default public nullarg constructor to this class.

addMethod

public SmallMethod addMethod(SmallMethod method)
                      throws SmallJavaBuildingException

Adds a new method to this class, returning the parameter for convenience.

See Also:
method(java.lang.String, java.lang.String[])

method

public SmallMethod method(java.lang.String signature,
                          java.lang.String[] argNames)
                   throws SmallJavaBuildingException

Adds a new public instance method to this class. 'signature' is the concatenation of the methods name and its method descriptor.

For example, the method

   void foo (int[] sax) { ... }
 

would have the signature

   foo([I)V
 
See Also:
SmallMethod

staticMethod

public SmallMethod staticMethod(java.lang.String signature,
                                java.lang.String[] argNames)
                         throws SmallJavaBuildingException

Adds a new public static method to this class. 'signature' is the concatenation of the methods name and its method descriptor.

For example, the method

   void foo (int[] sax) { ... }
 

would have the signature

   foo([I)V
 
See Also:
SmallMethod

addField

public SmallField addField(SmallField field)
                    throws SmallJavaBuildingException
Add the given field to this class.
See Also:
SmallField

field

public SmallField field(java.lang.String signature,
                        java.lang.String name)
                 throws SmallJavaBuildingException
Creates a new protected instance field, and adds it to this class.
See Also:
SmallField

finalize

public void finalize()
              throws SmallJavaValidationException
Does a final typecheck pass, and fills in required missing information with defaults (add null-arg constructors, etc)
Overrides:
finalize in class java.lang.Object

fields

public java.util.Iterator fields()
Returns an iterator over all fields (type SmallField) in this class

methods

public java.util.Iterator methods()
Returns an iterator over all methods (type SmallMethod) in this class

constructors

public java.util.Iterator constructors()
Returns iterator over this class's constructors (type SmallConstructor)

getFieldCount

public int getFieldCount()
Returns the number of construtors this class has defined.

getMethodCount

public int getMethodCount()
Returns the number of construtors this class has defined.

getConstructorCount

public int getConstructorCount()
Returns the number of construtors this class has defined.

writeAsJava

public void writeAsJava(java.lang.String fileName)
                 throws java.io.IOException
Writes a new text file with the given filename, containing the generated Java code from this SmallJava class.

writeAsJava

public void writeAsJava(java.io.Writer _out)
Writes the generated Java code from this SmallJava class to the given IndentPrintWriter. XXX maybe allow other people to pass in stuff for the comment at the top of the file??

writeAsBytecodes

public void writeAsBytecodes(java.lang.String fileName)
                      throws java.io.IOException
Writes a standard Java classfile to a new file with the given name.

toByteArray

public byte[] toByteArray()
Returns this class as a byte array.

writeAsBytecodes

public void writeAsBytecodes(java.io.OutputStream out)
                      throws java.io.IOException
Writes the compiled JVM classfile corresponding to this SmallJava class to the given output stream.

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Test: generate a class called 'Person', which has some methods and code within. Dump as a Java source file and as a class file.



This is documentation for Moksa Prolog, which can be found at http://www.svincent.com/moksa/

Copyright © 1999 Shawn P. Vincent.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.