• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2012, 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 package org.openjdk.tests.java.lang.invoke;
24 
25 import org.testng.annotations.Test;
26 
27 import java.io.Serializable;
28 import java.lang.invoke.SerializedLambda;
29 import java.lang.reflect.Method;
30 
31 import static org.testng.Assert.fail;
32 
33 /**
34  * Ensure that the $deserializeLambda$ method is present when it should be, and absent otherwise
35  */
36 
37 @Test(groups = { "serialization-hostile" })
38 public class DeserializeMethodTest {
assertDeserializeMethod(Class<?> clazz, boolean expectedPresent)39     private void assertDeserializeMethod(Class<?> clazz, boolean expectedPresent) {
40         try {
41             Method m = clazz.getDeclaredMethod("$deserializeLambda$", SerializedLambda.class);
42             if (!expectedPresent)
43                 fail("Unexpected $deserializeLambda$ in " + clazz);
44         }
45         catch (NoSuchMethodException e) {
46             if (expectedPresent)
47                 fail("Expected to find $deserializeLambda$ in " + clazz);
48         }
49     }
50 
51     static class Empty {}
52 
testEmptyClass()53     public void testEmptyClass() {
54         assertDeserializeMethod(Empty.class, false);
55     }
56 
57     static class Cap1 {
foo()58         void foo() {
59             Runnable r = (Runnable & Serializable) () -> { };
60         }
61     }
62 
testCapturingSerLambda()63     public void testCapturingSerLambda() {
64         assertDeserializeMethod(Cap1.class, true);
65     }
66 
67     static class Cap2 {
foo()68         void foo() {
69             Runnable r = () -> { };
70         }
71     }
72 
testCapturingNonSerLambda()73     public void testCapturingNonSerLambda() {
74         assertDeserializeMethod(Cap2.class, false);
75     }
76 
77     interface Marker { }
78     static class Cap3 {
foo()79         void foo() {
80             Runnable r = (Runnable & Marker) () -> { };
81         }
82     }
83 
testCapturingNonserIntersectionLambda()84     public void testCapturingNonserIntersectionLambda() {
85         assertDeserializeMethod(Cap3.class, false);
86     }
87 }
88