protected void generateSetObjectKeyMethod(SourcePrinter srcWriter) {
    srcWriter.println(
        "protected void setObjectKey("
            + getTargetObjectClassName()
            + " object, "
            + getKeyTypeName()
            + " key){");

    if (hasCompositeKey()) {
      for (int i = 0; i < keyPath.length; i++) {
        String k = keyPath[i];
        JType jType = JClassUtils.getTypeForProperty(k, targetObjectType);
        String setterMethod = JClassUtils.getSetterMethod(k, targetObjectType, jType);
        srcWriter.println(
            "object."
                + setterMethod
                + "((key==null?null:("
                + jType.getParameterizedQualifiedSourceName()
                + ")key["
                + i
                + "]));");
      }
    } else {
      String k = keyPath[0];
      JType jType = JClassUtils.getTypeForProperty(k, targetObjectType);
      String setterMethod = JClassUtils.getSetterMethod(k, targetObjectType, jType);
      srcWriter.println("object." + setterMethod + "(key);");
    }
    srcWriter.println("}");
    srcWriter.println();
  }
Esempio n. 2
0
 /**
  * @param srcWriter
  * @param type
  * @param parentVariable
  * @param added
  * @param iocContainerVariable
  * @param configurations
  */
 private static void injectFields(
     SourcePrinter srcWriter,
     JClassType type,
     String parentVariable,
     Set<String> added,
     String iocContainerVariable,
     Map<String, IocConfig<?>> configurations) {
   for (JField field : type.getFields()) {
     String fieldName = field.getName();
     if (!added.contains(fieldName)) {
       added.add(fieldName);
       JType fieldType = field.getType();
       if ((fieldType.isPrimitive() == null)) {
         String injectionExpression =
             getFieldInjectionExpression(field, iocContainerVariable, configurations);
         if (injectionExpression != null) {
           if (JClassUtils.isPropertyVisibleToWrite(type, field, false)) {
             if (JClassUtils.hasSetMethod(field, type)) {
               String setterMethodName =
                   "set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
               JMethod method = type.findMethod(setterMethodName, new JType[] {field.getType()});
               if (method.getAnnotation(Inject.class)
                   == null) // Annotated methods are handled apart
               {
                 srcWriter.println(
                     fieldType.getQualifiedSourceName()
                         + " field_"
                         + fieldName
                         + " = "
                         + injectionExpression
                         + ";");
                 srcWriter.println(
                     parentVariable + "." + setterMethodName + "(field_" + fieldName + ");");
               }
             } else {
               srcWriter.println(
                   parentVariable + "." + fieldName + " = " + injectionExpression + ";");
             }
           } else {
             throw new IoCException(
                 "IoC Error Field ["
                     + field.getName()
                     + "] from class ["
                     + type.getQualifiedSourceName()
                     + "] is not a writeable property.");
           }
         }
       }
     }
   }
 }
