DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • How To Approach Dependency Management in Java [Video]
  • IntelliJ IDEA Switches to JetBrains YouTrack
  • Building Micronaut Microservices Using MicrostarterCLI
  • Is IntelliJ Making Us Stupid? It's Complicated

Trending

  • How to Submit a Post to DZone
  • DZone's Article Submission Guidelines
  • Demystifying Project Loom: A Guide to Lightweight Threads in Java
  • Monkey-Patching in Java
  1. DZone
  2. Coding
  3. Tools
  4. Improvements in IntelliJ IDEA: Refactoring and Warnings

Improvements in IntelliJ IDEA: Refactoring and Warnings

Alongside with major improvements in IntelliJ IDEA 2017.2, there's been a bunch of smaller ones like better warnings or new refactorings.

Trisha Gee user avatar by
Trisha Gee
·
Jul. 31, 17 · News
Like (5)
Save
Tweet
Share
6.90K Views

Join the DZone community and get the full member experience.

Join For Free

Many of the features we’re going to talk about in this post have been mentioned elsewhere, but here we’re going to bring them all together and show how to use them.

Better Warnings

You’ll have seen before that IntelliJ IDEA can analyze your code and tell you if something isn’t needed, or perhaps doesn’t make sense. IntelliJ IDEA 2017.2 has even better analysis in several areas.  The first we’re going to look at is Control Flow Analysis – IntelliJ IDEA can look at the logic in your code and tell you if certain cases just won’t occur.

For example, if a Collection or String is known to be empty, IntelliJ IDEA will tell you that certain types of operations will error or simply aren’t required, like iterating over an empty collection.

Warnings on empty collections

This applies to a number of methods on List, Map and Set, as well as String.

Other warnings on empty collections

There’s also improved analysis around nulls, particularly when passing Collections as parameters.  If, for example, you have a Collection that can contain nulls and you use it when calling a method that expects a Collection that does not contain nulls, you’ll get a warning.

Warnings on nulls

See the blog post on Smarter Flow Control Analysis for more details on the analysis of empty Collections/Strings and potential null problems.

Similarly, IntelliJ IDEA 2017.2 can also detect if a number is going to be out of range and warn you about this before your program errors at run time.

Warnings on unlikely number ranges

See the section on Smarter control flow analysis: number ranges in this blog post for more details.

The Reflection API has always had its risks, which is one of the reasons Java 9 modularity (JPMS) aims to provide better encapsulation. IntelliJ IDEA now has more help for working with reflection if you do need to use it, with coding assistance and added warnings.

Warnings on reflection methods

For more information see the Reflection API section in this blog post.

More Advanced Refactoring

As well as improved warnings that can spot bugs before running the code, IntelliJ IDEA 2017.2 also helps you to refactor existing code to simplify it or to make it more readable. For example, if you have a statement that contains a number of or clauses, this can be converted into a Set.contains statement – new values can then easily be added to the Set, rather than added to a long and difficult to read condition.

Replace with Set.contains

IntelliJ IDEA continues to add refactoring and suggestions to help you use the features in Java 8. Now, if you extract a variable in a Stream operation, the IDE can suggest this is extracted as an additional map operation rather than adding an extra variable to the lambda expression.

Extract variable as map

Previously, you could extract functional parameters, letting you change a method to accept a lambda expression as a parameter.  Now you can also extract functional variables, so you reuse can some of these lambda expressions, or as a stepping stone to a larger refactoring, or maybe simply to create more readable code.

Extract functional variable

The last Java 8 refactoring to mention is the ability to reshape your code to use method references immediately. You may previously have used something like a combination of Extract Method and the “Lambda can be replaced with method reference” inspection in order to achieve this, now you can cut out a number of steps and extract to a method reference directly.

Refactor to method reference

Better Code Completion

Code completion has also been improved in IntelliJ IDEA 2017.2, smart completion can suggest much more than before. For example, if your code uses the classic builder pattern to create an instance (rather than calling new directly), smart completion will suggest using the builder when values of that type are required.

Suggested builder

When you select this option, the caret will automatically be placed into the correct position to set more values via the builder.

For more information see the Smart completion: builder classes section of this blog post.

Smart completion is also better at suggesting appropriate method chains in certain situations – if, for example, you frequently call a chain of methods this is now suggested on the first call to smart completion, whereas it was previously only suggested on the second call.

Chained method calls suggested

This feature relies on the project being indexed, and having been built by IntelliJ IDEA (not the Gradle compiler).

See the Smart Completion blog post for more information on this feature for chained method calls.

Notable Bug Fixes

Of course, releases aren’t just about new features, they’re also about improving the quality of the tool.  Here’s a summary of the most notable bug fixes that went into 2017.2.

  • The project window sometimes didn’t close on Windows IDEA-171010
  • The editor tabs limit didn’t work properly when set to 1 (2017.2.1) IDEA-173369
  • The project window didn’t properly restored in a maximised state on Windows IDEA-96168
  • When re-importing a Maven project, the IDE didn’t respect the language level IDEA-115977
  • The active editor/tool window tab was difficult to distinguish from a non-active IDEA-131683 and IDEA-103206
  • Per-monitor HiDPI scale-factor on Windows IDEA-164566
  • Automatically adjusted font size based on the HiDPI scale factor IDEA-151754
intellij

Published at DZone with permission of Trisha Gee, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How To Approach Dependency Management in Java [Video]
  • IntelliJ IDEA Switches to JetBrains YouTrack
  • Building Micronaut Microservices Using MicrostarterCLI
  • Is IntelliJ Making Us Stupid? It's Complicated

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: