External annotations by default

Answered

"External annotations" looks like a lovely feature but there are some bits which seem to be amiss:

  1. No way to configure IntelliJ Idea so that whenever it adds an annotation, the annotation would be added externally. For example, suppose that an interface has a @Nonnull annotation. IntelliJ notices that the implementation is missing this annotation and suggests to add it. If I agree, it adds it in the code rather than externally. So I have to go to the code and manually push it out to annotations.xml.
  2. There is no way to analyse the code and find cases where annotations are in the code rather than being external.
  3. There is also no way to automatically migrate annotations in the code to be external.
  4. Similarly, there appears to be no way of turning external annotations to be internal.

Am I missing something?

0
5 comments

Hello, please clarify the following points:

1) Can you please give a short code sample where the annotation is suggested to be added and is added internally after fix applying (even though was already added externally somewhere)?

2) Please tell if in 3 and 4 points you speak about annotation migration in batch mode? 

 

Thank you 

0

Hi Olga,

 

OK, suppose external annotations are enabled and the code looks like this:

import javax.annotation.Nonnull;

public class Test {

static class C implements I {
}

interface I {
void test(@Nonnull Object o);
}
}

Naturally, the code cannot be compiled because class "C" does not have an implementation of the method.

So I click on it, press Alt-Enter and select "Implement methods".
That does implement the method and I end up with this:

import javax.annotation.Nonnull;

public class Test {

static class C implements I {
@Override
public void test(@Nonnull Object o) {

}
}

interface I {
void test(@Nonnull Object o);
}
}

As you can see, @Nonnull is in the code, rather that put into an xml file with external annotations.

If I click on that annotation and press Alt-Enter, then I can choose from a pop-up menu "Annotate externally".
Which is OK, but

  • There appears to be no way to reverse it and move the annotation back to the code.
  • The original action which resulted in the method with the annotation in the code seems to be always putting those annotations in the code. There is no way to make it add that annotation as an external one automatically.

In 3 I mean a way of finding all annotations in the code and making them be external in a batch mode.

In 4 I mean any way of moving external annotations back into the code - be it a batch process or doing it manually on a case by case basis. Both seem to be impossible.

 

 

 

0

Hello,

Please see the "Make External Annotations Explicit":

Please also clarify what is the use case when you need to move all the annotations of the project to external and vice versa? 

Also it's not evident that if you have a dependency on @Nonnull in the project, the annotation should be inserted externally. Why do you have a dependency and want to use external annotations?

Thank you

0

OK, my initial understanding of the feature (based on https://www.jetbrains.com/help/idea/external-annotations.html) was that external annotations are essentially equivalent to annotations in the code. If that is the case you would expect them to be easily movable back and forth between the code and the relevant file, both individually and in batches. Just like in that description one may want to keep the code free from annotations by moving them externally.

However, now I think this the feature is meant to work differently. I found this document https://blog.jetbrains.com/using-external-annotations/  which says external annotations can be used to add annotations to the code which one cannot be modified directly. With that explanation in mind it is clear why there is no such feature as moving external annotations back into the code - it is just not necessary.

Back to your questions:

The use case where someone may want to move annotations to external file: just line in the first document, this may be desired as a way of de-cluttering the code.

The use case where someone may want them back in the code: the previous decision has been reversed for whatever reason. For example, someone needs to use an automatic code analysis tool which does not support external annotations and de-cluttering is a lesser concern. Or, perhaps, someone made an annotation be external in error.

In my example everything is self-contained - there is no external dependency. Assuming that the first document is right one would expect that automatically inserted annotations may either go into the code or into an external file, depending on how IDE is configured. However, if one considers the second document, that describes a different situation where an external [compiled] dependency needs an annotation to be added and that annotation cannot go anywhere but an external file as source is not available.

Overall I think the second document is closer to reality. External annotations are not a 100% replacement for in-code ones. I think I'll be avoiding external annotations going forwards except for those special cases where annotations have to be added to a third-party library.

0

Hello, the more common use case of external annotations usage is described in the blog post (https://blog.jetbrains.com/using-external-annotations). Please feel free to file the feature requests to our bug tracker: https://youtrack.jetbrains.com/issues/IDEA

Thank you 

0

Please sign in to leave a comment.