NPE during annotation processing, Maven, source:jar target.

64 views
Skip to first unread message

jan.go...@noesissolutions.com

unread,
Feb 26, 2015, 5:14:19 AM2/26/15
to scala...@googlegroups.com
I am having a weird build problem with Scala/Spring/Maven. I am posting it here since it *looks* like it's Scala related. Not sure though. It happens during "source:jar" target of Maven.

Running JDK6 build 45, Scala 2.11.4, Spring 4.1.3, Hibernate 4.2.15. I've run the very same builds against JDK7, Scala 2.11.3 and Scala 2.11.5. All leading to the same error. So I am a bit puzzled about the exact cause of the error.

Has anybody ever seen something like this before ? And if its a bug, where should I report it ?

Thanks !

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Consider the following code :

@Configuration("configuration")
class SpringConfiguration {

@Bean(autowire = Autowire.BY_TYPE)
def springContext = Spring

}

object Spring extends ApplicationContextAware with DisposableBean {

  ... etc ...

  private val _locale = new ThreadLocal[Locale] {
    override def initialValue(): Locale = defaultLocale
}

  ... etc ...

}

Running the Maven targets "clean compile source:jar" yield the following error :

An annotation processor threw an uncaught exception.
Consult the following stack trace for details.
java.lang.NullPointerException
	at com.sun.tools.javac.jvm.Code.width(Code.java:262)
	at com.sun.tools.javac.jvm.ClassReader.readMemberAttr(ClassReader.java:880)
	at com.sun.tools.javac.jvm.ClassReader.readMemberAttrs(ClassReader.java:1027)
	at com.sun.tools.javac.jvm.ClassReader.readCode(ClassReader.java:1080)
	at com.sun.tools.javac.jvm.ClassReader.readMemberAttr(ClassReader.java:834)
	at com.sun.tools.javac.jvm.ClassReader.readMemberAttrs(ClassReader.java:1027)
	at com.sun.tools.javac.jvm.ClassReader.readMethod(ClassReader.java:1490)
	at com.sun.tools.javac.jvm.ClassReader.readClass(ClassReader.java:1586)
	at com.sun.tools.javac.jvm.ClassReader.readClassFile(ClassReader.java:1658)
	at com.sun.tools.javac.jvm.ClassReader.fillIn(ClassReader.java:1845)
	at com.sun.tools.javac.jvm.ClassReader.complete(ClassReader.java:1777)
	at com.sun.tools.javac.code.Symbol.complete(Symbol.java:384)
	at com.sun.tools.javac.code.Symbol$ClassSymbol.complete(Symbol.java:766)
	at com.sun.tools.javac.code.Symbol$ClassSymbol.flags(Symbol.java:698)
	at com.sun.tools.javac.code.Symbol$TypeSymbol.getEnclosedElements(Symbol.java:525)
	at javax.lang.model.util.ElementScanner6.visitPackage(ElementScanner6.java:141)
	at com.sun.tools.javac.code.Symbol$PackageSymbol.accept(Symbol.java:630)
	at com.sun.tools.javac.processing.JavacRoundEnvironment$AnnotationSetScanner.scan(JavacRoundEnvironment.java:139)
	at com.sun.tools.javac.processing.JavacRoundEnvironment$AnnotationSetScanner.scan(JavacRoundEnvironment.java:122)
	at com.sun.tools.javac.processing.JavacRoundEnvironment.getElementsAnnotatedWith(JavacRoundEnvironment.java:116)
	at org.hibernate.validator.ap.ConstraintValidationProcessor.process(ConstraintValidationProcessor.java:117)

The annotation processor is configured as follows :

<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>2.2.4</version>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
</execution>
<execution>
<id>process-test</id>
<goals>
<goal>process-test</goal>
</goals>
<phase>generate-test-sources</phase>
</execution>
</executions>
<configuration>
<detail>true</detail>
<options>
<verbose>true</verbose>
<diagnosticKind>NOTE</diagnosticKind>
</options>
<outputDiagnostics>true</outputDiagnostics>
</configuration>
</plugin>

Apparently the origin of the NPE can be traced back to the override of the threadlocal variable _locale. If the override is removed it all works. 

  private val _locale = new ThreadLocal[Locale] {
    override def initialValue(): Locale = defaultLocale
}

Replaced by

  private val _locale = new ThreadLocal[Locale]

jan.go...@noesissolutions.com

unread,
Mar 2, 2015, 4:38:48 AM3/2/15
to scala...@googlegroups.com
Yet another case. It seems the annotation processor plugin can't cope with vals having an initializer expression. 

Is there really nobody who has a clue about the problem ?

val resourceKeys: String = {
val properties = new Properties
val is = getClass.getClassLoader.getResourceAsStream("bundles/labels.properties")
properties.load(is)
properties.keys.asScala.map(_.toString).toSeq.sorted.mkString(" ")
}

Works when replaced by: 

def resourceKeys: String = {
val properties = new Properties
val is = getClass.getClassLoader.getResourceAsStream("bundles/labels.properties")
properties.load(is)
properties.keys.asScala.map(_.toString).toSeq.sorted.mkString(" ")
}

