58
58
* Each key is a line number in this class, stored as an Integer object.
59
59
* Each value is information about the line, stored as a LineData object.
61
private Map branches = new HashMap();
61
private Map<Integer,LineData> branches = new HashMap<Integer,LineData>();
63
63
private boolean containsInstrumentationInfo = false;
65
private Set methodNamesAndDescriptors = new HashSet();
65
private Set<String> methodNamesAndDescriptors = new HashSet<String>();
67
67
private String name = null;
82
82
public LineData addLine(int lineNumber, String methodName,
83
83
String methodDescriptor)
85
LineData lineData = getLineData(lineNumber);
88
lineData = new LineData(lineNumber);
89
// Each key is a line number in this class, stored as an Integer object.
90
// Each value is information about the line, stored as a LineData object.
91
children.put(new Integer(lineNumber), lineData);
93
lineData.setMethodNameAndDescriptor(methodName, methodDescriptor);
95
// methodName and methodDescriptor can be null when cobertura.ser with
96
// no line information was loaded (or was not loaded at all).
97
if( methodName!=null && methodDescriptor!=null)
98
methodNamesAndDescriptors.add(methodName + methodDescriptor);
88
LineData lineData = getLineData(lineNumber);
91
lineData = new LineData(lineNumber);
92
// Each key is a line number in this class, stored as an Integer object.
93
// Each value is information about the line, stored as a LineData object.
94
children.put(new Integer(lineNumber), lineData);
96
lineData.setMethodNameAndDescriptor(methodName, methodDescriptor);
98
// methodName and methodDescriptor can be null when cobertura.ser with
99
// no line information was loaded (or was not loaded at all).
100
if( methodName!=null && methodDescriptor!=null)
101
methodNamesAndDescriptors.add(methodName + methodDescriptor);
103
111
* This is required because we implement Comparable.
105
public int compareTo(Object o)
113
public int compareTo(ClassData o)
107
if (!o.getClass().equals(ClassData.class))
108
return Integer.MAX_VALUE;
109
return this.name.compareTo(((ClassData)o).name);
115
return this.name.compareTo(o.name);
112
118
public boolean containsInstrumentationInfo()
114
return this.containsInstrumentationInfo;
123
return this.containsInstrumentationInfo;
129
143
ClassData classData = (ClassData)obj;
130
return super.equals(obj)
131
&& this.branches.equals(classData.branches)
132
&& this.methodNamesAndDescriptors
133
.equals(classData.methodNamesAndDescriptors)
134
&& this.name.equals(classData.name)
135
&& this.sourceFileName.equals(classData.sourceFileName);
144
getBothLocks(classData);
147
return super.equals(obj)
148
&& this.branches.equals(classData.branches)
149
&& this.methodNamesAndDescriptors
150
.equals(classData.methodNamesAndDescriptors)
151
&& this.name.equals(classData.name)
152
&& this.sourceFileName.equals(classData.sourceFileName);
157
classData.lock.unlock();
138
161
public String getBaseName()
156
for (Iterator iter = branches.values().iterator(); iter.hasNext();) {
157
LineData next = (LineData) iter.next();
158
if (methodNameAndDescriptor.equals(next.getMethodName() + next.getMethodDescriptor()))
160
total += next.getNumberOfValidBranches();
161
covered += next.getNumberOfCoveredBranches();
182
for (Iterator<LineData> iter = branches.values().iterator(); iter.hasNext();) {
183
LineData next = (LineData) iter.next();
184
if (methodNameAndDescriptor.equals(next.getMethodName() + next.getMethodDescriptor()))
186
total += next.getNumberOfValidBranches();
187
covered += next.getNumberOfCoveredBranches();
164
if (total == 0) return 1.0;
165
return (double) covered / total;
190
if (total == 0) return 1.0;
191
return (double) covered / total;
168
public Collection getBranches()
199
public Collection<Integer> getBranches()
170
return Collections.unmodifiableCollection(branches.keySet());
204
return Collections.unmodifiableCollection(branches.keySet());
196
Iterator iter = children.values().iterator();
197
while (iter.hasNext())
199
LineData next = (LineData) iter.next();
200
if (methodNameAndDescriptor.equals(next.getMethodName() + next.getMethodDescriptor()))
246
Iterator<CoverageData> iter = children.values().iterator();
247
while (iter.hasNext())
203
if (next.getHits() > 0) {
249
LineData next = (LineData) iter.next();
250
if (methodNameAndDescriptor.equals(next.getMethodName() + next.getMethodDescriptor()))
253
if (next.getHits() > 0) {
208
if (total == 0) return 1d;
209
return (double) hits / total;
258
if (total == 0) return 1d;
259
return (double) hits / total;
212
267
private LineData getLineData(int lineNumber)
214
return (LineData)children.get(new Integer(lineNumber));
217
public SortedSet getLines()
219
return new TreeSet(this.children.values());
222
public Collection getLines(String methodNameAndDescriptor)
224
Collection lines = new HashSet();
225
Iterator iter = children.values().iterator();
226
while (iter.hasNext())
228
LineData next = (LineData)iter.next();
229
if (methodNameAndDescriptor.equals(next.getMethodName()
230
+ next.getMethodDescriptor()))
272
return (LineData)children.get(Integer.valueOf(lineNumber));
280
public SortedSet<CoverageData> getLines()
285
return new TreeSet<CoverageData>(this.children.values());
293
public Collection<CoverageData> getLines(String methodNameAndDescriptor)
295
Collection<CoverageData> lines = new HashSet<CoverageData>();
299
Iterator<CoverageData> iter = children.values().iterator();
300
while (iter.hasNext())
302
LineData next = (LineData)iter.next();
303
if (methodNameAndDescriptor.equals(next.getMethodName()
304
+ next.getMethodDescriptor()))
239
318
* @return The method name and descriptor of each method found in the
240
319
* class represented by this instrumentation.
242
public Set getMethodNamesAndDescriptors()
321
public Set<String> getMethodNamesAndDescriptors()
244
return methodNamesAndDescriptors;
326
return methodNamesAndDescriptors;
247
334
public String getName()
297
400
public String getSourceFileName()
300
if (sourceFileName != null)
301
baseName = sourceFileName;
304
baseName = getBaseName();
305
int firstDollarSign = baseName.indexOf('$');
306
if (firstDollarSign == -1 || firstDollarSign == 0)
406
if (sourceFileName != null)
407
baseName = sourceFileName;
309
baseName = baseName.substring(0, firstDollarSign)
313
String packageName = getPackageName();
314
if (packageName.equals(""))
316
return packageName.replace('.', '/') + '/' + baseName;
410
baseName = getBaseName();
411
int firstDollarSign = baseName.indexOf('$');
412
if (firstDollarSign == -1 || firstDollarSign == 0)
415
baseName = baseName.substring(0, firstDollarSign)
419
String packageName = getPackageName();
420
if (packageName.equals(""))
422
return packageName.replace('.', '/') + '/' + baseName;
319
430
public int hashCode()
338
457
public boolean isValidSourceLineNumber(int lineNumber)
340
return children.containsKey(new Integer(lineNumber));
462
return children.containsKey(Integer.valueOf(lineNumber));
343
470
public void addLineJump(int lineNumber, int branchNumber)
345
LineData lineData = getLineData(lineNumber);
346
if (lineData != null)
348
lineData.addJump(branchNumber);
349
this.branches.put(new Integer(lineNumber), lineData);
475
LineData lineData = getLineData(lineNumber);
476
if (lineData != null)
478
lineData.addJump(branchNumber);
479
this.branches.put(Integer.valueOf(lineNumber), lineData);
353
488
public void addLineSwitch(int lineNumber, int switchNumber, int[] keys)
355
LineData lineData = getLineData(lineNumber);
356
if (lineData != null)
358
lineData.addSwitch(switchNumber, keys);
359
this.branches.put(new Integer(lineNumber), lineData);
493
LineData lineData = getLineData(lineNumber);
494
if (lineData != null)
496
lineData.addSwitch(switchNumber, keys);
497
this.branches.put(Integer.valueOf(lineNumber), lineData);
363
506
public void addLineSwitch(int lineNumber, int switchNumber, int min, int max)
365
LineData lineData = getLineData(lineNumber);
366
if (lineData != null)
368
lineData.addSwitch(switchNumber, min, max);
369
this.branches.put(new Integer(lineNumber), lineData);
511
LineData lineData = getLineData(lineNumber);
512
if (lineData != null)
514
lineData.addSwitch(switchNumber, min, max);
515
this.branches.put(Integer.valueOf(lineNumber), lineData);
383
534
if (!this.getName().equals(classData.getName()))
386
super.merge(coverageData);
388
// We can't just call this.branches.putAll(classData.branches);
389
// Why not? If we did a putAll, then the LineData objects from
390
// the coverageData class would overwrite the LineData objects
391
// that are already in "this.branches" And we don't need to
392
// update the LineData objects that are already in this.branches
393
// because they are shared between this.branches and this.children,
394
// so the object hit counts will be moved when we called
395
// super.merge() above.
396
for (Iterator iter = classData.branches.keySet().iterator(); iter.hasNext();)
537
getBothLocks(classData);
398
Object key = iter.next();
399
if (!this.branches.containsKey(key))
540
super.merge(coverageData);
542
// We can't just call this.branches.putAll(classData.branches);
543
// Why not? If we did a putAll, then the LineData objects from
544
// the coverageData class would overwrite the LineData objects
545
// that are already in "this.branches" And we don't need to
546
// update the LineData objects that are already in this.branches
547
// because they are shared between this.branches and this.children,
548
// so the object hit counts will be moved when we called
549
// super.merge() above.
550
for (Iterator<Integer> iter = classData.branches.keySet().iterator(); iter.hasNext();)
401
this.branches.put(key, classData.branches.get(key));
552
Integer key = iter.next();
553
if (!this.branches.containsKey(key))
555
this.branches.put(key, classData.branches.get(key));
405
this.containsInstrumentationInfo |= classData.containsInstrumentationInfo;
406
this.methodNamesAndDescriptors.addAll(classData
407
.getMethodNamesAndDescriptors());
408
if (classData.sourceFileName != null)
409
this.sourceFileName = classData.sourceFileName;
559
this.containsInstrumentationInfo |= classData.containsInstrumentationInfo;
560
this.methodNamesAndDescriptors.addAll(classData
561
.getMethodNamesAndDescriptors());
562
if (classData.sourceFileName != null)
563
this.sourceFileName = classData.sourceFileName;
568
classData.lock.unlock();
412
572
public void removeLine(int lineNumber)
414
Integer lineObject = new Integer(lineNumber);
415
children.remove(lineObject);
416
branches.remove(lineObject);
574
Integer lineObject = Integer.valueOf(lineNumber);
578
children.remove(lineObject);
579
branches.remove(lineObject);
419
587
public void setContainsInstrumentationInfo()
421
this.containsInstrumentationInfo = true;
592
this.containsInstrumentationInfo = true;
424
600
public void setSourceFileName(String sourceFileName)
426
this.sourceFileName = sourceFileName;
605
this.sourceFileName = sourceFileName;
430
614
* Increment the number of hits for a particular line of code.
432
616
* @param lineNumber the line of code to increment the number of hits.
617
* @param hits how many times the piece was called
434
public void touch(int lineNumber)
619
public void touch(int lineNumber,int hits)
436
LineData lineData = getLineData(lineNumber);
437
if (lineData == null)
438
lineData = addLine(lineNumber, null, null);
624
LineData lineData = getLineData(lineNumber);
625
if (lineData == null)
626
lineData = addLine(lineNumber, null, null);
627
lineData.touch(hits);
445
638
* @param lineNumber The line of code where the branch is
446
639
* @param branchNumber The branch on the line to change the hit counter
447
640
* @param branch The hit counter (true or false)
641
* @param hits how many times the piece was called
449
public void touchJump(int lineNumber, int branchNumber, boolean branch) {
450
LineData lineData = getLineData(lineNumber);
451
if (lineData == null)
452
lineData = addLine(lineNumber, null, null);
453
lineData.touchJump(branchNumber, branch);
643
public void touchJump(int lineNumber, int branchNumber, boolean branch,int hits) {
647
LineData lineData = getLineData(lineNumber);
648
if (lineData == null)
649
lineData = addLine(lineNumber, null, null);
650
lineData.touchJump(branchNumber, branch,hits);
459
661
* @param lineNumber The line of code where the branch is
460
662
* @param switchNumber The switch on the line to change the hit counter
461
663
* @param branch The hit counter
664
* @param hits how many times the piece was called
463
public void touchSwitch(int lineNumber, int switchNumber, int branch) {
464
LineData lineData = getLineData(lineNumber);
465
if (lineData == null)
466
lineData = addLine(lineNumber, null, null);
467
lineData.touchSwitch(switchNumber, branch);
666
public void touchSwitch(int lineNumber, int switchNumber, int branch,int hits) {
670
LineData lineData = getLineData(lineNumber);
671
if (lineData == null)
672
lineData = addLine(lineNumber, null, null);
673
lineData.touchSwitch(switchNumber, branch,hits);