File: ClassChecker.java

package info (click to toggle)
imagej 1.52j-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,604 kB
  • sloc: java: 120,017; sh: 279; xml: 161; makefile: 6
file content (107 lines) | stat: -rw-r--r-- 2,952 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package  ij.plugin;
import ij.*;
import ij.util.*;
import java.io.*;
import java.util.*;

/** Checks for duplicate class and JAR files in the plugins folders and deletes older duplicates. */
public class ClassChecker implements PlugIn {
	String[] paths;
	String[] names;

	public void run(String arg) {
		//long start = System.currentTimeMillis();
		deleteDuplicates();
		//IJ.log("Time: "+(System.currentTimeMillis()-start));
	}
	
	void deleteDuplicates() {
		getPathsAndNames();
		if (paths==null || paths.length<2) return;
		String[] sortedNames = new String[names.length];
		for (int i=0; i<names.length; i++)
			sortedNames[i] = names[i];
		StringSorter.sort(sortedNames);
		for (int i=0; i<sortedNames.length-1; i++) {
			if (sortedNames[i].equals(sortedNames[i+1]))
				delete(sortedNames[i]);
		}
	}
	
	void delete(String name) {
		String path1=null, path2=null;
		File file1, file2;
		long date1, date2;
		for (int i=0; i<names.length; i++) {
			if (path1==null && names[i].equals(name)) {
				path1 = paths[i] +names[i];
//IJ.log("path1: "+i+"   "+name+"   "+path1+"   "+paths[i]+"   "+names[i]);
			} else if (path2==null && names[i].equals(name)) {
				path2 = paths[i] +names[i];
//IJ.log("path2: "+i+"   "+name+"   "+path2+"   "+paths[i]+"   "+names[i]);
			}
			if (path1!=null && path2!=null) {
				file1 = new File(path1);
				file2 = new File(path2);
				if (file1==null || file2==null) return;
				date1 = file1.lastModified();
				date2 = file2.lastModified();
				if (date1<date2) {
					write(path1);
					file1.delete();
				} else {
					write(path2);
					file2.delete();
				}
				break;
			}
		}
	}

	void write(String path) {
		IJ.log("Deleting duplicate plugin: "+path);
	}

	/** Gets lists of all the class and jar files in the plugins
	     folder and subfolders of the plugins folder. */
	void getPathsAndNames() {
		String path = Menus.getPlugInsPath();
		if (path==null) return;
		File f = new File(path);
		String[] list = f.list();
		if (list==null) return;
		Vector v1 = new Vector(1000);
		Vector v2 = new Vector(1000);
		for (int i=0; i<list.length; i++) {
			String name = list[i];
			if (name.endsWith(".class") || name.endsWith(".jar")) {
				v1.addElement(path);
				v2.addElement(name);
			} else
				getSubdirectoryFiles(path, name, v1, v2);
		}
		paths = new String[v1.size()];
		v1.copyInto((String[])paths);
		names = new String[v2.size()];
		v2.copyInto((String[])names);
	}

	/** Looks for class and jar files in a subfolders of the plugins folder. */
	void getSubdirectoryFiles(String path, String dir, Vector v1, Vector v2) {
		if (dir.endsWith(".java")) return;
		File f = new File(path, dir);
		if (!f.isDirectory()) return;
		String[] list = f.list();
		if (list==null) return;
		dir += Prefs.separator;
		for (int i=0; i<list.length; i++) {
			String name = list[i];
			if (name.endsWith(".class") || name.endsWith(".jar")) {
				v1.addElement(path+dir);
				v2.addElement(name);
			}
		}
	}

}