KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > classfile > engine > ClassDataAnalysisEngine


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.engine;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25
26 import edu.umd.cs.findbugs.classfile.CheckedAnalysisException;
27 import edu.umd.cs.findbugs.classfile.ClassDescriptor;
28 import edu.umd.cs.findbugs.classfile.IAnalysisCache;
29 import edu.umd.cs.findbugs.classfile.IClassAnalysisEngine;
30 import edu.umd.cs.findbugs.classfile.IClassPath;
31 import edu.umd.cs.findbugs.classfile.ICodeBaseEntry;
32 import edu.umd.cs.findbugs.classfile.MissingClassException;
33 import edu.umd.cs.findbugs.classfile.ResourceNotFoundException;
34 import edu.umd.cs.findbugs.classfile.analysis.ClassData;
35 import edu.umd.cs.findbugs.io.IO;
36
37 /**
38  * Analysis engine to produce the data (bytes) of a class.
39  *
40  * @author David Hovemeyer
41  */

42 public class ClassDataAnalysisEngine implements IClassAnalysisEngine {
43
44     /* (non-Javadoc)
45      * @see edu.umd.cs.findbugs.classfile.IAnalysisEngine#analyze(edu.umd.cs.findbugs.classfile.IAnalysisCache, java.lang.Object)
46      */

47     public Object JavaDoc analyze(IAnalysisCache analysisCache,
48             ClassDescriptor descriptor) throws CheckedAnalysisException {
49         
50         // Compute the resource name
51
String JavaDoc resourceName = descriptor.toResourceName();
52         
53         // Look up the codebase entry for the resource
54
ICodeBaseEntry codeBaseEntry;
55         try {
56             codeBaseEntry = analysisCache.getClassPath().lookupResource(resourceName);
57         } catch (ResourceNotFoundException e) {
58             throw new MissingClassException(descriptor, e);
59         }
60
61         // Create a ByteArrayOutputStream to capture the class data
62
int length = codeBaseEntry.getNumBytes();
63         ByteArrayOutputStream JavaDoc byteSink;
64         if (length >= 0) {
65             byteSink = new ByteArrayOutputStream JavaDoc(length);
66         } else {
67             byteSink = new ByteArrayOutputStream JavaDoc();
68         }
69         
70         // Read the class data into the byte array
71
InputStream JavaDoc in = null;
72         try {
73             in = codeBaseEntry.openResource();
74             IO.copy(in, byteSink);
75         } catch (IOException JavaDoc e) {
76             throw new MissingClassException(descriptor, e);
77         } finally {
78             if (in != null) {
79                 IO.close(in);
80             }
81         }
82
83         // Construct the resulting ClassData object and return it
84
byte[] data = byteSink.toByteArray();
85         return new ClassData(descriptor, codeBaseEntry, data);
86     }
87     
88     /* (non-Javadoc)
89      * @see edu.umd.cs.findbugs.classfile.IAnalysisEngine#registerWith(edu.umd.cs.findbugs.classfile.IAnalysisCache)
90      */

91     public void registerWith(IAnalysisCache analysisCache) {
92         analysisCache.registerClassAnalysisEngine(ClassData.class, this);
93     }
94
95     /* (non-Javadoc)
96      * @see edu.umd.cs.findbugs.classfile.IAnalysisEngine#retainAnalysisResults()
97      */

98     public boolean retainAnalysisResults() {
99         // ClassData can be recomputed (reloaded) easily
100
return false;
101     }
102 }
103
Popular Tags