All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.malinskiy.sheldon.codegen.validator.SetValidator Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
package com.malinskiy.sheldon.codegen.validator;

import com.malinskiy.sheldon.annotation.Set;
import com.malinskiy.sheldon.codegen.ProcessingException;
import com.malinskiy.sheldon.codegen.Utils;
import com.malinskiy.sheldon.codegen.model.DefaultValue;

import java.util.List;
import java.util.Map;

import javax.annotation.Nonnull;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;

public class SetValidator {
    public static void checkValidSetter(@Nonnull ExecutableElement method, Map defaultsMap) throws ProcessingException {
        if (method.getAnnotation(Set.class) == null) {
            throw new ProcessingException(
                    method,
                    "No annotation @Set present. Did you annotate your method?"
            );
        }

        TypeMirror returnType = method.getReturnType();
        if (!returnType.getKind().equals(TypeKind.VOID)) {
            throw new ProcessingException(method,
                    "Invalid return type @%s for setter. Should be void",
                    returnType.toString());
        }

        List parameters = method.getParameters();
        if (parameters.size() != 1) {
            throw new ProcessingException(method,
                    "Invalid number of parameters for setter. Should be only one parameter");
        }

        if (!defaultsMap.containsKey(Utils.getName(method))) {
            throw new ProcessingException(
                    method,
                    "No default value found"
            );
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy