Posts Tagged ‘Code Analysis’

Java 8 and Method References in IntelliJ IDEA 12

Monday, October 22nd, 2012

As you might know IntelliJ IDEA 12 will bring support for new features of Java 8 language. Though this is still in progress, you can try some of these features now in IntelliJ IDEA 12 EAP.

Here we would like to provide some details on method and constructor refereces support.

IntelliJ IDEA as always provides a way to transform existing code to use new features. Dedicated inspection finds and highlights anonymous types which can be replaced with a method reference.

After you apply provided quick-fix, it gets just a line of code.

There is also another inspection that checks if a lambda expression in your code can be replaced with a method reference.

You can also use an intention  to replace a method reference with a lambda expression, in the case when you need additional logic.

All the common features such as completion, refactorings, formatting and many other were updated to support method and constructor references.

As always you are very welcome with suggestions and feedback in our issue tracker.

Develop with Pleasure!

Validation of unknown tags and attributes in Android XML files

Monday, March 12th, 2012

In the latest IntelliJ IDEA 11.1 EAP we’ve added 2 new inspections that warn you about unknown attributes and tags in Android XML resource files and AndroidManifest.xml:

IntelliJ IDEA can validate both system attributes and local ones defined in attrs.xml file in your project. Of course, you can disable or tune these inspections according to your needs in the IDE settings.

Integration with Android Lint tool in IntelliJ IDEA 11.1

Thursday, February 23rd, 2012

In IntelliJ IDEA 11.1 we’ve added many inspections based on Android Lint tool, which reports many kinds of potential problems in the sources of your Android project.

For example IntelliJ IDEA will notify you about useless views in layout xml files and will offer to remove it:

Another example is that IDE warns you about hardcoded text and offer to extract a new string resource:

Some issues (such as duplicated icons or incomplete translations) aren’t shown in the editor, but reported when you run inspections for your project. Simply launch Analyze | Inspect Code from the main menu:

You can see all the Lint based inspections and tune them in Settings | Inspections | Android | Lint.

The feature is available to try it in the latest IntelliJ IDEA 11.1 EAP.

New “Magic Constant” Inspection

Wednesday, February 15th, 2012

Have you ever cursed an API which uses magic integer constants instead of the proper enumeration?

What should I pass as the titleJustification parameter here? 0 or 1?
Can I pass “42″ for the titlePosition parameter? Even Javadoc won’t know.
Actually, you must use one of the constants in the TitledBorder class to make the code correct:

I call these constants “magic” because sometimes it is far from easy to figure them out.

Here comes a new “Magic constant” inspection that looks for usages of methods/variables which expect “magic constants” as their arguments:

Then it helps you to replace plain numbers or strings with a proper magic constant:


(more…)

How to check your RegExps in IntelliJ IDEA 11?

Friday, November 4th, 2011

In IntelliJ IDEA 11 you can check your Regular Expressions while coding without leaving the IDE. Just invoke the ‘Check RegExp’ intention action on a regular expression and play!

Tip: You can turn any string into a regular expression by injecting RegExp language. Try the ‘Inject Language’ intention action.

Better Spring with IntelliJ IDEA 10.5

Monday, May 9th, 2011

Yes! The sunny Spring has finally come to St.Petersburg too. However, it’s not that “Spring” we want to talk now…

We realize that many of you use the Spring framework in your every day work. So, many should be interested in the new Spring-related features that can be found in the upcoming IntelliJ IDEA 10.5 release.

1. Better navigation between xml configs and annotated stereotype components

2. Advanced usage search for @Autowired beans

3. More inspections for your configs, for instance, deprecated classes and members highlighting.

4. More powerful placeholder support. All spring model inspections get and analyze placeholder values before highlighting.

5. More clear Bean Dependencies Graph view. For instance, it obtains beans and dependencies from custom namespaces. Check out this spring integration schemas example.

There are also smaller changes here and there…

Download IntelliJ IDEA 10.5 RC to try it and enjoy the improved Spring framework support.

Quick prototyping in Groovy with “Convert Map to Class” intention

Tuesday, April 19th, 2011

Sometimes it is helpful to use a Map for fast prototyping of classes with some properties but without any logic. But in the end it would be nice to have a real class. In IntelliJ IDEA you can get it by invoking “Convert Map to Class” intention on a map.

You only need to type a name and a package for a new class:

And you’ll get something like this…

and a UserInfo class:

Quick and easy, right?

It is not something new, it’s been in IntelliJ IDEA for a while already. We just felt you might not be aware of it.

Java 7. @SafeVarargs

Friday, April 8th, 2011

Java 7 provides a way to remove a compiler warning about generics vararg invocation. With Java 7 you can annotate your vararg method with SafeVararg annotation and your clients won’t get these nasty warnings any more. IntelliJ IDEA 10.5 will help you to perform this migration. It will find for you all the places where @SafeVarargs annotation is applicable and suggest you to add this annotation. Of course, it will also check if existing @SafeVarargs annotations are applicable.

Suppose you have annotated your method with @SafeVararg but already have suppressions for unchecked warning in client’s code:

Since its version 6 IntelliJ IDEA contains an inspection to find redundant suppressions (available in batch mode only). Now you can use it to remove all suppressions which are garbage in your code. It will carefully check that these unchecked warnings were caused by generics vararg invocation and suggest you to remove all such suppressions. Simply run the ‘Redundant Suppression’ inspection (Ctrl-Alt-Shift-I).

All you need to do is to install Java 7 and setup language level to 7.

Download IntelliJ IDEA 10.5 EAP, try the new described features and let us know what you think.

More flexible and configurable @Nullable/@NotNull annotations

Tuesday, March 29th, 2011

For quite a while IntelliJ IDEA can detect probable NPE’s in your code. But you have to annotate it with JetBrains’ @Nullable/@NotNull. In order to leave your code free from JetBrains’ annotations you could use external annotations, but they were not so convenient to use as they were not visible in the code. With libraries and SDK code, however, there is no other way except using the external annotations.

So, to make things better, 10.5 IntelliJ IDEA will be able to recognize the most common annotations, such as javax @Nullable/@Nonnull, FindBugs @Nullable/@NonNull, as well as your own annotations with the same semantics.

You just need to configure which annotations are allowed in you code and which should be used by quick fixes to annotate the code.

Try it in IntelliJ IDEA 10.5 EAP and let us know what you think.

Auto-infer @Nullable/@NotNull Annotations

Tuesday, December 14th, 2010

For a long time already IntelliJ IDEA can detect probable NPE’s in your code. You need to annotate your methods/fields/variables with @Nullable/@NotNull annotations and IntelliJ IDEA will highlight possible contract violations. Good. But the annotations only begin to work when there is enough of them in the code. You do need to annotate your code to get the benefits.

IntelliJ IDEA 10 can do it for you.

Just run Analyze | Infer Nullity… and choose a scope where you want annotations to be inferred. IntelliJ IDEA uses many rules to analyze nullity and make a decision. Here are just 2 examples:

1) The IDE detects parameters that are used without checking for null:

and presumes that they are @NotNull:

2) Variables that are checked for null are assumed as @Nullable, etc.

As a result you can benefit from static code analysis without paying an entrance fee.