/**
  * オブジェクトのフィールドにデータを設定する<br>
  * Integer、Float、Float[]、Stringにしか対応していない
  *
  * @param obj 設定対象オブジェクト
  * @param fl 設定対象フィールド
  * @param ty 設定対象フィールドの型
  * @param data 設定データ
  * @throws IllegalArgumentException
  * @throws IllegalAccessException
  */
 protected void dataSetter(Object obj, Field fl, Class ty, String data)
     throws IllegalArgumentException, IllegalAccessException {
   // Integer型のデータを設定
   if (ty.toString().equals("class java.lang.Integer")) {
     fl.set(obj, Integer.parseInt(data));
   }
   // Float型のデータを設定
   if (ty.toString().equals("class java.lang.Float")) {
     fl.set(obj, Float.parseFloat(data));
   }
   // String型のデータを設定(""の内側)
   if (ty.toString().equals("class java.lang.String")) {
     fl.set(obj, getDoubleQuoatString(data));
   }
   // Float[]型のデータを設定
   if (ty.toString().equals("class [Ljava.lang.Float;")) {
     String[] s;
     s = data.split(" ");
     Float[] f = new Float[s.length];
     for (int i = 0; i < s.length; i++) {
       f[i] = Float.parseFloat(s[i]);
     }
     fl.set(obj, f);
   }
 }
 /**
  * An introspection based equality predicate for GenericObjects.
  *
  * @other the other object to test against.
  */
 public boolean equals(Object that) {
   if (!this.getClass().equals(that.getClass())) return false;
   Class myclass = this.getClass();
   Field[] fields = myclass.getDeclaredFields();
   Class hisclass = that.getClass();
   Field[] hisfields = hisclass.getDeclaredFields();
   for (int i = 0; i < fields.length; i++) {
     Field f = fields[i];
     Field g = hisfields[i];
     // Only print protected and public members.
     int modifier = f.getModifiers();
     if ((modifier & Modifier.PRIVATE) == Modifier.PRIVATE) continue;
     Class fieldType = f.getType();
     String fieldName = f.getName();
     if (fieldName.compareTo("stringRepresentation") == 0) {
       continue;
     }
     if (fieldName.compareTo("indentation") == 0) {
       continue;
     }
     if (fieldName.compareTo("inputText") == 0) {
       continue;
     }
     try {
       // Primitive fields are printed with type: value
       if (fieldType.isPrimitive()) {
         String fname = fieldType.toString();
         if (fname.compareTo("int") == 0) {
           if (f.getInt(this) != g.getInt(that)) return false;
         } else if (fname.compareTo("short") == 0) {
           if (f.getShort(this) != g.getShort(that)) return false;
         } else if (fname.compareTo("char") == 0) {
           if (f.getChar(this) != g.getChar(that)) return false;
         } else if (fname.compareTo("long") == 0) {
           if (f.getLong(this) != g.getLong(that)) return false;
         } else if (fname.compareTo("boolean") == 0) {
           if (f.getBoolean(this) != g.getBoolean(that)) return false;
         } else if (fname.compareTo("double") == 0) {
           if (f.getDouble(this) != g.getDouble(that)) return false;
         } else if (fname.compareTo("float") == 0) {
           if (f.getFloat(this) != g.getFloat(that)) return false;
         }
       } else if (g.get(that) == f.get(this)) return true;
       else if (f.get(this) == null) return false;
       else if (g.get(that) == null) return false;
       else return f.get(this).equals(g.get(that));
     } catch (IllegalAccessException ex1) {
       InternalErrorHandler.handleException(ex1);
     }
   }
   return false;
 }
示例#3
0
 private static int checkArray(
     Class expectedType, ArgumentsList arguments, List<LispObject> args, int argsCounter, int i) {
   Class componentType = expectedType.getComponentType();
   ArrayList<LispObject> array = new ArrayList<>();
   while (argsCounter != args.size()) {
     if (!componentType.isInstance(args.get(argsCounter)))
       throw new WrongTypeArgumentException(componentType.toString(), args.get(argsCounter));
     array.add(args.get(argsCounter));
     ++argsCounter;
   }
   arguments.setValue(i, customizeArrayList(componentType, array));
   return argsCounter;
 }
  static String[] c_signature(Class c, String expr) {
    if (c.isPrimitive()) return strs("j" + c.toString(), expr, expr);
    if (c == Pointer.class)
      return strs(
          "void*",
          "createPointerFromIO(env, " + expr + ", NULL)",
          "getPointerPeer(env, " + expr + ")"); // TODO callIO
    if (c == CLong.class)
      return strs("long", "BoxCLong(env, " + expr + ")", "UnBoxCLong(env, " + expr + ")");
    if (c == SizeT.class)
      return strs("size_t", "BoxSizeT(env, " + expr + ")", "UnBoxSizeT(env, " + expr + ")");
    if (c == TimeT.class)
      return strs("time_t", "BoxTimeT(env, " + expr + ")", "UnBoxTimeT(env, " + expr + ")");

    throw new UnsupportedOperationException("Cannot compute C signature for " + c.getName());
  }
