-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
264 lines (238 loc) · 9.02 KB
/
report.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//Copyright (c) 2013, The Impendulo Authors
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification,
//are permitted provided that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright notice, this
// list of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
//ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
//WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
//DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
//ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
//(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
//ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
//(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
//SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package findbugs
import (
"encoding/gob"
"encoding/xml"
"fmt"
"github.com/godfried/impendulo/tool"
"github.com/godfried/impendulo/tool/result"
"html/template"
"labix.org/v2/mgo/bson"
)
type (
DummyReport struct {
Time int `xml:"analysisTimestamp,attr"`
Summary *Summary `xml:"FindBugsSummary"`
//Instances is all the bugs found by Findbugs
Instances []*BugInstance `xml:"BugInstance"`
//Categories is the bug categories found by Findbugs.
Categories []*BugCategory `xml:"BugCategory"`
//Patterns is the bug patterns found by Findbugs.
Patterns []*BugPattern `xml:"BugPattern"`
}
//Report stores the results of running Findbugs. It is
//populated from XML output produced by findbugs.
Report struct {
Id bson.ObjectId
Time int
Summary *Summary
//Instances is all the bugs found by Findbugs
Instances []*BugInstance
//CategoryMap and PatternMap make it easier to use the bug categories and patterns.
CategoryMap map[string]*BugCategory
PatternMap map[string]*BugPattern
}
//Summary provides statistics about Findbugs's execution on a package and
//file level. Furthermore it gives performance information such as memory usage
//and where time was spent.
Summary struct {
ClassCount int `xml:"total_classes,attr"`
ReferenceCount int `xml:"referenced_classes,attr"`
BugCount int `xml:"total_bugs,attr"`
Size int `xml:"total_size,attr"`
PackageCount int `xml:"num_packages,attr"`
SecondsCPU int `xml:"cpu_seconds,attr"`
SecondsClock int `xml:"clock_seconds,attr"`
SecondsGC int `xml:"gc_seconds,attr"`
PeakMB int `xml:"peak_mbytes,attr"`
AllocMB int `xml:"alloc_mbytes,attr"`
Priority1 int `xml:"priority_1,attr"`
Priority2 int `xml:"priority_2,attr"`
Priority3 int `xml:"priority_3,attr"`
Files []*FileStats `xml:"FileStats"`
Packages []*PackageStats `xml:"PackageStats"`
}
//FileStats provides Findbugs statistics about a specific source file.
FileStats struct {
Path string `xml:"path,attr"`
BugCount int `xml:"bugCount,attr"`
Size int `xml:"size,attr"`
}
//PackageStats provides Findbugs statistics about a specific package
//as well as the classes found within it.
PackageStats struct {
Name string `xml:"package,attr"`
ClassCount int `xml:"total_types,attr"`
BugCount int `xml:"total_bugs,attr"`
Size int `xml:"total_size,attr"`
Priority1 int `xml:"priority_1,attr"`
Priority2 int `xml:"priority_2,attr"`
Priority3 int `xml:"priority_3,attr"`
Classes []*ClassStats `xml:"ClassStats"`
}
//ClassStats provides Findbugs statistics about a specific class
//within a package.
ClassStats struct {
Name string `xml:"class,attr"`
Source string `xml:"sourceFile,attr"`
IsInterface bool `xml:"interface,attr"`
BugCount int `xml:"bugs,attr"`
Size int `xml:"size,attr"`
Priority1 int `xml:"priority_1,attr"`
Priority2 int `xml:"priority_2,attr"`
Priority3 int `xml:"priority_3,attr"`
}
//BugInstance describes a particular bug detected by Findbugs.
//It contains information describing its location and state
//as well as its category, severity and type.
BugInstance struct {
Id bson.ObjectId
Type string `xml:"type,attr"`
Priority int `xml:"priority,attr"`
Abbreviation string `xml:"abbrev,attr"`
Category string `xml:"category,attr"`
Rank int `xml:"rank,attr"`
ShortMessage string `xml:"ShortMessage"`
LongMessage string `xml:"LongMessage"`
Class *Class `xml:"Class"`
Method *Method `xml:"Method"`
Field *Field `xml:"Field"`
Var *LocalVariable `xml:"LocalVariable"`
Line *SourceLine `xml:"SourceLine"`
Properties []*Property `xml:"Property"`
}
//Class describes a Java Class in which a bug was found.
Class struct {
Name string `xml:"classname,attr"`
IsPrimary bool `xml:"primary,attr"`
Line *SourceLine `xml:"SourceLine"`
Message string `xml:"Message"`
}
//Sourceline describes a line inside a Java Class.
SourceLine struct {
Class string `xml:"classname,attr"`
Start int `xml:"start,attr"`
End int `xml:"end,attr"`
StartBC int `xml:"startBytecode,attr"`
EndBC int `xml:"endBytecode,attr"`
File string `xml:"sourcefile,attr"`
Path string `xml:"sourcepath,attr"`
Message string `xml:"Message"`
}
//Method describes a method within a Java Class.
Method struct {
Name string `xml:"name,attr"`
Class string `xml:"classname,attr"`
Signature string `xml:"signature,attr"`
IsStatic bool `xml:"isStatic,attr"`
IsPrimary bool `xml:"primary,attr"`
Line *SourceLine `xml:"SourceLine"`
Message string `xml:"Message"`
}
//Field describes a global variable within a Java Class.
Field struct {
Name string `xml:"name,attr"`
Class string `xml:"classname,attr"`
Signature string `xml:"signature,attr"`
IsStatic bool `xml:"isStatic,attr"`
IsPrimary bool `xml:"primary,attr"`
Role string `xml:"role,attr"`
Line *SourceLine `xml:"SourceLine"`
Message string `xml:"Message"`
}
//LocalVariable describes a local variable within a Java Class.
LocalVariable struct {
Name string `xml:"name,attr"`
Register int `xml:"register,attr"`
PC string `xml:"pc,attr"`
Role string `xml:"role,attr"`
Message string `xml:"Message"`
}
//Property decribes some attribute associated with a bug.
Property struct {
Name string `xml:"name,attr"`
Value string `xml:"value,attr"`
}
//BugCategory describes a category in which bugs may fall.
BugCategory struct {
Name string `xml:"category,attr"`
Description string `xml:"Description"`
}
//BugPattern describes a pattern associated with a BugCategory.
BugPattern struct {
Type string `xml:"type,attr"`
Abbreviation string `xml:"abbrev,attr"`
Category string `xml:"category,attr"`
Description string `xml:"ShortDescription"`
Details template.HTML `xml:"Details"`
}
)
func init() {
gob.Register(new(Report))
}
//NewReport
func NewReport(id bson.ObjectId, data []byte) (*Report, error) {
var d *DummyReport
if e := xml.Unmarshal(data, &d); e != nil {
return nil, tool.NewXMLError(e, "findbugs/findbugsResult.go")
}
r := &Report{
Id: id,
Time: d.Time,
Summary: d.Summary,
Instances: d.Instances,
CategoryMap: make(map[string]*BugCategory),
PatternMap: make(map[string]*BugPattern),
}
for _, c := range d.Categories {
r.CategoryMap[c.Name] = c
}
for _, p := range d.Patterns {
r.PatternMap[p.Type] = p
}
for _, b := range r.Instances {
b.Id = bson.NewObjectId()
}
return r, nil
}
//Success returns true if Findbugs found no bugs and false otherwise.
func (r *Report) Success() bool {
return len(r.Instances) == 0
}
//String
func (r *Report) String() string {
return fmt.Sprintf("Id: %q; Summary: %s", r.Id, r.Summary)
}
func (r *Report) Lines() []*result.Line {
lines := make([]*result.Line, 0, len(r.Instances))
for _, b := range r.Instances {
lines = append(lines, &result.Line{Title: r.PatternMap[b.Type].Description, Description: b.LongMessage, Start: b.Line.Start, End: b.Line.End})
}
return lines
}
//String
func (s *Summary) String() string {
return fmt.Sprintf("BugCount: %d; ClassCount: %d", s.BugCount, s.ClassCount)
}