Пример #1
0
 @Override
 public boolean doTypeVarsMatch(Type sourceType, Type targetType) {
   boolean match = false;
   if (sourceType.getTypeParameters().size() == 1) {
     match = sourceType.getTypeParameters().get(0).isAssignableTo(targetType);
   }
   return match;
 }
Пример #2
0
  public Type getSourceElementType() {
    Type sourceParameterType = getSourceParameter().getType();

    if (sourceParameterType.isArrayType()) {
      return sourceParameterType.getComponentType();
    } else {
      return sourceParameterType.getTypeParameters().get(0).getTypeBound();
    }
  }
Пример #3
0
  @SuppressWarnings("checkstyle:parameternumber")
  private Decorator(
      TypeFactory typeFactory,
      String packageName,
      String name,
      Type decoratorType,
      String interfacePackage,
      String interfaceName,
      List<MappingMethod> methods,
      List<? extends Field> fields,
      Options options,
      VersionInformation versionInformation,
      Accessibility accessibility,
      SortedSet<Type> extraImports,
      DecoratorConstructor decoratorConstructor) {
    super(
        typeFactory,
        packageName,
        name,
        decoratorType.getName(),
        interfacePackage,
        interfaceName,
        methods,
        fields,
        options,
        versionInformation,
        accessibility,
        extraImports,
        decoratorConstructor);

    this.decoratorType = decoratorType;
  }
Пример #4
0
 private List<PropertyEntry> getSourceEntries(Type type, String[] entryNames) {
   List<PropertyEntry> sourceEntries = new ArrayList<PropertyEntry>();
   Type newType = type;
   for (String entryName : entryNames) {
     boolean matchFound = false;
     Map<String, ExecutableElement> sourceReadAccessors = newType.getPropertyReadAccessors();
     for (Map.Entry<String, ExecutableElement> getter : sourceReadAccessors.entrySet()) {
       if (getter.getKey().equals(entryName)) {
         newType =
             typeFactory.getReturnType(
                 (DeclaredType) newType.getTypeMirror(), getter.getValue());
         sourceEntries.add(new PropertyEntry(entryName, getter.getValue(), newType));
         matchFound = true;
         break;
       }
     }
     if (!matchFound) {
       break;
     }
   }
   return sourceEntries;
 }
Пример #5
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();
  }
  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;
  }
Пример #7
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);
    }