OSDN Git Service

ClassLoaderの作成にはdoPrivilegeメソッドで囲まなければならない.
[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 import org.objectweb.asm.Opcodes;
21
22 /**
23  * 
24  * 
25  *
26  * @author Haruaki TAMADA
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         try{
39             parseClassName();
40         } catch(ParseClassNameFailedException e){
41             className = null;
42         }
43     }
44     
45     public DefaultClassFileArchive(String file, String className){
46         this(new File(file), className);
47     }
48     
49     public DefaultClassFileArchive(File file, String className){
50         this.file = file;
51         this.className = className;
52     }
53
54     public URL getLocation(){
55         try {
56             return file.toURI().toURL();
57         } catch (MalformedURLException ex) {
58             return null;
59         }
60     }
61
62     public InputStream getInputStream(ClassFileEntry entry) throws IOException{
63         return new FileInputStream(file);
64     }
65
66     public Iterator<ClassFileEntry> iterator(){
67         List<ClassFileEntry> list = new ArrayList<ClassFileEntry>();
68         list.add(new ClassFileEntry(className, getLocation()));
69
70         return list.iterator();
71     }
72
73     public boolean hasEntry(String className){
74         return this.className.equals(className);
75     }
76
77     public ClassFileEntry getEntry(String className) throws ClassNotFoundException{
78         return new ClassFileEntry(className, getLocation());
79     }
80
81     public String getName(){
82         return className;
83     }
84
85     private void parseClassName() throws ParseClassNameFailedException{
86         FileInputStream in = null;
87         try {
88             in = new FileInputStream(file);
89             ClassReader reader = new ClassReader(in);
90             ClassNameExtractVisitor visitor = new ClassNameExtractVisitor();
91             reader.accept(visitor, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
92
93             this.className = visitor.getClassName();
94         } catch (FileNotFoundException ex) {
95             throw new ParseClassNameFailedException(ex);
96         } catch (IOException ex) {
97             throw new ParseClassNameFailedException(ex);
98         } finally{
99             if(in != null){
100                 try{
101                     in.close();
102                 } catch(IOException e){
103                     throw new ParseClassNameFailedException(e);
104                 }
105             }
106         }
107     }
108
109     private static class ClassNameExtractVisitor extends ClassVisitor{
110         private String className;
111
112         public ClassNameExtractVisitor(){
113             super(Opcodes.ASM4);
114         }
115
116         public String getClassName(){
117             return className;
118         }
119
120         @Override
121         public void visit(int version, int access, String name, String signature, 
122                 String superClassName, String[] interfaces){
123             className = name;
124         }
125
126         @Override
127         public AnnotationVisitor visitAnnotation(String arg0, boolean arg1){
128             return null;
129         }
130
131         @Override
132         public void visitAttribute(Attribute arg0){
133         }
134
135         @Override
136         public void visitEnd(){
137         }
138
139         @Override
140         public FieldVisitor visitField(int arg0, String arg1, String arg2,
141                 String arg3, Object arg4){
142             return null;
143         }
144
145         @Override
146         public void visitInnerClass(String arg0, String arg1, String arg2,
147                 int arg3){
148         }
149
150         @Override
151         public MethodVisitor visitMethod(int arg0, String arg1, String arg2,
152                 String arg3, String[] arg4){
153             return null;
154         }
155
156         @Override
157         public void visitOuterClass(String arg0, String arg1, String arg2){
158         }
159
160         @Override
161         public void visitSource(String arg0, String arg1){
162
163         }
164     }
165 }