Uploaded image for project: 'Weld'
  1. Weld
  2. WELD-2535

Weld handleJar does not handle packages with overlapping names correct

XMLWordPrintable

      We are using weld-junit using the JUnit 4 extension and JUnit @Rule annotation.

      We create the Weld instance using the WeldInitiator as follows

      WeldInitiator.createWeld().addPackage(false, ....)...

      If we have two packages that overlap in name and each of these two packages have an implementation of an Interface that later will be used for injection we get the following error:

      WELD-001409: Ambiguous dependencies for type...

      Example of package structure

      .../AnInterface
      .../impl1/Impl1OfAnInterface
      .../impl1extension/Impl2OfAnInterface

      When including the package corresponding to the class Impl1OfAnInterface using

      ...addPackage(false, Impl1OfAnInterface.class)....

      this will also include Impl2OfAnInterface. This is due to the following code line in class org.jboss.weld.environment.se.Weld, method handleJar:

      if (entry.getName().startsWith(packNamePath)) {
      

      Which should read something like this instead

      if (entry.getName().startsWith(packNamePath + '/')) {
      

      The full method looks like this:

          private void handleJar(URI resourceUri, boolean scanRecursively, String packName, Set<String> foundClasses) {
      
              // Currently we only support jar:file
              if (resourceUri.getSchemeSpecificPart().startsWith(PROCOTOL_FILE)) {
      
                  // Get the JAR file path, e.g. "jar:file:/home/duke/duke.jar!/com/foo/Bar" becomes "/home/duke/duke.jar"
                  String path = resourceUri.getSchemeSpecificPart().substring(PROTOCOL_FILE_PART.length());
                  if (path.lastIndexOf(JAR_URL_SEPARATOR) > 0) {
                      path = path.substring(0, path.lastIndexOf(JAR_URL_SEPARATOR));
                  }
      
                  JarFile jar = null;
                  String packNamePath = packName.replace('.', '/');
                  int expectedPartsLength = splitBySlash(packNamePath).length + 1;
      
                  try {
                      jar = new JarFile(new File(path));
                      Enumeration<JarEntry> entries = jar.entries();
                      while (entries.hasMoreElements()) {
                          JarEntry entry = entries.nextElement();
                          if (!entry.getName().endsWith(Files.CLASS_FILE_EXTENSION)) {
                              continue;
                          }
                          if (entry.getName().startsWith(packNamePath)) {
                              if (scanRecursively) {
                                  foundClasses.add(Files.filenameToClassname(entry.getName()));
                              } else {
                                  String[] parts = splitBySlash(entry.getName());
                                  if (parts.length == expectedPartsLength) {
                                      foundClasses.add(Files.filenameToClassname(entry.getName()));
                                  }
                              }
                          }
                      }
                  } catch (IOException e) {
                      CommonLogger.LOG.couldNotReadResource(resourceUri, e);
                  } finally {
                      if (jar != null) {
                          try {
                              jar.close();
                          } catch (IOException ignored) {
                          }
                      }
                  }
              }
          }
      

              manovotn Matěj Novotný
              itsmeden Lars-Fredrik Smedberg (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved: