Esempio n. 1
0
 @SuppressWarnings("deprecation")
 private static String getFieldInjectionExpression(
     JField field, String iocContainerVariable, Map<String, IocConfig<?>> configurations) {
   Inject inject = field.getAnnotation(Inject.class);
   if (inject != null) {
     JType fieldType = field.getType();
     if (!field.isStatic()) {
       if (fieldType.isClassOrInterface() != null) {
         String fieldTypeName = fieldType.getQualifiedSourceName();
         IocConfigImpl<?> iocConfig = (IocConfigImpl<?>) configurations.get(fieldTypeName);
         if (iocConfig != null) {
           if (inject
               .scope()
               .equals(org.cruxframework.crux.core.client.ioc.Inject.Scope.DEFAULT)) {
             return iocContainerVariable
                 + ".get"
                 + fieldTypeName.replace('.', '_')
                 + "("
                 + Scope.class.getCanonicalName()
                 + "."
                 + iocConfig.getScope().name()
                 + ", null)";
           }
           return iocContainerVariable
               + ".get"
               + fieldTypeName.replace('.', '_')
               + "("
               + Scope.class.getCanonicalName()
               + "."
               + getScopeName(inject.scope())
               + ", "
               + EscapeUtils.quote(inject.subscope())
               + ")";
         } else {
           return "GWT.create(" + fieldTypeName + ".class)";
         }
       } else {
         throw new IoCException(
             "Error injecting field ["
                 + field.getName()
                 + "] from type ["
                 + field.getEnclosingType().getQualifiedSourceName()
                 + "]. Primitive fields can not be handled by ioc container.");
       }
     } else {
       throw new IoCException(
           "Error injecting field ["
               + field.getName()
               + "] from type ["
               + field.getEnclosingType().getQualifiedSourceName()
               + "]. Static fields can not be handled by ioc container.");
     }
   }
   return null;
 }
Esempio n. 2
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);
    }
  }