/** * * Finds all class files that have more than one version on the classpath * * @param classpathResources - the list of classpath resources * @param excludeSameSizeDups - true if only duplicates of different class sizes are considered * @return - the list of class files that have multiple versions */ public List<ClasspathResource> findClassFileDuplicates(List<ClasspathResource> classpathResources, boolean excludeSameSizeDups) { List<ClasspathResource> classFilesWithDuplicates = ClasspathResources.findResourcesWithDuplicates(classpathResources, excludeSameSizeDups); return ClasspathResources.filterClassFilesOnly(classFilesWithDuplicates); }
@Override public String toString() { return "ClasspathResourceVersion{classpathEntry=" + classpathEntry.toString() + ", resourceName=" + resourceName + ", fileSize=" + fileSize + "}"; } }
public String getClassLoaderName() { return classLoader != null ? classLoader.getName() : null; }
public void print() { System.out.println("\n>> jHades multipleClassVersionsReport >> Duplicate classpath resources report: \n"); ClasspathResources.sortByNumberOfVersionsDesc(resourcesWithDuplicates); for (ClasspathResource resource : resourcesWithDuplicates) { if (!resourcesToExclude.contains(resource.getName())) { System.out.println(resource.getName() + " has " + resource.getResourceFileVersions().size() + " versions on these classpath locations:\n"); for (ClasspathResourceVersion resourceFileVersion : resource.getResourceFileVersions()) { String classLoaderName = resourceFileVersion.getClasspathEntry().getClassLoaderName(); System.out.println(" " + (classLoaderName != null ? classLoaderName : "") + " - " + urlFormatter.formatUrl(resourceFileVersion.getClasspathEntry().getUrl()) + " - class file size = " + resourceFileVersion.getFileSize()); } System.out.println(); } } if (resourcesWithDuplicates.isEmpty()) { System.out.println("No duplicates where found.\n"); } } }
public JHades findClass(Class clazz) { if (clazz == null) { throw new IllegalArgumentException("Class name cannot be null."); } System.out.println(">> jHades searchClass >> Searching for class: " + clazz.getCanonicalName() + "\n"); ClasspathResource foundClass = scanner.findClass(clazz); for (ClasspathResourceVersion version : foundClass.getResourceFileVersions()) { System.out.println(version.getClasspathEntry().getUrl() + foundClass.getName() + " size = " + version.getFileSize()); } endCommand(foundClass != null); return this; }
public JHades overlappingJarsReport() { System.out.println("\n>> jHades - scanning classpath for overlapping jars: \n"); List<JarPair> jarOverlapReportLines = scanner.findOverlappingJars(); for (JarPair jarOverlapReportLine : jarOverlapReportLines) { String reportLine = jarOverlapReportLine.getJar1().getUrl() + " overlaps with \n" + jarOverlapReportLine.getJar2().getUrl() + " - total overlapping classes: " + jarOverlapReportLine.getDupClassesTotal() + " - "; if (jarOverlapReportLine.getJar1().getClassLoader().equals(jarOverlapReportLine.getJar2().getClassLoader())) { reportLine += "same classloader ! This is an ERROR!\n"; } else { reportLine += "different classloaders.\n"; } System.out.println(reportLine); } endCommand(jarOverlapReportLines.size() > 0); return this; }
protected void addClasspathEntry(ClasspathEntry newEntry) { classpathEntries.add(newEntry); if (newEntry.isJar()) { List<ClasspathEntry> manifestClasspath = newEntry.findManifestClasspathEntries(); for (ClasspathEntry manifestEntry : manifestClasspath) { addClasspathEntry(manifestEntry); } } }
public UrlClazzLoader(String name, String details, URL[] urls) { super(name, details, true); for (URL url : urls) { if (url != null) { addClasspathEntry(new ClasspathEntry(this, url.toString())); } } } }
/** * * Scans the classpath for all resource files. * * @return - the full list of resources on the classpath, including all its known versions. */ public List<ClasspathResource> findAllClasspathResources() { List<ClasspathEntry> classpathEntries = findAllClasspathEntries(); return ClasspathEntries.findClasspathResourcesInEntries(classpathEntries, logger, null); }
@Override public int compare(JarPair line1, JarPair line2) { return -1 * line1.getDupClassesTotal().compareTo(line2.getDupClassesTotal()); } };
/** * * Finds a list of all classpath resources that contain duplicates. * * A duplicate classpath resource can be for example a configuration file such as log4.xml, that exists by accident * multiple times on the classpath. * * @param excludeSameSizeDups - excludes from the reports files for which all classpath versions have the same size * @return classpath resources that have multiple versions on the classpath */ public List<ClasspathResource> findAllResourcesWithDuplicates(boolean excludeSameSizeDups) { List<ClasspathResource> resourceFiles = findAllClasspathResources(); return ClasspathResources.findResourcesWithDuplicates(resourceFiles, excludeSameSizeDups); }
public void print() { System.out.println("\n>> jHades multipleClassVersionsReport >> Duplicate classpath resources report: \n"); ClasspathResources.sortByNumberOfVersionsDesc(resourcesWithDuplicates); for (ClasspathResource resource : resourcesWithDuplicates) { if (!resourcesToExclude.contains(resource.getName())) { System.out.println(resource.getName() + " has " + resource.getResourceFileVersions().size() + " versions on these classpath locations:\n"); for (ClasspathResourceVersion resourceFileVersion : resource.getResourceFileVersions()) { String classLoaderName = resourceFileVersion.getClasspathEntry().getClassLoaderName(); System.out.println(" " + (classLoaderName != null ? classLoaderName : "") + " - " + urlFormatter.formatUrl(resourceFileVersion.getClasspathEntry().getUrl()) + " - class file size = " + resourceFileVersion.getFileSize()); } System.out.println(); } } if (resourcesWithDuplicates.isEmpty()) { System.out.println("No duplicates where found.\n"); } } }
public JHades findClass(Class clazz) { if (clazz == null) { throw new IllegalArgumentException("Class name cannot be null."); } System.out.println(">> jHades searchClass >> Searching for class: " + clazz.getCanonicalName() + "\n"); ClasspathResource foundClass = scanner.findClass(clazz); for (ClasspathResourceVersion version : foundClass.getResourceFileVersions()) { System.out.println(version.getClasspathEntry().getUrl() + foundClass.getName() + " size = " + version.getFileSize()); } endCommand(foundClass != null); return this; }
protected void addClasspathEntry(ClasspathEntry newEntry) { classpathEntries.add(newEntry); if (newEntry.isJar()) { List<ClasspathEntry> manifestClasspath = newEntry.findManifestClasspathEntries(); for (ClasspathEntry manifestEntry : manifestClasspath) { addClasspathEntry(manifestEntry); } } }
public UrlClazzLoader(String name, String details, URL[] urls) { super(name, details, true); for (URL url : urls) { if (url != null) { addClasspathEntry(new ClasspathEntry(this, url.toString())); } } } }
/** * * Finds all class files that have more than one version on the classpath * * @param classpathResources - the list of classpath resources * @param excludeSameSizeDups - true if only duplicates of different class sizes are considered * @return - the list of class files that have multiple versions */ public List<ClasspathResource> findClassFileDuplicates(List<ClasspathResource> classpathResources, boolean excludeSameSizeDups) { List<ClasspathResource> classFilesWithDuplicates = ClasspathResources.findResourcesWithDuplicates(classpathResources, excludeSameSizeDups); return ClasspathResources.filterClassFilesOnly(classFilesWithDuplicates); }
/** * * Scans the classpath for all resource files. * * @return - the full list of resources on the classpath, including all its known versions. */ public List<ClasspathResource> findAllClasspathResources() { List<ClasspathEntry> classpathEntries = findAllClasspathEntries(); return ClasspathEntries.findClasspathResourcesInEntries(classpathEntries, logger, null); }
@Override public int compare(JarPair line1, JarPair line2) { return -1 * line1.getDupClassesTotal().compareTo(line2.getDupClassesTotal()); } };
public String getClassLoaderName() { return classLoader != null ? classLoader.getName() : null; }
/** * * Finds a list of all classpath resources that contain duplicates. * * A duplicate classpath resource can be for example a configuration file such as log4.xml, that exists by accident * multiple times on the classpath. * * @param excludeSameSizeDups - excludes from the reports files for which all classpath versions have the same size * @return classpath resources that have multiple versions on the classpath */ public List<ClasspathResource> findAllResourcesWithDuplicates(boolean excludeSameSizeDups) { List<ClasspathResource> resourceFiles = findAllClasspathResources(); return ClasspathResources.findResourcesWithDuplicates(resourceFiles, excludeSameSizeDups); }