Esempio n. 3
0
 /**
  * @param dataClass
  * @return
  */
 private String[] extractIdentifiers(JClassType dataClass) {
   List<String> ids = new ArrayList<String>();
   JField[] fields = JClassUtils.getDeclaredFields(dataClass);
   for (JField field : fields) {
     if (field.getAnnotation(DataObjectIdentifier.class) != null) {
       if (field.isPublic()) {
         ids.add(field.getName());
       } else {
         ids.add(JClassUtils.getGetterMethod(field.getName(), dataClass) + "()");
       }
     }
   }
   return ids.toArray(new String[ids.size()]);
 }
  private String getFormString(RestMethodInfo methodInfo) {
    StringBuilder str = new StringBuilder();
    boolean first = true;
    JParameter[] parameters = methodInfo.method.getParameters();

    try {
      for (int i = 0; i < methodInfo.parameterAnnotations.length; i++) {
        Annotation[] annotations = methodInfo.parameterAnnotations[i];
        for (Annotation annotation : annotations) {
          if (annotation instanceof FormParam) {
            if (!first) {
              str.append("&");
            }
            first = false;
            if (JClassUtils.isSimpleType(parameters[i].getType())) {
              buildFormStringForSimpleType(str, ((FormParam) annotation).value());
            } else {
              buildFormStringForComplexType(
                  str, parameters[i].getType(), ((FormParam) annotation).value());
            }
          }
        }
      }
    } catch (UnsupportedEncodingException e) {
      throw new CruxGeneratorException(
          "Unsupported encoding for parameter name on method ["
              + methodInfo.method.toString()
              + "]");
    }
    return str.toString();
  }
  protected void generateDeriveKeyMethod(SourcePrinter srcWriter) {
    srcWriter.println(
        "protected " + getKeyTypeName() + " getKey(" + getTargetObjectClassName() + " object){");

    srcWriter.print("boolean hasKey = ");
    boolean first = true;
    for (String k : keyPath) {
      if (!first) {
        srcWriter.print(" || ");
      }
      String getterMethod = JClassUtils.getGetterMethod(k, targetObjectType);
      srcWriter.print("object." + getterMethod + "() != null");
      first = false;
    }
    srcWriter.println(";");

    srcWriter.println("if (hasKey){");
    srcWriter.print(getKeyTypeName() + " key");
    if (hasCompositeKey()) {
      srcWriter.println(" = new Object[" + keyPath.length + "];");
      int i = 0;
      for (String k : keyPath) {
        String getterMethod = JClassUtils.getGetterMethod(k, targetObjectType);
        srcWriter.print("key [" + i + "] = object." + getterMethod + "();");
        i++;
      }
    } else {
      srcWriter.println(
          " = object." + JClassUtils.getGetterMethod(keyPath[0], targetObjectType) + "();");
    }
    srcWriter.println("return key;");

    if (autoIncrement) {
      if (!getKeyTypeName().equals("Integer")) {
        throw new CruxGeneratorException("Auto increment keys can only be used on integer keys");
      }
      srcWriter.println("} else {");
      srcWriter.println("return null;");
    } else {
      srcWriter.println("} else {");
      srcWriter.println("reportError(db.messages.objectStoreDeriveKeyError(name));");
      srcWriter.println("return null;");
    }
    srcWriter.println("}");
    srcWriter.println("}");
    srcWriter.println();
  }
 private void generateMethodParamToHeaderCodeForComplexType(
     SourcePrinter srcWriter,
     String builder,
     String headerName,
     JType parameterType,
     String parameterExpression,
     String parameterCheckExpression) {
   PropertyInfo[] propertiesInfo =
       JClassUtils.extractBeanPropertiesInfo(parameterType.isClassOrInterface());
   for (PropertyInfo propertyInfo : propertiesInfo) {
     if (JClassUtils.isSimpleType(propertyInfo.getType())) {
       generateMethodParamToHeaderCodeForSimpleType(
           srcWriter,
           builder,
           headerName + "." + propertyInfo.getName(),
           propertyInfo.getType(),
           parameterExpression + "." + propertyInfo.getReadMethod().getName() + "()",
           (propertyInfo.getType().isPrimitive() != null
               ? parameterCheckExpression
               : parameterCheckExpression
                   + " && "
                   + parameterExpression
                   + "."
                   + propertyInfo.getReadMethod().getName()
                   + "()!=null"));
     } else {
       generateMethodParamToHeaderCodeForComplexType(
           srcWriter,
           builder,
           headerName + "." + propertyInfo.getName(),
           propertyInfo.getType(),
           parameterExpression + "." + propertyInfo.getReadMethod().getName() + "()",
           parameterCheckExpression
               + " && "
               + parameterExpression
               + "."
               + propertyInfo.getReadMethod().getName()
               + "()!=null");
     }
   }
 }
 private void buildFormStringForComplexType(StringBuilder str, JType parameterType, String value)
     throws UnsupportedEncodingException {
   PropertyInfo[] propertiesInfo =
       JClassUtils.extractBeanPropertiesInfo(parameterType.isClassOrInterface());
   boolean first = true;
   for (PropertyInfo propertyInfo : propertiesInfo) {
     if (!first) {
       str.append("&");
     }
     first = false;
     String parameterName =
         (StringUtils.isEmpty(value)
             ? propertyInfo.getName()
             : value + "." + propertyInfo.getName());
     if (JClassUtils.isSimpleType(propertyInfo.getType())) {
       buildFormStringForSimpleType(str, parameterName);
     } else {
       buildFormStringForComplexType(str, propertyInfo.getType(), parameterName);
     }
   }
 }
