# Eclipse and J2SE 5.0

Since release 3.1, Eclipse includes full support for the new Java language features of J2SE 5.0. One of the most important consequences of this support is that you may not notice it at all--everything you expect to work for J2SE 1.4, including editing, compiling, debugging, quick fixes, refactorings, source actions, searching, etc., will work seamlessly with J2SE 5.0's new types and syntax. In this document, we will introduce some of the more interesting capabilities Eclipse users will find when working with J2SE 5.0.

Note that both version numbers '1.5' and '5.0' are used to identify the release of the Java 2 Platform Standard Edition. Version '5.0' is the product version, while '1.5' is the developer version and also used for the compliance level.

## Prerequisites

In order to develop code compliant with J2SE 5.0, you will need a J2SE 5.0 or J2SE 6.0 Java Runtime Environment (JRE). If you start Eclipse for the first time using a J2SE 5.0 JRE, then it will use it by default. Otherwise, you will need to use the Java > Installed JREs preference page to register it with Eclipse.

This document introduces some of the new language features in J2SE 5.0 very briefly, but it is not a proper tutorial for these features. See here for more information.

### Compiler Compliance Level

To use the new J2SE 5.0 features, you must be working on a project that has a 1.5 compliance level enabled and has a 5.0 JRE. New projects will automatically get 1.5-compliance when choosing a 5.0 JRE on the first page of the New Java Project wizard:

To convert an existing J2SE 1.4 project to J2SE 5.0, you can simply:

1. Make sure you have a J2SE 5.0 JRE installed.
2. Start using the 5.0 features in your code.
3. When a compile error is flagged, use Quick Fix to update the project's compliance level:

4. For more fine-tuned control, the compiler compliance level can be set globally for a workspace (with the Java > Compiler preference page) or individually for each project (from the project's context menu, choose Properties > Java Compiler).

Projects with different compliance levels can co-exist in the workspace, and depend on each other. You can also fine-tune the kinds of compile warnings and errors produced for each project using Properties > Java Compiler > Errors/Warnings. The Generic Types and the Annotations section contain options added for J2SE 5.0.

## Generic Types

Generic types allow objects of the same class to safely operate on objects of different types. For example, they allow compile-time assurances that a `List<String>` always contains `String`s, and a `List<Integer>` always contains `Integer`s.

Anywhere that Eclipse handles a non-generic type, it can handle a generic type:

- Generic types can be safely renamed.
- Type variables can be safely renamed.
- Generic methods can be safely extracted from / inlined into generic code.
- Code assist can automatically insert appropriate type parameters in parameterized types.

In addition, a new refactoring has been added: Infer Generic Type Arguments can infer type parameters for every type reference in a class, a package, or an entire project.

Invoking the refactoring produces:

Eclipse provides new options when searching for references to generic types. Consider this example:

Selecting the reference to `List<Integer>` and using Search > References > Project from the context menu will highlight the List types on all four lines:

Using the Search View menu, the results can be filtered:

Filter Incompatible Type Arguments leaves only references to types that are assignment-compatible with the selected type:

Filter Inexact Type Arguments leaves only type references with the exact same signature:

## Annotations

Annotations attach metadata about how Java types and methods are used and documented to the Java source and can then affect compilation or be queried at run-time. For example, `@Override` will trigger a compile warning if the annotated method does not override a method in a superclass:

Everything you can do with a Java type, you can do with an annotation:

- Create new annotations using New > Annotation
- Refactor: rename, move, change signatures of members, etc.
- Search for occurrences
- Use code assist to fill in names and values

A very useful annotation with full support in Eclipse is `@SuppressWarnings`. For example, consider a private method that is currently unused, but you'd rather not delete:

Invoking quick fix on the warning proposes adding a `@SuppressWarnings` annotation:

Selecting the quick fix adds the annotation. The Eclipse compiler honors the annotation by removing the warning on `foo`:

## Enumerations

Enumerations are types that are instantiated at runtime by a known, finite set of objects:

Again, anything you can do to a Java class can be done to an enumeration:

- Create new enumerations using New > Enum
- Refactor: rename, move, rename constants, etc.
- Search for occurrences
- Use code assist to fill in constants

## Autoboxing

Autoboxing and auto unboxing allow for elegant syntax when primitive types are assigned to or retrieved from Object references:

Eclipse's source manipulation features handle autoboxing seamlessly, giving the correct types to new local variables and correct code assists. For code understanding, it is also possible to flag instances of autoboxing or autounboxing conversions:

- mark them as compile warnings or errors (Boxing and unboxing conversions in the Potential Programming Problems section of the Java > Compiler > Errors/Warnings preference page)
- highlight them using syntax coloring (via the Java > Auto(un)boxed expressions section of the Java > Editor > Syntax Coloring preference page):

## Enhanced for loop

For the common case of operating on each element of an array or collection in turn, J2SE 5.0 allows a new, cleaner syntax. Eclipse provides a "foreach" code template that can automatically guess the collection to be iterated:

Choosing the template produces:

Eclipse also provides a "Convert to enhanced for loop" quick-assist to upgrade 1.4-style `for` loops where possible.

## Static Imports

Static imports allow you to use static fields and methods from other classes without qualification.

Content assist in the Java editor can suggest such static members and adds a static import when required. To get such proposals, configure the static import favorites on the Java > Editor > Content Assist > Favorites preference page.

## Converting J2SE 1.4 code to 5.0

Eclipse offers the following tools to help you bring J2SE 1.4 code to 5.0:

- Clean Up action
  - Add missing `@Override` and `@Deprecated` annotations
  - Convert `for` loops to enhanced
- Infer Generic Type Arguments refactoring

Happy coding!