com.svincent.smalljava
Class SmallType

java.lang.Object
  |
  +--com.svincent.util.BaseObject
        |
        +--com.svincent.smalljava.SmallType
Direct Known Subclasses:
SmallType.ArrayType, SmallType.MethodType, SmallType.ObjectType, SmallType.PrimitiveType

public abstract class SmallType
extends BaseObject

The SmallJava API uses two internal representations for types. The first, which is used by most of the APIs for succinctness, is a slightly modified syntax of standard JVM type descriptors.

The syntax for fields is as follows (This table, of course, looks much nicer in an HTML form):

production syntax description
FieldType BaseType|ComplexType|ArrayTypeA basic field
BaseType Bsigned byte (byte)
Ccharacter (char)
Ddouble precision (double)
Fsingle precision (float)
Iinteger (int)
Jlong integer (long)
Sshort integer (short)
Zboolean value (boolean)
VerboseTypeLbyte;signed byte (byte)
Lchar;character (char)
Ldouble;double precision (double)
Lfloat;single precision (float)
Lint;integer (int)
Llong;long integer (long)
Lshort;short integer (short)
Lboolean;boolean value (boolean)
LclassName;boolean value (boolean)
ArrayType[FieldTypeArray of type

The syntax for fields is as follows (Again, this table looks much nicer in an HTML form):

production syntax description
MethodType (FieldType*)ReturnDescriptorMethod type
ReturnTypeFieldTypestandard field type
Vvoid (no return)

The following are some examples of field type specifiers, and their equivalent Java types.

    I                      int
    [I                     int[]
    Ljava.lang.String;     java.lang.String
    [[I                    int[][]
    [Z                     boolean[]
    Lint;                  int
    [Lint;                 int[]
    [Ljava.lang.String;    java.lang.String[]
 

The following are some examples of method type specifiers, and their equivalent Java types.

    ()V                    void foo ()
    (I)I                   int foo (int)
    (II)I                  int foo (int, int)
    (Ljava.lang.String)V   void foo (java.lang.String)
    ([Lint;)V              void foo (int[])
    ([I[[I)Z               boolean foo (int[], int[][])
 

An alternative to using these type descriptors is to use the SmallType API to build them for you. An example of this follows.

 {
   // --- Make a field type

   // --- make a type using a combination of static methods & static
   //   - instances.
   SmallType myType =
     SmallType.array (SmallType.array (SmallType.Int));

   // --- print as java (outputs "int[][]")
   myType.writeAsJava (Util.out);

   // --- print as type descriptor (outputs "[[I")
   myType.writeDescriptor (Util.out);


   // --- Make a method type
   SmallType.MethodType methodType =
      new SmallType.MethodType (myType, 
                                      new SmallType[] {SmallType.Int});

   // --- print as java (outputs "(int)->int[][]")
   methodType.writeAsJava (Util.out);

   // --- print as type descriptor (outputs "(I)[[I")
   methodType.writeDescriptor (Util.out);
 }
 

Author:
Shawn Vincent

Inner Class Summary
static class SmallType.ArrayType
          A type which represents an array type.
static class SmallType.MethodType
          Represents a type for a method.
static class SmallType.ObjectType
          An instance of a class.
protected static class SmallType.PrimitiveType
          Reprsents a primitive type, such as int or boolean.
 
Field Summary
static SmallType Boolean
          A boolean value: true or false.
static SmallType Byte
          A signed byte value.
static SmallType Char
          An unsigned Unicode character.
static boolean debug
          Debug flag for the parsing algorithm.
static SmallType Double
          A double-precision IEEE 754 float
static SmallType[] EmptyArray
          A constant empty array of SmallTypes.
static SmallType Float
          A single-precision IEEE 754 float
static SmallType Int
          A signed integer
static SmallType Long
          A signed long integer
static SmallType Short
          A signed short integer.
static SmallType Void
          The Void type: only allowed as the return type of methods.
 
Constructor Summary
SmallType()
           
 
Method Summary
protected  void addAdd(ClassFileWriter out)
          make an appropriately typed ADD operator (IADD, FADD, etc)
protected  void addArrayNew(ClassFileWriter out)
          make an appropriately typed NEWARRAY instruction
protected  void addArrayStore(ClassFileWriter out)
          make an appropriately typed ASTORE operator (IASTORE, AASTORE, etc)
protected  void addCmp(ClassFileWriter out)
          make an appropriately typed CMP operator (ISUB, LCMP, etc)
protected  void addLoad(ClassFileWriter out, int index)
          make an appropriately typed LOAD operator (ILOAD, ALOAD, etc)
protected  void addReturn(ClassFileWriter out)
          make an appropriately typed RETURN operator (IRETURN, LRETURN, etc)
protected  void addStore(ClassFileWriter out, int index)
          make an appropriately typed STORE operator (ISTORE, ASTORE, etc)
protected  void addSub(ClassFileWriter out)
          make an appropriately typed SUB operator (ISUB, FSUB, etc)
static SmallType.ArrayType array(SmallType t)
          Make a new array of the given type.
 java.lang.String descriptorToString()
          Returns this type's type descriptor as a String.
 boolean isPrimitive()
          Returns 'true' if this type is a primitive type, such as 'int'.
static void main(java.lang.String[] args)
          A non-exhaustive test of the field and method descriptor parsing mechanism.
static SmallType parseFieldDescriptor(java.lang.String desc)
          Parse the given field descriptor into a SmallType.
static SmallType.MethodType parseMethodDescriptor(java.lang.String desc)
          Parses a method descriptor into a SmallType.MethodType structure.
 java.lang.String tag()
          Returns a short description of this object, suitable for printing.
static void testField(java.io.PrintWriter out, java.lang.String fieldDesc)
          Test method for testing the parsing of field descriptors
static void testMethod(java.io.PrintWriter out, java.lang.String methodDesc)
          Test method for testing the parsing of method descriptors
abstract  void writeAsJava(java.io.PrintWriter out)
          Write this type out as Java code, if possible.
abstract  void writeDescriptor(java.io.PrintWriter out)
          Write this type's type descriptor to the given PrintWriter.
 
Methods inherited from class com.svincent.util.BaseObject
dump, dump, dumpToString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

debug

public static final boolean debug
Debug flag for the parsing algorithm.

EmptyArray

public static final SmallType[] EmptyArray
A constant empty array of SmallTypes. Useful on rainy nights.

Void

public static final SmallType Void
The Void type: only allowed as the return type of methods.

Boolean

public static final SmallType Boolean
A boolean value: true or false.

Byte

public static final SmallType Byte
A signed byte value.

Char

public static final SmallType Char
An unsigned Unicode character.

Short

public static final SmallType Short
A signed short integer.

Int

public static final SmallType Int
A signed integer

Long

public static final SmallType Long
A signed long integer

Float

public static final SmallType Float
A single-precision IEEE 754 float

Double

public static final SmallType Double
A double-precision IEEE 754 float
Constructor Detail

SmallType

public SmallType()
Method Detail

isPrimitive

public boolean isPrimitive()
Returns 'true' if this type is a primitive type, such as 'int'.

tag

public java.lang.String tag()
Returns a short description of this object, suitable for printing.
Overrides:
tag in class BaseObject

descriptorToString

public java.lang.String descriptorToString()
Returns this type's type descriptor as a String.
See Also:
writeDescriptor(java.io.PrintWriter)

writeAsJava

public abstract void writeAsJava(java.io.PrintWriter out)
Write this type out as Java code, if possible. If not possible, do your best.

writeDescriptor

public abstract void writeDescriptor(java.io.PrintWriter out)
Write this type's type descriptor to the given PrintWriter.

addLoad

protected void addLoad(ClassFileWriter out,
                       int index)
make an appropriately typed LOAD operator (ILOAD, ALOAD, etc)

addStore

protected void addStore(ClassFileWriter out,
                        int index)
make an appropriately typed STORE operator (ISTORE, ASTORE, etc)

addArrayStore

protected void addArrayStore(ClassFileWriter out)
make an appropriately typed ASTORE operator (IASTORE, AASTORE, etc)

addReturn

protected void addReturn(ClassFileWriter out)
make an appropriately typed RETURN operator (IRETURN, LRETURN, etc)

addAdd

protected void addAdd(ClassFileWriter out)
make an appropriately typed ADD operator (IADD, FADD, etc)

addSub

protected void addSub(ClassFileWriter out)
make an appropriately typed SUB operator (ISUB, FSUB, etc)

addCmp

protected void addCmp(ClassFileWriter out)
make an appropriately typed CMP operator (ISUB, LCMP, etc)

addArrayNew

protected void addArrayNew(ClassFileWriter out)
make an appropriately typed NEWARRAY instruction

array

public static SmallType.ArrayType array(SmallType t)
Make a new array of the given type. If you pass an array type into this method, you will get a multidimensional array!

parseFieldDescriptor

public static SmallType parseFieldDescriptor(java.lang.String desc)
                                      throws InvalidDescriptorException

Parse the given field descriptor into a SmallType. Will not work on method descriptors. Reasonably efficient implementation.

Meaningful exceptions are thrown, including integer positions, if an error is found.


parseMethodDescriptor

public static SmallType.MethodType parseMethodDescriptor(java.lang.String desc)
                                                  throws InvalidDescriptorException

Parses a method descriptor into a SmallType.MethodType structure.

Will, of course, not work on fields (as they cannot be represented with MethodTypes). Reasonably efficient implementation.

Meaningful exceptions are thrown, including integer positions, if an error is found.


main

public static void main(java.lang.String[] args)
A non-exhaustive test of the field and method descriptor parsing mechanism. Many cases are dealt with, but rigorous tests have yet to be performed.

testField

public static void testField(java.io.PrintWriter out,
                             java.lang.String fieldDesc)
Test method for testing the parsing of field descriptors

testMethod

public static void testMethod(java.io.PrintWriter out,
                              java.lang.String methodDesc)
Test method for testing the parsing of method descriptors



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.