Exemplo n.º 1
0
  public static Object checkTypeAndNotNull(String field, Map data, Class expect)
      throws GeneralException {

    Object o = data.get(field);

    if (o == null) {

      throw new GeneralException("Expected a value for: " + field);
    }

    if (expect != null) {

      if (!expect.isAssignableFrom(o.getClass())) {

        throw new GeneralException(
            "Expected type for: "
                + field
                + " to be: "
                + expect.getClass().getName()
                + ", is: "
                + o.getClass().getName());
      }
    }

    return o;
  }
Exemplo n.º 2
0
  private Class<?> getUnwrappedClass(Class<? extends Object> class1) {
    Class<?> result = class1;

    while (null != result && result.getClass().getSimpleName().contains("$$EnhancerBy"))
      result = result.getSuperclass();

    return result;
  }
Exemplo n.º 3
0
 private void guessClassReference(TypedValues list, String name) {
   try {
     Class<?> type = Class.forName(fixPotentialArrayName(name));
     list.add(new TypedValue(type.getClass(), type));
   } catch (ClassNotFoundException y) {
     _logger.fine(name + " looked like a class reference but is not");
   }
 }
Exemplo n.º 4
0
 public LocalVariable getOrAddLocalVariable(String variable, LabelNode label, Class clazz) {
   LocalVariable local = getLocalVariable(variable);
   if (local == null) {
     local = addLocalVariable(variable, label, clazz);
   } else {
     if (local.getClazz() != clazz)
       throw new RuntimeException(
           "Invalid class for "
               + variable
               + " variable, "
               + local.getClazz()
               + " != "
               + clazz.getClass());
   }
   return local;
 }
  private void _checkMethodCalls(FlowGraphNode node) {
    if (node.getType() != FlowGraphNode.METHOD_CALL) return;

    MethodCallNode mc = (MethodCallNode) node;

    String mname = mc.getMethodName();
    Class c = mc.getCallerType();
    if (mname != null && c != null) {
      if (mname.equals("getDirector") && Util.isSubClass(c, ptolemy.actor.AtomicActor.class)) {
        _getDirectors.add(node);
      } else if ((mname.equals("fireAt") || mname.equals("fireAtCurrentTime"))
          && ((c.getClass().equals(ptolemy.actor.Director.class))
              || Util.isSubClass(c, ptolemy.actor.Director.class))) {
        _timedDelay.add(node);
      }
    }
  }
