1/* 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 8012557 27 * @summary Check that 8012557 is fixed, that interface lambda 28 * methods are private 29 * @author Robert Field 30 * @compile SAM.java 31 * @compile A.java 32 * @compile B.java 33 * @compile C.java 34 * @run main PrivateLambdas 35 * 36 * Unless the lambda methods are private, this will fail with: 37 * AbstractMethodError: 38 * Conflicting default methods: A.lambda$0 B.lambda$0 C.lambda$0 39 */ 40 41import java.lang.reflect.Method; 42import java.lang.reflect.Modifier; 43 44interface X extends A, B, C { 45 default String u() { return " "; } 46 default String name() { 47 return A.super.name() + B.super.name() + C.super.name(); 48 } 49} 50 51public class PrivateLambdas implements X { 52 public static void main(String[] args) throws Exception { 53 54 // Check that all the lambda methods are private instance synthetic 55 for (Class<?> k : new Class<?>[] { A.class, B.class, C.class }) { 56 Method[] methods = k.getDeclaredMethods(); 57 int lambdaCount = 0; 58 for(Method m : methods) { 59 if (m.getName().startsWith("lambda$")) { 60 ++lambdaCount; 61 int mod = m.getModifiers(); 62 if ((mod & Modifier.PRIVATE) == 0) { 63 throw new Exception("Expected " + m + " to be private"); 64 } 65 if (!m.isSynthetic()) { 66 throw new Exception("Expected " + m + " to be synthetic"); 67 } 68 if ((mod & Modifier.STATIC) != 0) { 69 throw new Exception("Expected " + m + " to be instance method"); 70 } 71 } 72 } 73 if (lambdaCount == 0) { 74 throw new Exception("Expected at least one lambda method"); 75 } 76 } 77 78 /* 79 * Unless the lambda methods are private, this will fail with: 80 * AbstractMethodError: 81 * Conflicting default methods: A.lambda$0 B.lambda$0 C.lambda$0 82 */ 83 X x = new PrivateLambdas(); 84 if (!x.name().equals(" A B C")) { 85 throw new Exception("Expected ' A B C' got: " + x.name()); 86 } 87 } 88} 89