Package org.destecs.tools.jprotocolgenerator.ast

Examples of org.destecs.tools.jprotocolgenerator.ast.Method


  public String generate(IInterface intf, String outputFolder)
  {
    stryctInterface = new IInterface();
    stryctInterface.setName("IStruct");

    Method toMapMethod = new Method();
    toMapMethod.name = "toMap";
    toMapMethod.returnType = new FreeTextType("Map<String,? extends Object>");// new Type( new MapType(new
    // Type(String.class), new
    // Type(Object.class));
    stryctInterface.imports.add(new Type(Map.class));
View Full Code Here


    {
      appendName = "StructParam";
    }

    // Constructor
    Method m = new Method();
    m.name = returnClass.getName();
    m.returnType = new VoidType();
    m.body = "";
    m.isConstructor = true;
    Integer count = 0;
    Parameter param = new Parameter(map, "value");
    m.parameters.add(param);
    SuppressWarningAnnotation swa = new SuppressWarningAnnotation();
    m.annotation = swa;
    for (String p : map.possibleEntries.keySet())
    {
      Field f = new Field();
      f.setName(p);
      returnClass.fields.add(f);

      ITypeNode type = map.possibleEntries.get(p);
      if (type instanceof ListType)
      {
        if (((ListType) type).type instanceof MapType)
        {
          swa.warnings.add(WarningTypes.unchecked);
          ClassDefinition newRet = new ClassDefinition();
          newRet.setName(m.name + p + appendName);
          newRet.packageName = packageName2;
          newRet.imports.add(new Type(Map.class));
          newRet.imports.add(new Type(List.class));
          newRet.implemented.add(stryctInterface);
          returnClass.imports.add(new Type(Vector.class));

          defs.addAll(generateStructs(newRet, packageName2, (MapType) ((ListType) type).type, isParameter));
          defs.add(newRet);
          f.type = new ListType(newRet);
          m.body += f.type.getName() + " tmp" + count
              + " = new Vector<" + newRet.getName()
              + ">();\n\t\t";

          m.body += "if( value.keySet().contains(\"" + f.getName()
              + "\" ))\n\t\t";
          m.body += "{\n\t\t\t";

          if (map.valueType instanceof List
              || map.valueType instanceof ListType)
          {
            m.body += "Object tmpL" + count + " = value.get(\""
                + f.getName() + "\");\n\t\t\t";
            m.body += "for( Object m : (Object[])tmpL" + count
                + ")\n\t\t\t";

          } else
          {
            m.body += "for( Object m : (Object[])value.get(\""
                + f.getName() + "\"))\n\t\t\t";
          }

          m.body += "{\n\t\t\t\t";

          m.body += "tmp" + count + ".add( new " + newRet.getName()
              + "( (" + ((ListType) type).type + ") m)";
          m.body += ");\n\t\t\t";

          m.body += "}\n\t\t";

          m.body += "}\n\t\t";

          m.body += "this." + f.getName() + " = tmp" + count
              + ";\n\n\n\t\t\t";

        } else
        {
          f.type = map.possibleEntries.get(p);
          // m.body += "this." + f.getName() + " = (" + f.type.getName()
          // + ") value.get(value.keySet().toArray()[" + count
          // + "]);\n\t\t";
          m.body += "if( value.keySet().contains(\"" + f.getName()
              + "\" ))\n\t\t";
          m.body += "{\n\t\t\t";

          if (map.valueType instanceof List
              || map.valueType instanceof ListType)
          {
            m.body += "Object tmpL" + count + " = value.get(\""
                + f.getName() + "\");\n\t\t\t";
            m.body += "for( Object m : (Object[])tmpL" + count
                + ")\n\t\t\t";

          } else
          {
            m.body += "for( Object m : (Object[])value.get(\""
                + f.getName() + "\"))\n\t\t\t";
          }

          m.body += "{\n\t\t\t\t";

          m.body += "this." + f.getName() + ".add(("
              + ((ListType) type).type + ") m";
          m.body += ");\n\t\t\t";

          m.body += "}\n\t\t";

          m.body += "}\n\t\t";

        }
      } else if (type instanceof MapType)
      {
        ClassDefinition newRet = new ClassDefinition();
        newRet.setName(m.name + p + appendName);
        newRet.packageName = packageName2;
        newRet.imports.add(new Type(Map.class));
        newRet.imports.add(new Type(List.class));
        newRet.implemented.add(stryctInterface);

        defs.addAll(generateStructs(newRet, packageName2, (MapType) type, isParameter));
        defs.add(newRet);
        f.type = newRet;
        m.body += "this." + f.getName() + " = new " + newRet.getName()
            + "( (" + f.type.getName() + ") value.get(" + count
            + "));\n\t\t";
      } else
      {
        f.type = map.possibleEntries.get(p);
        m.body += "this." + f.getName() + " = (" + f.type.getName()
            + ") value.get(\"" + f.getName() + "\");\n\t\t";
      }
      count++;
    }
    returnClass.definitions.add(m);

    m = new Method();
    m.isConstructor = true;
    m.name = returnClass.getName();
    m.body = "";
    returnClass.definitions.add(m);
View Full Code Here

    return defs;
  }

  private String generateSimpleMethod(Method m)
  {
    Method newM = m.clone();

    StringBuffer sb = new StringBuffer();

    if (m.returnType instanceof Type
        && ((Type) m.returnType).type == Void.class)
    {

    } else
    {
      sb.append("return ");
    }

    boolean addCast = false;
    if (newM.returnType instanceof MapType)
    {
      // MapType returnType = ((MapType) newM.returnType);
      // This code can remove all structs for simple types, but this leads to a problem when encoding again with a
      // unknown name
      // if (returnType.possibleEntries.size() == 1
      // && !(returnType.valueType instanceof ListType || returnType.valueType instanceof MapType))
      // {
      // newM.returnType = returnType.possibleEntries.get(returnType.possibleEntries.keySet().toArray()[0]);
      // addCast = true;
      //
      // } else
      // {
      ClassDefinition returnClass = null;
      returnClass = new ClassDefinition();
      returnClass.setName(m.name + "Struct");
      returnClass.packageName = structPackageName;
      returnClass.imports.add(new Type(Map.class));
      returnClass.imports.add(new Type(List.class));
      returnClass.implemented.add(stryctInterface);
      newM.returnType = returnClass;

      sb.append("new " + returnClass.getName() + "(");
      List<ClassDefinition> defs = generateStructs(returnClass, structPackageName, (MapType) m.returnType);
      defs.add(returnClass);
      printClasses(defs);
      // }

    }

    if (addCast)
    {
      sb.append("(" + newM.returnType.toSource() + ")");
    }
    sb.append("source." + m.name + "(");

    for (Parameter p : m.parameters)
    {
      sb.append(p.name);
      sb.append(",");
    }

    if (m.parameters.size() > 0)
    {
      sb.deleteCharAt(sb.length() - 1);
    }

    sb.append(")");

    if (addCast)
    {
      sb.append(".values().toArray()[0]");
    } else if (m.returnType instanceof MapType)
    {
      sb.append(")");
    }

    sb.append(";");
    newM.body = sb.toString();

    return newM.toSource();
  }
View Full Code Here

    return parameters;// sb.toString().trim();
  }

  private static Method getMethodSignature(Element element)
  {
    Method method = new Method();
    method.throwsTypes.add(new FreeTextType(Exception.class.getName()));
    StringBuilder sb = new StringBuilder();

    String name = "";
    String parameters = "";
View Full Code Here

TOP

Related Classes of org.destecs.tools.jprotocolgenerator.ast.Method

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.