Skip optimization for Accessors tailored by ri from different package. (#1527)
Signed-off-by: Daniel Kec <daniel.kec@oracle.com>
diff --git a/jaxb-ri/runtime/impl/src/main/java/org/glassfish/jaxb/runtime/v2/runtime/reflect/opt/OptimizedAccessorFactory.java b/jaxb-ri/runtime/impl/src/main/java/org/glassfish/jaxb/runtime/v2/runtime/reflect/opt/OptimizedAccessorFactory.java
index f713334..39c6cf4 100644
--- a/jaxb-ri/runtime/impl/src/main/java/org/glassfish/jaxb/runtime/v2/runtime/reflect/opt/OptimizedAccessorFactory.java
+++ b/jaxb-ri/runtime/impl/src/main/java/org/glassfish/jaxb/runtime/v2/runtime/reflect/opt/OptimizedAccessorFactory.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
@@ -187,6 +187,9 @@
logger.log(Level.INFO,"failed to load an optimized Accessor",e);
} catch (SecurityException e) {
logger.log(Level.INFO,"failed to load an optimized Accessor",e);
+ } catch (ClassCastException e) {
+ logger.log(Level.FINE,"failed to cast optimized Accessor " +
+ "created by repackaged jaxb "+opt,e);
}
return null;
}
diff --git a/jaxb-ri/runtime/impl/src/test/java/org/glassfish/jaxb/runtime/v2/runtime/reflect/opt/ForeignAccessorTest.java b/jaxb-ri/runtime/impl/src/test/java/org/glassfish/jaxb/runtime/v2/runtime/reflect/opt/ForeignAccessorTest.java
new file mode 100644
index 0000000..a718754
--- /dev/null
+++ b/jaxb-ri/runtime/impl/src/test/java/org/glassfish/jaxb/runtime/v2/runtime/reflect/opt/ForeignAccessorTest.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Distribution License v. 1.0, which is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+package org.glassfish.jaxb.runtime.v2.runtime.reflect.opt;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import jakarta.xml.bind.annotation.XmlAccessType;
+import jakarta.xml.bind.annotation.XmlAccessorType;
+import jakarta.xml.bind.annotation.XmlRootElement;
+import org.glassfish.jaxb.runtime.v2.bytecode.ClassTailor;
+import org.glassfish.jaxb.runtime.v2.runtime.reflect.Accessor;
+import org.junit.Test;
+
+public class ForeignAccessorTest {
+
+ /**
+ * Foreign accessor shouldn't throw ClassCastException, just skip optimization.
+ */
+ @Test
+ public void foreignAccessor() throws NoSuchFieldException{
+ String newClassName = EntityWithForeignAccessor.class.getName().replace('.','/') + "$JaxbAccessorF_author";
+ Class<?> foreignAccessor = AccessorInjector.prepare(EntityWithForeignAccessor.class,
+ ClassTailor.toVMClassName(ForeignAccessorTest.FieldAccessor_Ref.class),
+ newClassName);
+ assertNotNull(foreignAccessor);
+
+ Accessor<Object, Object> accessor = OptimizedAccessorFactory.get(EntityWithForeignAccessor.class.getDeclaredField("author"));
+ assertNull(accessor);
+ }
+
+ @Test
+ public void knownAccessor() throws NoSuchFieldException {
+ Accessor<Object, Object> accessor = OptimizedAccessorFactory.get(EntityWithKnownAccessor.class.getDeclaredField("author"));
+ assertNotNull(accessor);
+ }
+
+ @XmlRootElement
+ @XmlAccessorType(XmlAccessType.FIELD)
+ static class EntityWithKnownAccessor {
+ String author;
+ }
+
+ @XmlRootElement
+ @XmlAccessorType(XmlAccessType.FIELD)
+ static class EntityWithForeignAccessor {
+ String author;
+ }
+
+ /**
+ * Test template doesn't extend accessor intentionally.
+ */
+ public static class FieldAccessor_Ref /*extends Accessor*/ {
+ public FieldAccessor_Ref() {
+ // super(Ref.class);
+ }
+
+ public Object get(Object bean) {
+ return ((Bean)bean).f_ref;
+ }
+
+ public void set(Object bean, Object value) {
+ ((Bean)bean).f_ref = (Ref)value;
+ }
+ }
+}