Exemplo n.º 6
0
  /**
   * Binds data from reference model instance to XML binding classes
   *
   * @param obj
   * @return
   * @throws XMLBindingException
   */
  public Object bindToXML(Object obj) throws XMLBindingException {
    if (obj == null) {
      return null;
    }
    String className = obj.getClass().getSimpleName();
    Method[] methods = obj.getClass().getMethods();

    try {
      Class xmlClass = Class.forName(XML_BINDING_PACKAGE + className.toUpperCase());

      // debug code only
      if (xmlClass.getClasses().length != 1) {
        log.debug("XMLBinding: bindToXML(): xmlClass.getClass()=" + xmlClass.getClass());
        log.debug("XMLBinding: bindToXML(): xmlClass.toString()=" + xmlClass.toString());
        for (Class clazz : xmlClass.getClasses()) {
          log.debug("\t clazz.getClass()=" + clazz.getClass());
          log.debug("\t clazz.toString()=" + clazz.toString());
        }
      }

      Class factoryClass = xmlClass.getClasses()[0];

      // ES modification: Add xmlOptions containing openehr (default) and xsi namespaces

      // Method factoryMethod = factoryClass.getMethod(NEW_INSTANCE, null);
      // Changed to pick the method with an XmlOptions parameter instead
      Method factoryMethod = factoryClass.getMethod(NEW_INSTANCE, XmlOptions.class);

      // First prameter null because it's a static method, see:
      // http://java.sun.com/docs/books/tutorial/reflect/member/methodInvocation.html
      // Second parameter should be the parameter of XmlObject.Factory.newInstance(XmlOptions
      // options)
      // see:
      // http://xmlbeans.apache.org/docs/2.2.0/reference/org/apache/xmlbeans/XmlObject.Factory.html
      //
      // Previous code was: Object xmlObj = factoryMethod.invoke(null, null);
      Object xmlObj = factoryMethod.invoke(null, xopt);

      Map<String, Class> attributes = builder.retrieveAttribute(className);
      Set<String> attributeNames = attributes.keySet();
      Object attributeValue = null;
      Method setterMethod = null;

      for (Method method : methods) {
        String name = method.getName();

        // cause dead-loop
        if ("getParent".equals(name)) {
          continue; //
        }

        if (isGetter(name, attributeNames)) {

          log.debug("getter: " + name);

          if (method.getParameterTypes().length > 0) {
            continue;
          }

          attributeValue = method.invoke(obj, null);

          if (attributeValue == null) {
            continue;
          }

          log.debug("value.class: " + attributeValue.getClass());

          boolean isList = false;

          if (attributeValue.getClass().isArray()) {
            Object[] array = (Object[]) attributeValue;
            if (array.length == 0) {
              continue;
            }
            Object[] done = new Object[array.length];
            for (int i = 0; i < array.length; i++) {
              done[i] = bindToXML(array[i]);
            }
            attributeValue = done;

          } else if (ProportionKind.class.equals(attributeValue.getClass())) {

            ProportionKind kind = (ProportionKind) attributeValue;
            attributeValue = BigInteger.valueOf(kind.getValue());

          } else if (isOpenEHRRMClass(attributeValue)) {

            attributeValue = bindToXML(attributeValue);

          } else if (List.class.isAssignableFrom(attributeValue.getClass())) {

            isList = true;
            List list = (List) attributeValue;

            log.debug("list.size: " + list.size());

            String attributeName = getAttributeNameFromGetter(name);
            setterMethod = findSetter(attributeName, xmlClass, isList);

            Method addNew = findAddNew(attributeName, xmlClass);

            log.debug("setter: " + setterMethod.getName() + ", xmlClass: " + xmlClass);

            for (int i = 0, j = list.size() - 1; i <= j; i++) {
              Object value = list.get(i);

              Object[] array = new Object[2];

              addNew.invoke(xmlObj, null);

              array[0] = new Integer(i);
              array[1] = bindToXML(value);
              setterMethod.invoke(xmlObj, array);

              log.debug("list.member value set!!!!");
            }
          }

          if (!isList) {
            String attributeName = getAttributeNameFromGetter(name);

            log.debug(
                "attribute: "
                    + attributeName
                    + ", value("
                    + attributeValue
                    + "), "
                    + "type: "
                    + attributeValue.getClass());

            // TODO fix for mismatched attribute name in XSD and RM
            if ("nullFlavor".equals(attributeName)) {
              attributeName = "nullFlavour";
            }

            // skip function according to specs
            if ("isMerged".equals(attributeName)) {
              continue;
            }

            setterMethod = findSetter(attributeName, xmlClass, isList);

            if (setterMethod == null) {
              log.error(
                  "failed to find setterMethod for attribute: "
                      + attributeName
                      + " with type: "
                      + xmlClass);
              continue;
            }

            // special handling deals with 'real' typed
            // attributes in specs but typed 'float' in xsd
            String setter = setterMethod.getName();
            if ("setAccuracy".equals(setter)
                || "setDenominator".equals(setter)
                || "setNumerator".equals(setter)) {

              Double d = (Double) attributeValue;
              attributeValue = d.floatValue();
            }

            log.debug(
                "setter: "
                    + setterMethod.getName()
                    + ", xmlClass: "
                    + xmlClass
                    + ", attributeValue: "
                    + attributeValue
                    + ", attributeValue.class: "
                    + attributeValue.getClass());

            setterMethod.invoke(xmlObj, attributeValue);
          }
        }
      }

      return xmlObj;

    } catch (Exception e) {
      e.printStackTrace();
      throw new XMLBindingException(
          "exception caught when bind obj to " + className + ", " + e.getMessage());
    }
  }