Package com.habitsoft.kiyaa.rebind.typeinfo

Examples of com.habitsoft.kiyaa.rebind.typeinfo.GeneratorTypeInfo


                        Element newElem = (Element) loadAndParseTemplate(templatePath).copy();
              rootElement.replaceChild(elem, newElem);
              parseTree(newElem);
                        continue;
                        }
                        GeneratorTypeInfo tagClass = getTagClass(elem);
                        Element viewElem = new Element(XHTML_NAMESPACE.equals(elem.getNamespaceURI())?elem.getLocalName():"div", XHTML_NAMESPACE);
                        String id = identifier(elem.getAttributeValue("id"));
                        if (id == null)
                            id = "view" + insertedViews.size();
                        else {
View Full Code Here


      protected GeneratorTypeInfo getTagClass(Element elem) throws UnableToCompleteException {
        String[] namespaceAndTag = getNamespaceAndTag(elem);
        String namespace = namespaceAndTag[0];
        String tag = namespaceAndTag[1];
        GeneratorTypeInfo tagClass = null;
        if (tag.equals("custom") && namespace.equals(KIYAA_VIEW_TAGS_NAMESPACE)) {
            String viewClassName = elem.getAttributeValue("viewClass");
            if (viewClassName != null) {
                try {
                    tagClass = getType(viewClassName);
View Full Code Here

          } else if (!attributeAccessors.hasSetter()) {
              logger.log(TreeLogger.ERROR, "Unable to find a setter for attribute '" + key + "' in "
                              + type + "; value is '" + value + "', getter is "+attributeAccessors+" asyncGetter is "+attributeAccessors.getAsyncGetter(), null);
              throw new UnableToCompleteException();
          } else {
            final GeneratorTypeInfo attributeType = attributeAccessors.getType();
            if (attributeType.equals(commonTypes.action)
                      && (pathAccessors == null || !pathAccessors.hasGetter())
                      && (action = getAction(value, false)) != null) {
              if(attributeAccessors.hasSynchronousSetter() == false) {
                logger.log(TreeLogger.ERROR, "Async setters do not support for Actions yet.", null);
                throw new UnableToCompleteException();
              }
                sw.println(attributeAccessors.callSetter(action.toViewAction()).toString());
            } else if (path != null // If this is a ${...} or #{...}
                && commonTypes.value.equals(attributeType) // and the target attribute accepts a Value object
                  ) {
              valueExpr = getFieldValue(path);
              if(valueExpr == null) {
                logger.log(TreeLogger.ERROR, "Failed to evaluate expression to construct Value object for "+key+"="+path+" on "+type, null);
                throw new UnableToCompleteException();
              }
              if(attributeAccessors.hasSynchronousSetter() == false) {
                if(attributeAccessors.hasAsynchronousSetter())
                  logger.log(TreeLogger.ERROR, "Async setters not supported for Value yet; found use of async setter "+attributeAccessors.getAsyncSetter()+" for attribute "+key+" on "+type+" to store value "+path, null);
                else
                  logger.log(TreeLogger.ERROR, "No setter found for attribute "+key+" on "+type, null);
                throw new UnableToCompleteException();
              }
                sw.println(attributeAccessors.callSetter(valueExpr).toString());
            } else if (pathAccessors != null && pathAccessors.hasGetter()) {
                generateAttributeLoadSave(type, attributeAccessors, pathAccessors, readOnly, constant, earlyLoad);
            } else if (path != null && (valueExpr = getFieldValue(path)) != null) {
              logger.log(TreeLogger.WARN, "Using a Value "+valueExpr+"to read "+path+" for attribute "+key+"="+path);
              ExpressionInfo valueAccessors = findAccessors(new ExpressionInfo(path, valueExpr, commonTypes.value, true), "value", true, false);
              generateAttributeLoadSave(type, attributeAccessors, valueAccessors, readOnly, constant, earlyLoad);
            } else if(isExpr) {
              logger.log(TreeLogger.WARN, "Couldn't figure out how to set attribute "+key+" on "+type+"; couldn't find a getter for "+value, null);                     
            } else if (attributeType.equals(getType("java.lang.String"))) {
              ExpressionInfo valueAccessors = new ExpressionInfo(path, "\"" + backslashEscape(value) + "\"", getType("java.lang.String"), true);
              generateAttributeLoadSave(type, attributeAccessors, valueAccessors, true, true, true);
            } else if (attributeType.isEnum()) {
              if(attributeType.getEnumMembers().contains(value)) {
                    ExpressionInfo valueAccessors = new ExpressionInfo(path, attributeType.getParameterizedQualifiedSourceName()+"."+value, attributeType, true);
                    generateAttributeLoadSave(type, attributeAccessors, valueAccessors, true, true, true);
                } else {
                    logger.log(TreeLogger.ERROR, "Enum constant '" + value + "' not found in enum "+attributeType.getParameterizedQualifiedSourceName()+" for attribute "+key,
                        null);
                    throw new UnableToCompleteException();
                }
            } else if (attributeType.equals(getType("java.lang.Boolean"))) {
                if (!"true".equals(value) && !"false".equals(value)) {
                    logger.log(TreeLogger.ERROR, "Boolean attribute '" + key + "' should be true or false; got '"+value+"'",
                                    null);
                    throw new UnableToCompleteException();
                }
              ExpressionInfo valueAccessors = new ExpressionInfo(path, "Boolean." + value.toUpperCase(), attributeType, true);
              generateAttributeLoadSave(type, attributeAccessors, valueAccessors, true, true, true);
            } else if (attributeType.getName().equals("boolean")) {
                if (!"true".equals(value) && !"false".equals(value)) {
                    logger.log(TreeLogger.ERROR, "Boolean attribute '" + key + "' should be true or false; got '"+value+"'",
                                    null);
                    throw new UnableToCompleteException();
                }
              ExpressionInfo valueAccessors = new ExpressionInfo(path, value, attributeType, true);
              generateAttributeLoadSave(type, attributeAccessors, valueAccessors, true, true, true);
            } else if (attributeType.getName().equals("char")) {
              ExpressionInfo valueAccessors = new ExpressionInfo(path, "'"+backslashEscape(value)+"'", attributeType, true);
              generateAttributeLoadSave(type, attributeAccessors, valueAccessors, true, true, true);         
            } else if (attributeType.isPrimitive()) {
              ExpressionInfo valueAccessors = new ExpressionInfo(path, value, attributeType, true);
              generateAttributeLoadSave(type, attributeAccessors, valueAccessors, true, true, true);
            } else if (attributeType.equals(getType("java.lang.Class"))) {
                try {
                  ExpressionInfo valueAccessors = new ExpressionInfo(path, types.getType(value).getQualifiedSourceName()
                      + ".class", attributeType, true);
                  generateAttributeLoadSave(type, attributeAccessors, valueAccessors, true, true, true);
                } catch (NotFoundException caught) {
View Full Code Here

                } else {
                    int objectPathEnd = preargs.lastIndexOf('.');
                    String objectPath;
                    String methodName;
                    String getter;
                    final GeneratorTypeInfo objectType;
                    boolean searchingThis = (objectPathEnd == -1);
                    if (searchingThis) {
                        objectPath = getter = "this";
                        objectType = myClass;
                        methodName = preargs;
                    } else {
                        objectPath = preargs.substring(0, objectPathEnd);
                        methodName = preargs.substring(objectPathEnd+1);
                        ExpressionInfo accessors = findAccessors(objectPath, true, false);
                        if (accessors == null || !accessors.hasSynchronousGetter() || accessors.getType() == null) {
                            logger.log(TreeLogger.ERROR, "Can't find any object for " + objectPath + " for action "+ path, null);
                            return null;
                        }
                        getter = accessors.getterExpr();
                        objectType = accessors.getType();
                        if (objectType.isPrimitive()) {
                            logger.log(TreeLogger.ERROR, "Can't call a method on a primitive "
                                            + accessors.getType() + " for expression " + path, null);
                            throw new UnableToCompleteException();
                        }
                    }
   
                    boolean asyncMethod = false;
                    GeneratorMethodInfo actionMethod = null;
                    GeneratorTypeInfo searchType = objectType;
                    if(searchType == null) {
                        logger.log(TreeLogger.ERROR, "Can't call a method on a " + objectType + " for expression " + path, null);
                        throw new UnableToCompleteException();
                    }
                    final String asyncCallbackClassName = AsyncCallback.class.getName();
                    for(;;) {
                      // Look for a synchronous action method with the right number of parameters
                      actionMethod = searchType.findMethodMatching(methodName, true, PrimitiveTypeInfo.VOID, new GeneratorTypeInfo[args.length]);
                      if(actionMethod != null) {
                        asyncMethod = false;
                        break;
                      }
                     
                      // Look for the method with one extra parameter which is the async callback
                      GeneratorTypeInfo[] asyncParamTypes = new GeneratorTypeInfo[args.length+1];
                      asyncParamTypes[args.length] = commonTypes.asyncCallback;
            actionMethod = searchType.findMethodMatching(methodName, true, PrimitiveTypeInfo.VOID, asyncParamTypes );
                      if(actionMethod != null) {
                        asyncMethod = true;
                        break;
                      }
                        if(searchingThis && searchType instanceof GeneratedInnerClassInfo) {
                          searchType = searchType.getFieldType(PARENT_VIEW_FIELD_NAME, true);
                          if(searchType == null)
                            break;
                          getter = getter+"."+PARENT_VIEW_FIELD_NAME;
                          //System.out.println("Looking up into "+searchType+" getter "+getter+" for "+methodName);
                        } else break;
View Full Code Here

             */
            protected ExpressionInfo findAccessors(ExpressionInfo base, final String path, final boolean matchAsync, boolean staticAccess) throws UnableToCompleteException {
              LocalTreeLogger.pushLogger(logger.branch(TreeLogger.INFO, "findAccessors('"+base+"', path='"+path+"', matchAsync="+matchAsync+", staticAccess="+staticAccess+")"));
              try {
               
                  GeneratorTypeInfo inType = base.getType();
                  if(inType.isPrimitive()) {
                      logger.log(TreeLogger.ERROR, "Can't find any member inside of non-class type "+base.getType()+" with path "+path);
                      throw new UnableToCompleteException();
                  }
                  String expr = base.getGetter();
                 
                  //System.out.println("findAccessors("+inType+", '"+expr+"', '"+path+"', "+matchAsync+")");
                  // Split "path" into two parts - the part before the first dot and the "rest" of the expression
                  String[] splitPath = smartSplit(path, '.', 2);
                  String name = splitPath[0];
                  if (name.length() == 0) {
                      return null;
                  }
                  String getter;
                  boolean asyncGetter = false;
                  String setter = null;
                  boolean asyncSetter = false;
                  GeneratorTypeInfo type;
                  /*if(name.endsWith("]")) {
                  int openBraceIdx = smartIndexOf(name, '[');
                  if(openBraceIdx == -1) {
                    logger.log(TreeLogger.ERROR, "Can't find opening [ for ] in "+name, null);
                    throw new UnableToCompleteException();
                  }
                  // TODO array indexing
                throw new UnableToCompleteException();
                } else */
                  boolean endsWithParen = name.endsWith(")");
          boolean methodInvokation = endsWithParen // Has (), assume method call
              || inType.hasMethodMatching(name, true, null) // no parens, but expression has the same name as a zero-arg method
              || (matchAsync && inType.hasMethodMatching(name, true, null, commonTypes.asyncCallback)); // no parens, but expression has the same name as an async method and async is OK
          boolean lastOrOnlyPartOfTheExpression = splitPath.length == 1;
          if(methodInvokation) {
                      String getterMethodName;
                      ExpressionInfo[] args;
                      GeneratorTypeInfo[] argTypes;
                      if(endsWithParen) {
                      int openIdx = smartIndexOf(name, '(');
                      if(openIdx == -1) {
                        logger.log(TreeLogger.ERROR, "Can't find opening ( for ) in "+name, null);
                        throw new UnableToCompleteException();
                      }
                     
                          String[] argExprs = smartSplit(name.substring(openIdx + 1, name.length() - 1), ',', 100);
                          //System.out.println("Splitting '"+name.substring(openIdx + 1, name.length() - 1)+" around ',' gives "+args.length+" args: "+joinWithCommas(0, args));
                          // Check for an empty parameter list
                          if(argExprs.length == 1 && argExprs[0].length() == 0)
                              argExprs = new String[0];
                          getterMethodName = identifier(name.substring(0, openIdx));
                          args = new ExpressionInfo[argExprs.length];
                          argTypes = new GeneratorTypeInfo[argExprs.length];
                          for(int i=0; i < argExprs.length; i++) {
                            String arg = argExprs[i].trim();
                              ExpressionInfo argAccessors = findAccessors(arg, true, false);
                              if (argAccessors == null) {
                                  logger.log(TreeLogger.ERROR, "Couldn't evaluate '" + arg + "' as argument to '" + name + "'", null);
                                  throw new UnableToCompleteException();
                              }
                              args[i] = argAccessors;
                              argTypes[i] = argAccessors.getType();
                          }
                      } else {
                          getterMethodName = identifier(name);
                          args = new ExpressionInfo[0];
                          argTypes = new GeneratorTypeInfo[0];
                      }
                      GeneratorTypeInfo objectType = inType;
                      boolean searchingThis = objectType.equals(myClass);
                      boolean asyncMethod = false;
                      GeneratorMethodInfo getterMethod = null;
                      for(;;) {
                        GeneratorTypeInfo[] syncArgTypesWildcard = new GeneratorTypeInfo[argTypes.length];
                       
                        GeneratorTypeInfo[] asyncArgTypesWildcard = new GeneratorTypeInfo[argTypes.length+1];
                        asyncArgTypesWildcard[argTypes.length] = commonTypes.asyncCallback;
                        GeneratorTypeInfo[] asyncArgTypes = Arrays.copyOf(argTypes, argTypes.length+1);
                        asyncArgTypes[argTypes.length] = commonTypes.asyncCallback;
                       
                        HashSet<String> candidates = new HashSet<String>();
                        candidates.add(getterMethodName);
                        String capGetterMethodName = capitalize(getterMethodName);
              candidates.add("get"+capGetterMethodName);
                        candidates.add("is"+capGetterMethodName);
                        for(String candidate : candidates) {
                          getterMethod = objectType.findMethodMatching(candidate, true, null, argTypes);
                          if(getterMethod == null)
                            getterMethod = objectType.findMethodMatching(candidate, true, null, syncArgTypesWildcard);
                          if(getterMethod != null) {
                            asyncMethod = false;
                            type = getterMethod.getReturnType();
                            break;
                          }
                         
                          if(matchAsync) {
                            getterMethod = objectType.findMethodMatching(candidate, true, null, asyncArgTypes);
                            if(getterMethod == null)
                              getterMethod = objectType.findMethodMatching(candidate, true, null, asyncArgTypesWildcard);
                            if(getterMethod != null) {
                              asyncMethod = true;
                              type = getterMethod.getAsyncReturnType();
                              if(type == null) type = commonTypes.object;
                              break;
                            }
                          }
                        }
                        if(searchingThis && getterMethod == null && objectType instanceof GeneratedInnerClassInfo) {
                            objectType = objectType.getFieldType(PARENT_VIEW_FIELD_NAME, true);
                            if(objectType == null)
                              break;
                            expr = expr+"."+PARENT_VIEW_FIELD_NAME;
                            //System.out.println("Ascending to "+expr+" "+objectType);
                          } else break;
                      }
                      if (getterMethod == null) {
                          logger.log(TreeLogger.ERROR, "findAccessors(): Unable to find a "+(staticAccess?"static":"instance")+" method with the right number of arguments ("
                                          + args.length + (matchAsync?" [ + optional AsyncCallback]":"")+") with name '" + getterMethodName + "' in " + inType
                                          + " for expression '" + path + "'", null);
                          throw new UnableToCompleteException();
                      }
     
                      StringBuffer getterBuf = new StringBuffer();
                      getterBuf.append(expr).append('.').append(getterMethod.getName()).append('(');
                     
                      for (int i = 0; i < args.length; i++) {
                        if(i > 0) getterBuf.append(", ");
                        getterBuf.append(args[i].conversionExpr(getterMethod.getParameterTypes()[i]));
                      }
                     
                      if(asyncMethod) {
                        if(args.length > 0) getterBuf.append(","); // trailing comma for async methods so we can append the callback parameter when we call the method
                      } else getterBuf.append(')');
     
                      getter = getterBuf.toString();
                     
                      asyncGetter = asyncMethod;
                      type = asyncMethod?getterMethod.getAsyncReturnType():getterMethod.getReturnType();
                     
                      // Find the matching setter method (if any).
                      String setterMethodName = getterMethod.getName().replaceFirst("^(is|get)", "set");
                      GeneratorTypeInfo[] setterArgTypes = Arrays.copyOf(argTypes, argTypes.length+1);
                      setterArgTypes[argTypes.length] = type;
                      GeneratorMethodInfo setterMethod = objectType.findMethodMatching(setterMethodName, true, null, setterArgTypes);
                     
                      // If searching for something matching the types we got doesn't work, try it a wildcard for the type
                      if(setterMethod == null) {
                        setterArgTypes = new GeneratorTypeInfo[argTypes.length+1];
                        setterArgTypes[argTypes.length] = type;
                        setterMethod = objectType.findMethodMatching(setterMethodName, true, null, setterArgTypes);
                      }
                     
                      if(setterMethod == null) {
                        setterArgTypes = Arrays.copyOf(argTypes, argTypes.length+2);
                        setterArgTypes[argTypes.length] = type;
                        setterArgTypes[argTypes.length+1] = commonTypes.asyncCallback;
                          setterMethod = objectType.findMethodMatching(setterMethodName, true, PrimitiveTypeInfo.VOID, setterArgTypes);
                          if(setterMethod == null) {
                            setterArgTypes = new GeneratorTypeInfo[argTypes.length+2];
                            setterArgTypes[argTypes.length] = type;
                            setterArgTypes[argTypes.length+1] = commonTypes.asyncCallback;
                              setterMethod = objectType.findMethodMatching(setterMethodName, true, PrimitiveTypeInfo.VOID, setterArgTypes);
                          }
                          if(setterMethod != null)
                            asyncSetter = true;
                      } else {
                        asyncSetter = false;
View Full Code Here

              } else if(path.startsWith("\"") && path.endsWith("\"")) {
                return new ExpressionInfo(path, path, getType("java.lang.String"), true);
               
              }
              String thisExpr = innerType ? myClass.getSimpleSourceName() + ".this" : "this";
                GeneratorTypeInfo classToSearch = myClass;
                if (path.equals("this")) {
                  while(classToSearch instanceof GeneratedInnerClassInfo) {
                    classToSearch = ((GeneratedInnerClassInfo)classToSearch).getFieldType(PARENT_VIEW_FIELD_NAME, true);
                    if(classToSearch == null)
                      break;
                    thisExpr = thisExpr+"."+PARENT_VIEW_FIELD_NAME;
                  }
                 
                  // When they use the expression "this", be sure to use the superclass of the generated class;
                  // the generated class won't behave well with isAssignableFrom() and isAssignableTo() because
                  // it's a fake object we created and isn't "known" by the type oracle.
                    return new ExpressionInfo(path, thisExpr, null, classToSearch, false, false, false);
                }
               
                // like books.service.AccountType.ACCOUNTS_RECEIVABLE or abc.def.Foo.bar
                Matcher staticReference = Pattern.compile("([a-z0-9_]+(?:\\.[a-z0-9_]+)+(?:\\.[A-Z][A-Za-z0-9_]+)+)\\.([A-Za-z0-9_]+.*)").matcher(path);
                if(staticReference.matches()) {
                  String className = staticReference.group(1);
                  String property = staticReference.group(2);
                  //System.out.println("Static reference: "+className+" property "+property);
          try {
            JClassType staticType = types.getType(className);
                    JField field = staticType.getField(property);
                    JEnumType enum1 = staticType.isEnum();
                    if(field != null && field.isStatic()) {
              return new ExpressionInfo(path, path, field.getType(), field.isFinal() || enum1!=null);
                    }
                    if(enum1 != null && property.equals("values()")) {
                      return new ExpressionInfo(path, path, types.getArrayType(enum1), true);
                    }
                    return findAccessors(new ExpressionInfo(path, className, staticType, true), property, matchAsync, true);
          } catch (NotFoundException e) {
          } 
                }
               
                for (;;) {
                    ExpressionInfo accessors = findAccessors(new ExpressionInfo(path, thisExpr, classToSearch, true), path, matchAsync, false);
                    if (accessors != null) {
                        return accessors;
                    } else if(classToSearch instanceof GeneratedInnerClassInfo){
                        classToSearch = classToSearch.getFieldType(PARENT_VIEW_FIELD_NAME, true);
                        if (classToSearch == null) {
                            return null;
                        }
                        if(parentViewClass != null) {
                            thisExpr = thisExpr+"."+PARENT_VIEW_FIELD_NAME;
                        } else {
                            thisExpr = classToSearch.getSimpleSourceName() + ".this";
                        }
                    } else {
                      return null;
                    }
                }
View Full Code Here

        }
        if(right == null || !right.hasGetter()) {
          logger.log(TreeLogger.ERROR, "Can't evaluate "+rightPath+" in "+path+" match async = "+matchAsync, null);
          throw new UnableToCompleteException();
        }
        GeneratorTypeInfo targetOperandType;
        GeneratorTypeInfo resultType;
        GeneratorTypeInfo java_lang_String = RuntimeClassWrapper.STRING;
        if ("&&".equals(oper) || "||".equals(oper)){
          targetOperandType = PrimitiveTypeInfo.BOOLEAN;
          resultType = PrimitiveTypeInfo.BOOLEAN;
        } else if("+".equals(oper) && (right.getType().getParameterizedQualifiedSourceName().equals("java.lang.String") || left.getType().getParameterizedQualifiedSourceName().equals("java.lang.String"))) {
          targetOperandType = java_lang_String;
View Full Code Here

TOP

Related Classes of com.habitsoft.kiyaa.rebind.typeinfo.GeneratorTypeInfo

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.