Tue, 14 May 2013 11:11:09 -0700
8012556: Implement lambda methods on interfaces as static
8006140: Javac NPE compiling Lambda expression on initialization expression of static field in interface
Summary: Lambdas occurring in static contexts or those not needing instance information should be generated into static methods. This has long been the case for classes. However, as a work-around to the lack of support for statics on interfaces, interface lambda methods have been generated into default methods. For lambdas in interface static contexts (fields and static methods) this causes an NPE in javac because there is no 'this'. MethodHandles now support static methods on interfaces. This changeset allows lambda methods to be generated as static interface methods. An existing bug in Hotspot (8013875) is exposed in a test when the "-esa" flag is used. This test and another test that already exposed this bug have been marked with @ignore.
Reviewed-by: mcimadamore
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | /*SAM types: |
aoqi@0 | 25 | 1. An interface that has a single abstract method |
aoqi@0 | 26 | 2. Having more than one distinct methods, but only one is "real", the others are overriden public methods in Object - example: Comparator<T> |
aoqi@0 | 27 | 3. Having more than one methods due to inheritance, but they have the same signature |
aoqi@0 | 28 | 4. Having more than one methods due to inheritance, but one of them has a subsignature of all other methods |
aoqi@0 | 29 | a) parameter types compatible |
aoqi@0 | 30 | b) return type substitutable |
aoqi@0 | 31 | c) thrown type not conflicting with the thrown clause of any other method |
aoqi@0 | 32 | d) mixed up |
aoqi@0 | 33 | 5. Type-dependent SAM types |
aoqi@0 | 34 | non-SAM types: |
aoqi@0 | 35 | 6. An interface that has a single abstract method, which is also public method in Object |
aoqi@0 | 36 | 7. Having more than one methods due to inheritance, and none of them has a subsignature of all other methods |
aoqi@0 | 37 | */ |
aoqi@0 | 38 | |
aoqi@0 | 39 | import java.util.List; |
aoqi@0 | 40 | import java.util.Collection; |
aoqi@0 | 41 | import java.sql.SQLException; |
aoqi@0 | 42 | import java.sql.SQLTransientException; |
aoqi@0 | 43 | import java.util.concurrent.TimeoutException; |
aoqi@0 | 44 | import java.io.*; |
aoqi@0 | 45 | |
aoqi@0 | 46 | interface A {int getOldest(List<Number> list);} |
aoqi@0 | 47 | interface B {int getOldest(List list);} |
aoqi@0 | 48 | interface C {int getOldest(List<?> list);} |
aoqi@0 | 49 | interface D {int getOldest(List<Integer> list);} |
aoqi@0 | 50 | interface E {int getOldest(Collection<?> collection);} |
aoqi@0 | 51 | //Not SAM type, case #7 |
aoqi@0 | 52 | interface DE extends D, E {} |
aoqi@0 | 53 | |
aoqi@0 | 54 | interface Foo {int getAge(Number n);} |
aoqi@0 | 55 | interface Bar {int getAge(Integer i);} |
aoqi@0 | 56 | //Not SAM type, case #7 |
aoqi@0 | 57 | interface FooBar extends Foo, Bar {} |
aoqi@0 | 58 | |
aoqi@0 | 59 | //Not SAM type, case #6 |
aoqi@0 | 60 | interface Planet {boolean equals(Object o);} |
aoqi@0 | 61 | |
aoqi@0 | 62 | // SAM type interfaces: |
aoqi@0 | 63 | // type #2: |
aoqi@0 | 64 | //only one abstract non-Ojbect method getAge() |
aoqi@0 | 65 | interface Mars<T> extends Planet {int getAge(T t);} |
aoqi@0 | 66 | //only one abstract non-Ojbect method increment() |
aoqi@0 | 67 | interface Jupiter { |
aoqi@0 | 68 | boolean equals(Object o); |
aoqi@0 | 69 | String toString(); |
aoqi@0 | 70 | int increment(int i); |
aoqi@0 | 71 | } |
aoqi@0 | 72 | |
aoqi@0 | 73 | // type #3: |
aoqi@0 | 74 | interface X {int getTotal(List<String> arg);} |
aoqi@0 | 75 | interface Y {int getTotal(List<String> strs);} |
aoqi@0 | 76 | //SAM type ([List<String>], int, {}) |
aoqi@0 | 77 | interface XY extends X, Y {} |
aoqi@0 | 78 | //SAM type ([List<String>], int, {}) |
aoqi@0 | 79 | interface XYZ extends X, Y, XY {} |
aoqi@0 | 80 | |
aoqi@0 | 81 | // type #4 a): |
aoqi@0 | 82 | //SAM type ([List], int, {}) |
aoqi@0 | 83 | interface AB extends A, B {} |
aoqi@0 | 84 | |
aoqi@0 | 85 | // type #4 b): |
aoqi@0 | 86 | interface F {Number getValue(String str);} |
aoqi@0 | 87 | interface G {Integer getValue(String str);} |
aoqi@0 | 88 | interface H {Serializable getValue(String str);} |
aoqi@0 | 89 | interface I {Object getValue(String str);} |
aoqi@0 | 90 | //SAM type ([String], Integer, {}) |
aoqi@0 | 91 | interface FGHI extends F, G, H, I {} |
aoqi@0 | 92 | |
aoqi@0 | 93 | interface J {List<Number> getAll(String str);} |
aoqi@0 | 94 | interface K {List<?> getAll(String str);} |
aoqi@0 | 95 | interface L {List getAll(String str);} |
aoqi@0 | 96 | interface M {Collection getAll(String str);} |
aoqi@0 | 97 | //SAM type ([String], List<Number>/List, {}) - the return type is flexible to some degree |
aoqi@0 | 98 | interface JK extends J, K {} |
aoqi@0 | 99 | //SAM type ([String], List<Number>/List, {}) |
aoqi@0 | 100 | interface JL extends J, L {} |
aoqi@0 | 101 | //SAM type ([String], List<Number>/List, {}) |
aoqi@0 | 102 | interface JKL extends J, K, L {} |
aoqi@0 | 103 | //SAM type ([String], List<Number>/List, {}) |
aoqi@0 | 104 | interface JKLM extends J, K, L, M {} |
aoqi@0 | 105 | |
aoqi@0 | 106 | // type #4 c): |
aoqi@0 | 107 | interface N {String getText(File f) throws IOException;} |
aoqi@0 | 108 | interface O {String getText(File f) throws FileNotFoundException;} |
aoqi@0 | 109 | interface P {String getText(File f) throws NullPointerException;} |
aoqi@0 | 110 | //SAM type ([File], String, {FileNotFoundException}) |
aoqi@0 | 111 | interface NO extends N, O {} |
aoqi@0 | 112 | //SAM type ([File], String, {}) |
aoqi@0 | 113 | interface NOP extends N, O, P {} |
aoqi@0 | 114 | |
aoqi@0 | 115 | interface Boo {int getAge(String s) throws IOException;} |
aoqi@0 | 116 | interface Doo {int getAge(String s) throws SQLException;} |
aoqi@0 | 117 | //SAM type ([String], int, {}) |
aoqi@0 | 118 | interface BooDoo extends Boo, Doo {} |
aoqi@0 | 119 | |
aoqi@0 | 120 | // type #4 d): |
aoqi@0 | 121 | interface Q {Iterable m(Iterable<String> arg);} |
aoqi@0 | 122 | interface R {Iterable<String> m(Iterable arg);} |
aoqi@0 | 123 | //SAM type ([Iterable], Iterable<String>/Iterable, {}) |
aoqi@0 | 124 | interface QR extends Q, R {} |
aoqi@0 | 125 | |
aoqi@0 | 126 | interface U {Collection foo(List<String> arg) throws IOException, SQLTransientException;} |
aoqi@0 | 127 | interface V {List<?> foo(List<String> arg) throws EOFException, SQLException, TimeoutException;} |
aoqi@0 | 128 | interface W {List<String> foo(List arg) throws Exception;} |
aoqi@0 | 129 | //SAM type ([List<String>], List<String>/List, {EOFException, SQLTransientException}) |
aoqi@0 | 130 | interface UV extends U, V {} |
aoqi@0 | 131 | // SAM type ([List], List<String>/List, {EOFException, SQLTransientException}) |
aoqi@0 | 132 | interface UVW extends U, V, W {} |
aoqi@0 | 133 | |
aoqi@0 | 134 | // type #5: |
aoqi@0 | 135 | // Not a SAM because sam-ness depends on instantiation of type-variables |
aoqi@0 | 136 | interface Qoo<T> {void m(T arg);} |
aoqi@0 | 137 | interface Roo<S extends Number> {void m(S arg);} |
aoqi@0 | 138 | interface QooRoo<T1, T2 extends Number, T3> extends Qoo<T1>, Roo<T2> {} |