0

I am using aspectJ and facing a problem "nullPointerException" in jBoss6 as advices of aspect is not getting applied.

I have a multi maven modules project and each module is separated out as mentioned below.

"maven - submodule1":

Below FieldAspect is defined in maven sub module - "submodule1".

@Aspect
public abstract class FieldAspect {
@Pointcut
public abstract void getField();

@Pointcut
public abstract void setField();

@Around("getField()")
public String getFieldValue() {
return "Andy";
}
@Around("setField()")
public void setFieldValue() {
throw new RuntimeException();
}
}

Related aop.xml is defined in the same maven sub module - "submodule1".

<?xml version="1.0" encoding="UTF-8"?>
<aspectj>
<aspects>
<concrete-aspect name="main.java.aop.field.MyFieldAspect" extends="main.java.aop.field.FieldAspect">
<pointcut name="getField" expression="get(@main.java.aop.field.Field * *)" />
<pointcut name="setField" expression="set(@main.java.aop.field.Field * *)" />
</concrete-aspect>
</aspects>
</aspectj>

pom.xml of "submodule1" contains below entries.

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.2</version>
</dependency>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<configuration>
<complianceLevel>1.6</complianceLevel>
<source>1.6</source>
<target>1.6</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>       

"maven - submodule2":

An annotation is described as shown below in a maven sub module - say "submodule2".

@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Field {
}

"maven - submodule3":

A string field with above annotation is called in described in another maven sub module - say "submodule3".

@Field
private String value;

I am deploying this multi maven module in jBosss 6 and trying to run the application. When "value" annotated with "Field" is used in the code, "andy" should be the output as aspect should be have been applied using load time weaving (LTW) as aspectjweaver-1.8.5 is used as a aspectj java agent for implementing LTW. But this is not happening in multi maven module project and instead, am getting nullPointerException.

Could anyone please help me to understand whether pointcut defined needs to be corrected OR any special things needed to be done to make this example work.

Many thanks.

Complete exception trace:

