users@jaxb.java.net

Why is CopyBuilder copy method checking isAccessible

From: Simon Pett <simon.pett_at_gmail.com>
Date: Wed, 12 Nov 2008 13:07:30 +1000

Hi

Following the copyBuilder example posted on the mailing list last month, I
am using CopyBuilder to copy a jaxb object graph. (java 1.5) This seems to
work great until CopyBuilder runs into a problem cloning an
XMLGregorianCalendar embedded in the Jaxb graph.

The XMLGregorianCalendarImpl implements Cloneable and has a public clone
method. So i expect this to clone ok.

However in public Object copy(Object object), CopyBuilder calls isAccessible
which returns false in my unit tests. CopyBuilder then throws an exception
that a public clone is not available.

I dont think isAccessible is the correct call here.

Reading the AccessibleObject javadoc for setAccessible

"A value of <tt>true</tt> indicates that
the reflected object should suppress Java language access
checking when it is used.A value of <tt>false</tt> indicates
that the reflected object should enforce Java language access checks."

I interpret this to mean a false value returned from isAccessible requires
the jvm to perform default language checks, ie in this case the method must
be public to be accessed from a simple unit test.

If i'm correct can the check be changed/removed, if not can someone set me
straight.

thanks
Simon


--
example previously posted on mailing list
http://download.java.net/maven/2/org/jvnet/jaxb2_commons/samples-basic-maven/0.4.1/samples-basic-maven-0.4.1-src.zip
Update to CopyableTest from example
package org.jvnet.jaxb2_commons.tests.one;
import java.io.File;
import java.lang.reflect.Method;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import junit.framework.Assert;
import org.jvnet.jaxb2_commons.lang.builder.JAXBCopyBuilder;
import org.jvnet.jaxb2_commons.lang.builder.JAXBEqualsBuilder;
import org.jvnet.jaxb2_commons.test.AbstractSamplesTest;
public class CopyableTest extends AbstractSamplesTest {
    @Override
    protected void checkSample(File sample) throws Exception {
        final Object object =
createContext().createUnmarshaller().unmarshal(
                sample);
        final JAXBCopyBuilder builder = new JAXBCopyBuilder();
        final Object copy = builder.copy(object);
        final JAXBEqualsBuilder equalsBuilder = new JAXBEqualsBuilder();
        Assert.assertTrue("Source and copy must be equal.", equalsBuilder
                .append(object, copy).isEquals());
    }
    public void testGregorianCalendar() throws Exception {
        XMLGregorianCalendar calendar =
DatatypeFactory.newInstance().newXMLGregorianCalendar();
        XMLGregorianCalendar clonedCalendar =
(XMLGregorianCalendar)calendar.clone();
        assertEquals(calendar, clonedCalendar);
        assertTrue(calendar != clonedCalendar);
        Method cloneMethod = calendar.getClass().getMethod("clone");
        assertTrue(cloneMethod.isAccessible());
    }
}