KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > firstpartners > nounit > reader > bytecode > ByteCodeClassSnippetFactory


1
2 package net.firstpartners.nounit.reader.bytecode;
3
4 /**
5  * Title: NoUnit - Identify Classes that are not being unit Tested
6  *
7  * Copyright (C) 2001 Paul Browne , FirstPartners.net
8  *
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23  *
24  * @author Paul Browne
25  * @version 0.7
26  */

27
28 import java.io.File JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 import net.firstpartners.nounit.reader.ISnippetFactory;
32 import net.firstpartners.nounit.snippet.ISnippet;
33 import net.firstpartners.nounit.snippet.SnippetClass;
34 import net.firstpartners.nounit.snippet.Snippets;
35 import net.firstpartners.nounit.utility.NoUnitException;
36
37 import org.gjt.jclasslib.io.ClassFileReader;
38 import org.gjt.jclasslib.structures.ClassFile;
39 import org.gjt.jclasslib.structures.InvalidByteCodeException;
40
41 /**
42  * Reads Class Files (Byte Code) and returns them as snippets
43  */

44 public class ByteCodeClassSnippetFactory extends AbstractByteCodeSnippetFactory
45     implements ISnippetFactory {
46     
47     /**
48      * Inner store for the current class source
49      */

50     private File JavaDoc innerSourceFile;
51         
52     /**
53      * Constructor - Get (and store) source class file
54      * @param sourceFile - Compiled Byte Code to read
55      */

56     public ByteCodeClassSnippetFactory(File JavaDoc sourceFile ){
57         innerSourceFile = sourceFile;
58     }
59     
60     /**
61      * Constructor - Get (and store) source class file
62      * @param sourceFile - Compiled Byte Code to read
63      */

64     public ByteCodeClassSnippetFactory(String JavaDoc sourceFile ){
65         innerSourceFile = new File JavaDoc(sourceFile );
66     }
67     
68     /**
69      * Get a set of snippets from the current class source
70      * @return snippets Collection of ISnippets describing the file
71      */

72     public Snippets getSnippets() throws NoUnitException
73     {
74         Snippets thisClass = new Snippets();
75
76         try {
77             //Read the File
78
ClassFile thisClassFile =
79                 ClassFileReader.readFromFile( innerSourceFile );
80
81             //Get the sub parts of this Class
82
ISnippetFactory myMethodFactory =
83                 new ByteCodeMethodSnippetFactory( thisClassFile );
84             Snippets methods = myMethodFactory.getSnippets();
85
86             //Get any additional information
87
int thisClassInPool = thisClassFile.getThisClass();
88             String JavaDoc name =
89                 thisClassFile.getConstantPoolEntryName( thisClassInPool );
90             name = cleanValues( name );
91
92             String JavaDoc access = thisClassFile.getAccessFlagsVerbose();
93
94             int superClassInPool = thisClassFile.getSuperClass();
95             String JavaDoc superClass =
96                 thisClassFile.getConstantPoolEntryName( superClassInPool );
97             superClass=cleanValues( superClass );
98
99             //Now use to build a class snippet
100
ISnippet classSnippet = new SnippetClass( name, access,
101                                                       superClass, methods );
102
103             thisClass.add( classSnippet );
104         
105         } catch ( InvalidByteCodeException ibce ) {
106             throw new NoUnitException ( ibce,
107                                         "Could not read Snippets from Source" );
108         } catch ( IOException JavaDoc ie ) {
109             throw new NoUnitException ( ie,
110                                         "Could not read Snippets from Source" );
111         }
112         return thisClass;
113     }
114 }
115
Popular Tags