示例#1
0
 public boolean isSame(SourceMethod method) {
   return getSourceParameters().size() == 1
       && method.getSourceParameters().size() == 1
       && equals(
           first(getSourceParameters()).getType(), first(method.getSourceParameters()).getType())
       && equals(getResultType(), method.getResultType());
 }
示例#2
0
 public boolean canInheritFrom(SourceMethod method) {
   return isMapMapping() == method.isMapMapping()
       && isIterableMapping() == method.isIterableMapping()
       && isEnumMapping() == method.isEnumMapping()
       && getResultType().isAssignableTo(method.getResultType())
       && allParametersAreAssignable(getSourceParameters(), method.getSourceParameters());
 }
  /**
   * Returns the configuring inverse method's options in case the given method is annotated with
   * {@code @InheritInverseConfiguration} and exactly one such configuring method can unambiguously
   * be selected (as per the source/target type and optionally the name given via
   * {@code @InheritInverseConfiguration}).
   */
  private MappingOptions getInverseMappingOptions(
      List<SourceMethod> rawMethods,
      SourceMethod method,
      List<SourceMethod> initializingMethods,
      MapperConfiguration mapperConfig) {
    SourceMethod resultMethod = null;
    InheritInverseConfigurationPrism reversePrism =
        InheritInverseConfigurationPrism.getInstanceOn(method.getExecutable());

    if (reversePrism != null) {

      // is there a suitable constructor
      if (method.isBeanMapping()
          && !method.getResultType().isCollectionOrMapType()
          && !method.getResultType().hasEmptyAccessibleContructor()) {
        reportErrorWhenNoSuitableConstrutor(method, reversePrism);
        return null;
      }

      // method is configured as being reverse method, collect candidates
      List<SourceMethod> candidates = new ArrayList<SourceMethod>();
      for (SourceMethod oneMethod : rawMethods) {
        if (oneMethod.reverses(method)) {
          candidates.add(oneMethod);
        }
      }

      String name = reversePrism.name();
      if (candidates.size() == 1) {
        // no ambiguity: if no configuredBy is specified, or configuredBy specified and match
        if (name.isEmpty()) {
          resultMethod = candidates.get(0);
        } else if (candidates.get(0).getName().equals(name)) {
          resultMethod = candidates.get(0);
        } else {
          reportErrorWhenNonMatchingName(candidates.get(0), method, reversePrism);
        }
      } else if (candidates.size() > 1) {
        // ambiguity: find a matching method that matches configuredBy

        List<SourceMethod> nameFilteredcandidates = new ArrayList<SourceMethod>();
        for (SourceMethod candidate : candidates) {
          if (candidate.getName().equals(name)) {
            nameFilteredcandidates.add(candidate);
          }
        }

        if (nameFilteredcandidates.size() == 1) {
          resultMethod = nameFilteredcandidates.get(0);
        } else if (nameFilteredcandidates.size() > 1) {
          reportErrorWhenSeveralNamesMatch(nameFilteredcandidates, method, reversePrism);
        } else {
          reportErrorWhenAmbigousReverseMapping(candidates, method, reversePrism);
        }
      }
    }

    return extractInitializedOptions(resultMethod, rawMethods, mapperConfig, initializingMethods);
  }