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

com.github.taymindis.test.SetterProcessor Maven / Gradle / Ivy

Go to download

Page as a service for JSP to standardize the function by jsp page, guarantee zero downtime

The newest version!
package com.github.taymindis.test;

import com.github.taymindis.test.annotation.TestSetter;

import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;

@SupportedAnnotationTypes({"com.github.taymindis.test.annotation.TestSetter"})
@SupportedSourceVersion(SourceVersion.RELEASE_7)
public class SetterProcessor extends AbstractProcessor {

    private Messager messager;

    @Override
    public boolean process(Set annotations, RoundEnvironment roundEnv) {
        // get elements annotated with the @Setter annotation
        Set annotatedElements = roundEnv.getElementsAnnotatedWith(TestSetter.class);

        for (Element element : annotatedElements) {
            if (element.getKind() == ElementKind.METHOD) {
                // only handle methods as targets
                checkMethod((ExecutableElement) element);
            }
        }

        // don't claim annotations to allow other processors to process them
        return false;
    }

    private void checkMethod(ExecutableElement method) {
        // check for valid name
        String name = method.getSimpleName().toString();
        if (!name.startsWith("set")) {
            printError(method, "setter name must start with \"set\"");
        } else if (name.length() == 3) {
            printError(method, "the method name must contain more than just \"set\"");
        } else if (Character.isLowerCase(name.charAt(3))) {
            if (method.getParameters().size() != 1) {
                printError(method, "character following \"set\" must be upper case");
            }
        }

        // check, if setter is public
        if (!method.getModifiers().contains(Modifier.PUBLIC)) {
            printError(method, "setter must be public");
        }

        // check, if method is static
        if (method.getModifiers().contains(Modifier.STATIC)) {
            printError(method, "setter must not be static");
        }
    }

    private void printError(Element element, String message) {
        messager.printMessage(Diagnostic.Kind.ERROR, message, element);
    }

    @Override
    public void init(ProcessingEnvironment processingEnvironment) {
        super.init(processingEnvironment);

        // get messager for printing errors
        messager = processingEnvironment.getMessager();
    }
//    @Override
//    public Set getSupportedAnnotationTypes() {
//        // Return the set of annotations supported
//    }
//    @Override
//    public SourceVersion getSupportedSourceVersion() {
//        // Return the Java version supported
//    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy