From d9cbd23d508768f4b48a10eb92f2e405c406522a Mon Sep 17 00:00:00 2001 From: Michael Haupt Date: Fri, 17 Jul 2015 08:10:41 +0200 Subject: [PATCH] 8062543: Replace uses of MethodHandleImpl.castReference with Class.cast Reviewed-by: psandoz, vlivanov --- .../lang/invoke/InvokerBytecodeGenerator.java | 5 ++- .../java/lang/invoke/MethodHandleImpl.java | 36 +++++-------------- 2 files changed, 11 insertions(+), 30 deletions(-) diff --git a/jdk/src/java.base/share/classes/java/lang/invoke/InvokerBytecodeGenerator.java b/jdk/src/java.base/share/classes/java/lang/invoke/InvokerBytecodeGenerator.java index 6698d9da36e..2c94e081568 100644 --- a/jdk/src/java.base/share/classes/java/lang/invoke/InvokerBytecodeGenerator.java +++ b/jdk/src/java.base/share/classes/java/lang/invoke/InvokerBytecodeGenerator.java @@ -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. * * 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 LL_SIG = "(L" + OBJ + ";)L" + OBJ + ";"; 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*/ private static final String superName = OBJ; @@ -571,7 +570,7 @@ class InvokerBytecodeGenerator { mv.visitLdcInsn(constantPlaceholder(cls)); mv.visitTypeInsn(Opcodes.CHECKCAST, CLS); 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)) mv.visitTypeInsn(Opcodes.CHECKCAST, OBJARY); else if (PROFILE_LEVEL > 0) diff --git a/jdk/src/java.base/share/classes/java/lang/invoke/MethodHandleImpl.java b/jdk/src/java.base/share/classes/java/lang/invoke/MethodHandleImpl.java index 4de3439d0a4..3fad3315ea1 100644 --- a/jdk/src/java.base/share/classes/java/lang/invoke/MethodHandleImpl.java +++ b/jdk/src/java.base/share/classes/java/lang/invoke/MethodHandleImpl.java @@ -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. * * 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; MethodHandle fn; if (convSpec instanceof Class) { - fn = Lazy.MH_castReference.bindTo(convSpec); + fn = Lazy.MH_cast.bindTo(convSpec); } else { fn = (MethodHandle) convSpec; } @@ -239,7 +239,7 @@ import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP; if (convSpec == void.class) fn = null; else - fn = Lazy.MH_castReference.bindTo(convSpec); + fn = Lazy.MH_cast.bindTo(convSpec); } else { fn = (MethodHandle) convSpec; } @@ -302,7 +302,7 @@ import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP; Name conv; if (convSpec instanceof Class) { 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 { MethodHandle fn = (MethodHandle) convSpec; 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()))); } else if (convSpec instanceof Class) { 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 { MethodHandle fn = (MethodHandle) convSpec; if (fn.type().parameterCount() == 0) @@ -343,25 +343,6 @@ import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP; 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 castReference(Class 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, boolean strict, boolean monobox) { final int INARG_COUNT = srcType.parameterCount(); @@ -591,6 +572,7 @@ import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP; */ static class Lazy { private static final Class MHI = MethodHandleImpl.class; + private static final Class CLS = Class.class; private static final MethodHandle[] 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_profileBoolean; - static final MethodHandle MH_castReference; + static final MethodHandle MH_cast; static final MethodHandle MH_selectAlternative; static final MethodHandle MH_copyAsPrimitiveArray; static final MethodHandle MH_fillNewTypedArray; @@ -623,8 +605,8 @@ import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP; NF_throwException.resolve(); NF_profileBoolean.resolve(); - MH_castReference = IMPL_LOOKUP.findStatic(MHI, "castReference", - MethodType.methodType(Object.class, Class.class, Object.class)); + MH_cast = IMPL_LOOKUP.findVirtual(CLS, "cast", + MethodType.methodType(Object.class, Object.class)); MH_copyAsPrimitiveArray = IMPL_LOOKUP.findStatic(MHI, "copyAsPrimitiveArray", MethodType.methodType(Object.class, Wrapper.class, Object[].class)); MH_arrayIdentity = IMPL_LOOKUP.findStatic(MHI, "identity",