Beispiel #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());
 }
Beispiel #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());
 }
  /**
   * Creates a copy of this reference, which is adapted to the given method
   *
   * @param method the method to create the copy for
   * @return the copy
   */
  public SourceReference copyForInheritanceTo(SourceMethod method) {
    List<Parameter> replacementParamCandidates = new ArrayList<Parameter>();
    for (Parameter sourceParam : method.getSourceParameters()) {
      if (sourceParam.getType().isAssignableTo(parameter.getType())) {
        replacementParamCandidates.add(sourceParam);
      }
    }

    Parameter replacement = parameter;
    if (replacementParamCandidates.size() == 1) {
      replacement = first(replacementParamCandidates);
    }

    return new SourceReference(replacement, propertyEntries, isValid);
  }
    public SourceReference build() {

      String sourceName = mapping.getSourceName();

      if (sourceName == null) {
        return null;
      }

      boolean isValid = true;
      boolean foundEntryMatch;

      String[] sourcePropertyNames = new String[0];
      String[] segments = sourceName.split("\\.");
      Parameter parameter = null;

      List<PropertyEntry> entries = new ArrayList<PropertyEntry>();

      if (method.getSourceParameters().size() > 1) {

        // parameterName is mandatory for multiple source parameters
        if (segments.length > 0) {
          String sourceParameterName = segments[0];
          parameter = method.getSourceParameter(sourceParameterName);
          if (parameter == null) {
            reportMappingError(Message.PROPERTYMAPPING_INVALID_PARAMETER_NAME, sourceParameterName);
            isValid = false;
          }
        }
        if (segments.length > 1 && parameter != null) {
          sourcePropertyNames = Arrays.copyOfRange(segments, 1, segments.length);
          entries = getSourceEntries(parameter.getType(), sourcePropertyNames);
          foundEntryMatch = (entries.size() == sourcePropertyNames.length);
        } else {
          // its only a parameter, no property
          foundEntryMatch = true;
        }

      } else {

        // parameter name is not mandatory for single source parameter
        sourcePropertyNames = segments;
        parameter = method.getSourceParameters().get(0);
        entries = getSourceEntries(parameter.getType(), sourcePropertyNames);
        foundEntryMatch = (entries.size() == sourcePropertyNames.length);

        if (!foundEntryMatch) {
          // Lets see if the expression contains the parameterName, so
          // parameterName.propName1.propName2
          if (parameter.getName().equals(segments[0])) {
            sourcePropertyNames = Arrays.copyOfRange(segments, 1, segments.length);
            entries = getSourceEntries(parameter.getType(), sourcePropertyNames);
            foundEntryMatch = (entries.size() == sourcePropertyNames.length);
          } else {
            // segment[0] cannot be attributed to the parameter name.
            parameter = null;
          }
        }
      }

      if (!foundEntryMatch) {

        if (parameter != null) {
          reportMappingError(
              Message.PROPERTYMAPPING_NO_PROPERTY_IN_PARAMETER,
              parameter.getName(),
              Strings.join(Arrays.asList(sourcePropertyNames), "."));
        } else {
          reportMappingError(
              Message.PROPERTYMAPPING_INVALID_PROPERTY_NAME, mapping.getSourceName());
        }
        isValid = false;
      }

      return new SourceReference(parameter, entries, isValid);
    }