OSDN Git Service

Merge remote branch 'origin/HEAD'
[stigmata/digger.git] / src / main / java / jp / sourceforge / stigmata / digger / DefaultClassFileArchive.java
1 package jp.sourceforge.stigmata.digger;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.net.MalformedURLException;
9 import java.net.URL;
10 import java.util.ArrayList;
11 import java.util.Iterator;
12 import java.util.List;
13
14 import org.objectweb.asm.AnnotationVisitor;
15 import org.objectweb.asm.Attribute;
16 import org.objectweb.asm.ClassReader;
17 import org.objectweb.asm.ClassVisitor;
18 import org.objectweb.asm.FieldVisitor;
19 import org.objectweb.asm.MethodVisitor;
20
21 /**
22  * 
23  * 
24  *
25  * @author Haruaki TAMADA
26  */
27 public class DefaultClassFileArchive implements ClassFileArchive{
28     private File file;
29     private String className;
30     
31     public DefaultClassFileArchive(String file){
32         this(new File(file));
33     }
34     
35     public DefaultClassFileArchive(File file){
36         this.file = file;
37         parseClassName();
38     }
39     
40     public DefaultClassFileArchive(String file, String className){
41         this(new File(file), className);
42     }
43     
44     public DefaultClassFileArchive(File file, String className){
45         this.file = file;
46         this.className = className;
47     }
48
49     public URL getLocation(){
50         try {
51             return file.toURI().toURL();
52         } catch (MalformedURLException ex) {
53         }
54         return null;
55     }
56
57     public InputStream getInputStream(ClassFileEntry entry) throws IOException{
58         return new FileInputStream(file);
59     }
60
61     public Iterator<ClassFileEntry> iterator(){
62         List<ClassFileEntry> list = new ArrayList<ClassFileEntry>();
63         list.add(new ClassFileEntry(className, getLocation()));
64
65         return list.iterator();
66     }
67
68     public boolean hasEntry(String className){
69         return this.className.equals(className);
70     }
71
72     public ClassFileEntry getEntry(String className) throws ClassNotFoundException{
73         return new ClassFileEntry(className, getLocation());
74     }
75
76     public String getName(){
77         return className;
78     }
79
80     private void parseClassName(){
81         FileInputStream in = null;
82         try {
83             in = new FileInputStream(file);
84             ClassReader reader = new ClassReader(in);
85             ClassNameExtractVisitor visitor = new ClassNameExtractVisitor();
86             reader.accept(visitor, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
87
88             this.className = visitor.getClassName();
89         } catch (FileNotFoundException ex) {
90             ex.printStackTrace();
91         } catch (IOException ex) {
92             ex.printStackTrace();
93         } finally{
94             if(in != null){
95                 try{
96                     in.close();
97                 } catch(IOException e){
98                 }
99             }
100         }
101     }
102
103     private static class ClassNameExtractVisitor implements ClassVisitor{
104         private String className;
105
106         public String getClassName(){
107             return className;
108         }
109
110         @Override
111         public void visit(int version, int access, String name, String signature, 
112                 String superClassName, String[] interfaces){
113             className = name;
114         }
115
116         @Override
117         public AnnotationVisitor visitAnnotation(String arg0, boolean arg1){
118             return null;
119         }
120
121         @Override
122         public void visitAttribute(Attribute arg0){
123         }
124
125         @Override
126         public void visitEnd(){
127         }
128
129         @Override
130         public FieldVisitor visitField(int arg0, String arg1, String arg2,
131                 String arg3, Object arg4){
132             return null;
133         }
134
135         @Override
136         public void visitInnerClass(String arg0, String arg1, String arg2,
137                 int arg3){
138         }
139
140         @Override
141         public MethodVisitor visitMethod(int arg0, String arg1, String arg2,
142                 String arg3, String[] arg4){
143             return null;
144         }
145
146         @Override
147         public void visitOuterClass(String arg0, String arg1, String arg2){
148         }
149
150         @Override
151         public void visitSource(String arg0, String arg1){
152
153         }
154     }
155 }