Chapter 1. Getting Started with IntelliJ IDEA 12

This introductory chapter will present some features of IntelliJ IDEA (later referred to simply as IntelliJ) and how you can install and configure it. You will be presented with some of the great features of IntelliJ IDEA and will notice for yourself that, even if you've already seen some of the features presented here in other IDEs, none of them are so focused to provide facilities to the work of the developer.

After you've seen the features, you will learn how to install and configure IntelliJ. Despite these being really simple tasks, you will probably see great value in this information, mainly because the organization and nomenclature of some elements may differ from that of the IDE you used before. So, what is provided here is to help you remove some barriers you may encounter while using IntelliJ.

Presenting features

As the name suggests, IntelliJ IDEA tries to be an intelligent Integrated Development Environment (IDE). To do this, it applies techniques that permit the automation of some tasks and suggests actions that may facilitate the developer's work. Allied with this, IntelliJ provides out-of-the-box integration with popular frameworks used by Java developers. This native integration drastically reduces the existence of bugs and provides great synergy between the different technologies used to construct your project.

Here, I will show you some of the great features that exist in IntelliJ IDEA 12.

Smart code completion

Code completion is a set of techniques used to analyze and propose pieces of code to the developer. Like other IDE, IntelliJ also presents code completion; but, as compared to other techniques, code completion in IntelliJ is much smarter.

  • It can detect and differentiate between a big quantity of languages, such as Java, Scala (using a plugin), Groovy, SQL, JPQL, JavaScript, and so on, even when they are injected in another sentence as shown in the following screenshot (for example, a SQL string in Java code):
    Smart code completion
  • Beyond suggested variables, classes, and method names, it can suggest methods, parameters, properties, filenames, resources, Spring Framework's beans, database tables, and so on, even when you are working with non-Java files.
  • Smart code completion suggests sentences based on the context and the user needs. So it can suggest, for example, chain completion like getModule().getProject() that fit correctly in the context as shown in the following screenshot:
    Smart code completion
  • IntelliJ can propose completion based on middle name search and camel case abbreviation as shown in the following screenshot:
    Smart code completion

On-the-fly code analysis

An IDE isn't useful if it can't identify whether or not an expression is invalid and will bring compilation errors to the developer. Instead of only identifying these problems, IntelliJ tries to help you improve your code. While typing your source code, you will notice that IntelliJ will analyze your code in the background. In these analyses, IntelliJ will use its configurations to identify errors and possible improvements.

The kind of analyses that IntelliJ does in the code are fully configurable; these configurations are called inspections and are one of the most powerful features of this IDE. With inspections, IntelliJ can find a large range of existing errors, possible bugs, or improvements, such as identifying if a variable is never used, suggesting a simplified sentence, identifying thread problems, and more, as you can see in the following screenshot:

On-the-fly code analysis

Advanced refactoring

Sometimes, the refactoring process is very tiring because you need to make a lot of modifications to a lot of files. Even when you do the refactoring carefully, sometimes compile errors appear, for example, because you forgot to change a method usage. Fortunately, IntelliJ can help us do these tasks.

