OSDN Git Service

0655564de83f079e9477c64925f925cd24bfcd92
[stigmata/digger.git] / src / main / java / jp / sourceforge / stigmata / digger / DefaultClassFileArchive.java
1 package jp.sourceforge.stigmata.digger;
2
3 /*
4  * $Id$
5  */
6
7 import java.io.File;
8 import java.io.FileInputStream;
9 import java.io.FileNotFoundException;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.net.MalformedURLException;
13 import java.net.URL;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17
18 import org.objectweb.asm.ClassReader;
19 import org.objectweb.asm.commons.EmptyVisitor;
20
21 /**
22  * 
23  * 
24  *
25  * @author Haruaki TAMADA
26  * @version $Revision$ 
27  */
28 public class DefaultClassFileArchive implements ClassFileArchive{
29     private File file;
30     private String className;
31     
32     public DefaultClassFileArchive(String file){
33         this(new File(file));
34     }
35     
36     public DefaultClassFileArchive(File file){
37         this.file = file;
38         parseClassName();
39     }
40     
41     public DefaultClassFileArchive(String file, String className){
42         this(new File(file), className);
43     }
44     
45     public DefaultClassFileArchive(File file, String className){
46         this.file = file;
47         this.className = className;
48     }
49
50     public URL getLocation(){
51         try {
52             return file.toURI().toURL();
53         } catch (MalformedURLException ex) {
54         }
55         return null;
56     }
57
58     public InputStream getInputStream(ClassFileEntry entry) throws IOException{
59         return new FileInputStream(file);
60     }
61
62     public Iterator<ClassFileEntry> iterator(){
63         List<ClassFileEntry> list = new ArrayList<ClassFileEntry>();
64         list.add(new ClassFileEntry(className, getLocation()));
65
66         return list.iterator();
67     }
68
69     public boolean hasEntry(String className){
70         return this.className.equals(className);
71     }
72
73     public ClassFileEntry getEntry(String className) throws ClassNotFoundException{
74         return new ClassFileEntry(className, getLocation());
75     }
76
77     public String getName(){
78         return className;
79     }
80
81     private void parseClassName(){
82         FileInputStream in = null;
83         try {
84             in = new FileInputStream(file);
85             ClassReader reader = new ClassReader(in);
86             ClassNameExtractVisitor visitor = new ClassNameExtractVisitor();
87             reader.accept(visitor, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
88
89             this.className = visitor.getClassName();
90         } catch (FileNotFoundException ex) {
91             ex.printStackTrace();
92         } catch (IOException ex) {
93             ex.printStackTrace();
94         } finally{
95             if(in != null){
96                 try{
97                     in.close();
98                 } catch(IOException e){
99                 }
100             }
101         }
102     }
103
104     private static class ClassNameExtractVisitor extends EmptyVisitor{
105         private String className;
106
107         public String getClassName(){
108             return className;
109         }
110
111         @Override
112         public void visit(int version, int access, String name, String signature, 
113                 String superClassName, String[] interfaces){
114             className = name;
115         }
116     }
117 }