1 26 27 package net.sourceforge.groboutils.codecoverage.v2.compiler; 28 29 import java.lang.reflect.Method ; 30 import java.lang.reflect.Modifier ; 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 45 public class ParseCoverageLogger 46 { 47 53 private static final String DEFAULT_COVERAGELOGGER_CLASSNAME = 54 ICoverageLoggerConst.COVERAGELOGGER_CLASSNAME; 55 private static final String DEFAULT_COVERAGELOGGER_METHODNAME = 56 ICoverageLoggerConst.INVOKE_METHOD_NAME; 57 private static final Class COVERAGE_SIGNATURE[] = 58 ICoverageLoggerConst.COVERAGE_SIGNATURE; 59 private static final Class COVERAGE_RETURNTYPE = 60 ICoverageLoggerConst.COVERAGE_RETURNTYPE; 61 62 private String methodSignature; 63 private String methodName; 64 private String className; 65 66 67 70 public ParseCoverageLogger() 71 { 72 storeData( DEFAULT_COVERAGELOGGER_CLASSNAME, 76 DEFAULT_COVERAGELOGGER_METHODNAME ); 77 } 78 79 80 86 public ParseCoverageLogger( Class coverageClass, String methodName ) 87 { 88 if (methodName == null || coverageClass == null) 89 { 90 throw new IllegalArgumentException ( "No null args." ); 91 } 92 parseCoverageLoggerType( coverageClass, methodName ); 93 94 storeData( coverageClass.getName(), methodName ); 95 } 96 97 98 104 private void storeData( String className, String methodName ) 105 { 106 this.methodName = methodName; 107 this.className = className; 108 if (methodName == null || className == null) 109 { 110 throw new IllegalArgumentException ( "No null args." ); 111 } 112 113 this.methodSignature = createSignature( 114 COVERAGE_SIGNATURE, COVERAGE_RETURNTYPE ); 115 if (this.methodSignature == null) 116 { 117 throw new IllegalStateException ( 118 "Returned null method signature." ); 119 } 120 } 121 122 123 126 public String getClassName() 127 { 128 return this.className; 129 } 130 131 132 135 public String getMethodName() 136 { 137 return this.methodName; 138 } 139 140 141 144 public String getMethodSignature() 145 { 146 return this.methodSignature; 147 } 148 149 150 private void parseCoverageLoggerType( Class clazz, String methodName ) 151 { 152 Method m = null; 154 try 155 { 156 m = clazz.getMethod( methodName, COVERAGE_SIGNATURE ); 157 } 158 catch (NoSuchMethodException nsme) 159 { 160 m = null; 162 } 163 164 if (m == null) 165 { 166 throw new IllegalStateException ( 167 clazz.getName()+" doesn't have a method named '"+ 168 methodName+"'" ); 169 } 170 171 if (m.getReturnType() != null && 173 !m.getReturnType().equals( COVERAGE_RETURNTYPE )) 174 { 175 throw new IllegalStateException ( 176 clazz.getName()+" method '"+ 177 methodName+"' incorrectly has a return value." ); 178 } 179 180 int mod = m.getModifiers(); 182 if (!Modifier.isStatic( mod ) || !Modifier.isPublic( mod )) 183 { 184 throw new IllegalStateException ( 185 clazz.getName()+" method '"+ 186 methodName+"' is not public or static." ); 187 } 188 } 189 190 191 private static String createSignature( Class signature[], 192 Class 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 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 return new ObjectType( c.getName() ); 240 } 241 } 242 243 | Popular Tags |