001: /*
002: * @(#)PostCompileClass.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.codecoverage.v2.compiler;
028:
029: import java.io.IOException;
030: import java.io.OutputStream;
031:
032: import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule;
033: import net.sourceforge.groboutils.codecoverage.v2.datastore.AnalysisModuleSet;
034: import net.sourceforge.groboutils.codecoverage.v2.datastore.ClassRecord;
035: import net.sourceforge.groboutils.codecoverage.v2.datastore.IClassMetaDataWriter;
036: import net.sourceforge.groboutils.codecoverage.v2.datastore.IMetaDataWriter;
037:
038: /**
039: * This is the main entry-point for performing the post-compilation of
040: * class files.
041: *
042: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
043: * @version $Date: 2004/04/15 05:48:25 $
044: * @since December 16, 2002
045: */
046: public class PostCompileClass {
047: private IMetaDataWriter out;
048: private AnalysisModuleSet amSet;
049: private ParseCoverageLogger pcl;
050:
051: /**
052: * @param writer meta-data output for the analysis generated for
053: * class post-compiled; must not be <tt>null</tt>.
054: * @param modules a list of all analysis modules to use when post-compiling
055: * each class; must not be <tt>null</tt>.
056: * @exception IllegalArgumentException if either <tt>writer</tt> or
057: * <tt>modules</tt> is <tt>null</tt>.
058: */
059: public PostCompileClass(IMetaDataWriter writer,
060: IAnalysisModule[] modules) {
061: this (new ParseCoverageLogger(), writer, modules);
062: }
063:
064: /**
065: * @param pcl the parser for a specific CoverageLogger (may be null).
066: * @param writer meta-data output for the analysis generated for
067: * class post-compiled; must not be <tt>null</tt>.
068: * @param modules a list of all analysis modules to use when post-compiling
069: * each class; must not be <tt>null</tt>.
070: * @exception IllegalArgumentException if either <tt>writer</tt> or
071: * <tt>modules</tt> is <tt>null</tt>.
072: *
073: * @since December 28, 2002
074: */
075: public PostCompileClass(ParseCoverageLogger pcl,
076: IMetaDataWriter writer, IAnalysisModule[] modules) {
077: if (writer == null || modules == null || pcl == null) {
078: throw new IllegalArgumentException("no null args");
079: }
080:
081: this .out = writer;
082: this .amSet = new AnalysisModuleSet(modules);
083: this .pcl = pcl;
084: }
085:
086: /**
087: * Performs the post-compilation for the given class file for
088: * all the internal analysis modules, and will write the new class file to
089: * the given stream.
090: */
091: public void postCompile(String filename, byte[] classFile,
092: OutputStream classOut) throws IOException {
093: if (this .pcl == null) {
094: throw new IllegalStateException("pcl is null.");
095: }
096: ModifiedClass mc = new ModifiedClass(this .pcl, filename,
097: classFile);
098:
099: int amCount = amSet.getAnalysisModuleCount();
100: ModifiedMethod[] mmL = mc.getMethods();
101: for (short moduleIndex = 0; moduleIndex < amCount; ++moduleIndex) {
102: IAnalysisModule am = this .amSet
103: .getAnalysisModuleAt(moduleIndex);
104: ClassRecord cr = mc.createClassRecord(this .amSet);
105:
106: for (int methodI = 0; methodI < mmL.length; ++methodI) {
107: DefaultMethodCode dmc = new DefaultMethodCode(
108: moduleIndex, mmL[methodI], cr);
109: am.analyze(dmc);
110: }
111:
112: // record this class record
113: IClassMetaDataWriter cmdw = this .out.getClassWriter(am);
114: try {
115: cmdw.writeClassRecord(cr);
116: } finally {
117: cmdw.close();
118: }
119: }
120:
121: byte[] modClassFile = mc.getModifiedClass();
122: classOut.write(modClassFile);
123: }
124: }
|