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.
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.
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.
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:
IntelliJ can propose completion based on middle name search and camel case abbreviation as shown in the following screenshot:
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:
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:
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 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:
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:
It is possible to jump to the last change the developer did, no matter if the file is in the current module or closed
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
You can use a regular expression for find and replace
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:
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.
The Version 12 of IntelliJ comes with good enhancements and new features, such as the following:
Java 8: In this version, the next generation of the Java platform is fully supported with code assistance for the new syntax, such as Lambda expressions, type annotations, default methods, and method references
Spring Frameworks: This framework was already supported, however, new improvements were made that bring performance enhancements and better code assistance for a wider range of Spring Frameworks, such as Security and Batch
Now that you know some features of IntelliJ, we need to install it. On IntelliJ's website http://www.jetbrains.com/idea/, you will find two editions of IntelliJ: Community and Ultimate.
Issue tracking integration
Eclipse project interoperability
Swing GUI designer
Code spell checker
Dependency structure matrix
Maven dependency diagram
Structural search and replace
Code duplicates detection
Frameworks and technologies
Java EE 6
Google Web Toolkit
Java Persistence API, Hibernate
Adobe Flex, AIR
Cucumber for Java Groovy
Team Foundation Server
For more details about the features supported by each version, go to http://www.jetbrains.com/idea/features/editions_comparison_matrix.html?IU.
In this book, we will work only with the Ultimate Version 12.1.x. Don't worry, you can download an evaluation version and use it for 30 days. You can run IntelliJ on three different operating systems: Microsoft Windows, GNU Linux, and Apple Mac. Independent of the OS, you will need the following configuration to run IntelliJ:
1 GB RAM minimum, 2 GB RAM recommended
300 MB hard disk space plus at least 1 GB for caches
1024 x 768 minimum screen resolution
JDK 6 or higher
Microsoft Windows 8/7/Vista/2003/XP (including 64-bit)
Mac OS X 10.5 or higher, up to 10.8 (Mountain Lion)
GNU/Linux with GNOME or KDE desktop
The installation of IntelliJ is so simple that it can be done in the following way:
On Windows, the steps are as follows:
Run the downloaded file.
Follow the steps in the wizard.
On Mac, the steps are as follows:
Mount the downloaded
.dmgfile as another disk.
Copy IntelliJ IDEA to your
On Linux, the steps are as follows:
The first time you open IntelliJ, an initial configuration wizard will be shown to you. This wizard will ask you which technologies you want to enable or disable, then it will pick plugins or a group of dependent plugins. It is recommended that you just enable the plugins that you will use because the more plugins you have enabled, the more time is spent in starting the IDE. Also, you can enable or disable any plugin when you want, in the settings dialog.
The majority of the time, you won't need to configure anything in IntelliJ. For example, if your project uses a Git repository, when you open the project in IntelliJ, it will configure its VCS tool automatically based on the Git's files present in the
Git folder. Even if it is necessary to configure something, you will probably see a balloon on the upper-right corner indicating the event and a link which will direct you to the place where you can take action.
Anyway, all configurations are done in the settings dialog. Even when you see options in the context menus or in balloons, they are just shortcuts to sections of the settings dialog. You will also see them in some tabs and buttons used to configure it, but they are shortcuts too.
As you finish the initial configuration, IntelliJ will open the initial window where you can do some actions, such as creating a project or configuring the IDE. In this initial window, you can open the Settings window (or by pressing Ctrl + Alt + S on Windows or command + , on Mac when in the main IntelliJ window). In the Settings window, you will see various items, some of which are specific for the plugins you have enabled, others that are used to configure the entire IDE, and others for project configuration. They are divided in two big sections: Project Settings and IDE Settings.
The Project Settings section is used to change configurations in the IDE that are specific to the current project, such as the compiler, deployment, and VCS. On the other hand, the IDE Settings section is the place where you will manage the configurations that will impact all current and future projects you have, such as Plugins, Notifications, and Keymap. Navigating through the items of the settings window is really simple, but some of them are hidden in a tree structure, which makes it difficult to find them when we need. Fortunately, this can be solved using the search box, as shown in the following screenshot. As I mentioned before, searches are available in practically all IDE elements.
This dialog is used to configure the structure of the project and can be opened by clicking on the Project Structure button present in the main window or using the shortcut Ctrl + Alt + Shift + S (or command + ; in Mac). It's divided into the following five categories:
Modules: This module is a discrete elementary unit of the functionality which you can compile, run, test, and debug independently. In this section, you can configure settings that are specific to each module in the project, such as the dependencies, package's prefix, and paths.
Facets: Facets represent various frameworks, technologies, and languages used in a module. Here, you can manage the facets of the module. This way, you can define if the support for a particular framework is enabled for it (for example, Spring). Once a facet is activated in a module, IntelliJ will provide facilities to work with that facet.
Artifacts: An artifact is a draft that defines the layout of the project output. It could be an archive (
EAR) or a directory. In other words, an artifact defines what the final output of the project will be. In this section, you can configure the properties for the artifact of the project.
The following sections in the chapter are two other categories related to Platform Settings: SDKs and Global Libraries.
Perhaps someday you will notice that IntelliJ may be running slow; this may happen, for example, when your project has lots of classes. To avoid this, we could increase the available memory used by IntelliJ. Simply edit the file
INTELLIJ_FOLDER \bin\idea.exe.vmoptions on Windows). What you discover upon opening this file are the virtual machine options used to run IntelliJ; so, if you want to change other things related to Java VM, which executes IntelliJ, this is the place to do so.
You should take care while changing or adding options in this file. Depending on the options you use here, the speed of IntelliJ could get worse. For example, setting values very high for the arguments
-Xmx can make garbage collector decrease its performance. On the other hand, very small values could create an OutOfMemoryError in IntelliJ. You can use the memory usage indicator to help you to configure these arguments. It is localized in the bottom-right corner of the IDE. If the bar indicates that it is using almost the total reserved memory (for example, using 235 MB of 256 MB), it is recommended that you increase the memory reservation argument by at least 500 MB, as shown in the following screenshot:
If you are a user of another IDE (for example, Netbeans or Eclipse), you will notice that some differences exist in IntelliJ that could make you go crazy when using this IDE.
The nomenclature—the name of a feature in another IDE is probably different from that in IntelliJ (for example, project in Eclipse is the same as module in IntelliJ).
The shortcuts are different. You can change the keymaps (the relation between the shortcut and the action), although there are predefined keymaps for Eclipse and Netbeans users. A better approach is learning from scratch.
IntelliJ IDEA compiles files only when it's needed to save system resources for other tasks. IntelliJ is smart enough to know which files have changed, so it recompiles just what is needed. However, if you still want to make IntelliJ compile on every save, you can use the EclipseMode plugin.
It doesn't have the deploy-on-save feature. However, you can use the On frame deactivation option available in the dialog or, perhaps, you can use other options, such as the JRebel plugin for IntelliJ.
The JetBrains website provides complete lists of FAQs. for Netbeans at http://confluence.jetbrains.com/display/IntelliJIDEA/IntelliJ+IDEA+for+NetBeans+Users and Eclipse's users at http://confluence.jetbrains.com/display/IntelliJIDEA/IntelliJ+IDEA+for+Eclipse+Users, which can help you perform the transition to IntelliJ more easily.
At the time this book is being written, Jetbrains is developing a new release of IntelliJ. The new version will show improvements mainly in Android development. If you don't know, IntelliJ IDEA is the base of Android Studio, an IDE focused exclusively on Android applications development. The new features and facilities that are present at this time, in Android Studio will be available in IntelliJ IDEA 13, with the addition of other tools and features already present in this IDE. We can highlight some features that will probably be present in the next release:
Support for importing the configuration of Android projects from
build.gradleand building them through Gradle
External annotations (
@MagicConstant) for Android APIs
Folding of Android string resources in the Java editor to display the actual string value
Many new Lint Checks
Initial support for editing the RenderScript code
Support for working with fragment resources
Support for control variations in the palette (for example, multiple variations of TextField with different default settings)
Integrated 9-patch editor
New UI for viewing the list of connected devices in the Android Tool Window
In this chapter, some features were presented to you just to let you know what you can expect while using IntelliJ. As you've already seen, the tools are totally integrated with the IDE. This will give you the confidence to make the changes you need without worrying about the time you would spend on them. The fact that almost all configurations are centralized in the same dialog, added to the search options available in basically every control, gives you facilities to find what you need in a fast way.
You will probably suffer the first few times you use it; this is because IntelliJ uses a different approach to solve the same problems. However, when you're familiarized with it's way of work, you probably won't want to work any other way.
In the next chapter, you will learn more details about the organization of the IDE and how to explore specific features to improve your development performance.