Skip to content

Commit

Permalink
Skip optimization for Accessors tailored by ri from different package. (
Browse files Browse the repository at this point in the history
#1527)

Signed-off-by: Daniel Kec <[email protected]>
  • Loading branch information
danielkec authored Mar 31, 2021
1 parent 5b27c9f commit dae589b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -187,6 +187,9 @@ private static <B,V> Accessor<B,V> instanciate(Class opt) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit dae589b

Please sign in to comment.