⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 struct.java

📁 MobilerRPC enables remote procedure calls between J2ME applications and java sevlet server.
💻 JAVA
字号:
/*
 * Copyright (c) 2003, Artem Rudoy
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package net.sourceforge.mobilerpc;

import net.sourceforge.mobilerpc.simpletypes.BooleanType;

/**
 * $Id: Struct.java,v 1.5 2003/03/06 08:30:34 artyomr Exp $
 * @author Artem Rudoy
 */
public class Struct extends Type
{
    private String packageName = null;
    private Struct superStruct = null;
    private Attribute[] attributes = null;
    private Constant[] constants = null;
    private String additionalCode = null;

    public Struct(String name, String packageName, Struct superStruct)
    {
        super(name);

        this.packageName = packageName;
        this.superStruct = superStruct;
    }

    public String getFullName()
    {
        return packageName + "." + getName();
    }

    public void setAttributes(Attribute[] attributes)
    {
        this.attributes = attributes;
    }

    public void setConstants(Constant[] constants)
    {
        this.constants = constants;
    }

    public void setAdditionalCode(String additionalCode)
    {
        this.additionalCode = additionalCode;
    }

    private Attribute[] getAllAttributes()
    {
        if(superStruct == null)
            return attributes;
        else
        {
            Attribute[] superAttributes = superStruct.getAllAttributes();
            Attribute[] allAttributes = new Attribute[superAttributes.length + attributes.length];
            System.arraycopy(superAttributes, 0, allAttributes, 0, superAttributes.length);
            System.arraycopy(attributes, 0, allAttributes, superAttributes.length, attributes.length);
            return allAttributes;
        }
    }

    public void generateFile(IndentPrintStream ps)
    {
        ps.println("package " + packageName + ";");
        ps.println();
        ps.println("import java.io.*;");
        ps.println();
        ps.println(Generator.FILE_HEADER);
        ps.print("public class " + getName());
        if(superStruct != null)
        {
            ps.print(" extends " + superStruct.getName());
        }
        ps.println();
        ps.println("{");
        ps.increase();

        // Generate constants
        if(constants != null && constants.length > 0)
        {
            for(int i = 0; i < constants.length; i++)
            {
                constants[i].generateCode(ps);
            }
            ps.println();
        }

        // Generate attributes
        for(int i = 0; i < attributes.length; i++)
        {
            Attribute attr = attributes[i];
            ps.println("private " + attr.getFullTypeName() + " " + attr.getName() + ";");
        }
        ps.println();

        // Generate default constructor
        ps.println("public " + getName() + "() {}");
        ps.println();

        // Generate constructor for all attributes
        ps.print("public " + getName() + "(");
        Attribute[] allAttributes = getAllAttributes();
        for(int i = 0; i < allAttributes.length; i++)
        {
            Attribute attr = allAttributes[i];
            if(i > 0)
                ps.print(", ");
            ps.print(attr.getFullTypeName() + " " + attr.getName());
        }
        ps.println(")");
        ps.println("{");
        ps.increase();
        if(superStruct != null)
        {
            ps.print("super(");
            Attribute[] superAttributes = superStruct.getAllAttributes();
            for(int i = 0; i < superAttributes.length; i++)
            {
                Attribute attr = superAttributes[i];
                if(i > 0)
                    ps.print(", ");
                ps.print(attr.getName());
            }
            ps.println(");");
            ps.println();
        }
        for(int i = 0; i < attributes.length; i++)
        {
            Attribute attr = attributes[i];
            ps.println("this." + attr.getName() + " = " + attr.getName() + ";");
        }
        ps.decrease();
        ps.println("}");
        ps.println();

        // Generate getters and setters
        for(int i = 0; i < attributes.length; i++)
        {
            Attribute attr = attributes[i];
            String prefix;
            if(attr.getType() instanceof BooleanType && !attr.isArray())
                prefix = "is";
            else
                prefix = "get";
            ps.println("public " + attr.getFullTypeName() + " " + prefix + attr.getCapitalizedName() + "()");
            ps.println("{");
            ps.println("    return " + attr.getName() + ";");
            ps.println("}");
            ps.println();
            ps.println("public void set" + attr.getCapitalizedName() + "(" + attr.getFullTypeName() + " " + attr.getName() + ")");
            ps.println("{");
            ps.println("    this." + attr.getName() + " = " + attr.getName() + ";");
            ps.println("}");
            ps.println();
        }

        // Generate readObject method
        ps.println("public void readObject(DataInput dataInput) throws IOException");
        ps.println("{");
        ps.increase();
        if(superStruct != null)
        {
            ps.println("super.readObject(dataInput);");
            ps.println();
        }
        for(int i = 0; i < attributes.length; i++)
        {
            if(i > 0)
                ps.println();
            Attribute attr = attributes[i];
            if(attr.isArray())
            {
                attr.getType().generateLoadArrayCode(ps, attr.getName(), "dataInput");
            }
            else
            {
                attr.getType().generateLoadCode(ps, attr.getName(), "dataInput");
            }
        }
        ps.decrease();
        ps.println("}");
        ps.println();

        // Generate writeObject method
        ps.println("public void writeObject(DataOutput dataOutput) throws IOException");
        ps.println("{");
        ps.increase();
        if(superStruct != null)
        {
            ps.println("super.writeObject(dataOutput);");
            ps.println();
        }
        for(int i = 0; i < attributes.length; i++)
        {
            if(i > 0)
                ps.println();
            Attribute attr = attributes[i];
            if(attr.isArray())
            {
                attr.getType().generateWriteArrayCode(ps, attr.getName(), "dataOutput");
            }
            else
            {
                attr.getType().generateWriteCode(ps, attr.getName(), "dataOutput");
            }
        }
        ps.decrease();
        ps.println("}");

        // Write additional code
        if(additionalCode != null)
        {
            ps.println();
            ps.println(additionalCode);
        }

        ps.decrease();
        ps.println("}");
    }

    public void generateLoadCode(IndentPrintStream ps, String attributeName, String dataInputName)
    {
        ps.println("if(" + dataInputName + ".readBoolean())");
        ps.println("{");
        ps.increase();
        ps.println("try");
        ps.println("{");
        ps.increase();
        ps.println(attributeName + " = (" + getName() + ")Class.forName(" + dataInputName + ".readUTF()).newInstance();");
        ps.println(attributeName + ".readObject(" + dataInputName + ");");
        ps.decrease();
        ps.println("}");
        ps.println("catch(IOException e)");
        ps.println("{");
        ps.println("    throw e;");
        ps.println("}");
        ps.println("catch(Exception e)");
        ps.println("{");
        ps.println("    throw new IOException(e.getMessage());");
        ps.println("}");
        ps.decrease();
        ps.println("}");
        ps.println("else");
        ps.increase();
        ps.println(attributeName + " = null;");
        ps.decrease();
    }

    public void generateWriteCode(IndentPrintStream ps, String attributeName, String dataOutputName)
    {
        ps.println("if(" + attributeName + " == null)");
        ps.increase();
        ps.println(dataOutputName + ".writeBoolean(false);");
        ps.decrease();
        ps.println("else");
        ps.println("{");
        ps.increase();
        ps.println(dataOutputName + ".writeBoolean(true);");
        ps.println(dataOutputName + ".writeUTF(" + attributeName + ".getClass().getName());");
        ps.println(attributeName + ".writeObject(" + dataOutputName + ");");
        ps.decrease();
        ps.println("}");
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -