From 70a939e8ec4410475522130818ceee0920cc71e8 Mon Sep 17 00:00:00 2001 From: Babneet Singh Date: Mon, 11 Sep 2017 22:31:22 -0400 Subject: [PATCH] Translate TypeNotPresentException to NoClassDefFoundError While resolving bootstrap method handles, TypeNotPresentException is currently thrown if a class is not found since we rely upon the MethodType API. But, NoClassDefFoundError should be thrown if a class is not found while resolving bootstrap method handles. This change translates TypeNotPresentException to NoClassDefFoundError while resolving bootstrap method handles. Signed-off-by: Babneet Singh --- .../java/lang/invoke/MethodHandle.java | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/jcl/src/java.base/share/classes/java/lang/invoke/MethodHandle.java b/jcl/src/java.base/share/classes/java/lang/invoke/MethodHandle.java index d034a62f740..3e034af7fb0 100644 --- a/jcl/src/java.base/share/classes/java/lang/invoke/MethodHandle.java +++ b/jcl/src/java.base/share/classes/java/lang/invoke/MethodHandle.java @@ -823,7 +823,11 @@ private static final MethodHandle resolveInvokeDynamic(Class clazz, String na MethodType type = null; try { - type = MethodType.fromMethodDescriptorString(methodDescriptor, VM.getVMLangAccess().getClassloader(clazz)); + try { + type = MethodType.fromMethodDescriptorString(methodDescriptor, VM.getVMLangAccess().getClassloader(clazz)); + } catch (TypeNotPresentException e) { + throw throwNoClassDefFoundError(e); + } int bsmIndex = unsafe.getShort(bsmData); int bsmArgCount = unsafe.getShort(bsmData + BSM_ARGUMENT_COUNT_OFFSET); long bsmArgs = bsmData + BSM_ARGUMENTS_OFFSET; @@ -892,7 +896,11 @@ private static final MethodHandle resolveInvokeDynamic(Class clazz, String na cpEntry = cp.getDoubleAt(index); break; case 13: - cpEntry = getCPMethodTypeAt(clazz, index); + try { + cpEntry = getCPMethodTypeAt(clazz, index); + } catch (TypeNotPresentException e) { + throw throwNoClassDefFoundError(e); + } break; case 14: cpEntry = getCPMethodHandleAt(clazz, index); @@ -963,6 +971,27 @@ private static final MethodHandle resolveInvokeDynamic(Class clazz, String na return result; } + /** + * Helper method to throw NoClassDefFoundError if the cause of TypeNotPresentException + * is ClassNotFoundException. Otherwise, re-throw TypeNotPresentException. + * + * @param an instance of TypeNotPresentException + * + * @return Throwable to prevent any fall through case + * + * @throws NoClassDefFoundError if the cause of TypeNotPresentException is + * ClassNotFoundException. Otherwise, re-throw TypeNotPresentException. + */ + private static Throwable throwNoClassDefFoundError(TypeNotPresentException e) { + Throwable cause = e.getCause(); + if (cause instanceof ClassNotFoundException) { + NoClassDefFoundError noClassDefFoundError = new NoClassDefFoundError(cause.getMessage()); + noClassDefFoundError.initCause(cause); + throw noClassDefFoundError; + } + throw e; + } + @Override public String toString() { return "MethodHandle" + type.toString(); //$NON-NLS-1$