History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: IDEABKL-5291
Type: New Feature New Feature
Status: Open Open
Priority: Critical Critical
Assignee: Alexey Kudravtsev
Reporter: Derek Foster
Votes: 1
Watchers: 4
Available Workflow Actions

Mark as Stalled
Operations

If you were logged in you would be able to see more operations.
IDEA: Backlog

Want @PrintFormat, @RegExp, @Pattern parameter annotations to enable Idea error highlighting

Created: 29 Sep 05 23:03   Updated: 28 Sep 07 20:13
Component/s: Code Analysis. Inspection, Editor. Error Highlighting
Affects Version/s: None
Fix Version/s: None

Original Estimate: Unknown Remaining Estimate: Unknown Time Spent: Unknown

Build: 3,487
Severity: Medium


 Description  « Hide
I would like to be able to annotate methods like so:

void myNiftyFunction( int foo, @PrintFormat(args) String format, int bar, @RegExp String reg, @Pattern("a.*b.*c") String pat, Object args... )

{ Pattern.compile(reg); System.out.println(String.format(format, args)); }

so that Idea's edit-time detection of malformed printf format strings and malformed regular expressions can be applied to my own code. Basically, I want to be able to annotate my parameters, and have Idea check them for validity.

Also note the addition of the @Pattern annotation above to allow me to specify a regular expression which a particular string is required to match.

It would also be nice to be able to create my own annotations similar to @Pattern (perhaps extending a standard com.jetbrains base annotation), each of which forced strings to match a specific regular expression. That way, I could define @Identifier to match identifiers, @Lowercase to match all-lowercase words, and so forth.)

If Idea supported annotations like the above, it would be able to detect, during editing, invalid method calls like the following:

myNiftyFunction(5, "This is wrong %s %s %s", 42, "", "abc", firstArg, secondArg); // not enough printf arguments
myNiftyFunction(5, "This is wrong %s", 42, "", "abc", firstArg, secondArg, thirdArg); // too many printf arguments
myNiftyFunction(5, "This is wrong %s", 42, "", "abc", new Object[]{firstArg, secondArg, thirdArg}); // too many printf arguments
myNiftyFunction(12, "This is ok", 42, "bad regexp (", "abc", ); // malformed regular expression
myNiftyFunction(12, "This is ok", 42, "ok regexp", "abd" ); // value "abd" for parameter 'pat' does not match regular expression 'a.*b.*c'
myNiftyFunction(12, "This is ok", 42, "ok regexp", "abc" ); // no problems detected.

Currently, Idea performs these sorts of checks for JDK functions only (like String.format(), Pattern.compile(), and so forth). I'd like to be able to use annotations to cause Idea to check my own methods for correctness as well.



 All   Comments   Work Log   Change History      Sort Order:
Keith Lea - 29 Sep 05 23:12
@PrintFormat(args) is not valid Java code. I suggest @PrintFormat and @PrintArgs. Also, annotations cannot be extended.

I think a better solution to @RegExp would be a @Language("jdkre") or something, which could allow language support in the editor for other things like "mysql" "javascript" etc.


Derek Foster - 07 Oct 05 01:49
Seems reasonable, but do I hate having cryptic things like "jdkre" in my code. I'd prefer a better name for referring to the standard regular expression language.

I'd prefer to be somehow able to create my own annotations, each associated with a different language (presumably, as specified by a regular expression). This association could be done with Java Source code somehow, or by cooperation with some sort of Options settings in the IDE, although I'd prefer to have the IDE just read the Java source of the annotation definitions and be able to configure itself automatically. (This facilitates code sharing between different projects, and use of the annotations with tools other than Idea).


Keith Lea - 07 Oct 05 05:17
Yeah, I think you got it. It makes way more sense to define annotations like this:

@LanguageAnnotation("jdkre")
public @interface Regexp {
}

Then have IDEA understand @Regexp on a value, mapping to the "jdkre" language.


Derek Foster - 07 Oct 05 12:07
I like this approach. Sounds great, for predefined languages, like mysql, jdk regexp, SimpleDateFormat, LispExpression, etc. Perhaps Idea could allow plugins for adding parsers that could add new predefined languages.

I also, however, want to also be able to easily parse my own simple languages, without going to the trouble of writing a plugin. For most purposes, simply specifying a regular expression would be sufficient. (If I need more than that, I can use a plugin). Perhaps something like:

@LanguageRegexp("[a-zA-Z_][a-zA-Z0-9_]*")
public @interface Identifier {
}
@LanguageRegexp("\\d+(,
d+)*")
public @interface CommaSeparatedList {
}

and so forth.

(Of course, the coolest of all would be if I could automatically inject bytecode at compile time to test these expressions at runtime, for debugging purposes... not that I really expect this to be implemented, but hey, I can dream, can't I? )


Derek Foster - 27 Oct 05 05:19
Another thing that has occurred to me recently is that it would be nice if these annotations would work on variable declarations too. So I could, for instance, write:

public @Regexp String foo;
...

foo = "("; // Warning from Idea: Illegal regular expression '(' assigned to @RegExp-annotated variable.

Perhaps it could even provide an optional warning to let me know if I attempt to pass a non-@Regexp variable to a function parameter that is annotated as a @Regexp.


Sascha Weinreuter - 03 Oct 06 16:32
Most of this is implemented by the IntelliLang plugin available here: http://plugins.intellij.net/plugin/?id=1105

Derek Foster - 03 Oct 06 22:59
Wow. Just... wow. That's a far nicer solution than I envisioned when I posted this feature request.

I haven't had a chance to try it yet (I probably won't for a week or two), but from the description, it looks great! Especially the part about instrumenting the compiled classes with runtime assertions, which goes way beyond anything I had expected to be able to do.

I'm very, very impressed! Thank you for all your hard work!

Are there any plans to make this plugin ship as a default part of Idea?


Sascha Weinreuter - 15 Oct 06 20:15
Derek, sorry for the delayed response. Did you have a chance yet to try the plugin? I'm very interested in your opinion and/or further suggestions.

Thanks,
Sascha

PS:

Are there any plans to make this plugin ship as a default part of Idea?

This can only be answered by JetBrains