diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java index f9af7d7b80311..7dab78102de92 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java @@ -697,7 +697,7 @@ JCNewClass makeNewClass(Type ctype, List args) { rs.resolveConstructor(null, attrEnv, ctype, TreeInfo.types(args), List.nil())); } - private void addDeserializationCase(MethodHandleSymbol refSym, Type targetType, MethodSymbol samSym, + private void addDeserializationCase(MethodHandleSymbol refSym, Type targetType, MethodSymbol samSym, Type samType, DiagnosticPosition pos, List staticArgs, MethodType indyType) { String functionalInterfaceClass = classSig(targetType); String functionalInterfaceMethodName = samSym.getSimpleName().toString(); @@ -712,6 +712,7 @@ private void addDeserializationCase(MethodHandleSymbol refSym, Type targetType, String implClass = classSig(types.erasure(refSym.owner.type)); String implMethodName = refSym.getQualifiedName().toString(); String implMethodSignature = typeSig(types.erasure(refSym.type)); + String instantiatedMethodType = typeSig(types.erasure(samType)); JCExpression kindTest = eqTest(syms.intType, deserGetter("getImplMethodKind", syms.intType), make.Literal(refSym.referenceKind())); @@ -724,13 +725,14 @@ private void addDeserializationCase(MethodHandleSymbol refSym, Type targetType, ++i; } JCStatement stmt = make.If( - deserTest(deserTest(deserTest(deserTest(deserTest( - kindTest, - "getFunctionalInterfaceClass", functionalInterfaceClass), - "getFunctionalInterfaceMethodName", functionalInterfaceMethodName), - "getFunctionalInterfaceMethodSignature", functionalInterfaceMethodSignature), - "getImplClass", implClass), - "getImplMethodSignature", implMethodSignature), + deserTest(deserTest(deserTest(deserTest(deserTest(deserTest( + kindTest, + "getFunctionalInterfaceClass", functionalInterfaceClass), + "getFunctionalInterfaceMethodName", functionalInterfaceMethodName), + "getFunctionalInterfaceMethodSignature", functionalInterfaceMethodSignature), + "getImplClass", implClass), + "getImplMethodSignature", implMethodSignature), + "getInstantiatedMethodType", instantiatedMethodType), make.Return(makeIndyCall( pos, syms.lambdaMetafactory, @@ -751,6 +753,7 @@ private void addDeserializationCase(MethodHandleSymbol refSym, Type targetType, System.err.printf("*implClass: '%s'\n", implClass); System.err.printf("*implMethodName: '%s'\n", implMethodName); System.err.printf("*implMethodSignature: '%s'\n", implMethodSignature); + System.err.printf("*instantiatedMethodType: '%s'\n", instantiatedMethodType); ****/ stmts.append(stmt); } @@ -812,10 +815,11 @@ private JCExpression makeMetafactoryIndyCall(JCFunctionalExpression tree, List indy_args) { //determine the static bsm args MethodSymbol samSym = (MethodSymbol) types.findDescriptorSymbol(tree.target.tsym); + MethodType samType = typeToMethodType(tree.getDescriptorType(types)); List staticArgs = List.of( typeToMethodType(samSym.type), refSym.asHandle(), - typeToMethodType(tree.getDescriptorType(types))); + samType); //computed indy arg types ListBuffer indy_args_types = new ListBuffer<>(); @@ -879,7 +883,7 @@ private JCExpression makeMetafactoryIndyCall(JCFunctionalExpression tree, int prevPos = make.pos; try { make.at(kInfo.clazz); - addDeserializationCase(refSym, tree.type, samSym, + addDeserializationCase(refSym, tree.type, samSym, samType, tree, staticArgs, indyType); } finally { make.at(prevPos); diff --git a/test/langtools/tools/javac/lambda/LambdaSerializedClassCastException.java b/test/langtools/tools/javac/lambda/LambdaSerializedClassCastException.java new file mode 100644 index 0000000000000..7a388a9316ec4 --- /dev/null +++ b/test/langtools/tools/javac/lambda/LambdaSerializedClassCastException.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2024, Alphabet LLC. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* +@test +@bug 8208752 +@summary NPE generating serializedLambdaName for nested lambda +*/ + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; +import java.util.function.Function; + +public class LambdaSerializedClassCastException { + + public static void main(String[] args) throws Exception { + + Function lambda1 = + (Function & Serializable) Object::toString; + Function lambda2 = + (Function & Serializable) Object::toString; + + Function deserial = serialDeserial(lambda2); + deserial.apply(new Object()); + } + + @SuppressWarnings("unchecked") + static T serialDeserial(T object) throws Exception { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.writeObject(object); + oos.close(); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ObjectInputStream ois = new ObjectInputStream(bais); + T result = (T) ois.readObject(); + ois.close(); + return result; + } +} diff --git a/test/langtools/tools/javac/lambda/SerializableObjectMethodReferencesOnInterfaces.java b/test/langtools/tools/javac/lambda/SerializableObjectMethodReferencesOnInterfaces.java index 27fb3ac9aa5cd..efc69f3341788 100644 --- a/test/langtools/tools/javac/lambda/SerializableObjectMethodReferencesOnInterfaces.java +++ b/test/langtools/tools/javac/lambda/SerializableObjectMethodReferencesOnInterfaces.java @@ -85,12 +85,14 @@ public void run() throws Exception { getFunctionalInterfaceMethodSignature (Ljava/lang/Object;)Ljava/lang/Object; getImplClass java/lang/Object getImplMethodSignature ()I + getInstantiatedMethodType (LSerializableObjectMethodReferencesOnInterfaces$Test$I1;)Ljava/lang/Integer; getImplMethodKind 5 getFunctionalInterfaceClass SerializableObjectMethodReferencesOnInterfaces$Test$F getFunctionalInterfaceMethodName apply getFunctionalInterfaceMethodSignature (Ljava/lang/Object;)Ljava/lang/Object; getImplClass java/lang/Object getImplMethodSignature ()I + getInstantiatedMethodType (LSerializableObjectMethodReferencesOnInterfaces$Test$I2;)Ljava/lang/Integer; """; if (!actual.equals(expected)) { throw new AssertionError(