IntelliJ has a big range of refactoring options that help the developer refactor his/her code faster and in a more secure manner. Depending on the section of code you are working on, IntelliJ will automatically suggest the refactoring. In case it doesn't propose any refactoring or gives you an option you don't want to use, you can select an option from the refactor menu. The following are some of the refactoring options available:

  • Type migration: With this, the refactoring technique can be useful in changing a member type in a safe way because IntelliJ will find and modify all the usages of the member. You can use this refactoring in a bunch of situations, such as in method return type, local variable, and parameter.
  • Extract class: This refactoring option enables the developer to extract one class from another. The developer can choose which methods and variables he/she wants to transfer to the new class. This is very useful when you have a very huge and complex class and want to simplify it. There is a similar option that creates a superclass based in the current class being edited and another that extracts an interface in the same way.
  • XML-aware dedicated refactoring: There are more than 50 refactoring options exclusive to XML files. For example, you can rename tags and attributes, wrap and unwrap tags, replace attributes with tags and vice versa, convert tags to attributes, safely add and remove subtags and attributes, and so on. If you use some framework that works with XML files, such as Spring, you will see that you can, for example, refactor a bean parameter's name directly in XML.
  • Drag-and-drop items: Sometimes you look at your project tree and think that it would be better to move some classes and packages. In IntelliJ, you can drag-and-drop the elements from one place to another in your project, and it will automatically change the imports and usages for you.
  • Change method signature: You have probably already experienced a situation like this: you created a method and, after doing a lot of work, you've decided to change the method signature. Using IntelliJ, it is possible to minimize the effects of a method signature change. IntelliJ can remove, reorder, and add parameters to the signature; it is also possible to change the return type and the parameter types. In case you are adding a parameter, IntelliJ will insert a default value in all the usages of the method.
  • Introduce constant: Sometimes, while developing, we don't see that it is possible to use a constant instead of repeating the same value all throughout the class. You can easily create a constant based on the repeated values present in your code. Once the constant is created, IntelliJ will detect the entries and replace them as shown in the following screenshot:
Advanced refactoring

Navigation and search

One of the great characteristics you will find in IntelliJ is the ease of finding and navigating to a resource. You can run a search in practically all the controls that exist in the IDE, even when you can't see a search field. You can also use different elements to navigate to a specific code that you want.

The following features can be highlighted to navigate through the source code:

  • A dialog that shows the opened files and IDE windows can be used to open the desired element without using the mouse
  • You can navigate to a declaration from a usage
  • You can navigate to a usage from a declaration
  • A breadcrumb helps to access files that are in the current path or someplace near the actually used file, as shown in the following screenshot:
    Navigation and search
  • Small symbols are available on the left side of the code editor for navigation. Clicking on these icons will focus the cursor in the desired code as shown in the following screenshot:
    Navigation and search
  • It is possible to jump to the last change the developer did, no matter if the file is in the current module or closed

For running a search, the following features can be highlighted:

  • You can find the usages of a piece of code by just clicking on one menu
  • Structural searches can be used to find and replace sentences that can't be represented by a simple string pattern, such as finding only the values of field initializers
  • It is possible to find elements in places where you can't see any search field, for example, you can search for a class in the Project Structure tree
  • You can use a regular expression for find and replace

Tools and frameworks support

IntelliJ provides support for different tools that help you to develop your application using it. The integration between the tools and the IDE helps developers to focus and reduces some reworks, mainly because the changes that are made by one tool reflect in another tool and, sometimes, starts a refactoring process. A lot of frameworks have advanced support in IntelliJ; to cite some of them: Spring, Play, Grails, JSF, Struts, Flex, JPA, Hibernate, and so on. The support for these frameworks, in conjunction with the integration of different tools, makes the development very simple and productive.

  • IntelliJ has a Unified Modeling Language (UML) tool that you can use to create your classes or just to see how your classes are organized. The UML tool uses refactoring techniques, so you can change a class in the UML editor and be secure that the code will be changed too.
  • Basically, you won't need to use another program to create databases, tables, keys, and even to store procedures. No matter which database you are using, IntelliJ will guess the correct dialect for the database; in the rare case it doesn't choose well, you can configure the SQL dialect of the project, file, or directory. There is a powerful graphical table editor available, so you can do the majority of your work in a visual environment, as shown in the following screenshot:
    Tools and frameworks support
  • IntelliJ supports the most known Version Control Systems (VCS) such as Git, subversion, CVS, Mercurial, and more. With this tool the user can work with different VCSs in a similar way. You can view the differences, changes, and history, manage change lists, integrate with your favorite bug tracker, and more.
  • IntelliJ is also fully integrated with the following most commonly used build tools: Maven, Ant, Gradle, and Gant. They provide good integration for unit tests and not only to Java code, but also to JavaScript, Flex, Android, PHP, and others too.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.135.249