|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object
|
+--com.svincent.util.BaseObject
|
+--com.svincent.smalljava.SmallClass
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.
| 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 |
public SmallClass(java.lang.String className)
SmallClass(String,String,int,String[],String)
public SmallClass(java.lang.String className,
java.lang.String superClassName)
SmallClass(String,String,int,String[],String)
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.
SmallClass(String,String,int,String[],String)
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 |
public boolean isFinalized()
public java.lang.String getName()
public java.lang.String getRelativeName()
public java.lang.String getPackageName()
public java.lang.String getSuperClassName()
public SmallConstructor addConstructor(SmallConstructor constructor)
throws SmallJavaBuildingException
SmallConstructor
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
SmallConstructor
public void addNullArgConstructor()
throws SmallJavaBuildingException
public SmallMethod addMethod(SmallMethod method)
throws SmallJavaBuildingException
Adds a new method to this class, returning the parameter for convenience.
method(java.lang.String, java.lang.String[])
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
SmallMethod
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
SmallMethod
public SmallField addField(SmallField field)
throws SmallJavaBuildingException
SmallField
public SmallField field(java.lang.String signature,
java.lang.String name)
throws SmallJavaBuildingException
SmallField
public void finalize()
throws SmallJavaValidationException
public java.util.Iterator fields()
public java.util.Iterator methods()
public java.util.Iterator constructors()
public int getFieldCount()
public int getMethodCount()
public int getConstructorCount()
public void writeAsJava(java.lang.String fileName)
throws java.io.IOException
public void writeAsJava(java.io.Writer _out)
public void writeAsBytecodes(java.lang.String fileName)
throws java.io.IOException
public byte[] toByteArray()
public void writeAsBytecodes(java.io.OutputStream out)
throws java.io.IOException
public static void main(java.lang.String[] args)
throws java.lang.Exception
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
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.