출처
http://weblogs.java.net/blog/forax/archive/2009/08/29/seven-small-languages-changes-will-be-jdk7
http://blogs.sun.com/darcy/entry/project_coin_final_five


he seven small languages changes that will be in JDK7

Posted by forax on August 29, 2009 at 7:08 AM PDT

Joe Darcy has revealed the changes accepted to be included in JDK7 on coin-dev mailing list 
And the final Project Coin changes accepted are:

  1.     Improved Type Inference for Generic Instance Creation (diamond) 
  2.     Simplified Varargs Method Invocation
  3.     Strings in switch
  4.     An omnibus proposal for better integral literals
  5.     Automatic Resource Management
  6.     Language support for JSR 292
  7.     Language support for Collections

I think this set of proposal is coherent. Lot of those proposals was already in the starting blocks:

  • Diamond is already implemented, see my previous blog entry.
  • Simplified varargs is just a matter to add a new warning, so not a big deal.
  • String in switch. A little more complex but not that hard.
  • Better integral literals, the unsigned syntax need to be explored and implementation will be straighforward.
  • ARM block, need to be implemented but the current spec is crystal clear.
  • Language support for JSR292, There is already an implementation available in javac.
    I hope the JSR292 Expert Group will close the last corner cases during the JVM Summit, in two week.

About Language support for collections, hum, I was a complete surprise for me, knowing that Joe said more than one time than only 5 proposal will live in jdk7.
This proposal is a mix of two proposals:

  • Josh Bloch has proposed a syntax pretty clear for collection initialisation and
  • Neal Gafter has proposed a translation scheme to implement indexer.

And in my opinion, there are still lot of work on an unified proposal before its inclusion.
The deadline is short, end of October, I think help of the community is needed so if you want to see a better support for collection in Java,
join us on coin-dev list and help to define/implement this feature.

cheers,
Rémi


1. Improved Type Inference for Generic Instance Creation (Diamond)
List<String> list = new ArrayList<>();

객체 선언을 이렇게 단순시키는 것을 말한다.


2. Simplified Varargs Method Invocation
(출처) http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/000217.html

generic을 추가하면서 너무 타이트하게 waring이 나오는 부분이 있었고, 이런 부분에 대해서 좀 정확치 않은 쪽으로 warning이 나왔는데..
과거

static <T> List<T> asList(T... elements) { ... }

  static List<Callable<String>> stringFactories() {
    Callable<String> a, b, c;
    ...
    *// Warning: **"uses unchecked or unsafe operations"*
    return asList(a, b, c);
  }

=> 실제 메소드가 호출된 곳이 아니라 메소드 선언한 곳에서 warning이 찍히도록 하였다.

  *// Warning: **"enables unsafe generic array creation"*
  static <T> List<T> asList(T... elements) { ... }

  static List<Callable<String>> stringFactories() {
    Callable<String> a, b, c;
    ...
    return asList(a, b, c);
  }

=> 또한 이런 부분에 대해서 generic-varagrs 로 해서 warning이 안나오도록 할 수 있다.
  *@SuppressWarnings("generic-varargs")
  // Ensures only values of type T can be stored in elements.
*  static <T> List<T> asList(T... elements) { ... }



3. Strings in Switch
출처 : http://mail.openjdk.java.net/pipermail/coin-dev/2009-February/000001.html

String s = ...
switch(s) {
  case "foo":
    processFoo(s);
    break;
}


4. An omnibus proposal for better integral literals
아직 확정되지는 않았고, 아래 두 proposal중에서 고를 것 같다.
Binary literals and Underscores in numbers.
http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/000929.html


5. Automatic Resource Management
JDK7으로 가면, 훨씬 개발자가 편하게 개발을 할 수 있고, java io/nio 쪽 api를 쓰면서 stream이나 buffer에 대해서 close() 메소드를 강제로 하지 않아도 될 것 같다.
그리고, 아파치 common의 IOUtils 나 try-catch-finally 안써도 된다.
출처 : http://mail.openjdk.java.net/pipermail/coin-dev/2009-February/000011.html

static String readFirstLineFromFile(String path) throws IOException {

        BufferedReader br = new BufferedReader(new FileReader(path));

        try {

            return br.readLine();

        } finally {

            br.close();

        }

    }


=>JDK 7
try-catch 라는 무조건적 문법에서 try 만 쓸수도 있게 java language spec이 변화되었고, 알아서 내부적으로 resource 관리를 할 수 있게 한다.

static String readFirstLineFromFile2(String path) throws IOException {

        try (BufferedReader br = new BufferedReader(new FileReader(path)) {

           return br.readLine();

        }

    }




또한 이런 코드들도...

static void copy(String src, String dest) throws IOException {
        InputStream in = new FileInputStream(src);
        try {
            OutputStream out = new FileOutputStream(dest);
            try {
                byte[] buf = new byte[8 * 1024];
                int n;
                while ((n = in.read(buf)) >= 0)
                    out.write(buf, 0, n);
            } finally {
                out.close();
            }
        } finally {
            in.close();
        }
    }


=>JDK7에서는..


static void copy(String src, String dest) throws IOException {

        try (InputStream in = new FileInputStream(src);

             OutputStream out = new FileOutputStream(dest)) {

            byte[] buf = new byte[8192];

            int n;

            while ((n = in.read(buf)) >= 0)

                out.write(buf, 0, n);

        }

    }

6. Language support for JSR 292

일종 동적 클래스가 생겨서, Runtime때 자유롭게 쓸 수 있도록 한 클래스입니다.
http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/001131.html


Dynamic x = (any type of expression can go here);
Object  y = x.foo("ABC").bar(42).baz();

제가 예전에 한 것들을 적용시킬 수 있다면..
Ant를 돌릴 때, 특정 변수값으로 수정해서 컴파일을 달리하고 싶을 때, ant 컴파일 타임때 클래스를 만들거나 하는 작업들, 일종의 클립보드같은 것들을 여기다사 쑤셔 넣을 수도 있을 것 같군요.


7. Language support for collections
http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/001193.html
불편했던 입력방법이 바뀐다.


final List<Integer> piDigits =
Collections.unmodifiableList(Arrays.asList(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9 ));

JDK7에서는 이렇게 바뀐다.


final List<Integer> piDigits = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9];



8. 기타로는..
Enhanced null handling
기존


public String getPostcode(Person person) {
if (person != null) {
Address address = person.getAddress();
if (address != null) {
return address.getPostcode();
}
}
return null;
}

=> JDK7

public String getPostcode(Person person) {
return person?.getAddress()?.getPostcode();
}



String str = getStringMayBeNull();
str = (str == null ? “” : str);

=>JDK7


String str = getStringMayBeNull() ?: “”;


Improved catch clause
길게 catch 문없이  or 처럼 쓸 수 있다.

try {
return klass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new AssertionError(e);
}


Comparisons for Enums
enum도 비교가 되게~

boolean isRoyalty(Rank r) {
return rank >= Rank.JACK && rank != Rank.ACE;
}


'java core' 카테고리의 다른 글

Eclipse(JVM) Crash  (0) 2009.12.09
Cheking JMX Port Alive  (0) 2009.11.19
JDK 7 언어적으로 특징적으로 변하는 부분. (api 제외)  (0) 2009.11.13
파일 copy 테스트 (nio, inputstream) 테스트 in java  (0) 2009.09.11
Java Memroy Model  (0) 2009.08.06
Java Pattern 활용하기  (0) 2009.07.23
Posted by 김용환 '김용환'

댓글을 달아 주세요