001: package net.firstpartners.nounit.reader.bytecode;
002:
003: /**
004: * Title: NoUnit - Identify Classes that are not being unit Tested
005: *
006: * Copyright (C) 2001 Paul Browne , FirstPartners.net
007: *
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or (at your option) any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU General Public License for more details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * @author Paul Browne
024: * @version 0.7
025: */
026:
027: import java.io.File;
028: import java.io.IOException;
029:
030: import net.firstpartners.nounit.reader.ISnippetFactory;
031: import net.firstpartners.nounit.snippet.ISnippet;
032: import net.firstpartners.nounit.snippet.SnippetClass;
033: import net.firstpartners.nounit.snippet.Snippets;
034: import net.firstpartners.nounit.utility.NoUnitException;
035:
036: import org.gjt.jclasslib.io.ClassFileReader;
037: import org.gjt.jclasslib.structures.ClassFile;
038: import org.gjt.jclasslib.structures.InvalidByteCodeException;
039:
040: /**
041: * Reads Class Files (Byte Code) and returns them as snippets
042: */
043: public class ByteCodeClassSnippetFactory extends
044: AbstractByteCodeSnippetFactory implements ISnippetFactory {
045:
046: /**
047: * Inner store for the current class source
048: */
049: private File innerSourceFile;
050:
051: /**
052: * Constructor - Get (and store) source class file
053: * @param sourceFile - Compiled Byte Code to read
054: */
055: public ByteCodeClassSnippetFactory(File sourceFile) {
056: innerSourceFile = sourceFile;
057: }
058:
059: /**
060: * Constructor - Get (and store) source class file
061: * @param sourceFile - Compiled Byte Code to read
062: */
063: public ByteCodeClassSnippetFactory(String sourceFile) {
064: innerSourceFile = new File(sourceFile);
065: }
066:
067: /**
068: * Get a set of snippets from the current class source
069: * @return snippets Collection of ISnippets describing the file
070: */
071: public Snippets getSnippets() throws NoUnitException {
072: Snippets this Class = new Snippets();
073:
074: try {
075: //Read the File
076: ClassFile this ClassFile = ClassFileReader
077: .readFromFile(innerSourceFile);
078:
079: //Get the sub parts of this Class
080: ISnippetFactory myMethodFactory = new ByteCodeMethodSnippetFactory(
081: this ClassFile);
082: Snippets methods = myMethodFactory.getSnippets();
083:
084: //Get any additional information
085: int this ClassInPool = this ClassFile.getThisClass();
086: String name = this ClassFile
087: .getConstantPoolEntryName(this ClassInPool);
088: name = cleanValues(name);
089:
090: String access = this ClassFile.getAccessFlagsVerbose();
091:
092: int super ClassInPool = this ClassFile.getSuperClass();
093: String super Class = this ClassFile
094: .getConstantPoolEntryName(super ClassInPool);
095: super Class = cleanValues(super Class);
096:
097: //Now use to build a class snippet
098: ISnippet classSnippet = new SnippetClass(name, access,
099: super Class, methods);
100:
101: this Class.add(classSnippet);
102:
103: } catch (InvalidByteCodeException ibce) {
104: throw new NoUnitException(ibce,
105: "Could not read Snippets from Source");
106: } catch (IOException ie) {
107: throw new NoUnitException(ie,
108: "Could not read Snippets from Source");
109: }
110: return thisClass;
111: }
112: }
|