java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method) [:1.7.0_67]
at java.lang.Class.forName(Class.java:270) [:1.7.0_67]
at org.jboss.ejb3.jndi.deployers.metadata.SessionBeanTypeWrapper.<init>(SessionBeanTypeWrapper.java:62) [:0.1.7]
at sun.reflect.GeneratedConstructorAccessor171.newInstance(Unknown Source) [:1.7.0_67]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) [:1.7.0_67]
at java.lang.reflect.Constructor.newInstance(Constructor.java:526) [:1.7.0_67]
at org.jboss.reflect.plugins.introspection.ReflectionUtils.newInstance(ReflectionUtils.java:154) [jboss-reflect.jar:2.2.1.SP1]
at org.jboss.reflect.plugins.introspection.ReflectConstructorInfoImpl.newInstance(ReflectConstructorInfoImpl.java:102) [jboss-reflect.jar:2.2.1.SP1]
at org.jboss.joinpoint.plugins.BasicConstructorJoinPoint.dispatch(BasicConstructorJoinPoint.java:81) [jboss-reflect.jar:2.2.1.SP1]
at org.jboss.aop.microcontainer.integration.AOPConstructorJoinpoint.dispatch(AOPConstructorJoinpoint.java:109) [jboss-aop-mc-int.jar:2.2.0.SP2]
at org.jboss.kernel.plugins.dependency.KernelControllerContextAction$JoinpointDispatchWrapper.execute(KernelControllerContextAction.java:257) [jboss-kernel.jar:2.2.0.SP2]
at org.jboss.kernel.plugins.dependency.ExecutionWrapper.execute(ExecutionWrapper.java:47) [jboss-kernel.jar:2.2.0.SP2]
at org.jboss.kernel.plugins.dependency.KernelControllerContextAction.dispatchExecutionWrapper(KernelControllerContextAction.java:125) [jboss-kernel.jar:2.2.0.SP2]
at org.jboss.kernel.plugins.dependency.KernelControllerContextAction.dispatchJoinPoint(KernelControllerContextAction.java:72) [jboss-kernel.jar:2.2.0.SP2]
at org.jboss.kernel.plugins.dependency.InstantiateAction.installActionInternal(InstantiateAction.java:67) [jboss-kernel.jar:2.2.0.SP2]
at org.jboss.kernel.plugins.dependency.InstallsAwareAction.installAction(InstallsAwareAction.java:54) [jboss-kernel.jar:2.2.0.SP2]
at org.jboss.kernel.plugins.dependency.InstallsAwareAction.installAction(InstallsAwareAction.java:42) [jboss-kernel.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.action.SimpleControllerContextAction.simpleInstallAction(SimpleControllerContextAction.java:62) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.action.AccessControllerContextAction.install(AccessControllerContextAction.java:71) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractControllerContextActions.install(AbstractControllerContextActions.java:51) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractControllerContext.install(AbstractControllerContext.java:379) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:2044) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.incrementState(AbstractController.java:1083) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.executeOrIncrementStateDirectly(AbstractController.java:1322) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1246) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1139) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:894) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:641) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.deployers.vfs.deployer.kernel.BeanMetaDataDeployer.deploy(BeanMetaDataDeployer.java:182) [:2.2.2.GA]
at org.jboss.deployers.vfs.deployer.kernel.BeanMetaDataDeployer.deploy(BeanMetaDataDeployer.java:58) [:2.2.2.GA]
at org.jboss.deployers.spi.deployer.helpers.AbstractSimpleRealDeployer.internalDeploy(AbstractSimpleRealDeployer.java:63) [:2.2.2.GA]
at org.jboss.deployers.spi.deployer.helpers.AbstractRealDeployer.deploy(AbstractRealDeployer.java:55) [:2.2.2.GA]
at org.jboss.deployers.plugins.deployers.DeployerWrapper.deploy(DeployerWrapper.java:179) [:2.2.2.GA]
at org.jboss.deployers.plugins.deployers.DeployersImpl.doDeploy(DeployersImpl.java:1832) [:2.2.2.GA]
at org.jboss.deployers.plugins.deployers.DeployersImpl.doInstallParentFirst(DeployersImpl.java:1550) [:2.2.2.GA]
at org.jboss.deployers.plugins.deployers.DeployersImpl.doInstallParentFirst(DeployersImpl.java:1571) [:2.2.2.GA]
at org.jboss.deployers.plugins.deployers.DeployersImpl.doInstallParentFirst(DeployersImpl.java:1603) [:2.2.2.GA]
at org.jboss.deployers.plugins.deployers.DeployersImpl.install(DeployersImpl.java:1491) [:2.2.2.GA]
at org.jboss.dependency.plugins.AbstractControllerContext.install(AbstractControllerContext.java:379) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:2044) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.incrementState(AbstractController.java:1083) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.executeOrIncrementStateDirectly(AbstractController.java:1322) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1246) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1139) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:939) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:654) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.deployers.plugins.deployers.DeployersImpl.change(DeployersImpl.java:1983) [:2.2.2.GA]
at org.jboss.deployers.plugins.deployers.DeployersImpl.process(DeployersImpl.java:1076) [:2.2.2.GA]
at org.jboss.deployers.plugins.main.MainDeployerImpl.process(MainDeployerImpl.java:679) [:2.2.2.GA]
at org.jboss.system.server.profileservice.deployers.MainDeployerPlugin.process(MainDeployerPlugin.java:106) [:6.1.0.Final]
at org.jboss.profileservice.dependency.ProfileControllerContext$DelegateDeployer.process(ProfileControllerContext.java:143) [:0.2.2]
at org.jboss.profileservice.dependency.ProfileDeployAction.deploy(ProfileDeployAction.java:151) [:0.2.2]
at org.jboss.profileservice.dependency.ProfileDeployAction.installActionInternal(ProfileDeployAction.java:94) [:0.2.2]
at org.jboss.kernel.plugins.dependency.InstallsAwareAction.installAction(InstallsAwareAction.java:54) [jboss-kernel.jar:2.2.0.SP2]
at org.jboss.kernel.plugins.dependency.InstallsAwareAction.installAction(InstallsAwareAction.java:42) [jboss-kernel.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.action.SimpleControllerContextAction.simpleInstallAction(SimpleControllerContextAction.java:62) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.action.AccessControllerContextAction.install(AccessControllerContextAction.java:71) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractControllerContextActions.install(AbstractControllerContextActions.java:51) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractControllerContext.install(AbstractControllerContext.java:379) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:2044) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.incrementState(AbstractController.java:1083) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.executeOrIncrementStateDirectly(AbstractController.java:1322) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1246) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1139) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:939) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:654) [jboss-dependency.jar:2.2.0.SP2]
at org.jboss.profileservice.dependency.ProfileActivationWrapper$BasicProfileActivation.start(ProfileActivationWrapper.java:190) [:0.2.2]
at org.jboss.profileservice.dependency.ProfileActivationWrapper.start(ProfileActivationWrapper.java:87) [:0.2.2]
at org.jboss.profileservice.dependency.ProfileActivationService.activateProfile(ProfileActivationService.java:215) [:0.2.2]
at org.jboss.profileservice.dependency.ProfileActivationService.activate(ProfileActivationService.java:159) [:0.2.2]
at org.jboss.profileservice.bootstrap.AbstractProfileServiceBootstrap.activate(AbstractProfileServiceBootstrap.java:112) [:0.2.2]
at org.jboss.profileservice.resolver.BasicResolverFactory$ProfileResolverFacade.deploy(BasicResolverFactory.java:87) [:0.2.2]
at org.jboss.profileservice.bootstrap.AbstractProfileServiceBootstrap.start(AbstractProfileServiceBootstrap.java:91) [:0.2.2]
at org.jboss.system.server.profileservice.bootstrap.BasicProfileServiceBootstrap.start(BasicProfileServiceBootstrap.java:132) [:6.1.0.Final]
at org.jboss.system.server.profileservice.bootstrap.BasicProfileServiceBootstrap.start(BasicProfileServiceBootstrap.java:56) [:6.1.0.Final]
at org.jboss.bootstrap.impl.base.server.AbstractServer.startBootstraps(AbstractServer.java:827) [jboss-bootstrap-impl-base.jar:2.1.0-alpha-6]
at org.jboss.bootstrap.impl.base.server.AbstractServer$StartServerTask.run(AbstractServer.java:417) [jboss-bootstrap-impl-base.jar:2.1.0-alpha-6]
at java.lang.Thread.run(Thread.java:745) [:1.7.0_67]
Caused by: java.lang.NullPointerException
kriegaex
  • 63,017
  • 15
  • 111
  • 202
