You can do this as something below, which allows you to fulfill 3) and 4).
Example from java annotation processor example
@SupportedAnnotationTypes( "com.javacodegeeks.advanced.processor.Immutable" ) @SupportedSourceVersion( SourceVersion.RELEASE_7 ) public class SimpleAnnotationProcessor extends AbstractProcessor { @Override public boolean process(final Set< ? extends TypeElement > annotations, final RoundEnvironment roundEnv) { for( final Element element: roundEnv.getElementsAnnotatedWith( Immutable.class ) ) { if( element instanceof TypeElement ) { final TypeElement typeElement = ( TypeElement )element; for( final Element eclosedElement: typeElement.getEnclosedElements() ) { if( eclosedElement instanceof VariableElement ) { final VariableElement variableElement = ( VariableElement )eclosedElement; if( !variableElement.getModifiers().contains( Modifier.FINAL ) ) { processingEnv.getMessager().printMessage( Diagnostic.Kind.ERROR, String.format( "Class '%s' is annotated as @Immutable, but field '%s' is not declared as final", typeElement.getSimpleName(), variableElement.getSimpleName() ) ); } } } }
Another way to use projectlombok with a custom handler.
An example of a built-in handler from the GitHub Project Lombok. This annotation adds a catch catch block.
public class SneakyThrowsExample implements Runnable { @SneakyThrows(UnsupportedEncodingException.class) public String utf8ToString(byte[] bytes) { return new String(bytes, "UTF-8"); } @SneakyThrows public void run() { throw new Throwable(); } }
It is being processed
public String utf8ToString(byte[] bytes) { try { return new String(bytes, "UTF-8"); } catch (UnsupportedEncodingException e) { throw Lombok.sneakyThrow(e); } } public void run() { try { throw new Throwable(); } catch (Throwable t) { throw Lombok.sneakyThrow(t); } }
You can find the handler code on the same Github / lombok site.
bhantol
source share