From cf044e24b4d2ed9f8d961e990c99db1711daa032 Mon Sep 17 00:00:00 2001 From: Steven Schlansker Date: Wed, 21 Jun 2023 10:25:31 -0700 Subject: [PATCH 1/2] Blackbird Proxy access: if method handle cracking fails, try using the original in case it Just Works Fixes #181 --- .../module/blackbird/util/ReflectionHack.java | 8 +++++++- .../module/blackbird/TestAccessFallback.java | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/blackbird/src/main/java/com/fasterxml/jackson/module/blackbird/util/ReflectionHack.java b/blackbird/src/main/java/com/fasterxml/jackson/module/blackbird/util/ReflectionHack.java index 53015780..eb0f9e97 100644 --- a/blackbird/src/main/java/com/fasterxml/jackson/module/blackbird/util/ReflectionHack.java +++ b/blackbird/src/main/java/com/fasterxml/jackson/module/blackbird/util/ReflectionHack.java @@ -52,7 +52,13 @@ private static MethodHandle init() { } public static Lookup privateLookupIn(Class lookup, Lookup orig) { - return Unchecked.supplier(() -> (Lookup) FACTORY.invokeExact(lookup, orig)).get(); + try { + return (Lookup) FACTORY.invokeExact(lookup, orig); + } catch (ReflectiveOperationException e) { + return orig; + } catch (Throwable t) { + throw Sneaky.throwAnyway(t); + } } } diff --git a/blackbird/src/test/java/com/fasterxml/jackson/module/blackbird/TestAccessFallback.java b/blackbird/src/test/java/com/fasterxml/jackson/module/blackbird/TestAccessFallback.java index 52bb280b..8246975e 100644 --- a/blackbird/src/test/java/com/fasterxml/jackson/module/blackbird/TestAccessFallback.java +++ b/blackbird/src/test/java/com/fasterxml/jackson/module/blackbird/TestAccessFallback.java @@ -1,5 +1,7 @@ package com.fasterxml.jackson.module.blackbird; +import java.lang.reflect.Proxy; + import com.fasterxml.jackson.databind.ObjectMapper; public class TestAccessFallback extends BlackbirdTestBase @@ -69,4 +71,19 @@ public void testDeserializeAccess() throws Exception MyBean bean2 = abMapper.readValue(BEAN_JSON, MyBean.class); assertEquals("a", bean2.getE()); } + + public void testProxyAccessIssue181() throws Exception { + ObjectMapper om = newObjectMapper(); + String val = om.writeValueAsString(Proxy.newProxyInstance(TestAccessFallback.class.getClassLoader(), new Class[] { Beany.class }, (p, m, a) -> { + if (m.getName().equals("getA")) { + return 42; + } + return null; + })); + assertEquals("{\"a\":42}", val); + } + + public interface Beany { + int getA(); + } } From 9dcc41f647e038aea348b02c90e9cd238e018bd4 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Wed, 21 Jun 2023 12:28:23 -0700 Subject: [PATCH 2/2] Add release note wrt #181 --- release-notes/CREDITS-2.x | 1 + release-notes/VERSION-2.x | 3 +++ 2 files changed, 4 insertions(+) diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index 7b0a185f..9a45f5a6 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -75,6 +75,7 @@ Steven Schlansker (stevenschlansker@github) (2.12.0) * Fixed #141: Blackbird fails to deserialize varargs array (2.13.0) +... and many, many more (esp. to Afterburner/Blackbird projects) Marc Magon (GedMarc@github) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 5e90d222..263c00e4 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -23,6 +23,9 @@ Active maintainers: 2.16.0 (not yet released) +#181: BlackBird proxy object error in Java 17 + (fix by Steven S) + #209: Add guice7 (jakarta.inject) module (contributed by Joe B)