diff --git a/src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java b/src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java index 4dac59771e861..527da8c802374 100644 --- a/src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java +++ b/src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java @@ -438,10 +438,10 @@ public void accept(CodeBuilder cob) { .ldc(factoryType.returnType().getName().replace('.', '/')) .ldc(interfaceMethodName) .ldc(interfaceMethodType.toMethodDescriptorString()) - .ldc(implInfo.getReferenceKind()) - .ldc(implInfo.getDeclaringClass().getName().replace('.', '/')) - .ldc(implInfo.getName()) - .ldc(implInfo.getMethodType().toMethodDescriptorString()) + .ldc(implKind) + .ldc(implClass.getName().replace('.', '/')) + .ldc(implMethodName) + .ldc(implMethodDesc.descriptorString()) .ldc(dynamicMethodType.toMethodDescriptorString()) .loadConstant(argDescs.length) .anewarray(CD_Object); 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 b13c9e0fe2bb6..8cf9fce55db76 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,25 +697,19 @@ 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(); String functionalInterfaceMethodSignature = typeSig(types.erasure(samSym.type)); - Symbol baseMethod = refSym.baseSymbol(); - Symbol origMethod = baseMethod.baseSymbol(); - if (baseMethod != origMethod && origMethod.owner == syms.objectType.tsym) { - //the implementation method is a java.lang.Object method transferred to an - //interface that does not declare it. Runtime will refer to this method as to - //a java.lang.Object method, so do the same: - refSym = ((MethodSymbol) origMethod).asHandle(); - } 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)); + int implMethodKind = refSym.referenceKind(); JCExpression kindTest = eqTest(syms.intType, deserGetter("getImplMethodKind", syms.intType), - make.Literal(refSym.referenceKind())); + make.Literal(implMethodKind)); ListBuffer serArgs = new ListBuffer<>(); int i = 0; for (Type t : indyType.getParameterTypes()) { @@ -725,13 +719,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, @@ -752,6 +747,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); } @@ -813,10 +809,11 @@ private JCExpression makeMetafactoryIndyCall(JCFunctionalExpression tree, List indy_args) { //determine the static bsm args MethodSymbol samSym = (MethodSymbol) types.findDescriptorSymbol(tree.target.tsym); + Type samType = tree.getDescriptorType(types); List staticArgs = List.of( typeToMethodType(samSym.type), refSym.asHandle(), - typeToMethodType(tree.getDescriptorType(types))); + typeToMethodType(samType)); //computed indy arg types ListBuffer indy_args_types = new ListBuffer<>(); @@ -880,7 +877,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; + } +}