Announcement Announcement Module
Collapse
No announcement yet.
getPackage() returns null for classes loaded by ShadowingClassLoader Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • getPackage() returns null for classes loaded by ShadowingClassLoader

    I've found that if I use AbstractJpaTests to run my Hibernate JPA tests, Hibernate will fall over with a NullPointerException when attempting to build the mappings for the entities. Hibernate calls getPackage() on the package-info but gets a null back.
    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [context.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
    Caused by: java.lang.NullPointerException
    	at org.hibernate.annotations.common.reflection.java.JavaAnnotationReader.isAnnotationPresent(JavaAnnotationReader.java:27)
    	at org.hibernate.annotations.common.reflection.java.JavaXAnnotatedElement.isAnnotationPresent(JavaXAnnotatedElement.java:43)
    	at org.hibernate.cfg.AnnotationBinder.bindPackage(AnnotationBinder.java:220)
    	at org.hibernate.cfg.AnnotationConfiguration.addPackage(AnnotationConfiguration.java:165)
    	at org.hibernate.ejb.Ejb3Configuration.addClassesToSessionFactory(Ejb3Configuration.java:919)
    	at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:792)
    	at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:416)
    	at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:126)
    	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:218)
    	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:251)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1175)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1145)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:427)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:144)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:276)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:360)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.springframework.test.jpa.AbstractJpaTests.runBare(AbstractJpaTests.java:214)
    	at junit.framework.TestResult$1.protect(TestResult.java:110)
    	at junit.framework.TestResult.runProtected(TestResult.java:128)
    	at junit.framework.TestResult.run(TestResult.java:113)
    	at junit.framework.TestCase.run(TestCase.java:124)
    	at junit.framework.TestSuite.runTest(TestSuite.java:232)
    	at junit.framework.TestSuite.run(TestSuite.java:227)
    	at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:35)
    	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
    	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
    If I explicitly load the package in the test constructor before the test is run then the test will pass.

    I've finally tracked the problem down to the OrmXmlOverridingShadowingClassLoader (of type ShadowingClassLoader) that is used by the tests. Defining the package at the point the Class is loaded seems to do the trick. He is my modified doLoadClass(String name) from ShadowingClassLoader
    Code:
        private Class doLoadClass(String name) throws ClassNotFoundException {
            String internalName = StringUtils.replace(name, ".", "/") + ".class";
            InputStream is = this.enclosingClassLoader.getResourceAsStream(internalName);
            if (is == null) {
                throw new ClassNotFoundException(name);
            }
            try {
                byte[] bytes = FileCopyUtils.copyToByteArray(is);
                bytes = applyTransformers(name, bytes);
                Class cls = defineClass(name, bytes, 0, bytes.length);
                this.classCache.put(name, cls);
    
                // Additional check for package
                if (cls.getPackage() == null) {
                    String packageName = name.substring(0, name.lastIndexOf('.'));
                    definePackage(packageName, null, null, null, null, null, null, null);
                }
    
                return cls;
            }
            catch (IOException ex) {
                throw new ClassNotFoundException("Cannot load resource for class [" + name + "]", ex);
            }
        }
    Should I raise this in JIRA?

  • #2
    Originally posted by eddybongo View Post
    Should I raise this in JIRA?
    Can't hurt. Attach your patch, or a reference to this post, sand see what happens.

    Comment

    Working...
    X