Java find repeating Class/jar Package/Ordinary file

Source: Internet
Author: User

When developing a Web application, the class is sometimes updated but not in effect because the class with the same name (including the full path is the same) under the other advertised packages in Jboss/tomcat.

And then it started to do a program to check whether the specified folder has repeated classes (through ASM from the class file to take the full path of the Class), and also support the search for repeated files (by file MD5), repeated jar files.

The main code such as the following:

Package Cn.jerryhouse.util.dup_files;import Java.io.file;public Abstract class Fileprocessor {private long Totalfilecount = 0;private Long processedfilecount = 0;public void Processfiles (file[] dirs) throws Exception {for (File F Ile:dirs) {processfile (file);}} public void Processfile (file file) throws Exception {if (File.isfile ()) {if (isfileaccepted (file)) {handlefile (file); processedfilecount++;} totalfilecount++;} else {file[] files = file.listfiles (); for (File fileindir:files) {processfile (Fileindir);}}} Protected Boolean isfileaccepted (file file) throws Exception {return true;} protected abstract void Handlefile (file file);p ublic long Gettotalfilecount () {return totalfilecount;} Public long Getprocessedfilecount () {return processedfilecount;}}


Package Cn.jerryhouse.util.dup_files;import Java.io.file;import Java.io.ioexception;import java.util.LinkedList; Import Java.util.list;import Java.util.map;import Java.util.map.entry;import Java.util.concurrent.concurrenthashmap;public class Filedupfinder extends Fileprocessor {protected map<string, list<string>> Filemap = new concurrenthashmap<string, list<string>> ();p ublic void AnalyseResult ( ) {Boolean founddup = False;for (entry<string, list<string>> entry:fileMap.entrySet ()) {list<string> Classpathlist = Entry.getvalue (), if (Classpathlist.size () > 1) {System.out.println ("paths:" + classpathlist); Founddup = True;}} if (Founddup = = False) {System.out.println ("No duplicate file found.");}} protected void Handlefile (final file file) {try {String fileMD5 = filedigest.getfilemd5 (File); try {list<string> lis T;if (Filemap.get (fileMD5) = null) {list = Filemap.get (fileMD5); List.add (File.getcanonicalpath ());} else {list = new Lin Kedlist<strIng> (); List.add (File.getcanonicalpath ()); Filemap.put (fileMD5, list);}} catch (IOException e) {e.printstacktrace ();}} catch (Exception e) {e.printstacktrace ();}}}

Package Cn.jerryhouse.util.dup_files;import Java.io.file;public Class Jarfiledupfinder extends Filedupfinder { Protected Boolean isfileaccepted (final file file) throws Exception{if (File.getcanonicalpath (). EndsWith (". Jar")) { return true;} Else{return false;}}}

Package Cn.jerryhouse.util.dup_files;import Java.io.file;import Java.io.fileinputstream;import java.io.IOException ; Import Java.util.linkedlist;import Java.util.list;import Java.util.map.entry;import Org.objectweb.asm.annotationvisitor;import Org.objectweb.asm.attribute;import Org.objectweb.asm.ClassReader; Import Org.objectweb.asm.classvisitor;import Org.objectweb.asm.fieldvisitor;import Org.objectweb.asm.methodvisitor;public class Classdupfinder extends Filedupfinder {protected Boolean isfileaccepted ( Final file file) throws Exception {if (File.getcanonicalpath (). EndsWith (". Class")) {return true;} else {return false;}} public void Analyseresult () {Boolean founddup = False;for (entry<string, list<string>> Entry:fileMap.entryS ET ()) {list<string> classpathlist = Entry.getvalue (); if (Classpathlist.size () > 1) {System.out.println ("class : "+ entry.getkey () +" paths: "+ classpathlist); founddup = True;}} if (Founddup = = False) {System.out.println ("No duplicate class found."));}} protected void Handlefile (final file file) {try {String FilePath = File.getcanonicalpath (); Classvisitor visitor = new Classvisitor () {@Overridepublic void visit (int version, int access, String name,string Signatur E, String Supername, string[] interfaces) {Try {list<string> list;if (filemap.get (name)! = null) {List = Filemap.get (name); List.add (File.getcanonicalpath ());} else {list = new linkedlist<string> (); List.add (File.getcanonicalpath ()); Filemap.put (name, list);}} catch (IOException e) {e.printstacktrace ();}} @Overridepublic void Visitsource (string source, String debug) {} @Overridepublic void Visitouterclass (string owner, String name,string desc) {} @Overridepublic annotationvisitor visitannotation (String Desc,boolean visiable) {return null ;} @Overridepublic void Visitattribute (Attribute attr) {} @Overridepublic void Visitinnerclass (string name, string outername,string innername, int access) {} @Overridepublic fieldvisitor Visitfield (int access, String name,string desc, StRing signature, Object value) {return null;} @Overridepublic methodvisitor visitmethod (int access, string name,string desc, string signature, string[] exceptions) {RE Turn null;} @Overridepublic void Visitend () {}}; Classreader cr = new Classreader (new FileInputStream (FilePath)); cr.accept (visitor, 0);} catch (Exception e) {e.printstacktrace ();}}}

