KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > classfile > impl > SingleFileCodeBase


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.classfile.impl;
21
22 import java.io.BufferedInputStream JavaDoc;
23 import java.io.DataInputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileNotFoundException JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.util.NoSuchElementException JavaDoc;
30
31 import edu.umd.cs.findbugs.classfile.CheckedAnalysisException;
32 import edu.umd.cs.findbugs.classfile.ClassDescriptor;
33 import edu.umd.cs.findbugs.classfile.ICodeBaseEntry;
34 import edu.umd.cs.findbugs.classfile.ICodeBaseIterator;
35 import edu.umd.cs.findbugs.classfile.ICodeBaseLocator;
36 import edu.umd.cs.findbugs.classfile.IScannableCodeBase;
37 import edu.umd.cs.findbugs.classfile.InvalidClassFileFormatException;
38 import edu.umd.cs.findbugs.classfile.ResourceNotFoundException;
39 import edu.umd.cs.findbugs.classfile.analysis.ClassInfo;
40 import edu.umd.cs.findbugs.classfile.analysis.ClassNameAndSuperclassInfo;
41 import edu.umd.cs.findbugs.classfile.engine.ClassInfoAnalysisEngine;
42 import edu.umd.cs.findbugs.classfile.engine.ClassParser;
43 import edu.umd.cs.findbugs.io.IO;
44
45 /**
46  * Implementation of ICodeBase for a single classfile.
47  *
48  * @author David Hovemeyer
49  */

50 public class SingleFileCodeBase implements IScannableCodeBase {
51     
52     private ICodeBaseLocator codeBaseLocator;
53     private String JavaDoc fileName;
54     private boolean isAppCodeBase;
55     private int howDiscovered;
56     private long lastModifiedTime;
57     private boolean resourceNameKnown;
58     private String JavaDoc resourceName;
59     
60     public SingleFileCodeBase(ICodeBaseLocator codeBaseLocator, String JavaDoc fileName) {
61         this.codeBaseLocator = codeBaseLocator;
62         this.fileName = fileName;
63         this.lastModifiedTime = new File JavaDoc(fileName).lastModified();
64     }
65     
66     /* (non-Javadoc)
67      * @see edu.umd.cs.findbugs.classfile.ICodeBase#getCodeBaseLocator()
68      */

69     public ICodeBaseLocator getCodeBaseLocator() {
70         return codeBaseLocator;
71     }
72     
73     /* (non-Javadoc)
74      * @see edu.umd.cs.findbugs.classfile.IScannableCodeBase#containsSourceFiles()
75      */

76     public boolean containsSourceFiles() throws InterruptedException JavaDoc {
77         return false;
78     }
79     
80     /* (non-Javadoc)
81      * @see edu.umd.cs.findbugs.classfile.IScannableCodeBase#iterator()
82      */

83     public ICodeBaseIterator iterator() throws InterruptedException JavaDoc {
84         return new ICodeBaseIterator() {
85             boolean done = false;
86             
87             /* (non-Javadoc)
88              * @see edu.umd.cs.findbugs.classfile.ICodeBaseIterator#hasNext()
89              */

90             public boolean hasNext() throws InterruptedException JavaDoc {
91                 return !done;
92             }
93             
94             /* (non-Javadoc)
95              * @see edu.umd.cs.findbugs.classfile.ICodeBaseIterator#next()
96              */

97             public ICodeBaseEntry next() throws InterruptedException JavaDoc {
98                 if (done) {
99                     throw new NoSuchElementException JavaDoc();
100                 }
101                 done = true;
102                 return new SingleFileCodeBaseEntry(SingleFileCodeBase.this);
103             }
104         };
105     }
106     
107     /* (non-Javadoc)
108      * @see edu.umd.cs.findbugs.classfile.ICodeBase#lookupResource(java.lang.String)
109      */

110     public ICodeBaseEntry lookupResource(String JavaDoc resourceName) throws ResourceNotFoundException {
111         if (!resourceName.equals(getResourceName())) {
112             throw new ResourceNotFoundException(resourceName);
113         }
114         
115         return new SingleFileCodeBaseEntry(this);
116     }
117     
118     /* (non-Javadoc)
119      * @see edu.umd.cs.findbugs.classfile.ICodeBase#setApplicationCodeBase(boolean)
120      */

121     public void setApplicationCodeBase(boolean isAppCodeBase) {
122         this.isAppCodeBase = isAppCodeBase;
123     }
124     
125     /* (non-Javadoc)
126      * @see edu.umd.cs.findbugs.classfile.ICodeBase#isApplicationCodeBase()
127      */

128     public boolean isApplicationCodeBase() {
129         return isAppCodeBase;
130     }
131     
132     /* (non-Javadoc)
133      * @see edu.umd.cs.findbugs.classfile.ICodeBase#setHowDiscovered(int)
134      */

135     public void setHowDiscovered(int howDiscovered) {
136         this.howDiscovered = howDiscovered;
137     }
138     
139     /* (non-Javadoc)
140      * @see edu.umd.cs.findbugs.classfile.ICodeBase#getHowDiscovered()
141      */

142     public int getHowDiscovered() {
143         return howDiscovered;
144     }
145     
146     /* (non-Javadoc)
147      * @see edu.umd.cs.findbugs.classfile.ICodeBase#setLastModifiedTime(long)
148      */

149     public void setLastModifiedTime(long lastModifiedTime) {
150         if (lastModifiedTime > 0) {
151             this.lastModifiedTime = lastModifiedTime;
152         }
153     }
154     
155     /* (non-Javadoc)
156      * @see edu.umd.cs.findbugs.classfile.ICodeBase#getLastModifiedTime()
157      */

158     public long getLastModifiedTime() {
159         return lastModifiedTime;
160     }
161     
162     /* (non-Javadoc)
163      * @see edu.umd.cs.findbugs.classfile.ICodeBase#getPathName()
164      */

165     public String JavaDoc getPathName() {
166         return fileName;
167     }
168     
169     InputStream JavaDoc openFile() throws IOException JavaDoc {
170         return new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(fileName));
171     }
172     
173     /* (non-Javadoc)
174      * @see edu.umd.cs.findbugs.classfile.ICodeBase#close()
175      */

