2 Replies Latest reply on Jun 2, 2016 6:04 AM by fwiedenmann

    Weld-Se 2.3.4.Final: "Enclosing method not found" on anonymous inner class in lambda expression

    fwiedenmann

      Hi y'all!

       

      I just stumbled upon the following problem and I'm not sure whether it's according to the specification (just getting started with weld) or whether it's a bug.

       

      When a bean includes a method where within a lambda expression an anonymous inner class is created, the initalization of the weld container fails.

       

      Here's a simple demonstration:

       

      Create a class containing the anonymous inner class:

       

       

      import java.util.function.Supplier;
      
      public class Demo {
              // a random method - it doesn't have to be called
          public Supplier<Object> getSupplier() {
              // probably any lambda will do - using supplier as simple example  
              final Supplier<Object> s = () -> {
                              // this causes the problem - the anonymous new object
                  return new Object(){                              
                      public String toString() {
                          return "";
                      };
                  };
              };              
              return s;
          }  
      }
      

       

       

      Then try to initialize weld in a different class:

       

       

      import org.jboss.weld.environment.se.Weld;
      import org.jboss.weld.environment.se.WeldContainer;
      
      public class Application {
          public static void main(String[] args) {
              final Weld weld = new Weld();
              try(final WeldContainer c = weld.initialize()){          
              };
          }
      }
      

       

      As a result you should (hopefully) get this error:

       

      Exception in thread "main" org.jboss.weld.exceptions.DeploymentException: Enclosing method not found
          at org.jboss.weld.executor.AbstractExecutorServices.checkForExceptions(AbstractExecutorServices.java:66)
          at org.jboss.weld.executor.AbstractExecutorServices.invokeAllAndCheckForExceptions(AbstractExecutorServices.java:43)
          at org.jboss.weld.executor.AbstractExecutorServices.invokeAllAndCheckForExceptions(AbstractExecutorServices.java:51)
          at org.jboss.weld.bootstrap.ConcurrentBeanDeployer.createClassBeans(ConcurrentBeanDeployer.java:75)
          at org.jboss.weld.bootstrap.BeanDeployment.createBeans(BeanDeployment.java:256)
          at org.jboss.weld.bootstrap.WeldStartup.deployBeans(WeldStartup.java:396)
          at org.jboss.weld.bootstrap.WeldBootstrap.deployBeans(WeldBootstrap.java:83)
          at org.jboss.weld.environment.se.Weld.initialize(Weld.java:584)
          at anonymous_inner_class_weld.Application.main(Application.java:10)
      Caused by: java.lang.InternalError: Enclosing method not found
          at java.lang.Class.getEnclosingMethod(Unknown Source)
          at org.jboss.weld.util.reflection.Reflections.isStaticNestedClass(Reflections.java:402)
          at org.jboss.weld.util.reflection.Reflections.isTopLevelOrStaticNestedClass(Reflections.java:422)
          at org.jboss.weld.util.Beans.isTypeManagedBeanOrDecoratorOrInterceptor(Beans.java:506)
          at org.jboss.weld.bootstrap.BeanDeployer.createClassBean(BeanDeployer.java:213)
          at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$2.doWork(ConcurrentBeanDeployer.java:78)
          at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$2.doWork(ConcurrentBeanDeployer.java:75)
          at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:63)
          at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:56)
          at java.util.concurrent.FutureTask.run(Unknown Source)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
          at java.lang.Thread.run(Unknown Source)
      

      A work-around is moving the creation of the anonymous class into another method:

      import java.util.function.Supplier;
      
      public class Demo {
          public Supplier<Object> getSupplier() {          
              final Supplier<Object> s = this::getNewObject;          
              return s;
          }  
        
          public Object getNewObject(){
              return new Object(){
                  public String toString() {
                      return "";
                  };
              };
          }
      }
      

       

      I'd appreciate any insight into the nature of the problem!

       

      Thank you!

       

      P.S.:

       

      I'm using jdk1.8.0_91.