Package Cn.jerryhouse.util.dup_files;import Java.io.file;import Java.io.fileinputstream;import Java.math.biginteger;import Java.security.messagedigest;import Java.util.hashmap;import Java.util.Map;public class filedigest {/** * Gets the MD5 value of a single file * @param file */public static String getFileMD5 (file file) {if (!file.isfile ()) {return null ;} MessageDigest digest = null; FileInputStream in = null;byte buffer[] = new Byte[1024];int len;try {digest = messagedigest.getinstance ("MD5"); in = new F Ileinputstream (file), while (len = in.read (buffer, 0, 1024x768)! =-1) {digest.update (buffer, 0, Len);} In.close ();} catch (Exception e) {e.printstacktrace (); return null;} BigInteger bigInt = new BigInteger (1, Digest.digest ()); return bigint.tostring (16);} /** * Gets the MD5 value of the file in the folder * @param file * @param listchild to True, recursively the files in the subfolder; otherwise not recursive */public static map<string, string> get DirMD5 (File file, Boolean listchild) {if (!file.isdirectory ()) {return null;} map<string, string> map = new hashmap<string, string> (); String MD5; File files[] = File.listfiles (); for (int i = 0; i < files.length; i++) {File F = files[i];if (F.isdirectory () &&amp ; Listchild) {Map.putall (GetDirMD5 (f, Listchild)),} else {MD5 = getFileMD5 (f); if (MD5! = null) {Map.put (F.getpath (), MD5);} }}return map;}}

Package Cn.jerryhouse.util.dup_files;import Java.io.bufferedreader;import Java.io.file;import java.io.FileReader; Public abstract class Linereaderprocessor extends Fileprocessor {private long Totalfilecount = 0;private long Processedfil Ecount = 0;public void Processfiles (file[] dirs) throws Exception {for (file file:dirs) {processfile (file);}} public void Processfile (file file) throws Exception {if (File.isfile ()) {if (isfileaccepted (file)) {handlefile (file);}} E LSE {file[] files = file.listfiles (); for (File fileindir:files) {processfile (Fileindir);}}} Protected Boolean isfileaccepted (file file) throws Exception {return true;} protected void Handlefile (file file) {try {bufferedreader br = new BufferedReader (new FileReader (file)); String Line;while ((Line=br.readline ())!=null) {readLine (file,line);} Br.close ();} catch (Exception e) {e.printstacktrace ();}} protected abstract void ReadLine (File file,string line);p ublic long Gettotalfilecount () {return totalfilecount;} Public long GetprocessedfilEcount () {return processedfilecount;}} 

Package Cn.jerryhouse.util.dup_files;import Java.io.bufferedreader;import Java.io.bufferedwriter;import Java.io.file;import Java.io.filereader;import Java.io.filewriter;import Java.util.uuid;public abstract class Lineupdateprocessor extends Fileprocessor {private long Totalfilecount = 0;private long Processedfilecount = 0;private Str ing new_line = system.getproperty ("Line.separator");p ublic void Processfiles (file[] dirs) throws Exception {for (File fil E:dirs) {processfile (file);}} public void Processfile (file file) throws Exception {if (File.isfile ()) {if (isfileaccepted (file)) {handlefile (file);}} E LSE {file[] files = file.listfiles (); for (File fileindir:files) {processfile (Fileindir);}}} Protected Boolean isfileaccepted (file file) throws Exception {return true;} protected void Handlefile (file file) {try {bufferedreader br = new BufferedReader (new FileReader (file)); File Tmpfile = new file (Tmpfilepath (file)); BufferedWriter bw = new BufferedWriter (new FileWriter (tmpfile)); String LINE;WHIle ((Line=br.readline ())!=null) {String updatedline = Updateline (file,line); Bw.write (updatedline+new_line);} Br.close (); Bw.close (); File.delete (); Tmpfile.renameto (file); catch (Exception e) {e.printstacktrace ();}} Private String Tmpfilepath (file file) {string dir = File.getparent (); String FilePath = dir+ "" +getuniqfilename (); return filePath;} Private String Getuniqfilename () {return Uuid.randomuuid (). toString ();} Protected abstract String updateline (File file,string line);p ublic long Gettotalfilecount () {return totalfilecount;} Public long Getprocessedfilecount () {return processedfilecount;}}


Simple test Code:

Package Cn.jerryhouse.util.dup_files.test;import Java.io.file;import Org.junit.test;import cn.jerryhouse.util.dup_ Files. Classdupfinder;import Cn.jerryhouse.util.dup_files. Filedupfinder;import Cn.jerryhouse.util.dup_files. Jarfiledupfinder;public class Duptest {@Testpublic void Testjarfiles () {try {file[] files = new File[1];files[0] = new Fil E ("e:\\workspace\\yinxing"); Jarfiledupfinder Dupfinder = new Jarfiledupfinder ();d upfinder.processfiles (Files);d upfinder.analyseresult (); catch (Exception e) {e.printstacktrace ();}} @Testpublic void Testfiledup () {try {file[] files = new File[1];files[0] = new File ("e:\\workspace\\yinxing"); Filedupfinder Classdupfinder = new Filedupfinder (); Classdupfinder.processfiles (files); Classdupfinder.analyseresult ();} catch (Exception e) {e.printstacktrace ();}} @Testpublic void Testclassdup () {try {file[] files = new File[1];files[0] = new File ("e:\\workspace\\yinxing"); Classdupfinder Classdupfinder = new Classdupfinder (); Classdupfinder.processfiles (files); classDupfinder.analyseresult ();} catch (Exception e) {e.printstacktrace ();}}}


Note: The dependency jar package Asm.jar.




Java find repeating Class/jar Package/Ordinary file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.