-
Bug
-
Resolution: Done
-
Major
-
2.4.8.Final
-
None
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) { } } } } }