Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 363962 - NPE got when dealing with Bootstrap classloaded classes
Summary: NPE got when dealing with Bootstrap classloaded classes
Status: RESOLVED FIXED
Alias: None
Product: AspectJ
Classification: Tools
Component: Compiler (show other bugs)
Version: 1.6.12   Edit
Hardware: PC Mac OS X - Carbon (unsup.)
: P3 major (vote)
Target Milestone: 1.7.0   Edit
Assignee: aspectj inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-11-16 14:24 EST by Abraham Nevado CLA
Modified: 2012-04-03 16:17 EDT (History)
1 user (show)

See Also:


Attachments
Patch (1.30 KB, patch)
2011-11-16 14:26 EST, Abraham Nevado CLA
aclement: iplog+
Details | Diff
TestCase attachment (2.15 KB, text/plain)
2011-11-16 14:51 EST, Abraham Nevado CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Abraham Nevado CLA 2011-11-16 14:24:15 EST
Using a LifeRay + Spring + jBoss  + extra LTW  we got the next exception:

2011-11-15 17:23:23,735 ERROR [org.springframework.web.context.ContextLoader] (main) Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.liferay.portal.spring.bean.BeanReferenceAnnotationBeanPostProcessor#0' defined in class path resource [META-INF/base-spring.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception; nested exception is java.lang.NullPointerException
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:452)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
        at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:722)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:410)
        at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
        at com.liferay.portal.spring.context.PortalContextLoaderListener.contextInitialized(PortalContextLoaderListener.java:65)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3910)


A LOT OF CAUSED BY - NESTED:
[...]


Caused by: java.lang.NullPointerException
        at org.aspectj.weaver.WeakClassLoaderReference.<init>(WeakClassLoaderReference.java:43)
        at org.aspectj.weaver.tools.PointcutParser.setClassLoader(PointcutParser.java:218)
        at org.aspectj.weaver.tools.PointcutParser.<init>(PointcutParser.java:205)
        at org.aspectj.weaver.tools.PointcutParser.getPointcutParserSupportingSpecifiedPrimitivesAndUsingSpecifiedClassLoaderForResolution(PointcutParser.java:167)
        at org.springframework.aop.aspectj.AspectJExpressionPointcut.initializePointcutParser(AspectJExpressionPointcut.java:207)
        at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:189)
        at org.springframework.aop.aspectj.AspectJExpressionPointcut.checkReadyToMatch(AspectJExpressionPointcut.java:181)
        at org.springframework.aop.aspectj.AspectJExpressionPointcut.getMethodMatcher(AspectJExpressionPointcut.java:167)
        at org.springframework.aop.aspectj.AbstractAspectJAdvice.buildSafePointcut(AbstractAspectJAdvice.java:188)
        at org.springframework.aop.aspectj.AspectJPointcutAdvisor.<init>(AspectJPointcutAdvisor.java:51)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126)
        ... 96 more


Where, the code where the NPE is arising is:

	public WeakClassLoaderReference(ClassLoader loader) {
42->		loaderRef = new WeakReference(loader);
43->		hashcode = loader.hashCode() * 37;
	}


Because when setting the classLoader for the PointcutParser a null is received: 

	private PointcutParser(Set<PointcutPrimitive> supportedPointcutKinds) {
		supportedPrimitives = supportedPointcutKinds;
		for (PointcutPrimitive pointcutPrimitive : supportedPointcutKinds) {
			if ((pointcutPrimitive == PointcutPrimitive.IF) || (pointcutPrimitive == PointcutPrimitive.CFLOW)
					|| (pointcutPrimitive == PointcutPrimitive.CFLOW_BELOW)) {
				throw new UnsupportedOperationException("Cannot handle if, cflow, and cflowbelow primitives");
			}
		}
205->		setClassLoader(PointcutParser.class.getClassLoader());
	}


I guess this is caused because  the PointcutParser is considered a base type (loaded from a bootstrap class loader), so the class.getClassLoader that is involved returns null as it was loaded from the javaagent option. This happens when both types of Weaving (loadtime + offline) are put in place...

I would like to suggest the next patch:

	public WeakClassLoaderReference(ClassLoader loader) {
		loaderRef = new WeakReference(loader);
		if(loader == null){
			hashcode = System.identityHashCode(this);
		}else{
			hashcode = loader.hashCode() * 37;
		}
	}

Please find it attached to this bug report.

What do you think?

Thanks in advance,

Best regards.
Comment 1 Abraham Nevado CLA 2011-11-16 14:26:43 EST
Created attachment 207112 [details]
Patch

Proposed patch
Comment 2 Abraham Nevado CLA 2011-11-16 14:51:15 EST
Created attachment 207114 [details]
TestCase attachment

This is a diffpatch for tests (added to 1610 repo, as far is the latest test I can bind to right now).
Comment 3 Andrew Clement CLA 2011-11-17 16:20:41 EST
thanks for the patch Abraham.
All committed.