176     public void close() {
177         // Nothing to do
178
}
179
180     /**
181      * Get the resource name of the single file.
182      * We have to open the file and parse the constant pool
183      * in order to find this out.
184      *
185      * @return the resource name (e.g., "java/lang/String.class"
186      * if the class is java.lang.String)
187      */

188     String JavaDoc getResourceName() {
189         if (!resourceNameKnown) {
190             // The resource name of a classfile can only be determined by reading
191
// the file and parsing the constant pool.
192
// If we can't do this for some reason, then we just
193
// make the resource name equal to the filename.
194

195             try {
196                 resourceName = getClassDescriptor().toResourceName();
197             } catch (Exception JavaDoc e) {
198                 resourceName = fileName;
199             }
200             
201             resourceNameKnown = true;
202         }
203         return resourceName;
204     }
205     
206     ClassDescriptor getClassDescriptor() throws ResourceNotFoundException, InvalidClassFileFormatException {
207         DataInputStream JavaDoc in = null;
208         try {
209             try {
210                 in = new DataInputStream JavaDoc(new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(fileName)));
211                 ClassParser classParser = new ClassParser(in, null, new SingleFileCodeBaseEntry(this));
212                 ClassNameAndSuperclassInfo classInfo = new ClassNameAndSuperclassInfo();
213                 classParser.parse(classInfo);
214                 return classInfo.getClassDescriptor();
215             } finally {
216                 if (in != null) {
217                     IO.close(in);
218                 }
219             }
220         } catch (IOException JavaDoc e) {
221             // XXX: file name isn't really the resource name, but whatever
222
throw new ResourceNotFoundException(fileName);
223         }
224     }
225
226     /**
227      * Return the number of bytes in the file.
228      *
229      * @return the number of bytes in the file, or -1 if the file's length
230      * can't be determined
231      */

232     int getNumBytes() {
233         File JavaDoc file = new File JavaDoc(fileName);
234         if (!file.exists()) {
235             return -1;
236         }
237         return (int) file.length();
238     }
239 }
240
Popular Tags