示例#5
0
  public Type navigateParameterized(String name, Type[] params) throws OclTypeException {
    Type ret = Basic.navigateAnyParameterized(name, params);
    if (ret != null) return ret;

    Method foundmethod = null;

    // this is very similar to tudresden.ocl.lib.OclAnyImpl.findMethod
    // if you find a bug here, its probably there as well.

    // suprisingly one has not to go after interfaces since methods
    // inherited from interfaces are automatically included into the
    // implementing class. This does not happen for methods inherited
    // from the superclass, so we have to ascend to all superclasses.

    // unfortunately the above is only true for classes, getSuperclass invoked
    // on an interface returns null, regardless of the interfaces extended by the
    // interface. Therefore, we need to check out getInterfaces() if iclass is
    // an interface
    HashSet hsVisited = new HashSet();
    LinkedList llToVisit = new LinkedList();
    if (c.isInterface()) {
      // as we're dealing with actual instances, it can be assumed that Object is
      // a superclass
      llToVisit.add(java.lang.Object.class);
    }

    classloop:
    for (Class iclass = c; iclass != null; ) // iclass=iclass.getSuperclass())
    {
      Method[] methods = iclass.getDeclaredMethods();
      methodloop:
      for (int i = 0; i < methods.length; i++) {
        if (!name.equals(methods[i].getName())) continue methodloop;
        Class[] methodparams = methods[i].getParameterTypes();
        if (params.length != methodparams.length) continue methodloop;

        System.err.print("Checking method " + name + " (");
        for (int j = 0; j < methodparams.length; j++) {
          if (j != 0) {
            System.err.print(", ");
          }

          System.err.print(methodparams[j]);
        }
        System.err.println(")");

        for (int j = 0; j < params.length; j++)
          if (!params[j].conformsTo(getTypeForClass(methodparams[j]))) {
            System.err.println("No conformance for paramter # " + j);
            continue methodloop;
          }
        if (foundmethod == null) foundmethod = methods[i];
        else throw new OclTypeException("ambigious method " + name + " of " + c + ") queried.");
        break classloop;
      }

      // determine classes to be visited
      if (iclass.isInterface()) {
        Class[] ca = iclass.getInterfaces();
        for (int i = 0; i < ca.length; i++) {
          if (!hsVisited.contains(ca[i])) {
            llToVisit.add(ca[i]);
          }
        }
      } else {
        if (!hsVisited.contains(iclass.getSuperclass())) {
          llToVisit.add(iclass.getSuperclass());
        }
      }

      // mark current class visited
      hsVisited.add(iclass);

      // go to next class
      if (!llToVisit.isEmpty()) {
        iclass = (Class) llToVisit.remove(0);
      } else {
        iclass = null;
      }
    }

    if (foundmethod == null) {
      StringBuffer sb = new StringBuffer();
      sb.append(c.toString() + " has no method " + name + " with parameters (");
      for (int i = 0; i < params.length; i++) {
        if (i != 0) sb.append(", ");
        sb.append(params[i] + "/" + params[i]);
      }
      sb.append(")");
      throw new OclTypeException(sb.toString());
    }

    return getTypeForClass(foundmethod.getReturnType());
  }
  /**
   * Generic print formatting function: Does depth-first descent of the structure and recursively
   * prints all non-private objects pointed to by this object. <bf> Warning - the following generic
   * string routine will bomb (go into infinite loop) if there are any circularly linked structures
   * so if you have these, they had better be private! </bf> We dont have to worry about such things
   * for our structures (we never use circular linked structures).
   */
  public String toString() {
    stringRepresentation = "";
    Class myclass = getClass();
    sprint(myclass.getName());
    sprint("{");
    sprint("inputText:");
    sprint(inputText);
    Field[] fields = myclass.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
      Field f = fields[i];
      // Only print protected and public members.
      int modifier = f.getModifiers();
      if ((modifier & Modifier.PRIVATE) == Modifier.PRIVATE) continue;
      Class fieldType = f.getType();
      String fieldName = f.getName();
      if (fieldName.compareTo("stringRepresentation") == 0) {
        // avoid nasty recursions...
        continue;
      }
      if (fieldName.compareTo("indentation") == 0) {
        // formatting stuff - not relevant here.
        continue;
      }
      sprint(fieldName + ":");
      try {
        // Primitive fields are printed with type: value
        if (fieldType.isPrimitive()) {
          String fname = fieldType.toString();
          sprint(fname + ":");
          if (fname.compareTo("int") == 0) {
            int intfield = f.getInt(this);
            sprint(intfield);
          } else if (fname.compareTo("short") == 0) {
            short shortField = f.getShort(this);
            sprint(shortField);
          } else if (fname.compareTo("char") == 0) {
            char charField = f.getChar(this);
            sprint(charField);
          } else if (fname.compareTo("long") == 0) {
            long longField = f.getLong(this);
            sprint(longField);
          } else if (fname.compareTo("boolean") == 0) {
            boolean booleanField = f.getBoolean(this);
            sprint(booleanField);
          } else if (fname.compareTo("double") == 0) {
            double doubleField = f.getDouble(this);
            sprint(doubleField);
          } else if (fname.compareTo("float") == 0) {
            float floatField = f.getFloat(this);
            sprint(floatField);
          }
        } else if (Class.forName(SIP_PACKAGE + ".GenericObject").isAssignableFrom(fieldType)) {
          if (f.get(this) != null) {
            sprint(((GenericObject) f.get(this)).toString(indentation + 1));
          } else {
            sprint("<null>");
          }

        } else if (Class.forName(SIP_PACKAGE + ".GenericObjectList").isAssignableFrom(fieldType)) {
          if (f.get(this) != null) {
            sprint(((GenericObjectList) f.get(this)).toString(indentation + 1));
          } else {
            sprint("<null>");
          }

        } else {
          // Dont do recursion on things that are not
          // of our header type...
          if (f.get(this) != null) {
            sprint(f.get(this).getClass().getName() + ":");
          } else {
            sprint(fieldType.getName() + ":");
          }

          sprint("{");
          if (f.get(this) != null) {
            sprint(f.get(this).toString());
          } else {
            sprint("<null>");
          }
          sprint("}");
        }
      } catch (IllegalAccessException ex1) {
        continue; // we are accessing a private field...
      } catch (Exception ex) {
        InternalErrorHandler.handleException(ex);
      }
    }
    sprint("}");
    return stringRepresentation;
  }
 /**
  * An introspection based predicate matching using a template object. Allows for partial match of
  * two protocl Objects.
  *
  * @other the match pattern to test against. The match object has to be of the same type (class).
  *     Primitive types and non-sip fields that are non null are matched for equality. Null in any
  *     field matches anything. Some book-keeping fields are ignored when making the comparison.
  */
 public boolean match(Object other) {
   if (other == null) return true;
   if (!this.getClass().equals(other.getClass())) return false;
   GenericObject that = (GenericObject) other;
   Class myclass = this.getClass();
   Field[] fields = myclass.getDeclaredFields();
   Class hisclass = other.getClass();
   Field[] hisfields = hisclass.getDeclaredFields();
   for (int i = 0; i < fields.length; i++) {
     Field f = fields[i];
     Field g = hisfields[i];
     // Only print protected and public members.
     int modifier = f.getModifiers();
     if ((modifier & Modifier.PRIVATE) == Modifier.PRIVATE) continue;
     Class fieldType = f.getType();
     String fieldName = f.getName();
     if (fieldName.compareTo("stringRepresentation") == 0) {
       continue;
     }
     if (fieldName.compareTo("indentation") == 0) {
       continue;
     }
     if (fieldName.compareTo("inputText") == 0) {
       continue;
     }
     try {
       // Primitive fields are printed with type: value
       if (fieldType.isPrimitive()) {
         String fname = fieldType.toString();
         if (fname.compareTo("int") == 0) {
           if (f.getInt(this) != g.getInt(that)) return false;
         } else if (fname.compareTo("short") == 0) {
           if (f.getShort(this) != g.getShort(that)) return false;
         } else if (fname.compareTo("char") == 0) {
           if (f.getChar(this) != g.getChar(that)) return false;
         } else if (fname.compareTo("long") == 0) {
           if (f.getLong(this) != g.getLong(that)) return false;
         } else if (fname.compareTo("boolean") == 0) {
           if (f.getBoolean(this) != g.getBoolean(that)) return false;
         } else if (fname.compareTo("double") == 0) {
           if (f.getDouble(this) != g.getDouble(that)) return false;
         } else if (fname.compareTo("float") == 0) {
           if (f.getFloat(this) != g.getFloat(that)) return false;
         }
       } else {
         Object myObj = f.get(this);
         Object hisObj = g.get(that);
         if (hisObj == myObj) return true;
         else if (hisObj != null && myObj == null) return false;
         else if (GenericObject.isMySubclass(myObj.getClass())
             && !((GenericObject) myObj).match(hisObj)) return false;
         else if (hisObj instanceof java.lang.String && myObj instanceof java.lang.String) {
           if (((String) myObj).compareToIgnoreCase((String) hisObj) != 0) return false;
         } else if (GenericObjectList.isMySubclass(myObj.getClass())
             && !((GenericObjectList) myObj).match(hisObj)) return false;
       }
     } catch (IllegalAccessException ex1) {
       InternalErrorHandler.handleException(ex1);
     }
   }
   return true;
 }
