KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)ParseCoverageLogger.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.lang.reflect.Method JavaDoc;
30 import java.lang.reflect.Modifier JavaDoc;
31
32 import net.sourceforge.groboutils.codecoverage.v2.logger.ICoverageLoggerConst;
33
34 import org.apache.bcel.generic.ObjectType;
35 import org.apache.bcel.generic.Type;
36
37 /**
38  * Parses the CoverageLogger class to discover the BCEL compatible invocation
39  * objects and names.
40  *
41  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
42  * @version $Date: 2004/04/15 05:48:25 $
43  * @since December 17, 2002
44  */

45 public class ParseCoverageLogger
46 {
47     //private static final Class COVERAGELOGGER_CLASS = CoverageLogger.class;
48
/**
49      * This is not the simplest or safest way to default to the coverage
50      * logger, but it does prevent loading the coverage logger class, thus
51      * preventing a possible error message.
52      */

53     private static final String JavaDoc DEFAULT_COVERAGELOGGER_CLASSNAME =
54         ICoverageLoggerConst.COVERAGELOGGER_CLASSNAME;
55     private static final String JavaDoc DEFAULT_COVERAGELOGGER_METHODNAME =
56         ICoverageLoggerConst.INVOKE_METHOD_NAME;
57     private static final Class JavaDoc COVERAGE_SIGNATURE[] =
58         ICoverageLoggerConst.COVERAGE_SIGNATURE;
59     private static final Class JavaDoc COVERAGE_RETURNTYPE =
60         ICoverageLoggerConst.COVERAGE_RETURNTYPE;
61     
62     private String JavaDoc methodSignature;
63     private String JavaDoc methodName;
64     private String JavaDoc className;
65     
66     
67     /**
68      * Reference the default CoverageLogger for creation.
69      */

70     public ParseCoverageLogger()
71     {
72         // We will assume that the defaults given are correct. If they
73
// aren't valid, then this is a major bug in the logger, not
74
// this class.
75
storeData( DEFAULT_COVERAGELOGGER_CLASSNAME,
76             DEFAULT_COVERAGELOGGER_METHODNAME );
77     }
78     
79     
80     /**
81      * Discovers the method signature and accompanying information for a
82      * specific coverage logger to generate. The given method must exist
83      * with the correct parameters, attributes, and return type.
84      * Otherwise, an exception is raised.
85      */

86     public ParseCoverageLogger( Class JavaDoc coverageClass, String JavaDoc methodName )
87     {
88         if (methodName == null || coverageClass == null)
89         {
90             throw new IllegalArgumentException JavaDoc( "No null args." );
91         }
92         parseCoverageLoggerType( coverageClass, methodName );
93         
94         storeData( coverageClass.getName(), methodName );
95     }
96     
97     
98     /**
99      * Inner method to store the data of the class and method for the
100      * logger. It doesn't do any checking besides null. Thus, this
101      * method must be private to prevent inadvertent invalid class and
102      * method assignment.
103      */

104     private void storeData( String JavaDoc className, String JavaDoc methodName )
105     {
106         this.methodName = methodName;
107         this.className = className;
108         if (methodName == null || className == null)
109         {
110             throw new IllegalArgumentException JavaDoc( "No null args." );
111         }
112         
113         this.methodSignature = createSignature(
114             COVERAGE_SIGNATURE, COVERAGE_RETURNTYPE );
115         if (this.methodSignature == null)
116         {
117             throw new IllegalStateException JavaDoc(
118                 "Returned null method signature." );
119         }
120     }
121     
122     
123     /**
124      * Returns the fully-qualified class name for the CoverageLogger.
125      */

126     public String JavaDoc getClassName()
127     {
128         return this.className;
129     }
130     
131     
132     /**
133      * Returns the name of the method, without the signature.
134      */

135     public String JavaDoc getMethodName()
136     {
137         return this.methodName;
138     }
139     
140     
141     /**
142      * Returns the signature of the method, without the method name.
143      */

144     public String JavaDoc getMethodSignature()
145     {
146         return this.methodSignature;
147     }
148     
149     
150     private void parseCoverageLoggerType( Class JavaDoc clazz, String JavaDoc methodName )
151     {
152         // ensure that the method exists with the correct signature.
153
Method JavaDoc m = null;
154         try
155         {
156             m = clazz.getMethod( methodName, COVERAGE_SIGNATURE );
157         }
158         catch (NoSuchMethodException JavaDoc nsme)
159         {
160             // this will throw the next exception. Hide this one!
161
m = null;
162         }
163         
164         if (m == null)
165         {
166             throw new IllegalStateException JavaDoc(
167                 clazz.getName()+" doesn't have a method named '"+
168                 methodName+"'" );
169         }
170         
171         // the method MUST NOT return anything!
172
if (m.getReturnType() != null &&
173             !m.getReturnType().equals( COVERAGE_RETURNTYPE ))
174         {
175             throw new IllegalStateException JavaDoc(
176                 clazz.getName()+" method '"+
177                 methodName+"' incorrectly has a return value." );
178         }
179         
180         // the method must also be static & public
181
int mod = m.getModifiers();
182         if (!Modifier.isStatic( mod ) || !Modifier.isPublic( mod ))
183         {
184             throw new IllegalStateException JavaDoc(
185                 clazz.getName()+" method '"+
186                 methodName+"' is not public or static." );
187         }
188     }
189     
190     
191     private static String JavaDoc createSignature( Class JavaDoc signature[],
192             Class JavaDoc returnClass )
193     {
194         Type returnType = getType( returnClass );
195         Type args[] = new Type[ signature.length ];
196         for (int i = 0; i < signature.length; ++i)
197         {
198             args[i] = getType( signature[i] );
199         }
200         return Type.getMethodSignature( returnType, args );
201     }
202     
203     
204     private static Type getType( Class JavaDoc c )
205     {
206         if (c.equals( Void.TYPE ))
207         {
208             return Type.VOID;
209         }
210         if (c.equals( Byte.TYPE ))
211         {
212             return Type.BYTE;
213         }
214         if (c.equals( Character.TYPE ))
215         {
216             return Type.CHAR;
217         }
218         if (c.equals( Short.TYPE ))
219         {
220             return Type.SHORT;
221         }
222         if (c.equals( Integer.TYPE ))
223         {
224             return Type.INT;
225         }
226         if (c.equals( Long.TYPE ))
227         {
228             return Type.LONG;
229         }
230         if (c.equals( Float.TYPE ))
231         {
232             return Type.FLOAT;
233         }
234         if (c.equals( Double.TYPE ))
235         {
236             return Type.DOUBLE;
237         }
238         // else
239
return new ObjectType( c.getName() );
240     }
241 }
242
243
Popular Tags