KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > compiler > PostCompileClass


1 /*
2  * @(#)PostCompileClass.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.codecoverage.v2.compiler;
28
29 import java.io.IOException JavaDoc;
30 import java.io.OutputStream JavaDoc;
31
32 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule;
33 import net.sourceforge.groboutils.codecoverage.v2.datastore.AnalysisModuleSet;
34 import net.sourceforge.groboutils.codecoverage.v2.datastore.ClassRecord;
35 import net.sourceforge.groboutils.codecoverage.v2.datastore.IClassMetaDataWriter;
36 import net.sourceforge.groboutils.codecoverage.v2.datastore.IMetaDataWriter;
37
38
39 /**
40  * This is the main entry-point for performing the post-compilation of
41  * class files.
42  *
43  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
44  * @version $Date: 2004/04/15 05:48:25 $
45  * @since December 16, 2002
46  */

47 public class PostCompileClass
48 {
49     private IMetaDataWriter out;
50     private AnalysisModuleSet amSet;
51     private ParseCoverageLogger pcl;
52     
53     
54     /**
55      * @param writer meta-data output for the analysis generated for
56      * class post-compiled; must not be <tt>null</tt>.
57      * @param modules a list of all analysis modules to use when post-compiling
58      * each class; must not be <tt>null</tt>.
59      * @exception IllegalArgumentException if either <tt>writer</tt> or
60      * <tt>modules</tt> is <tt>null</tt>.
61      */

62     public PostCompileClass( IMetaDataWriter writer,
63             IAnalysisModule[] modules )
64     {
65         this( new ParseCoverageLogger(), writer, modules );
66     }
67     
68     
69     /**
70      * @param pcl the parser for a specific CoverageLogger (may be null).
71      * @param writer meta-data output for the analysis generated for
72      * class post-compiled; must not be <tt>null</tt>.
73      * @param modules a list of all analysis modules to use when post-compiling
74      * each class; must not be <tt>null</tt>.
75      * @exception IllegalArgumentException if either <tt>writer</tt> or
76      * <tt>modules</tt> is <tt>null</tt>.
77      *
78      * @since December 28, 2002
79      */

80     public PostCompileClass( ParseCoverageLogger pcl, IMetaDataWriter writer,
81             IAnalysisModule[] modules )
82     {
83         if (writer == null || modules == null || pcl == null)
84         {
85             throw new IllegalArgumentException JavaDoc("no null args");
86         }
87         
88         this.out = writer;
89         this.amSet = new AnalysisModuleSet( modules );
90         this.pcl = pcl;
91     }
92     
93     
94     /**
95      * Performs the post-compilation for the given class file for
96      * all the internal analysis modules, and will write the new class file to
97      * the given stream.
98      */

99     public void postCompile( String JavaDoc filename, byte[] classFile,
100             OutputStream JavaDoc classOut )
101             throws IOException JavaDoc
102     {
103         if (this.pcl == null)
104         {
105             throw new IllegalStateException JavaDoc( "pcl is null." );
106         }
107         ModifiedClass mc = new ModifiedClass( this.pcl, filename, classFile );
108         
109         int amCount = amSet.getAnalysisModuleCount();
110         ModifiedMethod[] mmL = mc.getMethods();
111         for (short moduleIndex = 0; moduleIndex < amCount; ++moduleIndex)
112         {
113             IAnalysisModule am = this.amSet.getAnalysisModuleAt( moduleIndex );
114             ClassRecord cr = mc.createClassRecord( this.amSet );
115             
116             for (int methodI = 0; methodI < mmL.length; ++methodI)
117             {
118                 DefaultMethodCode dmc = new DefaultMethodCode( moduleIndex,
119                     mmL[ methodI ], cr );
120                 am.analyze( dmc );
121             }
122             
123             // record this class record
124
IClassMetaDataWriter cmdw = this.out.getClassWriter( am );
125             try
126             {
127                 cmdw.writeClassRecord( cr );
128             }
129             finally
130             {
131                 cmdw.close();
132             }
133         }
134         
135         byte[] modClassFile = mc.getModifiedClass();
136         classOut.write( modClassFile );
137     }
138 }
139
140
Popular Tags