示例#8
0
  /**
   * Generates code to convert a wrapped value type to a primitive type. Handles unwrapping
   * java.lang.Boolean, and java.lang.Number types. Generates the appropriate RETURN bytecode.
   */
  static void generateReturnResult(
      ClassFileWriter cfw, Class<?> retType, boolean callConvertResult) {
    // wrap boolean values with java.lang.Boolean, convert all other
    // primitive values to java.lang.Double.
    if (retType == Void.TYPE) {
      cfw.add(ByteCode.POP);
      cfw.add(ByteCode.RETURN);

    } else if (retType == Boolean.TYPE) {
      cfw.addInvoke(
          ByteCode.INVOKESTATIC, "aurora/javascript/Context", "toBoolean", "(Ljava/lang/Object;)Z");
      cfw.add(ByteCode.IRETURN);

    } else if (retType == Character.TYPE) {
      // characters are represented as strings in JavaScript.
      // return the first character.
      // first convert the value to a string if possible.
      cfw.addInvoke(
          ByteCode.INVOKESTATIC,
          "aurora/javascript/Context",
          "toString",
          "(Ljava/lang/Object;)Ljava/lang/String;");
      cfw.add(ByteCode.ICONST_0);
      cfw.addInvoke(ByteCode.INVOKEVIRTUAL, "java/lang/String", "charAt", "(I)C");
      cfw.add(ByteCode.IRETURN);

    } else if (retType.isPrimitive()) {
      cfw.addInvoke(
          ByteCode.INVOKESTATIC, "aurora/javascript/Context", "toNumber", "(Ljava/lang/Object;)D");
      String typeName = retType.getName();
      switch (typeName.charAt(0)) {
        case 'b':
        case 's':
        case 'i':
          cfw.add(ByteCode.D2I);
          cfw.add(ByteCode.IRETURN);
          break;
        case 'l':
          cfw.add(ByteCode.D2L);
          cfw.add(ByteCode.LRETURN);
          break;
        case 'f':
          cfw.add(ByteCode.D2F);
          cfw.add(ByteCode.FRETURN);
          break;
        case 'd':
          cfw.add(ByteCode.DRETURN);
          break;
        default:
          throw new RuntimeException("Unexpected return type " + retType.toString());
      }

    } else {
      String retTypeStr = retType.getName();
      if (callConvertResult) {
        cfw.addLoadConstant(retTypeStr);
        cfw.addInvoke(
            ByteCode.INVOKESTATIC,
            "java/lang/Class",
            "forName",
            "(Ljava/lang/String;)Ljava/lang/Class;");

        cfw.addInvoke(
            ByteCode.INVOKESTATIC,
            "aurora/javascript/JavaAdapter",
            "convertResult",
            "(Ljava/lang/Object;" + "Ljava/lang/Class;" + ")Ljava/lang/Object;");
      }
      // Now cast to return type
      cfw.add(ByteCode.CHECKCAST, retTypeStr);
      cfw.add(ByteCode.ARETURN);
    }
  }