sridhar
  • 1,117
  • 5
  • 31
  • 59
  • What do you see on the console when you include `` in your *aop.xml*? Oh, and BTW, your project references three different AspectJ versions: The weaving agent is 1.8.5, the runtime according to the POM is 1.8.2 and the compiler contained in AspectJ Maven Plugin 1.6 is 1.7.4. Maybe you want to align those. – kriegaex Feb 07 '15 at 12:32
  • hi kriegaex.. many thanks. i tried the solution but didn't get the result. I also tried the below solution given in http://wiki.eclipse.org/LTWJboss7 for utilizing load time weaving in jBoss6 but it is not. Do I need to define specific classloaders to load aspectJ agent (aspectweaver) to make it work in jBoss 6. – sridhar Feb 09 '15 at 11:22
  • I am getting error - java.lang.ExceptionInInitializerError in jBoss6 and this should be related to configuration issue. nullpointerexception occurs as aspect advices are not applied. – sridhar Feb 09 '15 at 11:31

1 Answers1

0

I tried the solution but didn't get the result.

Which solution? I did not suggest any solution, just some aop.xml settings for the weaver to produce more log output so you can see if any advices get woven into your desired joinpoints.

I am getting error java.lang.ExceptionInInitializerError in JBoss6 and this should be related to configuration issue.

No, it is related to your advice throwing an exception. Quoting your own code:

@Around("setField()")
public void setFieldValue() {
    throw new RuntimeException();
}

The ExceptionInInitializerError occurs inside a static initialiser as described in the JDK documentation. So obviously you use a @Field annotation on a static member, e.g. I can reproduce the exception like this:

@Field
private static String someStaticMember = "dummy value";

There you have a static member with an initializer. So why the heck do you throw an exception whenever a field value is set?

And one more issue with your other pointcut/advice:

@Around("getField()")
public String getFieldValue() {
    return "Andy";
}

This advice always returns a String, so if you declare any fields which are not of type String such as

@Field
private int id;

you will get this AspectJ compilation error: incompatible return type applying to field-get(...)

Please fix your aspect code, your problems clearly come from the aspect being applied. Your assumption that it is not applied is wrong, otherwise the ExceptionInInitializerError would not occur.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • many thanks .. kriegaex. I am using a set method that throws runtime exception and intention behind this is that only in aspect, value should be set and not allowed in other places. In my example, I am only reading the variable. Reason you told is right but I feel this is not because of aspect code. AspectJ logs cannot be observed in jBoss 6 because of the LTW problem as mentioned in http://wiki.eclipse.org/LTWJboss7 and I follow this solution. I have copied the "complete trace" in the question and I feel aspectJ weaving is not all applied. In the trace, jboss-aop related stuffs are observed. – sridhar Feb 10 '15 at 11:40
  • Adding additional info .. I am applying aspect to local singleton EJB and the app server used is jBoss 6. Looking at the trace, the reason you told is right and I am not aware whether aspectJ is usable for EJB. Advice is not getting applied into static block and hence, the error I believe. Is there a limitation that aspectJ is not applicable for EJBs ?. – sridhar Feb 11 '15 at 09:35
  • Can you please publish a minimal sample project as an [SSCCE](http://sscce.org/) on GitHub? Something everybody can use to reproduce your problem? – kriegaex Feb 14 '15 at 12:22