Design Time Inspection and Suggestions for Annotations

Answered

I need help trying to write an Annotation for empty methods, interfaces, classes, etc.

I am trying to make this Annotation (@Empty) such that it has an optional message parameter.

The following is what I have written.

/**
* An {@link java.lang.annotation.Annotation} for Functions that are Empty by Default
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PARAMETER, TYPE, TYPE_USE})
public @interface Empty {

/**
* Displays Message at Compile Time
* @return A Message Regarding an Empty Definition
*/
String msg() default "";
}

I believe I have this much correct.

Secondly, I want to setup a design time inspection to suggest @Empty for all methods, classes, etc. that have empty definition bodies.

Thirdly, I want to setup a design time inspection to warn the developer when they use something that has been annotated with @Empty.

Fourthly, I want the @Override annotation to suppress the aforementioned warning.

 

In use it should work like this.

1) Write Empty Method

...|
5 |
6 | public class MyClass {
7 |
8 | public void foo() {}
9 |
10 | }
...|

2) Suggest @Empty Annotation

...|
5 |
6 | public class MyClass {
7 |
8 @| public void foo() {}
9 |
10 | }
...

3) Approve Suggestion

...|
5 |
6 | public class MyClass {
7 |
8 | @Empty
9 | public void foo() {}
10 |
11 | }
...|

4) Warn on Non-Overridden Usage

...|
21 |
22 | public class MyMain {
23 |
24 | public static void main(String[] args) {
25 | MyClass bar = new MyClass();
⚠| bar.foo();
27 | }
28 |
29 | }
30 |
...|

5) Override Method

...|
4 |
5 | MySubClass extends MyClass {
6 |
7 | @Override
8 | public void foo() {
9 | System.out.println("Hello World");
10 | }
11 |
12 | }
13 |
...|

6) Suppress Usage Warning on Overriden Usages

...|
21 |
22 | public class MyMain {
23 |
24 | public static void main(String[] args) {
25 | MySubClass baz = new MyClass();
26 | baz.foo();
27 | }
28 |
29 | }
30 |
...|

Does anybody know how to do something like this? If so, I would love to learn and get in touch at drewmerit.personal@gmail.com

0
4 comments

Hi Drewmerit Personal , could you please elaborate on your question? 

design time inspection

Is it aborting the IDEA function, or are you developing an IDEA plugin or something else? Any similar product screenshot is welcome.

0

Jacky Liu 

I am trying to create an inspection in IntelliJ for this @Empty Annotation. I am just developing a Maven project but would like to start including this Annotation and the aforementioned functionality for it in this project and future projects and I am unsure of how to approach that.

0

Sorry for the late reply, what's the design time mean? I know that there is something called Annotation Processor as described here: https://reflectoring.io/java-annotation-processing/ , but it seems that you want these suggestions to happen in the IDEA code editor, right?

So far I know the only way might be writting a IDEA plugin: https://plugins.jetbrains.com/docs/intellij/code-inspections-and-intentions.html 

If you have similiar functions in IDEA or other products, please provide some reference or screenshot.

 

 

0

Jacky Liu 

Yes that is exactly what I am looking for. Design Time is the term that I have seen the official JetBrains description use in describing their annotations processing, meaning while writing code in the editor before compilation. I will look into writing an IDEA plugin at another time because that seems like the best solution to my problem.

0

Please sign in to leave a comment.