Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use Java annotations to define compile time checks?

For example, I wanted to create the annotation @Out to target parameters. Then I would somehow use the compiler to check if the parameter value is set before the function returns. Is this possible?

Also was thinking about a @Immutable annotation that would not allow any method not annotaded with @Const to be invoked or access to any public fields. (compile time and probably runtime?)

So far I have this:

//I'm assuming Class retention is a subset of Runtime retention
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Out
{
    //no idea what to go in here.
}

this is the other annotation. again, I have no complete definition for it:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Immutable
{

}

I think I can begin to devise a strategy to implement that at runtime using reflection, but I wanted to instruct the compiler or pre-processor to check that stuff for me instead, so my annotations would have zero overhead.

This is one of those things that you think "if this could've been done, it would already be out there, and if it is, where can I grab it".

Edit: After further thought about @Const and @Immutable and after remembering java passes pointers to objects by value, I expanded the definition of @Const, got rid of @Immutable, and altered the definition of @Out, as follows bellow:

/**
* When Applied to a method, ensures the method doesn't change in any
* way the state of the object used to invoke it, i.e., all the fields
* of the object must remain the same, and no field may be returned,
* unless the field itself is marked as {@code @Const}. A method 
* annotated with {@code @Const} can only invoke other {@code @Const}
* methods of its class, can only use the class's fields to invoke
* {@code @Const} methods of the fields classes and can only pass fields
* as parameters to methods that annotate that formal parameter as
* {@code @Const}.
*
* When applied to a formal parameter, ensures the method will not
* modify the value referenced by the formal parameter. A formal   
* parameter annotated as {@code @Const} will not be aliased inside the
* body of the method. The method is not allowed to invoke another 
* method and pass the annotated parameter, save if the other method 
* also annotates the formal parameter as {@code @Const}. The method is 
* not allowed to use the parameter to invoke any of its type's methods,
* unless the method being invoked is also annotated as {@code @Const}
* 
* When applied to a field, ensures the field cannot be aliased and that
* no code can alter the state of that field, either from inside the   
* class that owns the field or from outside it. Any constructor in any
* derived class is allowed to set the value of the field and invoke any
* methods using it. As for methods, only those annotated as
* {@code @Const} may be invoked using the field. The field may only be
* passed as a parameter to a method if the method annotates the 
* corresponding formal parameter as {@code @Const}
* 
* When applied to a local variable, ensures neither the block where the
* variable is declared or any nested block will alter the value of that 
* local variable. The local variable may be defined only once, at any
* point where it is in scope. Only methods annotated as
* {@code @Const} may be invoked using this variable, and the variable 
* may only be passed as a parameter to another method if said method
* annotates its corresponding formal parameter as {@code @Const}
*
*/
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD,
ElementType.LOCAL_VARIABLE})
@Inherited
public @interface Const
{

}

now the @Out:

/**
* The formal parameter annotated with {@code @Out} must be undefined in 
* the scope of the caller, and it's the responsibility of the method to
* define it. If allowNull is true, the parameter can be explicitly set
* to null in the body of the method.
*/
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.PARAMETER)
public @interface Out
{
    boolean allowNull() default false;
}

Edit: I'm trying to implement this as an eclipse plugin, but I'm completely lost reading the manual. I wrote a plugin with the basic logic for accessing the AST and visiting methods and fields. I then made a bunch of dummy annotations that my plugin should detect, then I try to print the results, but I'm not even sure what to expect. My plugin is a "Incremental Build" plugin. Here's the code for it, If someone could take a look and just explain a few things to me. I'm completely lost in this API.

https://github.com/Starless2001/Plugin-for-Eclipse

like image 827
FinnTheHuman Avatar asked Apr 11 '16 00:04

FinnTheHuman


People also ask

How do you create compile time annotations?

go to Project Properties > Build > Compiling. add check marks for Enable Annotation Processing and Enable Annotation Processing in Editor. click Add next to the annotation processor list. in the popup that appears enter the fully qualified class name of the annotation processor and click Ok .

What is the use of annotations in Java?

Annotations are used to provide supplemental information about a program. Annotations start with '@'. Annotations do not change the action of a compiled program. Annotations help to associate metadata (information) to the program elements i.e. instance variables, constructors, methods, classes, etc.

Can classes be annotated in Java?

Annotations can be applied to declarations: declarations of classes, fields, methods, and other program elements.

What is a Java annotation processor?

Annotations provide information to a program at compile time or at runtime based on which the program can take further action. An annotation processor processes these annotations at compile time or runtime to provide functionality such as code generation, error checking, etc.


1 Answers

The javac compiler supports user-definable plugins, called annotation processors, that accomplish exactly what you want. You can think of annotations as language extensions.

The definition public @interface Immutable { ... } defines the syntax: the @Immutable annotation that you can write in your program. The annotation processor (the compiler plug-in) defines the semantics: it enforces the semantic rules and issues compiler warnings when your program violates the rules.

One framework that makes it easy to write such annotation processors is the Checker Framework, and it contains definitions for annotations like @NonNull and @Immutable. Here are two tutorials about how to use the Checker Framework to validate code: tutorial 1, tutorial 2.

Ordinary Java annotation processing is invoked on each declaration, such as classes, fields, methods, and method parameters, and ordinary Java gives the annotation processor no access to the program's full AST. You can think of the Checker Framework as a library that extends the power of Java annotation processing. It gives you access to the full AST of each class, and it lets you define rules for every statement in your program. Thus, your annotation processor can issue warnings when a statement invokes a non-@Const method on an @Immutable object.

Your annotation processor should be modular, working one class at a time. The annotation processor has access to the AST of the current class, plus the signatures, including annotations, of all classes that it uses. Annotation processing gives you that information (but not to the whole project's AST all at once).

like image 72
mernst Avatar answered Oct 22 '22 22:10

mernst