-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeReviewerIntegrationExample.java
More file actions
277 lines (230 loc) · 10.5 KB
/
CodeReviewerIntegrationExample.java
File metadata and controls
277 lines (230 loc) · 10.5 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* EXAMPLE INTEGRATION FILE
*
* This file shows how to integrate the AI Code Reviewer Agent into your project
* Copy and modify as needed for your use case
*/
import com.ai.reviewer.agent.CodeReviewerAgent;
import com.ai.reviewer.model.ReviewComment;
import com.ai.reviewer.model.ReviewResult;
import com.ai.reviewer.model.ReviewSeverity;
import com.ai.reviewer.util.ConfigManager;
import com.ai.reviewer.util.FileReader;
import java.util.List;
public class CodeReviewerIntegrationExample {
/**
* Example 1: Simple single file review
*/
public static void singleFileReview() {
CodeReviewerAgent agent = new CodeReviewerAgent("My Reviewer", "1.0.0");
String javaCode = """
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int multiply(int a, int b) {
try {
return a * b;
} catch (Exception e) {
// Silent catch
}
return 0;
}
}
""";
ReviewResult result = agent.reviewCode("Calculator.java", javaCode);
System.out.println(result);
}
/**
* Example 2: Batch review multiple files
*/
public static void batchReview(String[] filePaths) {
CodeReviewerAgent agent = new CodeReviewerAgent("Batch Reviewer", "1.0.0");
double totalScore = 0;
int fileCount = 0;
for (String filePath : filePaths) {
try {
String code = FileReader.readFile(filePath);
String fileName = FileReader.getFileName(filePath);
ReviewResult result = agent.reviewCode(fileName, code);
totalScore += result.getOverallScore();
fileCount++;
System.out.println(result);
System.out.println("\n");
} catch (Exception e) {
System.err.println("Error reviewing " + filePath + ": " + e.getMessage());
}
}
if (fileCount > 0) {
System.out.println("Average Score: " + (totalScore / fileCount) + "%");
}
}
/**
* Example 3: Review with filtering by severity
*/
public static void reviewWithSeverityFilter() {
CodeReviewerAgent agent = new CodeReviewerAgent("Filter Reviewer", "1.0.0");
String code = """
public class Example {
public static int X = 100;
public void process() {
try {
doSomething();
} catch (Exception e) {
}
}
private void doSomething() {
System.out.println("Processing");
}
}
""";
ReviewResult result = agent.reviewCode("Example.java", code);
// Show only critical and major issues
List<ReviewComment> criticalIssues = result.getCommentsBySeverity(ReviewSeverity.CRITICAL);
List<ReviewComment> majorIssues = result.getCommentsBySeverity(ReviewSeverity.MAJOR);
System.out.println("CRITICAL ISSUES: " + criticalIssues.size());
for (ReviewComment comment : criticalIssues) {
System.out.println(" - Line " + comment.getLineNumber() + ": " + comment.getMessage());
System.out.println(" Fix: " + comment.getSuggestion());
}
System.out.println("\nMAJOR ISSUES: " + majorIssues.size());
for (ReviewComment comment : majorIssues) {
System.out.println(" - Line " + comment.getLineNumber() + ": " + comment.getMessage());
System.out.println(" Fix: " + comment.getSuggestion());
}
}
/**
* Example 4: Custom configuration
*/
public static void configureReviewer() {
ConfigManager config = new ConfigManager();
// Customize configuration
config.setProperty("agent.name", "My Custom Reviewer");
config.setProperty("agent.version", "2.0.0");
config.setProperty("review.max.method.lines", "50");
config.setProperty("review.check.magic.numbers", "true");
config.setProperty("review.check.naming", "true");
// Create agent with custom config
CodeReviewerAgent agent = new CodeReviewerAgent(
config.getString("agent.name"),
config.getString("agent.version")
);
String code = """
public class Service {
private void longMethod() {
// ... many lines of code (>50 lines)
}
}
""";
ReviewResult result = agent.reviewCode("Service.java", code);
System.out.println(result);
}
/**
* Example 5: CI/CD Pipeline Integration
*/
public static int reviewCodeForCIPipeline(String[] sourceFiles, double minScore) {
CodeReviewerAgent agent = new CodeReviewerAgent("CI Pipeline Reviewer", "1.0.0");
int failedFiles = 0;
for (String filePath : sourceFiles) {
try {
String code = FileReader.readFile(filePath);
ReviewResult result = agent.reviewCode(filePath, code);
if (result.getOverallScore() < minScore) {
System.out.println("FAIL: " + filePath);
System.out.println("Score: " + result.getOverallScore() + "% (minimum: " + minScore + "%)");
System.out.println("Critical Issues: " + result.getCriticalIssues());
System.out.println("Major Issues: " + result.getMajorIssues());
failedFiles++;
}
} catch (Exception e) {
System.err.println("Error reviewing: " + filePath);
failedFiles++;
}
}
return failedFiles;
}
/**
* Example 6: Detailed issue analysis
*/
public static void analyzeIssuesInDetail() {
CodeReviewerAgent agent = new CodeReviewerAgent("Analysis Reviewer", "1.0.0");
String code = """
public class DetailedExample {
public static int CONFIG = 5000;
public void process(String s) {
int r = s.length();
try {
doWork(r);
} catch (Exception e) {
}
}
private void doWork(int val) {
if (val > 100) {
System.out.println("Large");
}
}
}
""";
ReviewResult result = agent.reviewCode("DetailedExample.java", code);
System.out.println("File: " + result.getFileName());
System.out.println("Overall Score: " + result.getOverallScore() + "/100");
System.out.println("Review took: " + result.getReviewTimeMs() + "ms");
System.out.println("Total Issues: " + result.getTotalIssues());
System.out.println();
// Detailed analysis by category
for (ReviewComment comment : result.getComments()) {
System.out.println("[" + comment.getSeverity().getLabel() + "]");
System.out.println("Location: " + comment.getCategory() + " at line " + comment.getLineNumber());
System.out.println("Issue: " + comment.getMessage());
System.out.println("Code: " + comment.getCode());
System.out.println("Suggestion: " + comment.getSuggestion());
System.out.println("---");
}
}
/**
* Example 7: Create report summary
*/
public static void generateReportSummary(List<ReviewResult> results) {
System.out.println("╔════════════════════════════════════════════════════════╗");
System.out.println("║ CODE REVIEW SUMMARY REPORT ║");
System.out.println("╚════════════════════════════════════════════════════════╝");
System.out.println();
double totalScore = 0;
int totalIssues = 0;
int totalCritical = 0;
int totalMajor = 0;
for (ReviewResult result : results) {
totalScore += result.getOverallScore();
totalIssues += result.getTotalIssues();
totalCritical += result.getCriticalIssues();
totalMajor += result.getMajorIssues();
String status = result.getOverallScore() >= 80 ? "✓ PASS" : "✗ FAIL";
System.out.printf("%s | %-30s | Score: %6.1f%% | Issues: %3d\n",
status,
result.getFileName(),
result.getOverallScore(),
result.getTotalIssues()
);
}
System.out.println();
System.out.println("SUMMARY STATISTICS:");
System.out.printf("Average Score: %.1f%%\n", totalScore / results.size());
System.out.printf("Total Issues: %d\n", totalIssues);
System.out.printf("Critical Issues: %d\n", totalCritical);
System.out.printf("Major Issues: %d\n", totalMajor);
System.out.println();
}
// Main method to run examples
public static void main(String[] args) {
System.out.println("=== AI CODE REVIEWER INTEGRATION EXAMPLES ===\n");
System.out.println("1. Single File Review:");
System.out.println("------------------------");
singleFileReview();
System.out.println("\n\n2. Review with Severity Filtering:");
System.out.println("-----------------------------------");
reviewWithSeverityFilter();
System.out.println("\n\n3. Detailed Analysis:");
System.out.println("--------------------");
analyzeIssuesInDetail();
}
}