Search in sources :

Example 1 with TypeConverter

use of com.bluelinelabs.logansquare.typeconverters.TypeConverter in project LoganSquare by bluelinelabs.

the class JsonFieldProcessor method ensureTypeConverterClassValid.

private boolean ensureTypeConverterClassValid(TypeMirror typeConverterClassMirror, Elements elements, Types types) {
    TypeElement typeConverterElement = elements.getTypeElement(typeConverterClassMirror.toString());
    if (typeConverterElement != null) {
        boolean isTypeConverterType = false;
        TypeElement element = typeConverterElement;
        while (!isTypeConverterType && element != null) {
            for (TypeMirror iface : element.getInterfaces()) {
                if (types.erasure(iface).toString().equals(TypeConverter.class.getCanonicalName())) {
                    isTypeConverterType = true;
                }
            }
            TypeMirror superClassMirror = element.getSuperclass();
            if (superClassMirror != null) {
                superClassMirror = types.erasure(superClassMirror);
                element = elements.getTypeElement(superClassMirror.toString());
            } else {
                element = null;
            }
        }
        if (!isTypeConverterType) {
            error(element, "TypeConverter elements must implement the TypeConverter interface or extend from one of the convenience helpers (ie StringBasedTypeConverter or DateTypeConverter).");
            return false;
        }
        boolean constructorIsDeclared = false;
        boolean hasAccessibleConstructor = false;
        List<? extends Element> enclosedElements = typeConverterElement.getEnclosedElements();
        for (Element enclosedElement : enclosedElements) {
            ElementKind enclosedElementKind = enclosedElement.getKind();
            if (enclosedElementKind == ElementKind.CONSTRUCTOR) {
                constructorIsDeclared = true;
                if (!enclosedElement.getModifiers().contains(Modifier.PRIVATE)) {
                    ExecutableElement executableElement = (ExecutableElement) enclosedElement;
                    if (executableElement.getParameters().size() == 0) {
                        hasAccessibleConstructor = true;
                    }
                }
            }
        }
        if (constructorIsDeclared && !hasAccessibleConstructor) {
            error(element, "TypeConverter classes must have a non-private zero argument constructor.");
            return false;
        }
    }
    return true;
}
Also used : TypeConverter(com.bluelinelabs.logansquare.typeconverters.TypeConverter) ElementKind(javax.lang.model.element.ElementKind) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement)

Aggregations

TypeConverter (com.bluelinelabs.logansquare.typeconverters.TypeConverter)1 Element (javax.lang.model.element.Element)1 ElementKind (javax.lang.model.element.ElementKind)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeElement (javax.lang.model.element.TypeElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1