Esempio n. 8
0
  protected String getDataObjectWriteExpression(
      JClassType dataObjectType, String bindPath, String value) throws NoSuchFieldException {
    StringBuilder writeExpression = new StringBuilder();

    String converterVariable = getConverterVariable();
    if (converterVariable != null) {
      value = converterVariable + ".from(" + value + ")";
    }

    JClassUtils.buildSetValueExpression(
        writeExpression, dataObjectType, bindPath, DATA_OBJECT_VAR_REF, value);
    // TODO validate conveter type and expression type here
    return writeExpression.toString();
  }
Esempio n. 9
0
  protected String getDataObjectReadExpression(JClassType dataObjectType, String bindPath)
      throws NoSuchFieldException {
    StringBuilder getExpression = new StringBuilder();

    bindPathType =
        JClassUtils.buildGetValueExpression(
            getExpression, dataObjectType, bindPath, DATA_OBJECT_VAR_REF, false, true);

    String converterVariable = getConverterVariable();
    if (converterVariable != null) {
      getExpression.insert(0, converterVariable + ".to(").append(")");
      JClassType typeConverterType =
          converterType.getOracle().findType(TypeConverter.class.getCanonicalName());
      JClassType[] types = JClassUtils.getActualParameterTypes(converterType, typeConverterType);
      bindInfoType = types[1];
    } else {
      bindInfoType = bindPathType;
    }

    // TODO validate conveter type and expression type here

    return "(" + getExpression.toString() + ")";
  }
