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 reportErrorWhenInheritForwardAlsoHasInheritReverseMapping(SourceMethod method) {
   InheritInverseConfigurationPrism reversePrism =
       InheritInverseConfigurationPrism.getInstanceOn(method.getExecutable());
   if (reversePrism != null) {
     messager.printMessage(
         method.getExecutable(), reversePrism.mirror, Message.INHERITCONFIGURATION_BOTH);
   }
 }
 private void reportMappingError(Message msg, Object... objects) {
   messager.printMessage(
       method.getExecutable(),
       mapping.getMirror(),
       mapping.getSourceAnnotationValue(),
       msg,
       objects);
 }
 private void reportErrorIfNoImplementationTypeIsRegisteredForInterfaceReturnType(
     SourceMethod method) {
   if (method.getReturnType().getTypeMirror().getKind() != TypeKind.VOID
       && method.getReturnType().isInterface()
       && method.getReturnType().getImplementationType() == null) {
     messager.printMessage(
         method.getExecutable(), Message.GENERAL_NO_IMPLEMENTATION, method.getReturnType());
   }
 }
  private void reportErrorWhenNoSuitableConstrutor(
      SourceMethod method, InheritInverseConfigurationPrism reversePrism) {

    messager.printMessage(
        method.getExecutable(),
        reversePrism.mirror,
        Message.INHERITINVERSECONFIGURATION_NO_SUITABLE_CONSTRUCTOR,
        reversePrism.name());
  }
  private void reportErrorWhenNonMatchingName(
      SourceMethod onlyCandidate, SourceMethod method, InheritConfigurationPrism prims) {

    messager.printMessage(
        method.getExecutable(),
        prims.mirror,
        Message.INHERITCONFIGURATION_NO_NAME_MATCH,
        prims.name(),
        onlyCandidate.getName());
  }
  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 #9
