Project: clover
Issue CLOV-1339: The <testmethod name=".*> does not match constructors
*Problem:* If you declare custom test detector, for instance: {code:xml} {code} Then the test detector does not match constructors. As a consequence, a class like this: {code:java} public class SomeException extends Exception { public SomeException(String s) { super(s); } } {code} will be treated as APPLICATION not as TEST code. *Background reason:* {code:java} public boolean isMethodMatch(SourceContext sourceContext, MethodContext methodContext) { final MethodSignature signature = methodContext.getSignature(); return methodMatches(signature.getName()) && methodAnnotationMatches(signature.getModifiers()) && methodReturnPatternMatches(signature.getReturnType()) && methodTagMatches(signature.getTags()); } public boolean methodReturnPatternMatches(String methodReturnType) { return methodReturnType != null && (methodReturnTypePattern == null || methodReturnTypePattern.matcher(methodReturnType).matches()); } {code} The methodReturnType==null for constructors, thus constructor do not match the pattern. *Solution:* We could rewrite check like this: {code:java} return methodReturnTypePattern == null || (methodReturnType != null && methodReturnTypePattern.matcher(methodReturnType).matches()); {code} However, this would require entirely new handling of code instrumentation for constructors. The reason is that code would be currently rewritten like: {code:java} public SomeException(String s) { __CLR3_1_12_100hjv4vvu6.R.globalSliceStart(getClass().getName(), 0); int $CLV_p$ = 0; java.lang.Throwable $CLV_t$ = null; try { __CLR3_1_12_162c4pt0(s); // <<< ORIGINAL CONSTRUCTOR $CLV_p$ = 1; } catch (java.lang.Throwable $CLV_t2$) { if ($CLV_p$ == 0 && $CLV_t$ == null) { $CLV_t$ = $CLV_t2$; } __CLR3_1_12_100hjv4vvu6.R.rethrow($CLV_t2$); } finally { __CLR3_1_12_100hjv4vvu6.R.globalSliceEnd(getClass().getName(), "SomeException.SomeException", 0, $CLV_p$, $CLV_t$); } } private __CLR3_1_12_162c4pt0(String s) { // << COMPILATION FAILURE, MISSING RETURN TYPE super(s); __CLR3_1_12_100hjv4vvu6.R.inc(1); __CLR3_1_12_100hjv4vvu6.R.inc(0); } {code} Problems: * call to super() must be first * we should not move constructor body into another method
Story points: 20