Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -697,25 +697,19 @@ JCNewClass makeNewClass(Type ctype, List<JCExpression> 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<LoadableConstant> 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<JCExpression> serArgs = new ListBuffer<>();
int i = 0;
for (Type t : indyType.getParameterTypes()) {
Expand All @@ -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,
Expand All @@ -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);
}
Expand Down Expand Up @@ -813,10 +809,11 @@ private JCExpression makeMetafactoryIndyCall(JCFunctionalExpression tree,
List<JCExpression> indy_args) {
//determine the static bsm args
MethodSymbol samSym = (MethodSymbol) types.findDescriptorSymbol(tree.target.tsym);
Type samType = tree.getDescriptorType(types);
List<LoadableConstant> staticArgs = List.of(
typeToMethodType(samSym.type),
refSym.asHandle(),
typeToMethodType(tree.getDescriptorType(types)));
typeToMethodType(samType));

//computed indy arg types
ListBuffer<Type> indy_args_types = new ListBuffer<>();
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> lambda1 =
(Function<String, String> & Serializable) Object::toString;
Function<Object, String> lambda2 =
(Function<Object, String> & Serializable) Object::toString;

Function<Object, String> deserial = serialDeserial(lambda2);
deserial.apply(new Object());
}

@SuppressWarnings("unchecked")
static <T> 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;
}
}