Java 7 on Eclipse Indigo

Update: Eclipse Indigo 3.7.1 now has built-in support for Java 7. The post below only applies to Eclipse Indigo versions below 3.7.1.

Here are the steps needed to start playing with the new Java 7 language changes (aka Project Coin) in Eclipse Indigo.

1. Download and install Java SE 7 JDK. The JDK installer also comes with the JRE installer.

2. A few days ago, you needed the Eclipse Java Development Tools Patch for Java 7 Support (BETA). from the Eclipse update site http://build.eclipse.org/eclipse/java7patch/. This is no longer available and developers have to download a maintenance build. Download the latest version of Eclipse 3.7 from the 3.7.x Stream Builds section. You now should be able to use the Java 7 compiler. Also verify the Installed JREs in Eclipse.

3. Create a new project using the Java 7 compiler and library.

4. Read Project Coin to know more about the language changes introduced in Java 7.

5. Create a new class to try out the language changes in Eclipse. Try the code below.

package com.teamextension.java7;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ProjectCoin {
	public static void main(String[] args) throws Exception1, Exception2 {
		stringInSwitch();
		numericLiterals();
		diamond();
		tryWithResources();
		multiCatch();
		preciseRethrow();
		simplifiedVarArgs();
	}

	private static void stringInSwitch() {
		switch ("apple") {
		case "apple":
			System.out.println("cider");
		}
	}

	private static void numericLiterals() {
		System.out.println(1_000_000);

		System.out.println(0b1000);
	}

	private static void diamond() {
		List<String> list = new ArrayList<>();
	}

	private static void tryWithResources() {
		try (BufferedReader br = new BufferedReader(new FileReader("README.txt"))) {
			System.out.println(br.readLine());
		} catch (IOException e) {
			e.printStackTrace();
		}
		// no more finally
	}

	private static class Exception1 extends Exception {
	}

	private static class Exception2 extends Exception {
	}

	private static void multiCatch() {
		try {
			if (true) {
				throw new Exception1();
			} else {
				throw new Exception2();
			}
		} catch (Exception1 | Exception2 e) {
			e.printStackTrace();
		}
	}

	private static void preciseRethrow() throws Exception1, Exception2 {
		try {
			if (true) {
				throw new Exception1();
			} else {
				throw new Exception2();
			}
		} catch (Exception e) {
			throw e; // notice Exception1 and Exception2 in the throws clause
		}
	}

	private static void simplifiedVarArgs() {
		// See http://download.oracle.com/javase/tutorial/java/generics/non-reifiable-varargs-type.html
		List<String> list = null;
		asList(list);
	}

	@SafeVarargs
	private static <T> List<T> asList(T... elements) {
		// Type safety: Potential heap pollution via varargs parameter elements
		return null;
	}
}

The code is self explanatory, and includes some comments about the not so obvious parts of the code. What requires further reading is the Simplified varargs method invocation. I suggest you read Using Non-Reifiable Parameters with Varargs Methods for more information.


No Comments


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>