Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package org.jtbank.codeant.codeant_demo.controller;

import java.util.HashMap;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Iterator;
import java.util.Random;

/**
*
* @author JT0020031_admin
*/
//Constant name should not be found
//Object array passed to varargs should be found
//Unnecessary String assignment instead of direct return should be found
//Unnecessary throws Exception should be found
//Generic exception instead of a specific one should be found
//Unnecessary boxing of a boolean value should be found
//Parametrized value for HashMap should be found, eg. new HashMap<>();
//Equals and to lowercase should be modified to equailsIgnoreCase call should be found
//Unused imports should be found
//Recommendation to switch to SecureRandom from Random should be found
@RestController
@RequestMapping("/codeant")
public class CodeantController {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Rename the public class to match the file name to prevent a compilation error caused by Java's public class naming rule. [bug]

Suggested change
public class CodeantController {
public class CodeantController2 {
Why this change? In Java, a public class must have the same name as the file it resides in. The file is named CodeantController2.java, but the class is declared as CodeantController, which would cause a compilation error. Renaming the class to CodeantController2 aligns with Java's naming rules and resolves the compilation issue. This change does not alter the behavior of any methods or endpoints. The annotations (@RestController and @RequestMapping) and method bodies remain unchanged. The logger initialization still references CodeantController.class, which compiles because that class exists elsewhere in the project; while it may not be ideal for logging categorization, it does not introduce a runtime or compilation error. Therefore, the improved code is syntactically correct, compiles, and preserves runtime behavior.

private final Logger LOGGER = LogManager.getLogger(CodeantController.class);
private static final HashMap<String, String> API_ENDPOINTS = new HashMap();

@GetMapping("/test-method")
public String testMethod() throws Exception {
LOGGER.log(Level.DEBUG, "{} {} {} {} {}{}", new Object[]{
"This",
"is",
"a",
"test",
"message",
"."
}
);
return getTestMethodResponse();
}

@GetMapping("/generic-exception")
public ResponseEntity<Exception> genericException(){
try
{
getGenericException();
return ResponseEntity.ok().build();
}
catch(Exception ex)
{
return ResponseEntity.internalServerError().body(ex);
Comment on lines +54 to +62
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Avoid returning the caught exception in the HTTP response and change the response type to not expose internal details, returning a generic error without a body instead. [security]

}
}

@GetMapping("/evaluate-condition")
public void conditionEval(){
evaluateCondition(Boolean.TRUE);
}

@GetMapping("/string-compare")
private ResponseEntity<Boolean> compareString(){
return ResponseEntity.ok(checkStrings("a", "B"));
}

@GetMapping
private ResponseEntity<Integer> getRandomNumber(){
Comment on lines +72 to +77
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Make request handler methods public to ensure Spring can reliably map and invoke them. [possible bug]

return ResponseEntity.ok(randomNumber());
}

private String getTestMethodResponse() throws Exception {
String returnVal = "";
returnVal = "This is a test message";
return returnVal;
}

private void getGenericException() throws Exception {
int val = 1 + 1;
if(val == 2)
{
throw new Exception("Generic Exception");
}
}

private void evaluateCondition(Boolean condition){
if(condition)
{
//This log should not be found
LOGGER.log(Level.DEBUG, "Condition {} is evaluated as {}", condition, condition);
}
}

private boolean checkStrings(String a, String b){
return a.toLowerCase().equals(b.toLowerCase());
}

private int randomNumber(){
Random r = new Random();
return r.nextInt();
}
}
Loading