private void reportErrorWhenAmbigousReverseMapping(
      List<SourceMethod> candidates,
      SourceMethod method,
      InheritInverseConfigurationPrism reversePrism) {

    List<String> candidateNames = new ArrayList<String>();
    for (SourceMethod candidate : candidates) {
      candidateNames.add(candidate.getName());
    }

    String name = reversePrism.name();
    if (name.isEmpty()) {
      messager.printMessage(
          method.getExecutable(),
          reversePrism.mirror,
          Message.INHERITINVERSECONFIGURATION_DUPLICATES,
          Strings.join(candidateNames, "(), "));

    } else {
      messager.printMessage(
          method.getExecutable(),
          reversePrism.mirror,
          Message.INHERITINVERSECONFIGURATION_INVALID_NAME,
          Strings.join(candidateNames, "(), "),
          name);
    }
  }
  private void mergeInheritedOptions(
      SourceMethod method,
      MapperConfiguration mapperConfig,
      List<SourceMethod> availableMethods,
      List<SourceMethod> initializingMethods) {
    if (initializingMethods.contains(method)) {
      // cycle detected

      initializingMethods.add(method);

      messager.printMessage(
          method.getExecutable(),
          Message.INHERITCONFIGURATION_CYCLE,
          Strings.join(initializingMethods, " -> "));
      return;
    }

    initializingMethods.add(method);

    MappingOptions mappingOptions = method.getMappingOptions();
    List<SourceMethod> applicablePrototypeMethods = method.getApplicablePrototypeMethods();

    MappingOptions inverseMappingOptions =
        getInverseMappingOptions(availableMethods, method, initializingMethods, mapperConfig);

    MappingOptions templateMappingOptions =
        getTemplateMappingOptions(
            join(availableMethods, applicablePrototypeMethods),
            method,
            initializingMethods,
            mapperConfig);

    if (templateMappingOptions != null) {
      mappingOptions.applyInheritedOptions(
          templateMappingOptions, false, method, messager, typeFactory);
    } else if (inverseMappingOptions != null) {
      mappingOptions.applyInheritedOptions(
          inverseMappingOptions, true, method, messager, typeFactory);
    } else if (mapperConfig.getMappingInheritanceStrategy() == AUTO_INHERIT_FROM_CONFIG) {
      if (applicablePrototypeMethods.size() == 1) {
        mappingOptions.applyInheritedOptions(
            first(applicablePrototypeMethods).getMappingOptions(),
            false,
            method,
            messager,
            typeFactory);
      } else if (applicablePrototypeMethods.size() > 1) {
        messager.printMessage(
            method.getExecutable(),
            Message.INHERITCONFIGURATION_MULTIPLE_PROTOTYPE_METHODS_MATCH,
            Strings.join(applicablePrototypeMethods, ", "));
      }
    }

    mappingOptions.markAsFullyInitialized();
  }
  private void reportErrorWhenSeveralNamesMatch(
      List<SourceMethod> candidates, SourceMethod method, InheritConfigurationPrism prism) {

    messager.printMessage(
        method.getExecutable(),
        prism.mirror,
        Message.INHERITCONFIGURATION_DUPLICATE_MATCHES,
        prism.name(),
        Strings.join(candidates, ", "));
  }
Beispiel #4
0
  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder(returnType.toString());
    sb.append(" ");

    if (declaringMapper != null) {
      sb.append(declaringMapper).append(".");
    }

    sb.append(getName()).append("(").append(Strings.join(parameters, ", ")).append(")");

    return sb.toString();
  }
  public LifecycleCallbackMethodReference(
      SourceMethod method,
      MapperReference mapperReference,
      List<ParameterBinding> parameterBindings,
      Type methodReturnType,
      Type methodResultType,
      Set<String> existingVariableNames) {
    super(method, mapperReference, parameterBindings);
    this.declaringType = method.getDeclaringMapper();
    this.methodReturnType = methodReturnType;
    this.methodResultType = methodResultType;

    if (hasReturnType()) {
      this.targetVariableName = Strings.getSaveVariableName("target", existingVariableNames);
      existingVariableNames.add(this.targetVariableName);
    } else {
      this.targetVariableName = null;
    }
  }
    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);
    }
    public IterableMappingMethod build() {

      Type sourceParameterType = first(method.getSourceParameters()).getType();
      Type resultType = method.getResultType();

      Type sourceElementType =
          sourceParameterType.isArrayType()
              ? sourceParameterType.getComponentType()
              : first(sourceParameterType.getTypeParameters()).getTypeBound();
      Type targetElementType =
          resultType.isArrayType()
              ? resultType.getComponentType()
              : first(resultType.getTypeParameters()).getTypeBound();

      String loopVariableName =
          Strings.getSaveVariableName(sourceElementType.getName(), method.getParameterNames());

      Assignment assignment =
          ctx.getMappingResolver()
              .getTargetAssignment(
                  method,
                  "collection element",
                  sourceElementType,
                  targetElementType,
                  null, // there is no targetPropertyName
                  formattingParameters,
                  selectionParameters,
                  loopVariableName,
                  false);

      if (assignment == null) {
        if (method instanceof ForgedMethod) {
          // leave messaging to calling property mapping
          return null;
        } else {
          ctx.getMessager()
              .printMessage(method.getExecutable(), Message.ITERABLEMAPPING_MAPPING_NOT_FOUND);
        }
      } else {
        if (method instanceof ForgedMethod) {
          ForgedMethod forgedMethod = (ForgedMethod) method;
          forgedMethod.addThrownTypes(assignment.getThrownTypes());
        }
      }
      // target accessor is setter, so decorate assignment as setter
      if (resultType.isArrayType()) {
        assignment = new LocalVarWrapper(assignment, method.getThrownTypes());
      } else {
        assignment = new SetterWrapper(assignment, method.getThrownTypes());
      }

      // mapNullToDefault
      boolean mapNullToDefault = false;
      if (method.getMapperConfiguration() != null) {
        mapNullToDefault = method.getMapperConfiguration().isMapToDefault(nullValueMappingStrategy);
      }

      MethodReference factoryMethod = null;
      if (!method.isUpdateMethod()) {
        factoryMethod =
            ctx.getMappingResolver().getFactoryMethod(method, method.getResultType(), null);
      }

      List<LifecycleCallbackMethodReference> beforeMappingMethods =
          LifecycleCallbackFactory.beforeMappingMethods(method, selectionParameters, ctx);
      List<LifecycleCallbackMethodReference> afterMappingMethods =
          LifecycleCallbackFactory.afterMappingMethods(method, selectionParameters, ctx);

      return new IterableMappingMethod(
          method,
          assignment,
          factoryMethod,
          mapNullToDefault,
          loopVariableName,
          beforeMappingMethods,
          afterMappingMethods);
    }
 public String getIndex2Name() {
   return Strings.getSaveVariableName(
       "j", loopVariableName, getSourceParameter().getName(), getResultName());
 }