Exemplo n.º 1
0
 protected Type getTypeForClass(Class c) {
   Type t = rf.reflAdapter.getTypeForClass(c);
   if (t == null) return rf.getClassAny(c);
   else return t;
 }
Exemplo n.º 2
0
  public Type navigateQualified(String name, Type[] qualifiers) throws OclTypeException {
    Type theQualifier = null;

    if (qualifiers != null) {
      if (qualifiers.length == 1) theQualifier = qualifiers[0];
      else throw new OclTypeException("ReflectionFacade can handle one qualifier only");
    }
    // System.out.println("ClassAny.navigateQualified:"+this+" "+name);
    Type ret = Basic.navigateAnyQualified(name, this, qualifiers);
    if (ret != null) return ret;
    String[] javaNames = rf.nameAdapter.getNames(name);
    Field f = null;
    for (int i = 0; i < javaNames.length && f == null; i++) {
      Class nextClass = c;
      while (nextClass != null && f == null) {
        try {
          f = nextClass.getDeclaredField(javaNames[i]);
        } catch (NoSuchFieldException nsf) {
          // try next class
        }
        nextClass = nextClass.getSuperclass();
      }
    }
    if (f == null) {
      throw new OclTypeException(c.getName() + " has no field " + name);
    }
    Class type = f.getType();
    Type modeltype = getTypeForClass(type);

    if (modeltype instanceof Collection) {
      Class elementtype = rf.getElementType(f);

      if (rf.reflAdapter.isMap(type)) {
        if (theQualifier == null) {
          if (elementtype != null)
            return new Collection(
                ((Collection) modeltype).getCollectionKind(), getTypeForClass(elementtype));
          else return modeltype;
        } else {
          Class keytype_class = rf.getKeyType(f);
          Type keytype = null;
          if (keytype_class != null) keytype = getTypeForClass(keytype_class);

          if (keytype != null) {
            if (!theQualifier.equals(keytype))
              throw new OclTypeException(
                  "feature "
                      + name
                      + " in classifier "
                      + c
                      + ": expected qualifier type "
                      + keytype
                      + " found "
                      + theQualifier
                      + ".");
            if (elementtype != null) return getTypeForClass(elementtype);
            else
              throw new OclTypeException(
                  "feature "
                      + name
                      + "["
                      + keytype
                      + "] in classifier "
                      + c
                      + " has no @element-type tag.");
          } else
            throw new OclTypeException(
                "feature "
                    + name
                    + " in classifier "
                    + c
                    + ": qualified with type "
                    + theQualifier
                    + ", but feature has no @keytype tag.");
        }
      } else {
        if (theQualifier != null)
          throw new OclTypeException(
              "feature " + name + " in classifier " + c + " cannot be qualified.");

        if (elementtype != null)
          return new Collection(
              ((Collection) modeltype).getCollectionKind(), getTypeForClass(elementtype));
        else return modeltype;
      }
    } else {
      if (theQualifier != null)
        throw new OclTypeException(
            "feature " + name + " in classifier " + c + " cannot be qualified.");
      return modeltype;
    }
  }