예제 #1
1
  private Prop findRelProperty(Class<?> fromCls, String rel, Class<?> toCls) {
    Object entity = !fromCls.isInterface() ? data.constructor.create(fromCls) : null;

    for (Prop prop : Beany.propertiesOf(fromCls).select(data.relPropSelector)) {

      String relName = null;

      if (!fromCls.isInterface()) {
        Object value = prop.getRaw(entity);
        if (hasEntityLinks(value)) {
          EntityLinks links = entityLinks(value);
          relName = links.relationName();
        }
      } else {
        Rel relation = prop.getAnnotation(Rel.class);
        if (relation != null) {
          relName = relation.value();
        }
      }

      if (relName != null && relName.equals(rel)) {
        if (prop.getRawTypeArg(0).equals(toCls)) {
          return prop;
        }
      }
    }

    Log.warn(
        "Didn't find inverse relation property!", "relation", rel, "from", fromCls, "to", toCls);
    return null;
  }
예제 #2
0
  private RelPair getRelPair(Object entity, Prop prop, String rel, boolean inverse) {

    Class<?> cls = prop.getRawTypeArg(0);
    Class<? extends Object> entCls = Cls.unproxy(entity.getClass());

    Class<?> srcType = inverse ? cls : entCls;
    Class<?> destType = inverse ? entCls : cls;

    Tuple key = new Tuple(rel, srcType, destType);
    RelPair relPair = data.relPairs.get(key);

    Prop srcProp, destProp;

    if (relPair != null) {
      srcProp = relPair.srcProp;
      destProp = relPair.destProp;
    } else {
      String invRel = inverse ? rel : "^" + rel;
      Prop p = findRelProperty(cls, invRel, entCls);
      srcProp = inverse ? p : prop;
      destProp = inverse ? prop : p;

      relPair = new RelPair(rel, srcType, destType, srcProp, destProp);
      data.relPairs.putIfAbsent(key, relPair);

      if (srcType == null || srcProp == null || destType == null || destProp == null) {
        Log.warn("Incomplete relation pair!", "relation", relPair);
      }
    }

    return relPair;
  }