6 Replies Latest reply on Mar 28, 2017 1:50 AM by pmm

    Wildfly and Reflections library

    avishaybennatan

      Hi

      I am trying to scan classes with Reflections library, if I add the Dynamic Web project to another project (plain Java one), I get the classes I want, if run inside a @Startup bean, it is empty.

      Here is the code:

      Reflections reflections = new Reflections(new ConfigurationBuilder().filterInputsBy(new FilterBuilder.Include(FilterBuilder.prefix("my.package"))).setUrls(ClasspathHelper.forJavaClassPath()).setScanners(new SubTypesScanner(false)));
      Set<Class<? extends Object>> testClasses = reflections.getSubTypesOf(Object.class);
      ;

      The tv,goopi should be changed to whatever package prefix used.

      the testClasses Set is empty.

       

      If the same code is running in a different project referencing this one, no other change, then the Set is populated with all classes inside the package.

       

      The Maven dependency for Reflections is:

      <dependency>
      <groupId>org.reflections</groupId>
      <artifactId>reflections</artifactId>
      <version>0.9.9-RC1</version>

      </dependency>

      Wildfly 8.2.0

      For now, I can save the file extracted in the external project and use the load function, but this will not be dynamic as it should be.

      Thanks

        • 1. Re: Wildfly and Reflections library
          fharms

          Not entirely sure what you are trying to achieve, but it sound like a class loader issue. Depending on which archive type you are using, you might have to give access to the classes you want to scan

           

          Look into jboss-deployment-structure.xml Class Loading in WildFly - WildFly 8 - Project Documentation Editor

           

          /Flemming

          • 2. Re: Wildfly and Reflections library
            pmm

            Avishay Ben Natan wrote:

             

            Hi

            I am trying to scan classes with Reflections library,

            Don't

            • 3. Re: Wildfly and Reflections library
              robert.baum

              Might be a bit late, but I was having similar issues when running on Wildfly and managed to get it working. I fixed it by instantiating the Reflections object with a servletContext.

               

              To get a servletContext, the class you are using this in needs to implement ServletContextAware and Override setServletContext. To ensure the Reflections object gets set up correctly with the servletContext, I also implemented InitializingBean and instantiated it in afterPropertiesSet

               

              if (servletContext.getResourcePaths("/WEB-INF/lib") != null) {     //This folder doesn't exist if running in IDE for me.

                   urlsToScan.addAll(ClasspathHelper.forWebInfLib(servletContext));

                   urlsToScan.add(ClasspathHelper.forWebInfClasses(servletContext));

              } else {

                   urlsToScan.addAll(ClasspathHelper.forJavaClassPath());

              }

               

              final Reflections eligibleClasses = new Reflections(new ConfigurationBuilder().setScanners(new SubTypesScanner(false)).setUrls(urlsToScan)

                                  .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix(basePackage))));

              //Use it as normal . . .

               

              Hope someone finds this useful.

              • 4. Re: Wildfly and Reflections library
                kot.filemon

                > Don't

                 

                could You write why not?

                 

                is it connected with the library or just generaly with java reflection API?

                 

                any recommended way to use reflection in Wildfly?

                • 5. Re: Wildfly and Reflections library
                  russ396
                  Reflections reflections = new Reflections("com");

                   

                  This worked for me.

                  • 6. Re: Wildfly and Reflections library
                    pmm

                    Marcin M wrote:

                     

                    > Don't

                     

                    could You write why not?

                    You can not introspect into a class loader, this is not supported by the class loader API. Only the container can introspect into the class loader as it controls the class loader. The only way you can introspect into a class loader is making a whole lot of assumptions that are not guaranteed and will fail eventually.

                    If you insist I would recommend introspecting at build time and writing that information into a file that you include in your deployment and then read that file at runtime.