Jason Zaugg

unread,
Mar 4, 2015, 12:17:16 AM3/4/15
to jan.go...@noesissolutions.com, scala-user
On Mon, Mar 2, 2015 at 7:38 PM, <jan.go...@noesissolutions.com> wrote:
Yet another case. It seems the annotation processor plugin can't cope with vals having an initializer expression. 

Is there really nobody who has a clue about the problem ?

Hi Jan,

I've created a small test project that test interop between Scala class files and the Java annotation processing framework Unfortunately, I can’t reproduce your error.

Could you either try to modify that project (by trying different Scala constructs in sample.ScalaClass) to see if you can reproduce the error? Alternatively, could you create a standalone test project using the that Maven plugin to trigger the error?

From a quick look at the OpenJDK code, the stack trace suggests that a class file contains a LocalVariableTable attribute (used to store variable names for debuggers) in a part of the class file where it doesn’t make sense. You could try adding -g:none to the Scala compiler options to disable that part of the classfile writing to test this theory.

I notice that that NetBeans has a fork of the JDK code that throws this NPE, and in that fork they have hardened against the null parameter type list.

-jason

Jan Goyvaerts

unread,
Mar 4, 2015, 4:52:50 AM3/4/15
to Jason Zaugg, scala-user
Hello Jason,

Our the setup is a bit different. It's a traditional multi-module Maven application running both Java and Scala with Spring/Hibernate/....  I'll try to distill a simple case from that using the very same libraries, plugins, ... 

I noticed the Netbeans info also. What I think to be a bit hard to believe is that it's actually the Java Compiler throwing an NPE ! Presumably by an abuse of some API somewhere. I was expecting it to work when using JDK7 - still on JDK6. Alas ...

Here's the full stack trace.

java.lang.NullPointerException
at com.sun.tools.javac.jvm.Code.width(Code.java:262)
at com.sun.tools.javac.jvm.ClassReader.readMemberAttr(ClassReader.java:880)
at com.sun.tools.javac.jvm.ClassReader.readMemberAttrs(ClassReader.java:1027)
at com.sun.tools.javac.jvm.ClassReader.readCode(ClassReader.java:1080)
at com.sun.tools.javac.jvm.ClassReader.readMemberAttr(ClassReader.java:834)
at com.sun.tools.javac.jvm.ClassReader.readMemberAttrs(ClassReader.java:1027)
at com.sun.tools.javac.jvm.ClassReader.readMethod(ClassReader.java:1490)
at com.sun.tools.javac.jvm.ClassReader.readClass(ClassReader.java:1586)
at com.sun.tools.javac.jvm.ClassReader.readClassFile(ClassReader.java:1658)
at com.sun.tools.javac.jvm.ClassReader.fillIn(ClassReader.java:1845)
at com.sun.tools.javac.jvm.ClassReader.complete(ClassReader.java:1777)
at com.sun.tools.javac.code.Symbol.complete(Symbol.java:384)
at com.sun.tools.javac.code.Symbol$ClassSymbol.complete(Symbol.java:766)
at com.sun.tools.javac.code.Symbol$ClassSymbol.flags(Symbol.java:698)
at com.sun.tools.javac.code.Symbol$TypeSymbol.getEnclosedElements(Symbol.java:525)
at javax.lang.model.util.ElementScanner6.visitPackage(ElementScanner6.java:141)
at com.sun.tools.javac.code.Symbol$PackageSymbol.accept(Symbol.java:630)
at com.sun.tools.javac.processing.JavacRoundEnvironment$AnnotationSetScanner.scan(JavacRoundEnvironment.java:139)
at com.sun.tools.javac.processing.JavacRoundEnvironment$AnnotationSetScanner.scan(JavacRoundEnvironment.java:122)
at com.sun.tools.javac.processing.JavacRoundEnvironment.getElementsAnnotatedWith(JavacRoundEnvironment.java:116)
at org.hibernate.validator.ap.ConstraintValidationProcessor.process(ConstraintValidationProcessor.java:117)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:627)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:556)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:701)
at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:987)
at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:727)
at com.sun.tools.javac.main.Main.compile(Main.java:353)
at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:113)
at org.bsc.maven.plugin.processor.AbstractAnnotationProcessorMojo.executeWithExceptionsHandled(AbstractAnnotationProcessorMojo.java:545)
at org.bsc.maven.plugin.processor.AbstractAnnotationProcessorMojo.execute(AbstractAnnotationProcessorMojo.java:285)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.MojoExecutor.executeForkedExecutions(MojoExecutor.java:364)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:198)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:355)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:216)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:160)
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.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
at org.codehaus.classworlds.Launcher.main(Launcher.java:47)
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
--
===============================================================
Jan Goyvaerts
Senior Software Engineer
Noesis Solutions NV 
Gaston Geenslaan 11, B4 
3001 Leuven, Belgium

+32 (0)16 317 040 phone 
+32 (0)16 317 048 fax

jan.go...@noesissolutions.com
www.noesissolutions.com

===============================================================


Reply all
Reply to author
Forward
0 new messages