8062543: Replace uses of MethodHandleImpl.castReference with Class.cast

Reviewed-by: psandoz, vlivanov
This commit is contained in:
Michael Haupt 2015-07-17 08:10:41 +02:00
parent a57b0dd1c8
commit d9cbd23d50
2 changed files with 11 additions and 30 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -61,7 +61,6 @@ class InvokerBytecodeGenerator {
private static final String LFN_SIG = "L" + LFN + ";"; private static final String LFN_SIG = "L" + LFN + ";";
private static final String LL_SIG = "(L" + OBJ + ";)L" + OBJ + ";"; private static final String LL_SIG = "(L" + OBJ + ";)L" + OBJ + ";";
private static final String LLV_SIG = "(L" + OBJ + ";L" + OBJ + ";)V"; private static final String LLV_SIG = "(L" + OBJ + ";L" + OBJ + ";)V";
private static final String CLL_SIG = "(L" + CLS + ";L" + OBJ + ";)L" + OBJ + ";";
/** Name of its super class*/ /** Name of its super class*/
private static final String superName = OBJ; private static final String superName = OBJ;
@ -571,7 +570,7 @@ class InvokerBytecodeGenerator {
mv.visitLdcInsn(constantPlaceholder(cls)); mv.visitLdcInsn(constantPlaceholder(cls));
mv.visitTypeInsn(Opcodes.CHECKCAST, CLS); mv.visitTypeInsn(Opcodes.CHECKCAST, CLS);
mv.visitInsn(Opcodes.SWAP); mv.visitInsn(Opcodes.SWAP);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, MHI, "castReference", CLL_SIG, false); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, CLS, "cast", LL_SIG, false);
if (Object[].class.isAssignableFrom(cls)) if (Object[].class.isAssignableFrom(cls))
mv.visitTypeInsn(Opcodes.CHECKCAST, OBJARY); mv.visitTypeInsn(Opcodes.CHECKCAST, OBJARY);
else if (PROFILE_LEVEL > 0) else if (PROFILE_LEVEL > 0)

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -219,7 +219,7 @@ import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
if (convSpec == null) continue; if (convSpec == null) continue;
MethodHandle fn; MethodHandle fn;
if (convSpec instanceof Class) { if (convSpec instanceof Class) {
fn = Lazy.MH_castReference.bindTo(convSpec); fn = Lazy.MH_cast.bindTo(convSpec);
} else { } else {
fn = (MethodHandle) convSpec; fn = (MethodHandle) convSpec;
} }
@ -239,7 +239,7 @@ import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
if (convSpec == void.class) if (convSpec == void.class)
fn = null; fn = null;
else else
fn = Lazy.MH_castReference.bindTo(convSpec); fn = Lazy.MH_cast.bindTo(convSpec);
} else { } else {
fn = (MethodHandle) convSpec; fn = (MethodHandle) convSpec;
} }
@ -302,7 +302,7 @@ import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
Name conv; Name conv;
if (convSpec instanceof Class) { if (convSpec instanceof Class) {
Class<?> convClass = (Class<?>) convSpec; Class<?> convClass = (Class<?>) convSpec;
conv = new Name(Lazy.MH_castReference, convClass, names[INARG_BASE + i]); conv = new Name(Lazy.MH_cast, convClass, names[INARG_BASE + i]);
} else { } else {
MethodHandle fn = (MethodHandle) convSpec; MethodHandle fn = (MethodHandle) convSpec;
conv = new Name(fn, names[INARG_BASE + i]); conv = new Name(fn, names[INARG_BASE + i]);
@ -326,7 +326,7 @@ import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
conv = new Name(LambdaForm.constantZero(BasicType.basicType(srcType.returnType()))); conv = new Name(LambdaForm.constantZero(BasicType.basicType(srcType.returnType())));
} else if (convSpec instanceof Class) { } else if (convSpec instanceof Class) {
Class<?> convClass = (Class<?>) convSpec; Class<?> convClass = (Class<?>) convSpec;
conv = new Name(Lazy.MH_castReference, convClass, names[OUT_CALL]); conv = new Name(Lazy.MH_cast, convClass, names[OUT_CALL]);
} else { } else {
MethodHandle fn = (MethodHandle) convSpec; MethodHandle fn = (MethodHandle) convSpec;
if (fn.type().parameterCount() == 0) if (fn.type().parameterCount() == 0)
@ -343,25 +343,6 @@ import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
return SimpleMethodHandle.make(srcType, form); return SimpleMethodHandle.make(srcType, form);
} }
/**
* Identity function, with reference cast.
* @param t an arbitrary reference type
* @param x an arbitrary reference value
* @return the same value x
*/
@ForceInline
@SuppressWarnings("unchecked")
static <T,U> T castReference(Class<? extends T> t, U x) {
// inlined Class.cast because we can't ForceInline it
if (x != null && !t.isInstance(x))
throw newClassCastException(t, x);
return (T) x;
}
private static ClassCastException newClassCastException(Class<?> t, Object obj) {
return new ClassCastException("Cannot cast " + obj.getClass().getName() + " to " + t.getName());
}
static Object[] computeValueConversions(MethodType srcType, MethodType dstType, static Object[] computeValueConversions(MethodType srcType, MethodType dstType,
boolean strict, boolean monobox) { boolean strict, boolean monobox) {
final int INARG_COUNT = srcType.parameterCount(); final int INARG_COUNT = srcType.parameterCount();
@ -591,6 +572,7 @@ import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
*/ */
static class Lazy { static class Lazy {
private static final Class<?> MHI = MethodHandleImpl.class; private static final Class<?> MHI = MethodHandleImpl.class;
private static final Class<?> CLS = Class.class;
private static final MethodHandle[] ARRAYS; private static final MethodHandle[] ARRAYS;
private static final MethodHandle[] FILL_ARRAYS; private static final MethodHandle[] FILL_ARRAYS;
@ -600,7 +582,7 @@ import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
static final NamedFunction NF_throwException; static final NamedFunction NF_throwException;
static final NamedFunction NF_profileBoolean; static final NamedFunction NF_profileBoolean;
static final MethodHandle MH_castReference; static final MethodHandle MH_cast;
static final MethodHandle MH_selectAlternative; static final MethodHandle MH_selectAlternative;
static final MethodHandle MH_copyAsPrimitiveArray; static final MethodHandle MH_copyAsPrimitiveArray;
static final MethodHandle MH_fillNewTypedArray; static final MethodHandle MH_fillNewTypedArray;
@ -623,8 +605,8 @@ import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
NF_throwException.resolve(); NF_throwException.resolve();
NF_profileBoolean.resolve(); NF_profileBoolean.resolve();
MH_castReference = IMPL_LOOKUP.findStatic(MHI, "castReference", MH_cast = IMPL_LOOKUP.findVirtual(CLS, "cast",
MethodType.methodType(Object.class, Class.class, Object.class)); MethodType.methodType(Object.class, Object.class));
MH_copyAsPrimitiveArray = IMPL_LOOKUP.findStatic(MHI, "copyAsPrimitiveArray", MH_copyAsPrimitiveArray = IMPL_LOOKUP.findStatic(MHI, "copyAsPrimitiveArray",
MethodType.methodType(Object.class, Wrapper.class, Object[].class)); MethodType.methodType(Object.class, Wrapper.class, Object[].class));
MH_arrayIdentity = IMPL_LOOKUP.findStatic(MHI, "identity", MH_arrayIdentity = IMPL_LOOKUP.findStatic(MHI, "identity",