Suggestions for Better Java 7 Support in DrJava

I just looked through the new language features in Java 7, and experimented with them in DrJava.

None of the changes are supported in the Interactions Pane, and there will be a lot of required work to make these things work there. We’ll have to change the parser, change the type checker, change the interpreter, and so on.

There are, however, smaller changes that we should do to provide better support in the Definitions Pane:

  1. Binary literals [cci lang=”java”]int i = 0b100000;[/cci] are not syntax-highlighted correctly.
  2. Numeric literals with an underscore in them [cci lang=”java]int i = 1_000; double d = 3.141_5927;[/cci] are not syntax-highlighted correctly.
  3. In some cases, try-with-resources is indented badly:
    [cc lang=”java”] // badly indented
    public static void tryWithResources2(String f) {
    try (InputStream is = new FileInputStream(new File(f));
    InputStream is2 = new FileInputStream(new File(f))) {
    is.read();
    }
    catch(IOException e) {
    e.printStackTrace();
    }
    }[/cc]

Everything else looks pretty good. Here’s the sample class that I used:

[cc lang=”java”]import java.io.*;
import java.util.*;

public class Java7Samples {
public static void main(String[] args) {
stringSwitch(args[0]);
intSwitch(new Integer(args[1]));
tryWithResources(args[2]);
tryWithResources2(args[2]);
multiCatch(new Integer(args[3]));

// bad integer syntax highlighting
int i = 0b100000;
System.out.println(i);

// bad numeric syntax highlighting
i = 1_000_000;
System.out.println(i);
i = 2__000__000;
System.out.println(i);
// int illegal = _1000;
// int illegal2 = 1000_;
double d = 123_456.789_012;
System.out.println(d);
}

public static void stringSwitch(String arg) {
switch(arg) {
case “one”:
System.out.println(“case one”);
break;
case “two”:
case “three”:
System.out.println(“case two or three”);
break;
default:
System.out.println(“default case”);
}
}
public static void intSwitch(int arg) {
switch(arg) {
case 1:
System.out.println(“case 1”);
break;
case 2:
case 3:
System.out.println(“case 2 or 3”);
break;
default:
System.out.println(“default case”);
}
}

public static void tryWithResources(String f) {
try (InputStream is = new FileInputStream(new File(f))) {
is.read();
}
catch(IOException e) {
e.printStackTrace();
}
}

// badly indented
public static void tryWithResources2(String f) {
try (InputStream is = new FileInputStream(new File(f));
InputStream is2 = new FileInputStream(new File(f))) {
is.read();
}
catch(IOException e) {
e.printStackTrace();
}
}

public static class Ex1 extends RuntimeException { }
public static class Ex2 extends RuntimeException { }
public static class Ex3or4 extends RuntimeException { }
public static class Ex3 extends Ex3or4 { }
public static class Ex4 extends Ex3or4 { }

public static void doSomething(int i) throws Ex1, Ex2, Ex3, Ex4 {
switch(i) {
case 1:
throw new Ex1();
case 2:
throw new Ex2();
case 3:
throw new Ex3();
case 4:
throw new Ex4();
}
}

public static void handleEx1Or2(RuntimeException e) {
System.out.println(“handlEx1Or2”);
}

public static void handleEx3Or4(Ex3or4 e) {
System.out.println(“handlEx3Or4”);
}

public static void multiCatch(int i) {
try {
doSomething(i);
}
catch(Ex1 | Ex2 ex) {
handleEx1Or2(ex);
}
catch(Ex3 | Ex4 ex) {
handleEx3Or4(ex);
}
}

public static void multiCatch2(int i) {
try {
doSomething(i);
}
catch(Ex1 |
Ex2 ex) {
handleEx1Or2(ex);
}
catch(Ex3 |
Ex4 ex) {
handleEx3Or4(ex);
}
}

public static Map diamond() {
return new HashMap<>();
}

public static void diamond2() {
Map map = new HashMap<>();
}
}[/cc]

Update

I filed bugs 3404387 and 3404389.

Share

About Mathias

Software development engineer. Principal developer of DrJava. Recent Ph.D. graduate from the Department of Computer Science at Rice University.
This entry was posted in DrJava. Bookmark the permalink.

Leave a Reply