Class ClassesWithSameName


  • public class ClassesWithSameName
    extends java.lang.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.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String classFilePath
      the path to the .class file.
      private java.util.List<ClassFile> list  
      private org.apache.maven.enforcer.rule.api.EnforcerLogger log  
    • Constructor Summary

      Constructors 
      Constructor Description
      ClassesWithSameName​(org.apache.maven.enforcer.rule.api.EnforcerLogger log, ClassFile initialClassFile, ClassFile... additionalClassFiles)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      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).
      java.util.Set<org.apache.maven.artifact.Artifact> getAllArtifactsThisClassWasFoundIn()  
      private java.util.Map<java.lang.String,​java.util.Set<org.apache.maven.artifact.Artifact>> groupArtifactsWhoseClassesAreExactMatch()  
      boolean hasDuplicates​(boolean ignoreWhenIdentical)
      Main logic to determine if this object represents more than one of the exact same class on the classpath.
      private java.lang.String joinWithSeparator​(java.util.Set<org.apache.maven.artifact.Artifact> artifacts, java.lang.String separator)  
      ClassFile previous()  
      private static void throwIfClassNameDoesNotMatch​(ClassFile classFile, java.lang.String otherClassFilePath)  
      java.lang.String toOutputString​(boolean ignoreWhenIdentical)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • log

        private final org.apache.maven.enforcer.rule.api.EnforcerLogger log
      • classFilePath

        private final java.lang.String classFilePath
        the path to the .class file. Example: org/apache/maven/Stuff.class
      • list

        private final java.util.List<ClassFile> list
    • 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 java.util.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 java.lang.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
      • throwIfClassNameDoesNotMatch

        private static void throwIfClassNameDoesNotMatch​(ClassFile classFile,
                                                         java.lang.String otherClassFilePath)
      • joinWithSeparator

        private java.lang.String joinWithSeparator​(java.util.Set<org.apache.maven.artifact.Artifact> artifacts,
                                                   java.lang.String separator)
      • groupArtifactsWhoseClassesAreExactMatch

        private java.util.Map<java.lang.String,​java.util.Set<org.apache.maven.artifact.Artifact>> groupArtifactsWhoseClassesAreExactMatch()