private List<MapperReference> initReferencedMappers( TypeElement element, MapperConfiguration mapperConfig) { List<MapperReference> result = new LinkedList<MapperReference>(); List<String> variableNames = new LinkedList<String>(); for (TypeMirror usedMapper : mapperConfig.uses()) { DefaultMapperReference mapperReference = DefaultMapperReference.getInstance( typeFactory.getType(usedMapper), MapperPrism.getInstanceOn(typeUtils.asElement(usedMapper)) != null, typeFactory, variableNames); result.add(mapperReference); variableNames.add(mapperReference.getVariableName()); } return result; }
private Set<ConstraintCheckError> checkAnnotationValue( TypeElement element, AnnotationMirror annotation) { Set<ConstraintCheckError> errors = CollectionHelper.newHashSet(); AnnotationValue value = annotationApiHelper.getAnnotationValue(annotation, "value"); TypeMirror valueType = (TypeMirror) value.getValue(); TypeElement valueElement = (TypeElement) typeUtils.asElement(valueType); if (valueElement.getKind().isInterface() || valueElement.getModifiers().contains(Modifier.ABSTRACT)) { errors.add( new ConstraintCheckError( element, annotation, "GROUP_SEQUENCE_PROVIDER_ANNOTATION_VALUE_MUST_BE_AN_IMPLEMENTATION_CLASS")); } else { // the TypeElement hosting the annotation is a concrete implementation of the // DefaultGroupSequenceProvider // interface. In that case, we need to check that it has a public default constructor. if (!hasPublicDefaultConstructor(valueElement)) { errors.add( new ConstraintCheckError( element, annotation, "GROUP_SEQUENCE_PROVIDER_ANNOTATION_VALUE_CLASS_MUST_HAVE_DEFAULT_CONSTRUCTOR", valueType)); } } TypeMirror genericProviderType = retrieveGenericProviderType(valueType); if (!typeUtils.isSubtype(element.asType(), genericProviderType)) { errors.add( new ConstraintCheckError( element, annotation, "GROUP_SEQUENCE_PROVIDER_ANNOTATION_VALUE_DEFINED_PROVIDER_CLASS_WITH_WRONG_TYPE", genericProviderType, element.asType())); } return errors; }
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 void processMethod( String name, ExecutableElement getterElt, ExecutableElement setterElt, ExecutableElement adderElt) { PropertyKind propKind = null; TypeInfo propType = null; TypeMirror propTypeMirror = null; // if (setterElt != null) { VariableElement paramElt = setterElt.getParameters().get(0); propTypeMirror = paramElt.asType(); propType = typeFactory.create(propTypeMirror); propKind = PropertyKind.forType(propType.getKind()); switch (propKind) { case LIST: case SET: propType = ((ParameterizedTypeInfo) propType).getArgs().get(0); propTypeMirror = ((DeclaredType) propTypeMirror).getTypeArguments().get(0); break; case MAP: propType = ((ParameterizedTypeInfo) propType).getArgs().get(1); propTypeMirror = ((DeclaredType) propTypeMirror).getTypeArguments().get(1); break; } } // if (getterElt != null) { TypeMirror getterTypeMirror = getterElt.getReturnType(); TypeInfo getterType = typeFactory.create(getterTypeMirror); PropertyKind getterKind = PropertyKind.forType(getterType.getKind()); switch (getterKind) { case LIST: case SET: getterType = ((ParameterizedTypeInfo) getterType).getArgs().get(0); getterTypeMirror = ((DeclaredType) getterTypeMirror).getTypeArguments().get(0); break; case MAP: getterType = ((ParameterizedTypeInfo) getterType).getArgs().get(1); getterTypeMirror = ((DeclaredType) getterTypeMirror).getTypeArguments().get(1); break; } if (propType != null) { if (propKind != getterKind) { throw new GenException( getterElt, name + " getter " + getterKind + " does not match the setter " + propKind); } if (!getterType.equals(propType)) { throw new GenException( getterElt, name + " getter type " + getterType + " does not match the setter type " + propType); } } else { propTypeMirror = getterTypeMirror; propType = getterType; propKind = getterKind; } } // if (adderElt != null) { switch (adderElt.getParameters().size()) { case 1: { VariableElement paramElt = adderElt.getParameters().get(0); TypeMirror adderTypeMirror = paramElt.asType(); TypeInfo adderType = typeFactory.create(adderTypeMirror); if (propTypeMirror != null) { if (propKind != PropertyKind.LIST && propKind != PropertyKind.SET) { throw new GenException( adderElt, name + "adder does not correspond to non list/set"); } if (!adderType.equals(propType)) { throw new GenException( adderElt, name + " adder type " + adderType + " does not match the property type " + propType); } } else { propTypeMirror = adderTypeMirror; propType = adderType; propKind = PropertyKind.LIST; } break; } case 2: { VariableElement paramElt = adderElt.getParameters().get(1); TypeMirror adderTypeMirror = paramElt.asType(); TypeInfo adderType = typeFactory.create(adderTypeMirror); if (propTypeMirror != null) { if (propKind != PropertyKind.MAP) { throw new GenException(adderElt, name + "adder does not correspond to non map"); } if (!adderType.equals(propType)) { throw new GenException( adderElt, name + " adder type " + adderType + " does not match the property type " + propType); } } else { propTypeMirror = adderTypeMirror; propType = adderType; propKind = PropertyKind.MAP; } break; } } } // boolean jsonifiable; switch (propType.getKind()) { case OBJECT: if (propKind == PropertyKind.VALUE) { return; } case PRIMITIVE: case BOXED_PRIMITIVE: case STRING: case API: case JSON_OBJECT: case JSON_ARRAY: case ENUM: jsonifiable = true; break; case DATA_OBJECT: Element propTypeElt = typeUtils.asElement(propTypeMirror); jsonifiable = propTypeElt.getAnnotation(DataObject.class) == null || Helper.isJsonifiable(elementUtils, typeUtils, (TypeElement) propTypeElt); break; default: return; } boolean declared = false; Doc doc = null; for (ExecutableElement methodElt : Arrays.asList(setterElt, adderElt, getterElt)) { if (methodElt != null) { // A stream that list all overriden methods from super types // the boolean control whether or not we want to filter only annotated // data objects Function<Boolean, Stream<ExecutableElement>> overridenMeths = (annotated) -> { Set<DeclaredType> ancestorTypes = Helper.resolveAncestorTypes(modelElt, true, true); return ancestorTypes .stream() .map(DeclaredType::asElement) .filter(elt -> !annotated || elt.getAnnotation(DataObject.class) != null) .flatMap(Helper.cast(TypeElement.class)) .flatMap(elt -> elementUtils.getAllMembers(elt).stream()) .flatMap(Helper.instanceOf(ExecutableElement.class)) .filter( executableElt -> executableElt.getKind() == ElementKind.METHOD && elementUtils.overrides(methodElt, executableElt, modelElt)); }; // if (doc == null) { doc = docFactory.createDoc(methodElt); if (doc == null) { Optional<Doc> first = overridenMeths .apply(false) .map(docFactory::createDoc) .filter(d -> d != null) .findFirst(); doc = first.orElse(null); } } // if (!declared) { Element ownerElt = methodElt.getEnclosingElement(); if (ownerElt.equals(modelElt)) { Object[] arr = overridenMeths .apply(true) .limit(1) .filter(elt -> !elt.getModifiers().contains(Modifier.ABSTRACT)) .toArray(); // Handle the case where this methods overrides from another data object declared = arr.length == 0; } else { declared = ownerElt.getAnnotation(DataObject.class) == null; } } } } PropertyInfo property = new PropertyInfo( declared, name, doc, propType, setterElt != null ? setterElt.getSimpleName().toString() : null, adderElt != null ? adderElt.getSimpleName().toString() : null, getterElt != null ? getterElt.getSimpleName().toString() : null, propKind, jsonifiable); propertyMap.put(property.name, property); }