Exemplo n.º 1
0
 public For_c(Position pos, List inits, Expr cond, List iters, Stmt body) {
   super(pos);
   this.inits = TypedList.copyAndCheck(inits, ForInit.class, true);
   this.cond = cond;
   this.iters = TypedList.copyAndCheck(iters, ForUpdate.class, true);
   this.body = body;
 }
Exemplo n.º 2
0
  /** Reconstruct the statement. */
  protected For_c reconstruct(List inits, Expr cond, List iters, Stmt body) {
    if (!CollectionUtil.equals(inits, this.inits)
        || cond != this.cond
        || !CollectionUtil.equals(iters, this.iters)
        || body != this.body) {
      For_c n = (For_c) copy();
      n.inits = TypedList.copyAndCheck(inits, ForInit.class, true);
      n.cond = cond;
      n.iters = TypedList.copyAndCheck(iters, ForUpdate.class, true);
      n.body = body;
      return n;
    }

    return this;
  }
 public void setValue(String value) {
   if (!accepts(value)) {
     throw new IllegalArgumentException(this + " cannot accept argument " + value);
   }
   if (Integer.class.equals(type)) {
     this.value = Integer.parseInt(value);
   } else if (Double.class.equals(type)) {
     this.value = Double.parseDouble(value);
   } else if (Boolean.class.equals(type)) {
     this.value = ArrayUtils.contains(value, new String[] {"y", "yes", "true"});
   } else if (EnumWrapper.class.equals(type)) {
     final EnumWrapper wrapper = EnumWrapper.getWrapper(typeArguments);
     wrapper.setValue(value);
     this.value = wrapper;
   } else if (TypedList.class.isAssignableFrom(type)) {
     TypedList typeObject = null;
     try {
       typeObject = (TypedList) type.newInstance();
     } catch (Exception ignored) {
     }
     if (!value.matches("\\{.*?\\}")) {
       typeObject.addString(value);
       this.value = typeObject;
       return;
     }
     value = value.substring(1, value.length() - 1);
     String token = "";
     while (!value.isEmpty()) {
       int pos = 0;
       Character quote = null;
       if ("\"'`".contains(Character.toString(value.charAt(0)))) {
         quote = value.charAt(0);
         pos++;
       }
       while (pos < value.length()) {
         if (value.substring(pos).startsWith("\\\\")
             || value.substring(pos).startsWith("\\\"")
             || value.substring(pos).startsWith("\\'")
             || value.substring(pos).startsWith("\\`")) {
           token += value.charAt(pos + 1);
           pos += 2;
           continue;
         }
         if (value.substring(pos).startsWith("\\n")) {
           token += "\n";
           pos += 2;
           continue;
         }
         if (value.substring(pos).startsWith("\\t")) {
           token += "\t";
           pos += 2;
           continue;
         }
         if (value.substring(pos).startsWith("\\r")) {
           token += "\r";
           pos += 2;
           continue;
         }
         token += value.charAt(pos);
         if (quote == null && token.endsWith(",")) {
           token = token.substring(0, token.length() - 1);
           break;
         }
         if (quote != null && !token.isEmpty() && token.charAt(token.length() - 1) == quote) {
           break;
         }
         pos++;
       }
       if (quote != null) {
         token = token.substring(0, token.length() - 1);
       }
       value = value.substring(token.length());
       typeObject.addString(token);
       if (!value.startsWith(",")) {
         token = value;
         break;
       } else {
         token = "";
       }
       value = value.substring(1);
     }
     this.value = typeObject;
   } else {
     this.value = value;
   }
 }
 public static boolean accepts(String value, Class<?> type, String typeArguments) {
   try {
     if (Integer.class.equals(type)) {
       Integer.parseInt(value);
     } else if (Double.class.equals(type)) {
       Double.parseDouble(value);
     } else if (EnumWrapper.class.equals(type)) {
       final EnumWrapper wrapper = EnumWrapper.getWrapper(typeArguments);
       return wrapper.matches(value);
     } else if (Boolean.class.equals(type)) {
       return ArrayUtils.contains(
           value.toLowerCase(), new String[] {"y", "n", "yes", "no", "true", "false"});
     } else if (TypedList.class.isAssignableFrom(type)) {
       final TypedList typeObject = (TypedList) type.newInstance();
       if (!value.matches("\\{.*?\\}")) {
         return accepts(value, typeObject.getType(), typeArguments);
       }
       value = value.substring(1, value.length() - 1);
       String token = "";
       while (!value.isEmpty()) {
         int pos = 0;
         Character quote = null;
         if ("\"'`".contains(Character.toString(value.charAt(0)))) {
           quote = value.charAt(0);
           pos++;
         }
         while (pos < value.length()) {
           if (value.substring(pos).startsWith("\\\\")
               || value.substring(pos).startsWith("\\\"")
               || value.substring(pos).startsWith("\\'")
               || value.substring(pos).startsWith("\\`")) {
             token += value.charAt(pos + 1);
             pos += 2;
             continue;
           }
           if (value.substring(pos).startsWith("\\n")) {
             token += "\n";
             pos += 2;
             continue;
           }
           if (value.substring(pos).startsWith("\\t")) {
             token += "\t";
             pos += 2;
             continue;
           }
           if (value.substring(pos).startsWith("\\r")) {
             token += "\r";
             pos += 2;
             continue;
           }
           token += value.charAt(pos);
           if (quote == null && token.endsWith(",")) {
             token = token.substring(0, token.length() - 1);
             break;
           }
           if (quote != null && !token.isEmpty() && token.charAt(token.length() - 1) == quote) {
             break;
           }
           pos++;
         }
         if (quote != null) {
           token = token.substring(0, token.length() - 1);
         }
         value = value.substring(token.length());
         if (!accepts(token, typeObject.getType(), typeArguments)) {
           System.out.println(token);
           return false;
         }
         if (!value.startsWith(",")) {
           token = value;
           break;
         } else {
           token = "";
         }
         value = value.substring(1);
       }
     }
   } catch (NumberFormatException e) {
     return false;
   } catch (InstantiationException e) {
     return false;
   } catch (IllegalAccessException e) {
     return false;
   }
   return true;
 }
Exemplo n.º 5
0
 /** Set the iterator expressions of the statement. */
 public For iters(List iters) {
   For_c n = (For_c) copy();
   n.iters = TypedList.copyAndCheck(iters, ForUpdate.class, true);
   return n;
 }
Exemplo n.º 6
0
 /** Set the inits of the statement. */
 public For inits(List inits) {
   For_c n = (For_c) copy();
   n.inits = TypedList.copyAndCheck(inits, ForInit.class, true);
   return n;
 }