FileDocCategorySizeDatePackage
JavaClassWriterHelper.javaAPI DocGlassfish v2 API18079Fri May 04 22:35:22 BST 2007com.sun.jdo.spi.persistence.utility.generator

JavaClassWriterHelper

public class JavaClassWriterHelper extends com.sun.jdo.spi.persistence.utility.JavaTypeHelper

Fields Summary
public static final String
javaExtension_
public static final String
void_
public static final String
boolean_
public static final String
byte_
public static final String
byteArray_
public static final String
param_
public static final String
param0_
public static final String
null_
public static final String
home_
public static final String
delim_
public static final String
paramInitializer_
public static final String
paramSeparator_
public static final String
paramList_
public static final String
paramConcatenator_
public static final String
space_
public static final String
none_
public static final String
escapedEmptyString_
public static final String
dot_
public static final String
parenleft_
public static final String
parenright_
public static final String
parenthesis_
public static final String
new_
public static final String
endLine_
public static final String
true_
public static final String
false_
public static final String
Collection_
public static final String
Set_
public static final String
PersistenceCapable_
public static final String
brackets_
public static final String
get_
public static final String
set_
public static final String
Oid_
public static final String
Helper_
public static final String
returnNull_
public static final String
fileName_
public static final String
int_
public static final String
String_
public static final String
Class_
public static final String
Date_
public static final String
SqlDate_
public static final String
SqlTime_
public static final String
SqlTimestamp_
public static final String
Object_
public static final String[]
super_
private static final String[]
indentation_
Constructors Summary
Methods Summary
public static voidaddFields(java.lang.String prop, int modifiers, JavaClassWriter writer)
Adds fields to a class parsing the multi-String property.

param
prop String to use for field generation.
param
modifiers other field modifiers for these fields.
param
JavaClassWriter the Class writer.
throws
IOException if writer fails to add a field.

        String[] v = getBodyAsStrings(prop);
        for (int i = 0; i < v.length; i++) {
            StringTokenizer st = new StringTokenizer(v[i], space_);
            String type = st.nextToken();
            String name = st.nextToken();
            StringBuffer value = new StringBuffer();
            while(st.hasMoreTokens())
               value.append(st.nextToken() + space_);

            int l = value.length();
            value.deleteCharAt(l - 1); // last space
            writer.addField(name, // name
               modifiers, // modifiers
               type, // type
               value.toString(), // value,
               null); // comments
        }
    
public static voidaddGenericMethod(java.lang.String mname, java.lang.String[] body, JavaClassWriter writer)
Adds a private void no-args method to a class with this method body.

param
mname method name
param
body the method body as String[]
param
JavaClassWriter the Class writer.
throws
IOException if writer fails to add a field.

        addGenericMethod(mname, void_, body, writer);
    
public static voidaddGenericMethod(java.lang.String mname, java.lang.String type, java.lang.String[] body, JavaClassWriter writer)
Adds a private no-args method to a class with this method body and return type.

param
mname method name
param
type return type of the method
param
body the method body as String[]
param
JavaClassWriter the Class writer.
throws
IOException if writer fails to add a field.

        addGenericMethod(mname, Modifier.PRIVATE, type, body, writer);
    
public static voidaddGenericMethod(java.lang.String mname, int modifiers, java.lang.String[] body, JavaClassWriter writer)
Adds a private void no-args method to a class with this method body and modifiers.

param
mname method name
param
modifiers the method modifiers
param
body the method body as String[]
param
JavaClassWriter the Class writer.
throws
IOException if writer fails to add a field.

        addGenericMethod(mname, modifiers, void_, body, writer);
    
public static voidaddGenericMethod(java.lang.String mname, int modifiers, java.lang.String type, java.lang.String[] body, JavaClassWriter writer)
Adds a private void no-args method to a class with this method body, modifiers, and return type.

param
mname method name
param
modifiers the method modifiers
param
type return type of the method
param
body the method body as String[]
param
JavaClassWriter the Class writer.
throws
IOException if writer fails to add a field.

        writer.addMethod(mname, // name
            modifiers, // modifiers
            type, // returnType
            null, // parameterNames
            null,// parameterTypes
            null,// exceptions
            body, // body
            null);// comments
    
public static voidaddGenericMethod(java.lang.reflect.Method m, java.lang.String mname, java.lang.String mtype, java.lang.String body, JavaClassWriter writer)
Adds a method to a class parsing the multi-String property.

param
m Method that describes one to be added to the bean.
param
mname method name if different from Method#getName(). This will be true for finder/selector/create methods.
param
type return type of the method, that can differ from the bean's local or remote interface.
param
body String to use for method body generation.
param
JavaClassWriter the Class writer.
throws
IOException if writer fails to add a field.


        Class[] types = m.getParameterTypes();
        int count = types.length;
        String[] parameterTypes = new String[count];
        String[] parameterNames = new String[count];
        for (int ii = 0; ii < count; ii++) {
            parameterTypes[ii] = getTypeRepr(types[ii]);
            parameterNames[ii] = param_ + ii;
        }

        String[] exceptionTypes = getExceptionNames(m);

        int modifiers = m.getModifiers();
        if (Modifier.isAbstract(modifiers))
            modifiers -= Modifier.ABSTRACT;

        writer.addMethod(mname, // name
            modifiers, // modifiers
            mtype, // returnType
            parameterNames, // parameterNames
            parameterTypes,// parameterTypes
            exceptionTypes,// exceptions
            getBodyAsStrings(body), // body
            null);// comments
    