0
  public static MapMapping fromPrism(
      MapMappingPrism mapMapping, ExecutableElement method, FormattingMessager messager) {
    if (mapMapping == null) {
      return null;
    }

    NullValueMappingStrategyPrism nullValueMappingStrategy =
        mapMapping.values.nullValueMappingStrategy() == null
            ? null
            : NullValueMappingStrategyPrism.valueOf(mapMapping.nullValueMappingStrategy());

    boolean keyTargetTypeIsDefined = !TypeKind.VOID.equals(mapMapping.keyTargetType().getKind());
    boolean valueTargetTypeIsDefined =
        !TypeKind.VOID.equals(mapMapping.valueTargetType().getKind());
    if (mapMapping.keyDateFormat().isEmpty()
        && mapMapping.keyNumberFormat().isEmpty()
        && mapMapping.keyQualifiedBy().isEmpty()
        && mapMapping.keyQualifiedByName().isEmpty()
        && mapMapping.valueDateFormat().isEmpty()
        && mapMapping.valueNumberFormat().isEmpty()
        && mapMapping.valueQualifiedBy().isEmpty()
        && mapMapping.valueQualifiedByName().isEmpty()
        && !keyTargetTypeIsDefined
        && !valueTargetTypeIsDefined
        && (nullValueMappingStrategy == null)) {

      messager.printMessage(method, Message.MAPMAPPING_NO_ELEMENTS);
    }

    SelectionParameters keySelection =
        new SelectionParameters(
            mapMapping.keyQualifiedBy(),
            mapMapping.keyQualifiedByName(),
            keyTargetTypeIsDefined ? mapMapping.keyTargetType() : null);

    SelectionParameters valueSelection =
        new SelectionParameters(
            mapMapping.valueQualifiedBy(),
            mapMapping.valueQualifiedByName(),
            valueTargetTypeIsDefined ? mapMapping.valueTargetType() : null);

    FormattingParameters keyFormatting =
        new FormattingParameters(mapMapping.keyDateFormat(), mapMapping.keyNumberFormat());

    FormattingParameters valueFormatting =
        new FormattingParameters(mapMapping.valueDateFormat(), mapMapping.valueNumberFormat());

    return new MapMapping(
        keyFormatting,
        keySelection,
        valueFormatting,
        valueSelection,
        mapMapping.mirror,
        nullValueMappingStrategy);
  }
  private void reportErrorWhenNonMatchingName(
      SourceMethod onlyCandidate,
      SourceMethod method,
      InheritInverseConfigurationPrism reversePrism) {

    messager.printMessage(
        method.getExecutable(),
        reversePrism.mirror,
        Message.INHERITINVERSECONFIGURATION_NO_NAME_MATCH,
        reversePrism.name(),
        onlyCandidate.getName());
  }
  private Decorator getDecorator(
      TypeElement element, List<SourceMethod> methods, String implName, String implPackage) {
    DecoratedWithPrism decoratorPrism = DecoratedWithPrism.getInstanceOn(element);

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

    TypeElement decoratorElement = (TypeElement) typeUtils.asElement(decoratorPrism.value());

    if (!typeUtils.isAssignable(decoratorElement.asType(), element.asType())) {
      messager.printMessage(element, decoratorPrism.mirror, Message.DECORATOR_NO_SUBTYPE);
    }

    List<MappingMethod> mappingMethods = new ArrayList<MappingMethod>(methods.size());

    for (SourceMethod mappingMethod : methods) {
      boolean implementationRequired = true;
      for (ExecutableElement method :
          ElementFilter.methodsIn(decoratorElement.getEnclosedElements())) {
        if (elementUtils.overrides(method, mappingMethod.getExecutable(), decoratorElement)) {
          implementationRequired = false;
          break;
        }
      }
      Type declaringMapper = mappingMethod.getDeclaringMapper();
      if (implementationRequired && !(mappingMethod.isDefault() || mappingMethod.isStatic())) {
        if ((declaringMapper == null) || declaringMapper.equals(typeFactory.getType(element))) {
          mappingMethods.add(new DelegatingMethod(mappingMethod));
        }
      }
    }

    boolean hasDelegateConstructor = false;
    boolean hasDefaultConstructor = false;
    for (ExecutableElement constructor :
        ElementFilter.constructorsIn(decoratorElement.getEnclosedElements())) {
      if (constructor.getParameters().isEmpty()) {
        hasDefaultConstructor = true;
      } else if (constructor.getParameters().size() == 1) {
        if (typeUtils.isAssignable(element.asType(), first(constructor.getParameters()).asType())) {
          hasDelegateConstructor = true;
        }
      }
    }

    if (!hasDelegateConstructor && !hasDefaultConstructor) {
      messager.printMessage(element, decoratorPrism.mirror, Message.DECORATOR_CONSTRUCTOR);
    }

    Decorator decorator =
        new Decorator.Builder()
            .elementUtils(elementUtils)
            .typeFactory(typeFactory)
            .mapperElement(element)
            .decoratorPrism(decoratorPrism)
            .methods(mappingMethods)
            .hasDelegateConstructor(hasDelegateConstructor)
            .options(options)
            .versionInformation(versionInformation)
            .implName(implName)
            .implPackage(implPackage)
            .extraImports(getExtraImports(element))
            .build();

    return decorator;
  }
  private List<MappingMethod> getMappingMethods(
      MapperConfiguration mapperConfig, List<SourceMethod> methods) {
    List<MappingMethod> mappingMethods = new ArrayList<MappingMethod>();

    for (SourceMethod method : methods) {
      if (!method.overridesMethod()) {
        continue;
      }

      mergeInheritedOptions(method, mapperConfig, methods, new ArrayList<SourceMethod>());

      MappingOptions mappingOptions = method.getMappingOptions();

      boolean hasFactoryMethod = false;

      if (method.isIterableMapping()) {

        IterableMappingMethod.Builder builder = new IterableMappingMethod.Builder();

        FormattingParameters formattingParameters = null;
        SelectionParameters selectionParameters = null;
        NullValueMappingStrategyPrism nullValueMappingStrategy = null;

        if (mappingOptions.getIterableMapping() != null) {
          formattingParameters = mappingOptions.getIterableMapping().getFormattingParameters();
          selectionParameters = mappingOptions.getIterableMapping().getSelectionParameters();
          nullValueMappingStrategy =
              mappingOptions.getIterableMapping().getNullValueMappingStrategy();
        }

        IterableMappingMethod iterableMappingMethod =
            builder
                .mappingContext(mappingContext)
                .method(method)
                .formattingParameters(formattingParameters)
                .selectionParameters(selectionParameters)
                .nullValueMappingStrategy(nullValueMappingStrategy)
                .build();

        hasFactoryMethod = iterableMappingMethod.getFactoryMethod() != null;
        mappingMethods.add(iterableMappingMethod);
      } else if (method.isMapMapping()) {

        MapMappingMethod.Builder builder = new MapMappingMethod.Builder();

        SelectionParameters keySelectionParameters = null;
        FormattingParameters keyFormattingParameters = null;
        SelectionParameters valueSelectionParameters = null;
        FormattingParameters valueFormattingParameters = null;
        NullValueMappingStrategyPrism nullValueMappingStrategy = null;

        if (mappingOptions.getMapMapping() != null) {
          keySelectionParameters = mappingOptions.getMapMapping().getKeySelectionParameters();
          keyFormattingParameters = mappingOptions.getMapMapping().getKeyFormattingParameters();
          valueSelectionParameters = mappingOptions.getMapMapping().getValueSelectionParameters();
          valueFormattingParameters = mappingOptions.getMapMapping().getValueFormattingParameters();
          nullValueMappingStrategy = mappingOptions.getMapMapping().getNullValueMappingStrategy();
        }

        MapMappingMethod mapMappingMethod =
            builder
                .mappingContext(mappingContext)
                .method(method)
                .keyFormattingParameters(keyFormattingParameters)
                .keySelectionParameters(keySelectionParameters)
                .valueFormattingParameters(valueFormattingParameters)
                .valueSelectionParameters(valueSelectionParameters)
                .nullValueMappingStrategy(nullValueMappingStrategy)
                .build();

        hasFactoryMethod = mapMappingMethod.getFactoryMethod() != null;
        mappingMethods.add(mapMappingMethod);
      } else if (method.isValueMapping()) {
        // prefer value mappings over enum mapping
        ValueMappingMethod valueMappingMethod =
            new ValueMappingMethod.Builder()
                .mappingContext(mappingContext)
                .souceMethod(method)
                .valueMappings(mappingOptions.getValueMappings())
                .build();
        mappingMethods.add(valueMappingMethod);
      } else if (method.isEnumMapping()) {

        messager.printMessage(method.getExecutable(), Message.ENUMMAPPING_DEPRECATED);

        EnumMappingMethod.Builder builder = new EnumMappingMethod.Builder();
        MappingMethod enumMappingMethod =
            builder.mappingContext(mappingContext).souceMethod(method).build();

        if (enumMappingMethod != null) {
          mappingMethods.add(enumMappingMethod);
        }
      } else {

        NullValueMappingStrategyPrism nullValueMappingStrategy = null;
        SelectionParameters selectionParameters = null;

        if (mappingOptions.getBeanMapping() != null) {
          nullValueMappingStrategy = mappingOptions.getBeanMapping().getNullValueMappingStrategy();
          selectionParameters = mappingOptions.getBeanMapping().getSelectionParameters();
        }
        BeanMappingMethod.Builder builder = new BeanMappingMethod.Builder();
        BeanMappingMethod beanMappingMethod =
            builder
                .mappingContext(mappingContext)
                .souceMethod(method)
                .nullValueMappingStrategy(nullValueMappingStrategy)
                .selectionParameters(selectionParameters)
                .build();

        if (beanMappingMethod != null) {
          hasFactoryMethod = beanMappingMethod.getFactoryMethod() != null;
          mappingMethods.add(beanMappingMethod);
        }
      }

      if (!hasFactoryMethod) {
        // A factory method  is allowed to return an interface type and hence, the generated
        // implementation as well. The check below must only be executed if there's no factory
        // method that could be responsible.
        reportErrorIfNoImplementationTypeIsRegisteredForInterfaceReturnType(method);
      }
    }
    return mappingMethods;
  }