Class ClassesWithSameName


  • public class ClassesWithSameName
    extends Object
    Represents one or more class files that have the same exact name. In this case the class name is a relative, file system path to the class file. For example: org/apache/maven/Stuff.class Example of how we can have two of the same class: - mockito-core-1.9.5.jar contains org/mockito/Mockito.class - mockito-all-1.9.5.jar contains org/mockito/Mockito.class With that example you're not supposed to have both on the classpath. Typically you'd choose the maven way (mockito-core) or the convenient-for-non-maven-users way (mockito-all) but not both.
    • Constructor Detail

      • ClassesWithSameName

        public ClassesWithSameName​(org.apache.maven.enforcer.rule.api.EnforcerLogger log,
                                   ClassFile initialClassFile,
                                   ClassFile... additionalClassFiles)
        Parameters:
        log - (required) the logger
        initialClassFile - (required) we require at least one class file. Splitting this param from the next one lets us require at least one at compile time (instead of runtime).
        additionalClassFiles - (optional) additional class files
    • Method Detail

      • previous

        public ClassFile previous()
        Returns:
        the previous ClassFile, meaning, the one added before the most recent one. Psuedo-code: add("Class1.class") add("Class2.class") previous() // returns "Class1.class"
      • add

        public void add​(ClassFile classFile)
        Add a new .class file with the same exact path and name as the other classes this file represents (though the artifact can be different).
        Parameters:
        classFile - The path to the .class file. Example: org/apache/maven/Stuff.class
      • getAllArtifactsThisClassWasFoundIn

        public Set<org.apache.maven.artifact.Artifact> getAllArtifactsThisClassWasFoundIn()
        Returns:
        Return a Set rather than a List so we can use this as the key in another Map. List.of(3,2,1) doesn't equal List.of(1,2,3) but Set.of(3,2,1) equals Set.of(1,2,3)
      • hasDuplicates

        public boolean hasDuplicates​(boolean ignoreWhenIdentical)
        Main logic to determine if this object represents more than one of the exact same class on the classpath.
        Parameters:
        ignoreWhenIdentical - True if we should ignore two or more classes when they have the exact same bytecode; false means fail whenever there's more than one of the same class, regardless of bytecode.
        Returns:
        true if there are duplicates, false if not.
      • toOutputString

        public String toOutputString​(boolean ignoreWhenIdentical)
        Parameters:
        ignoreWhenIdentical - True if we should ignore two or more classes when they have the exact same bytecode; false means fail whenever there's more than one of the same class, regardless of bytecode.
        Returns:
        the output string displayed on the command line when there are duplicate classes. Example (ignoreWhenIdentical = false): org/apache/maven/Stuff.class Example (ignoreWhenIdentical = true): org/apache/maven/Stuff.class -- the bytecode exactly matches in these: a.jar and b.jar