Kann ich Java-Annotationen verwenden, um Kompilierungszeitprüfungen zu definieren?

Ich wollte zum Beispiel die Annotation @Out für Zielparameter erstellen. Dann würde ich irgendwie den Compiler verwenden, um zu überprüfen, ob der Parameterwert gesetzt ist, bevor die Funktion zurückkehrt. Ist das möglich

Auch dachte an eine @Immutable-Annotation, mit der keine mit @Const nicht annotierte Methode aufgerufen werden oder auf öffentliche Felder zugegriffen werden konnte. (Kompilierzeit und wahrscheinlich Laufzeit?)

So weit ich das habe:

//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.
}

dies ist die andere Anmerkung. Auch hier habe ich keine vollständige Definition:

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

}

Ich denke, ich kann anfangen, eine Strategie zu entwickeln, um diese zur Laufzeit mithilfe von Reflection zu implementieren, aber ich wollte den Compiler oder Pre-Prozessor anweisen, das Zeug stattdessen für mich zu überprüfen, damit meine Annotationen keinen Overhead haben.

Dies ist eines der Dinge, von denen du denkst, dass es "wenn dies hätte getan werden können, wäre es bereits da draußen und wenn ja, wo kann ich es finden".

Bearbeite: Nach weiteren Überlegungen zu@Const und@Immutable und nachdem ich mich daran erinnert habe, dass Java Zeiger nach Wert an Objekte übergibt, habe ich die Definition von @ erweiter@Const, losgeworden@Immutable und änderte die Definition von@Out, wie folgt unten:

/**
* 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
{

}

jetzt die@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;
}

Bearbeiten Ich versuche, dieses Plugin als Eclipse-Plugin zu implementieren, habe aber das Handbuch nicht mehr gelesen. Ich habe ein Plugin mit der grundlegenden Logik für den Zugriff auf das AST und den Besuch von Methoden und Feldern geschrieben. Ich habe dann ein paar Dummy-Annotationen gemacht, die mein Plugin erkennen sollte, und dann habe ich versucht, die Ergebnisse auszudrucken, bin mir aber nicht einmal sicher, was mich erwartet. Mein Plugin ist ein "Incremental Build" -Plugin. Hier ist der Code dafür, wenn jemand einen Blick darauf werfen und mir nur ein paar Dinge erklären könnte. Ich habe mich in dieser API völlig verlaufen.

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

Antworten auf die Frage(2)

Ihre Antwort auf die Frage