11 Replies Latest reply on Sep 26, 2014 5:02 AM by Jozef Hartinger

    NPE in BackedAnnotatedMethod.<init>

    Alexander Eckert Newbie

      Hi,

       

      I use Weld in conjunction with Java 8. I found nothing about the current state of Java 8 support.

      So I ask here before sending a bug report. I reduced my to a few lines of code:

       

      public class ClassToInject {
           public <E extends MyClass> Collection<E> readAlll(Class<E> type) throws Exception {
                return Files.walk(path)
                     .map(p -> load(p, type))
                     .collect(Collectors.toList());
           }
      
           private <E extends MyClass> E load(Path p, Class<E> type) {
                return null;
           }
      }
      

       

      Line 4 causes a NPE in the constructor of BackedAnnotatedMethod as method.getGenericReturnType() return null for the lambda expression.

      I have two workarounds:

      1. replace .map(p -> load(p, type)) with .map(p -> (MyClass) load(p, type)) and casting the return value to Collection<E>
      2. use an anonymous inner class implementng Function

      Extracting the lambda expression to an local variable of type Function<Path, E> does not help (if I still use a lambda expression).

       

      Greetings

      Alex

        • 1. Re: NPE in BackedAnnotatedMethod.<init>
          Martin Kouba Master

          Hi Alexander,

          Java 8 support is on the agenda right now. There are already some issues reported: https://issues.jboss.org/issues/?jql=project%20%3D%20Weld%20and%20labels%20%3D%20java8. Please create a new issue for your use case (simple reproducer app or arquillian test would be helpful).

          • 2. Re: NPE in BackedAnnotatedMethod.<init>
            Martin Kouba Master

            Alexander, could you also post the full stack trace?

            • 4. Re: NPE in BackedAnnotatedMethod.<init>
              Martin Kouba Master

              alexander.eckert I'm not able to reproduce the problem. What version of JDK do you use? In my opinion, Method.getGenericReturnType() should not return null for any lambda expression.

              • 5. Re: NPE in BackedAnnotatedMethod.<init>
                Alexander Eckert Newbie

                Just wanted to say that I'll create an issue. But you already did. I hope I can give more details this weekend. I had this problem when running or debugging a unit test with eclipse and testng. Have to try command line.

                • 6. Re: Re: NPE in BackedAnnotatedMethod.<init>
                  Alexander Eckert Newbie

                  ulke I've testet it with Oracle JDK 1.8.0 64bit on (Linux) command line and I had no problems.

                  When running with Eclipse (Kepler SR2) I'll get this stacktrace.

                   

                  java.lang.NullPointerException
                      at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:213)
                      at com.google.common.cache.LocalCache.get(LocalCache.java:3933)
                      at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3938)
                      at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4821)
                      at org.jboss.weld.util.cache.LoadingCacheUtils.getCacheValue(LoadingCacheUtils.java:52)
                      at org.jboss.weld.resources.SharedObjectCache.getTypeClosureHolder(SharedObjectCache.java:80)
                      at org.jboss.weld.annotated.slim.backed.BackedAnnotatedMember.initTypeClosure(BackedAnnotatedMember.java:29)
                      at org.jboss.weld.annotated.slim.backed.BackedAnnotated.<init>(BackedAnnotated.java:19)
                      at org.jboss.weld.annotated.slim.backed.BackedAnnotatedMember.<init>(BackedAnnotatedMember.java:23)
                      at org.jboss.weld.annotated.slim.backed.BackedAnnotatedCallable.<init>(BackedAnnotatedCallable.java:33)
                      at org.jboss.weld.annotated.slim.backed.BackedAnnotatedMethod.<init>(BackedAnnotatedMethod.java:38)
                      at org.jboss.weld.annotated.slim.backed.BackedAnnotatedMethod.of(BackedAnnotatedMethod.java:32)
                      at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType$BackedAnnotatedMethods.computeValue(BackedAnnotatedType.java:193)
                      at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType$BackedAnnotatedMethods.computeValue(BackedAnnotatedType.java:186)
                      at org.jboss.weld.util.LazyValueHolder.get(LazyValueHolder.java:35)
                      at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType$EagerlyInitializedLazyValueHolder.<init>(BackedAnnotatedType.java:154)
                      at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType$BackedAnnotatedMethods.<init>(BackedAnnotatedType.java:186)
                      at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType$BackedAnnotatedMethods.<init>(BackedAnnotatedType.java:186)
                      at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType.<init>(BackedAnnotatedType.java:66)
                      at org.jboss.weld.annotated.slim.backed.BackedAnnotatedType.of(BackedAnnotatedType.java:47)
                      at org.jboss.weld.resources.ClassTransformer$TransformClassToBackedAnnotatedType.load(ClassTransformer.java:83)
                      at org.jboss.weld.resources.ClassTransformer$TransformClassToBackedAnnotatedType.load(ClassTransformer.java:80)
                      at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3524)
                      at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2317)
                      at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2280)
                      at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2195)
                      at com.google.common.cache.LocalCache.get(LocalCache.java:3934)
                      at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3938)
                      at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4821)
                      at org.jboss.weld.util.cache.LoadingCacheUtils.getCacheValue(LoadingCacheUtils.java:52)
                      at org.jboss.weld.util.cache.LoadingCacheUtils.getCastCacheValue(LoadingCacheUtils.java:80)
                      at org.jboss.weld.resources.ClassTransformer.getBackedAnnotatedType(ClassTransformer.java:175)
                      at org.jboss.weld.resources.ClassTransformer.getBackedAnnotatedType(ClassTransformer.java:194)
                      at org.jboss.weld.bootstrap.AnnotatedTypeLoader.loadAnnotatedType(AnnotatedTypeLoader.java:78)
                      at org.jboss.weld.bootstrap.AnnotatedTypeLoader.loadAnnotatedType(AnnotatedTypeLoader.java:60)
                      at org.jboss.weld.bootstrap.BeanDeployer.addClass(BeanDeployer.java:97)
                      at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$1.doWork(ConcurrentBeanDeployer.java:65)
                      at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$1.doWork(ConcurrentBeanDeployer.java:62)
                      at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:60)
                      at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:53)
                      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
                      at java.lang.Thread.run(Thread.java:744)
                  
                  

                   

                  I'll get this when I run TestNG and Weld is used by DeltaSpike. To simplify it, I've written a small sample application.

                  If I call mvn compile exec:java -Dexec.mainClass="de.eckerta.weld.problem.App" from command line everything is fine.

                  But if I start from within eclipse I get an exception. In fact it is not important where I start the application, it is imorten where I compile it.

                  So it probably is a problem with the eclipse compiler.

                  • 7. Re: Re: NPE in BackedAnnotatedMethod.<init>
                    Martin Kouba Master

                    Yes, Eclipse has its own incremental Java compiler. Let me check your sample and verify the weld code...

                    • 8. Re: NPE in BackedAnnotatedMethod.<init>
                      Martin Kouba Master

                      It looks like an Eclipse compiler bug - for some reason the reflection API is not able to reconstruct the generic return type for a parameterized type.

                      • 9. Re: Re: NPE in BackedAnnotatedMethod.<init>
                        Theo Gülcher Newbie

                        On Wildfly 8.1.0.Final with patched Weld 2.2.5.Final...

                         

                        I have the same problem with a Lambda expression like this:

                         

                          int index = Collections.binarySearch(results, key, (o1, o2) -> {

                          Date o1ResetTime = CalendarDateUtil.resetTimeOfDate(o1.getDate(), TimeZone.getDefault());

                          Date o2ResetTime = CalendarDateUtil.resetTimeOfDate(o2.getDate(), TimeZone.getDefault());

                          int result1 = o1ResetTime.compareTo(o2ResetTime);

                          if (result1 == 0) {

                          result1 = o1.getSequence().compareTo(o2.getSequence());

                          }

                          return result1;

                          });

                         

                        I replaced with old style:

                         

                          int index = Collections.binarySearch(results, key, new Comparator<T>() {

                         

                          @Override

                          public int compare(T o1, T o2) {

                          Date o1ResetTime = CalendarDateUtil.resetTimeOfDate(o1.getDate(), TimeZone.getDefault());

                          Date o2ResetTime = CalendarDateUtil.resetTimeOfDate(o2.getDate(), TimeZone.getDefault());

                          int result = o1ResetTime.compareTo(o2ResetTime);

                          if (result == 0) {

                          result = o1.getSequence().compareTo(o2.getSequence());

                          }

                          return result;

                          }

                         

                          });

                        • 10. Re: NPE in BackedAnnotatedMethod.<init>
                          Theo Gülcher Newbie

                          Hi Martin,

                           

                          I think however that if the reflection API cannot construct the generic return type, WELD should throw an exception. To find the cause of the NullPointer Exception there was now no other way then to debug into WELD to find the exact cause of the Exception.

                           

                          Regards,

                           

                          Theo Gülcher

                          • 11. Re: NPE in BackedAnnotatedMethod.<init>
                            Jozef Hartinger Master

                            This is most likely a bug in the eclipse compiler. The API should normally never return null. Therefore, it is not checked by Weld. Weld cannot really check every value returned from a public API for compliance with its contract.