public static voidaddPrivateField(java.lang.String prop, int modifiers, JavaClassWriter writer)
Adds private fields to a class parsing the multi-String property.

param
prop String to use for field generation.
param
modifiers field modifiers for these fields.
param
JavaClassWriter the Class writer.
throws
IOException if writer fails to add a field.

        addFields(prop, Modifier.PRIVATE + modifiers, writer);
    
public static java.lang.String[]getBodyAsStrings(java.lang.String body)
Converts method body into String array.

param
body as String with each substring separated by "\n"
return
method body as String array.

 // NOI18N

                               
         
        StringTokenizer st = new StringTokenizer(body, endLine_);
        String[] rc = new String[st.countTokens()];
        int ii = 0;
        while(st.hasMoreTokens()) {
            String s = st.nextToken();
            int i = s.lastIndexOf('\t");
            if (i > -1)
                 rc[ii] = indentation_[i] + s.substring(i + 1);
            else
                 rc[ii] = s;

            ii++;
        }

        return rc;
    
public static java.lang.String[]getExceptionNames(java.lang.reflect.Method m)
Returns exception type names as String[].

param
m the Method to identify exception types for.
return
exception type names as String[].

        Class[] cls = m.getExceptionTypes();
        String[] rc = new String[cls.length];
        for (int ii = 0; ii < cls.length; ii++) {
            rc[ii] = cls[ii].getName();
        }
        return rc;
    
public static java.lang.StringgetParameterTypesList(java.lang.reflect.Method m)
Returns list of method parameter types in format type0[,type1[,...]]

param
m the Method to identify list of method parameters for.
return
list of method parameter types as String

       if (m == null)
            return none_;
 
        StringBuffer buf = new StringBuffer();
        Class[] paramTypes = m.getParameterTypes();
        for (int i = 0; i < paramTypes.length; i++) {
            if (i > 0)
                buf.append(paramList_);
            buf.append(getTypeRepr(paramTypes[i]));
        }
        return buf.toString();

    
public static java.lang.StringgetParametersList(java.lang.reflect.Method m)
Returns list of method parameters in format param0[, param1[,...]]

param
m the Method to identify list of method parameters for.
return
list of method parameters as String

        return getParametersListWithSeparator(m, paramSeparator_);
    
public static java.lang.StringgetParametersListWithSeparator(java.lang.reflect.Method m, java.lang.String sep)
Returns list of method parameters delimited by specified separator

param
m the Method to identify list of method parameters for.
param
sep the separator to be used to delimit the parameter names
return
list of method parameters as String

        int count = m.getParameterTypes().length;
        StringBuffer rc = new StringBuffer();

        for (int ii = 0; ii < count; ii++) {
            if (ii > 0)
                rc.append(sep);

            rc.append(param_ + ii);
        }
        return rc.toString();
    
public static java.lang.StringgetPrimitiveType(java.lang.Class cls)
Returns name of the primitive type corresponding to the Object wrapper Class passed as a parameter. If the parameter is of primitive type, its name is returned.

param
cls the Object wrapper Class to find name of the primitive type for.
return
name of the primitive type as String.

        String rc = getPrimitiveName(cls);
        if (rc == null) { // not an Object
            rc = cls.getName();
        }

        return rc;
    
public static java.lang.StringgetTypeRepr(java.lang.Class clazz)
Returns the String representation of the specified class instance. An array is represented as the name of the element type followed by [].

        return (clazz.isArray() ?
                getTypeRepr(clazz.getComponentType()) + brackets_ :
                clazz.getName());
    
public static java.lang.StringgetUnwrapMethodName(java.lang.Class primitiveType)
Returns the name of the method to access the value of the primitive type of the wrapper class. example: boolean.class is mapped to "booleanValue()".

param
primitiveType the class object of the primitive type.
return
the name of the method to access the primitive value of the wrapper

        return primitiveType.getName() + "Value()"; //NOI18N
    
public static java.lang.StringgetWrapperExpr(java.lang.Class exprType, java.lang.String expr)
A helper method which generates an expression to wrap a primitive datatype to its corresponding wrapper class.

param
exprType The class of the primitive type
param
expr The expression representing a primtive typevalue
return
A String containing the expression for wrapping the primitive datatype in its corresponding wrapperclass


        StringBuffer wrapped = new StringBuffer();

        wrapped.append(new_);
        wrapped.append(space_);
        wrapped.append(getWrapperType(exprType).getName());
        wrapped.append(parenleft_);
        wrapped.append(expr);
        wrapped.append(parenright_);

        return wrapped.toString();
    
public static java.lang.ClassgetWrapperType(java.lang.Class cls)
Returns java Object wrapper Class corresponding to the primitive Class if the passed class represents a primitive. If the parameter is of Object type, it is returned.

param
cls the primitive Class to find Object wrapper for.
return
Object type Class.

        Class rc = getWrapperClass(cls);
        if (rc == null) { // not a primitive
            rc = cls;
        }

        return rc;