예제 #1
0
  private static void markMapperReferenceAsUsed(List<MapperReference> references, Method method) {
    for (MapperReference ref : references) {
      if (ref.getType().equals(method.getDeclaringMapper())) {
        if (!ref.isUsed() && !method.isStatic()) {
          ref.setUsed(true);
        }
        ref.setTypeRequiresImport(true);

        return;
      }
    }
  }
예제 #2
0
 private static boolean isValidCandidate(
     SourceMethod candidate, Method method, List<Parameter> parameterAssignments) {
   if (parameterAssignments == null) {
     return false;
   }
   if (!candidate.matches(extractSourceTypes(parameterAssignments), method.getResultType())) {
     return false;
   }
   return (candidate.getReturnType().isVoid()
       || candidate.getReturnType().isTypeVar()
       || candidate.getReturnType().isAssignableTo(method.getResultType()));
 }
예제 #3
0
  private static List<Parameter> getAvailableParameters(Method method, MappingBuilderContext ctx) {
    List<Parameter> availableParams = new ArrayList<Parameter>(method.getParameters());
    if (method.getMappingTargetParameter() == null) {
      availableParams.add(new Parameter(null, method.getResultType(), true, false, false));
    }

    Parameter targetTypeParameter =
        new Parameter(
            null, ctx.getTypeFactory().classTypeOf(method.getResultType()), false, true, false);

    availableParams.add(targetTypeParameter);
    return availableParams;
  }
예제 #4
0
 private static List<LifecycleCallbackMethodReference> toLifecycleCallbackMethodRefs(
     Method method,
     List<SourceMethod> candidates,
     Map<SourceMethod, List<Parameter>> parameterAssignmentsForSourceMethod,
     MappingBuilderContext ctx,
     Set<String> existingVariableNames) {
   List<LifecycleCallbackMethodReference> result =
       new ArrayList<LifecycleCallbackMethodReference>();
   for (SourceMethod candidate : candidates) {
     markMapperReferenceAsUsed(ctx.getMapperReferences(), candidate);
     result.add(
         new LifecycleCallbackMethodReference(
             candidate,
             parameterAssignmentsForSourceMethod.get(candidate),
             method.getReturnType(),
             method.getResultType(),
             existingVariableNames));
   }
   return result;
 }
예제 #5
0
 private IterableMappingMethod(
     Method method,
     Assignment parameterAssignment,
     MethodReference factoryMethod,
     boolean mapNullToDefault,
     String loopVariableName,
     List<LifecycleCallbackMethodReference> beforeMappingReferences,
     List<LifecycleCallbackMethodReference> afterMappingReferences) {
   super(method, beforeMappingReferences, afterMappingReferences);
   this.elementAssignment = parameterAssignment;
   this.factoryMethod = factoryMethod;
   this.overridden = method.overridesMethod();
   this.mapNullToDefault = mapNullToDefault;
   this.loopVariableName = loopVariableName;
 }
예제 #6
0
    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);
    }