Esempio n. 10
0
  /**
   * @param srcWriter
   * @param className
   */
  private void generateContainerInstatiationMethod(SourcePrinter srcWriter, String className) {
    try {
      srcWriter.println(
          "public  "
              + className
              + " get"
              + className.replace('.', '_')
              + "("
              + Scope.class.getCanonicalName()
              + " scope, String subscope){");
      JClassType type =
          JClassUtils.getType(context.getGeneratorContext().getTypeOracle(), className);

      IocConfigImpl<?> iocConfig = (IocConfigImpl<?>) configurations.get(className);
      Class<?> providerClass = iocConfig.getProviderClass();
      if (providerClass != null) {
        srcWriter.println(
            className
                + " result = _getScope(scope).getValue(GWT.create("
                + providerClass.getCanonicalName()
                + ".class), "
                + EscapeUtils.quote(className)
                + ", subscope, ");
        generateFieldsPopulationCallback(srcWriter, type);
        srcWriter.println(");");
      } else if (iocConfig.getToClass() != null) {
        srcWriter.println(
            className
                + " result = _getScope(scope).getValue(new "
                + IocProvider.class.getCanonicalName()
                + "<"
                + className
                + ">(){");
        srcWriter.println("public " + className + " get(){");
        srcWriter.println(
            "return GWT.create(" + iocConfig.getToClass().getCanonicalName() + ".class);");
        srcWriter.println("}");
        srcWriter.println("}, " + EscapeUtils.quote(className) + ", subscope, ");
        generateFieldsPopulationCallback(srcWriter, type);
        srcWriter.println(");");
      } else {
        srcWriter.println(
            className
                + " result = _getScope(scope).getValue(new "
                + IocProvider.class.getCanonicalName()
                + "<"
                + className
                + ">(){");
        srcWriter.println("public " + className + " get(){");
        String instantiationClass = getInstantiationClass(className);
        JClassType instantiationType =
            context.getGeneratorContext().getTypeOracle().findType(instantiationClass);
        if (instantiationType == null) {
          throw new CruxGeneratorException("Can not found type: " + instantiationClass);
        }
        if (instantiationType.isAssignableTo(remoteServiceType)
            && ConfigurationFactory.getConfigurations()
                .sendCruxViewNameOnClientRequests()
                .equals("true")) {
          srcWriter.println(className + " ret = GWT.create(" + instantiationClass + ".class);");
          srcWriter.println(
              "(("
                  + ServiceDefTarget.class.getCanonicalName()
                  + ")ret).setRpcRequestBuilder(new "
                  + CruxRpcRequestBuilder.class.getCanonicalName()
                  + "(getBoundCruxViewId()));");
          srcWriter.println("return ret;");
        } else {
          srcWriter.println("return GWT.create(" + instantiationClass + ".class);");
        }
        srcWriter.println("}");
        srcWriter.println("}, " + EscapeUtils.quote(className) + ", subscope, ");
        generateFieldsPopulationCallback(srcWriter, type);
        srcWriter.println(");");
      }

      if (type.isAssignableTo(viewBindableType)) {
        srcWriter.println(
            "if (scope != "
                + Scope.class.getCanonicalName()
                + "."
                + Scope.SINGLETON.name()
                + " && result.getBoundCruxViewId() == null){");
        srcWriter.println("result.bindCruxView(this.getBoundCruxViewId());");
        srcWriter.println("}");
      }
      srcWriter.println("return result;");
      srcWriter.println("}");
    } catch (NotFoundException e) {
      throw new IoCException("IoC Error Class [" + className + "] not found.", e);
    }
  }
 private boolean generateMethodParamToBodyCodeForAnnotatedParameter(
     SourcePrinter srcWriter,
     String builder,
     JParameter[] parameters,
     boolean formEncoded,
     int i,
     Annotation annotation,
     JType parameterType,
     String parameterName) {
   if (annotation instanceof FormParam) {
     if (!formEncoded) {
       srcWriter.println(
           builder
               + ".setHeader(\""
               + HttpHeaderNames.CONTENT_TYPE
               + "\", \"application/x-www-form-urlencoded\");");
       formEncoded = true;
     }
   }
   if (JClassUtils.isSimpleType(parameterType)) {
     if (annotation instanceof FormParam) {
       generateMethodParamToCodeForSimpleType(
           srcWriter,
           "requestData",
           parameterType,
           ((FormParam) annotation).value(),
           parameterName,
           (parameterType.isPrimitive() != null ? "true" : parameterName + "!=null"),
           annotation);
     }
     if (annotation instanceof HeaderParam) {
       generateMethodParamToHeaderCodeForSimpleType(
           srcWriter,
           builder,
           ((HeaderParam) annotation).value(),
           parameterType,
           parameterName,
           (parameterType.isPrimitive() != null ? "true" : parameterName + "!=null"));
     }
     if (annotation instanceof CookieParam) {
       generateMethodParamToCookieCodeForSimpleType(
           srcWriter,
           ((CookieParam) annotation).value(),
           parameterType,
           parameterName,
           (parameterType.isPrimitive() != null ? "true" : parameterName + "!=null"));
     }
   } else {
     if (annotation instanceof FormParam) {
       generateMethodParamToCodeForComplexType(
           srcWriter,
           "requestData",
           parameterType,
           ((FormParam) annotation).value(),
           parameterName,
           parameterName + "!=null",
           annotation);
     }
     if (annotation instanceof HeaderParam) {
       generateMethodParamToHeaderCodeForComplexType(
           srcWriter,
           builder,
           ((HeaderParam) annotation).value(),
           parameterType,
           parameterName,
           parameterName + "!=null");
     }
     if (annotation instanceof CookieParam) {
       generateMethodParamToCookieCodeForComplexType(
           srcWriter,
           ((CookieParam) annotation).value(),
           parameterType,
           parameterName,
           parameterName + "!=null");
     }
   }
   return formEncoded;
 }