defaultj.annotations.processor.PostConstructoAnnotationValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of defailtj-annotations Show documentation
Show all versions of defailtj-annotations Show documentation
The module for DefaultJ Annotations.
// MIT License
//
// Copyright (c) 2017-2023 Nawa Manusitthipol
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package defaultj.annotations.processor;
import static java.lang.String.format;
import static javax.lang.model.element.ElementKind.METHOD;
import java.util.LinkedHashSet;
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.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import defaultj.annotations.PostConstruct;
/**
* This annotation process ensures that {@link PostConstruct} is only annotated to a method that take to parameter.
*
* @author NawaMan -- [email protected]
*/
public class PostConstructoAnnotationValidator extends AbstractProcessor {
private static final String POST_CONSTRCT = PostConstruct.class.getSimpleName();
private Messager messager;
private boolean hasError;
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
messager = processingEnv.getMessager();
}
@Override
public Set getSupportedAnnotationTypes() {
Set annotations = new LinkedHashSet();
annotations.add(PostConstruct.class.getCanonicalName());
return annotations;
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
private void error(Element e, String msg) {
hasError = true;
messager.printMessage(Diagnostic.Kind.ERROR, msg, e);
}
@Override
public boolean process(Set extends TypeElement> annotations, RoundEnvironment roundEnv) {
hasError = false;
for (Element element : roundEnv.getElementsAnnotatedWith(PostConstruct.class)) {
if (METHOD.equals(element.getKind())) {
if (0 == ((ExecutableElement)element).getParameters().size())
continue;
error(element, format("Only methods with no parameter can be annotated with @%s!", POST_CONSTRCT));
} else {
error(element, format("Only methods can be annotated with @%s!", POST_CONSTRCT));
}
}
return hasError;
}
}