FileDocCategorySizeDatePackage
AbstractGenerator.javaAPI DocphoneME MR2 API (J2ME)14751Wed May 02 18:00:34 BST 2007com.sun.j2mews.sg

AbstractGenerator

public abstract class AbstractGenerator extends Object
We'll generate the Java client proxy for JSR-172.

Fields Summary
public static final String
FLOAT_DOUBLE_TO_STRING
public static final String
SHOW_ALL_CLDC1_0_INFO
public static final String
EXPAND_ARGUMENTS
public static final String
OPTIMIZE
protected com.sun.xml.rpc.processor.util.ProcessorEnvironment
env
protected com.sun.xml.rpc.processor.model.Service
service
protected Port
port
protected JavaWriter
jw
protected String
fullClassName
protected String
className
protected String
packageName
protected File
sourceDir
protected LocalizableMessageFactory
messageFactory
If you're going to call getMessage, then you need to set messageFactory.
protected Localizer
localizer
protected boolean
expandArguments
Whether or not the formal parameters on the Stub should get expanded. It will be done for each parameter and return type, if there's only 1 of them and it's a JavaStructureType.
protected boolean
optimize
Whether to trade readable generated code for more efficient generated code.
protected boolean
showCldc1_0Info
Whether or not to display every info message, or just 1 (similar to javac's -deprecation flag).
Constructors Summary
Methods Summary
protected static java.lang.StringbaseName(java.lang.String fullName)
This will return a name from @param fullName where everything upto and including the last '.' is removed. eg: "java.lang.String[]" -> "String[]" "java.util.ArrayList" -> "ArrayList"

        int pos = fullName.lastIndexOf('.");
        if (pos == -1)
            return fullName;
        return fullName.substring(pos+1, fullName.length());
    
public voidgenerate(com.sun.xml.rpc.processor.model.Service service)

        for (Iterator it = service.getPorts(); it.hasNext(); ) {
            port = (Port) it.next();
            setFullClassName(getFullClassName());
            generate();
        }
    
protected voidgenerate()
At this point, the service and port variables are set.

        jw = new JavaWriter();

        jw.comment("This class was generated by 172 StubGenerator.");
        jw.comment("Contents subject to change without notice.");
        jw.comment("@generated");
        jw.cr();
        jw.writePackage(packageName);
        jw.cr();
        writeImports();

        generateClass();

        writeOutGeneratedFile();
    
protected abstract voidgenerateClass()

protected java.util.ListgetExpandedParametersList(JavaMethod method)

return
a List of parameters to this method in the stub, taking into account expandArguments.

        List parameterList = method.getParametersList();
        if (parameterList.size() == 1 && expandArguments) {
            JavaParameter parameter = (JavaParameter) parameterList.iterator().next();
            Parameter p = parameter.getParameter();
            JavaType parameterType = parameter.getType();
            Block parameterBlock = p.getBlock();
            boolean isNillable = parameterBlock.getType().isNillable();
            if (parameterType instanceof JavaStructureType && !isNillable) {
                JavaStructureType jst = (JavaStructureType) parameterType;
                parameterList = new ArrayList(jst.getMembersCount());
                for (Iterator members = jst.getMembers(); members.hasNext(); ) {
                    JavaStructureMember jsm = (JavaStructureMember) members.next();
                    String memberName = jsm.getName();
                    JavaType memberType = jsm.getType();
                    parameter = new JavaParameter(memberName, memberType, p);
                    parameterList.add(parameter);
                }
            }
        }
        return parameterList;
    
protected JavaTypegetExpandedReturnType(JavaMethod method)

return
the return type taking into account expandArguments.

        JavaType returnType = method.getReturnType();
        if (returnType == null || "void".equals(javaTypeToString(returnType)))
            return returnType;
        boolean isNillable = false;
        if (expandArguments && !isNillable &&
            returnType instanceof JavaStructureType) {
            JavaStructureType jst = (JavaStructureType) returnType;
            if (jst.getMembersCount() == 1) {
                returnType = ((JavaStructureMember)jst.getMembers().next()).getType();
            }
        }
        return returnType;
    
protected abstract java.lang.StringgetFullClassName()
The port and env variables will be set correctly before calling this method.

protected LocalizablegetMessage(java.lang.String key)

        return messageFactory.getMessage(key);
    
protected LocalizablegetMessage(java.lang.String key, java.lang.String arg)

        return messageFactory.getMessage(key, new Object[] {arg});
    
protected LocalizablegetMessage(java.lang.String key, java.lang.String arg1, java.lang.String arg2)

        return messageFactory.getMessage(key, new Object[] {arg1, arg2});
    
protected LocalizablegetMessage(java.lang.String key, java.lang.String arg1, java.lang.String arg2, java.lang.String arg3)

        return messageFactory.getMessage(key, new Object[] {arg1, arg2, arg3});
    
protected LocalizablegetMessage(java.lang.String key, Localizable l)

        return messageFactory.getMessage(key, new Object[] {l});
    
protected LocalizablegetMessage(java.lang.String key, java.lang.Object[] args)

        return messageFactory.getMessage(key, args);
    
protected abstract java.io.FilegetSourceFile()
The port and env variables will be set correctly before calling this method. This is the File that gets written to.

protected abstract java.lang.StringgetSourceFileType()

see
GeneratorConstants

protected java.lang.StringinstanceOf(JavaType type, java.lang.String suffix, java.util.Map usedNames)

        String name = type.getName();
        return instanceOf(name, suffix, usedNames);
    
protected java.lang.StringinstanceOf(JavaType type, java.util.Map usedNames)

        return instanceOf(type, null, usedNames);
    
protected java.lang.StringinstanceOf(javax.xml.namespace.QName name, java.lang.String suffix, java.util.Map usedNames)

        return instanceOf(name.getLocalPart(), suffix, usedNames);
    
protected java.lang.StringinstanceOf(java.lang.String name, java.lang.String suffix, java.util.Map usedNames)
Make a variable name valid and unique.

        int pos;
        // Remove everything before the .
        pos = name.lastIndexOf('.");
        if (pos > 0)
            name = name.substring(pos+1, name.length());

        // Get rid of any trailing []
        int arrayDimensions = 0;
        while (name.endsWith("[]")) {
            name = name.substring(0, name.length()-2);
            ++arrayDimensions;
        }
        for (int i = 0; i < arrayDimensions; ++i)
            name += "Array";

        if (JavaUtil.isPrimitiveType(name))
            name = "a_"+name;

        if (suffix != null)
            name += suffix;

        /*
        // make first char lowercase
        name = java.beans.Introspector.decapitalize(name);
        */
        name = env.getNames().validJavaMemberName(name);

        return makeUniq(name, usedNames);
    
public static booleanisValidType(java.lang.String type)
Is @param type valid for MIDP? This assumes CLDC 1.1 (where float and double are okay).

        if (type == null)  // "void" type
            return true;
        type = type.intern();
        if (type == "java.math.BigDecimal" || type == "java.math.BigInteger")
            return false;
        return true;
    
protected java.lang.StringjavaTypeToString(JavaType type)

        if (type == null)
            return null;
        String result;
        if (type.isHolder())
            result = env.getNames().holderClassName(port, type);
        else
            result = env.getNames().typeClassName(type);
        return result;
    
protected java.lang.Stringlocalize(Localizable msg)

        return localizer.localize(msg);
    
protected voidlog(java.lang.String msg)

        if (env.verbose()) {
            System.out.println("["+env.getNames().stripQualifier(this.getClass().getName())+": "+msg+"]");
        }
    
protected java.lang.StringmakeUniq(java.lang.String name, java.util.Map usedNames)
Make a variable name unique. Puts the name into usedNames.

        if (usedNames.containsKey(name) || env.getNames().isJavaReservedWord(name)) {
            String baseName = name;
            
            for (int count = optimize ? 0 : 2; usedNames.containsKey(name); ++count)
                name = baseName + (optimize ? Integer.toString(count, Character.MAX_RADIX) : ""+count);
        }
        usedNames.put(name, null);
        return name;
    
protected java.lang.StringmakeVarName(javax.xml.namespace.QName name, java.lang.String prefix, java.util.Map usedNames)
Try to get a unique and somewhat nice looking name.

        String result = prefix+name.getLocalPart();
        result = result.replace('.", '_");
        result = result.replace('/", '_");
        result = result.replace(':", '_");
        if (usedNames != null && usedNames.containsKey(result)) {
            String abbrevNamespace = name.getNamespaceURI();
            if (abbrevNamespace != null) {
                int pos = abbrevNamespace.lastIndexOf('/");
                if (pos >= 0)
                    abbrevNamespace = abbrevNamespace.substring(pos+1, abbrevNamespace.length());
                abbrevNamespace = abbrevNamespace.replace('.", '_");
                abbrevNamespace = abbrevNamespace.replace(':", '_");
                abbrevNamespace = abbrevNamespace.replace('/", '_");
                abbrevNamespace = abbrevNamespace.replace('%", '_");
                result = prefix+abbrevNamespace+"_"+name.getLocalPart();
            }
            result = makeUniq(result, usedNames);
        }
        return result;
    
protected voidonError(Localizable msg)

        //System.err.println("Error: "+msg);
        env.error(msg);
    
protected voidonInfo(Localizable msg)

        //System.err.println("Info: "+msg);
        env.info(msg);
    
protected voidonWarning(Localizable msg)

        //System.err.println("Warning: "+msg);
        env.warn(msg);
    
public voidperform(com.sun.xml.rpc.processor.model.Model model, com.sun.xml.rpc.processor.config.Configuration config, java.util.Properties options)

        //env = config.getEnvironment();
        sourceDir = new File(options.getProperty(ProcessorOptions.SOURCE_DIRECTORY_PROPERTY));
        if ("true".equals(options.getProperty(EXPAND_ARGUMENTS)))
            expandArguments = true;
        else
            expandArguments = false;
        if ("true".equals(options.getProperty(SHOW_ALL_CLDC1_0_INFO)))
            showCldc1_0Info = true;
        else
            showCldc1_0Info = false;
        if ("true".equals(options.getProperty(OPTIMIZE)))
            optimize = true;
        else
            optimize = false;

        try {
            for (Iterator it = model.getServices(); it.hasNext(); ) {
                service = (Service) it.next();
                generate(service);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    
public voidsetEnvironment(com.sun.xml.rpc.processor.util.ProcessorEnvironment e)

        env = e;
    
protected voidsetFullClassName(java.lang.String name)
Sets the internal state of fullClassName, className, & packageName.

param
name the full class name


                        
        
        fullClassName = name;
        int pos = fullClassName.lastIndexOf('.");
        if (pos >= 0) {
            packageName = fullClassName.substring(0, pos);
            className = fullClassName.substring(pos+1, fullClassName.length());
        } else {
            packageName = null;
            className = fullClassName;
        }
    
public voidsetLocalizer(Localizer l)

        localizer = l;
    
protected voidwriteImports()
Here's your chance to import anything.

    
protected voidwriteOutGeneratedFile()

        File srcFile = getSourceFile();
        log("Generating Java class for: "+srcFile.getName());
        OutputStream out = new FileOutputStream(srcFile);
        jw.writeTo(out);
        out.close();
        GeneratedFileInfo fi = new GeneratedFileInfo();
        fi.setFile(srcFile);
        fi.setType(getSourceFileType());
        env.addGeneratedFile(fi);