Java Suppress Specific Annotation

Context: I want to create an annotation that generates a compile-time error in production, which is suppressed in development. This annotation will be used to mock the code to make sure it never gets from developer to production.

I created Annotation and Annotation Processor as a Maven project.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com</groupId>
    <artifactId>Fail-Annotation</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArgument>-proc:none</compilerArgument>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

      

Currently, the annotation itself is pretty trivial

package com.fail.annotation;

public @interface Fail
{

    String value();
}

      

The processor is similarly simple

package com.fail.annotation;

import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;

@SupportedAnnotationTypes({"com.fail.annotation.Fail"})
public class FailProcessor extends AbstractProcessor
{

    @Override
    public boolean process(Set<? extends TypeElement> set, RoundEnvironment re)
    {
        for (Element e : re.getElementsAnnotatedWith(Fail.class))
        {
            processingEnv.getMessager().printMessage(
                    Diagnostic.Kind.ERROR,
                    "Forced Fail: " + e.getAnnotation(Fail.class).value(),
                    e
            );
        }
        return true;
    }
}

      

When included as a dependency in a new project, the annotation works flawlessly.

package com.faildemo;

import com.fail.annotation.Fail;

public class FailHere
{

    @Fail("Mock Data")
    int userid = 1;
}

      

I can change the pom.xml in the demo project to disable annotation processors (-proc: none), but that won't work if we want to use other annotation processors in our project.

We can manually specify which processors to use with the maven annotation annotation config field, but then we have to manually code each one, which will be tedious and error prone in a large project, especially with multiple profiles.

TL; DR: Is there a way, in maven and / or javac arguments, to globally suppress a particular annotation or annotation handler?

+3


source to share





All Articles