Skip to content

Commit

Permalink
[5457] Add new rules to PMD - final touch
Browse files Browse the repository at this point in the history
  • Loading branch information
wkurniawan07 committed May 26, 2016
1 parent 643f054 commit 64095a8
Show file tree
Hide file tree
Showing 31 changed files with 296 additions and 426 deletions.
5 changes: 2 additions & 3 deletions devdocs/staticAnalysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ The plugin for Eclipse can be found [here](https://sourceforge.net/projects/pmd/


#####Suppressing PMD warnings
To introduce code that violates PMD rules, PMD provides several methods of [suppressing](http://pmd.sourceforge.net/snapshot/usage/suppressing.html) rule violations, such as
the `SuppressWarnings` annotation or the `NOPMD` marker, which can be used to tell PMD to ignore specific parts of the code.
The suppression should be as specific as possible, and the reason for violating the rule should be explained.
To introduce code that violates PMD rules, use `@SuppressWarnings("PMD.RuleName")` annotation at the narrowest possible scope. PMD also provides several other methods of suppressing rule violations, which can be found in the [documentation here](http://pmd.sourceforge.net/snapshot/usage/suppressing.html).
The suppression should be as specific as possible, and the reason for violating the rule should be explained.

### FindBugs

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private FeedbackResponseAttributes injectRealIds(FeedbackResponseAttributes resp
FeedbackQuestionsLogic.inst().getFeedbackQuestion(
response.feedbackSessionName, response.courseId,
qnNumber).getId();
} catch (NumberFormatException e) { // NOPMD
} catch (NumberFormatException e) {
// Correct question ID was already attached to response.
}

Expand Down
194 changes: 99 additions & 95 deletions src/main/java/teammates/common/util/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,107 @@
* this class contains several nested classes, each containing a specific
* category of constants.
*/
@SuppressWarnings("PMD.FieldDeclarationsShouldBeAtStartOfClass")
public final class Const {

/*
* This section holds constants that are defined as constants primarily
* because they are repeated in many places.
*/
public static final String EOL = System.getProperty("line.separator");
public static final String HTML_BR_TAG = "<br>";

public static final String USER_NOBODY_TEXT = "-";
public static final String USER_UNKNOWN_TEXT = "Unknown user";
public static final String TEAM_OF_EMAIL_OWNER = "'s Team";
public static final String REGEXP_TEAM = String.format("^.*%s$", TEAM_OF_EMAIL_OWNER);

public static final String FEEDBACK_SESSION_QUESTIONS_HIDDEN = "Some questions may be hidden due to visibility options";
public static final String NONE_OF_THE_ABOVE = "None of the above";

public static final String INSTRUCTOR_FEEDBACK_SESSION_VISIBLE_TIME_CUSTOM = "custom";
public static final String INSTRUCTOR_FEEDBACK_SESSION_VISIBLE_TIME_ATOPEN = "atopen";
public static final String INSTRUCTOR_FEEDBACK_SESSION_VISIBLE_TIME_NEVER = "never";

public static final String INSTRUCTOR_FEEDBACK_RESULTS_VISIBLE_TIME_CUSTOM = "custom";
public static final String INSTRUCTOR_FEEDBACK_RESULTS_VISIBLE_TIME_ATVISIBLE = "atvisible";
public static final String INSTRUCTOR_FEEDBACK_RESULTS_VISIBLE_TIME_LATER = "later";
public static final String INSTRUCTOR_FEEDBACK_RESULTS_VISIBLE_TIME_NEVER = "never";
public static final String INSTRUCTOR_FEEDBACK_RESULTS_MISSING_RESPONSE = "No Response";

public static final String STUDENT_COURSE_STATUS_YET_TO_JOIN = "Yet to join";
public static final String STUDENT_COURSE_STATUS_JOINED = "Joined";
public static final String STUDENT_PROFILE_FIELD_NOT_FILLED = "Not Specified";

public static final String USER_NAME_FOR_SELF = "Myself";
public static final String USER_TEAM_FOR_INSTRUCTOR = "Instructors";
public static final String USER_NOT_IN_A_SECTION = "Not in a section";

public static final String ACTION_RESULT_FAILURE = "Servlet Action Failure";
public static final String ACTION_RESULT_SYSTEM_ERROR_REPORT = "System Error Report";

// for course sorting in instructorHomePage
public static final String SORT_BY_COURSE_ID = "id";
public static final String SORT_BY_COURSE_NAME = "name";
public static final String SORT_BY_COURSE_CREATION_DATE = "createdAt";
public static final String DEFAULT_SORT_CRITERIA = SORT_BY_COURSE_CREATION_DATE;

// used for instructor details single line form
public static final int LENGTH_FOR_NAME_EMAIL_INSTITUTION = 3;

public static final String DEFAULT_SECTION = "None";

public static final String EVAL_PREFIX_FOR_INSTRUCTOR_PRIVILEGES = "eval%";

/*
* These constants are used as variable values to mean that the variable
* is in a 'special' state.
*/
public static final int INT_UNINITIALIZED = -9999;
public static final double DOUBLE_UNINITIALIZED = -9999.0;

public static final int MAX_POSSIBLE_RECIPIENTS = -100;

public static final int POINTS_EQUAL_SHARE = 100;
public static final int POINTS_NOT_SURE = -101;
public static final int POINTS_NOT_SUBMITTED = -999;

public static final int VISIBILITY_TABLE_GIVER = 0;
public static final int VISIBILITY_TABLE_RECIPIENT = 1;

public static final String GENERAL_QUESTION = "%GENERAL%";
public static final String USER_IS_TEAM = "%TEAM%";
public static final String USER_IS_NOBODY = "%NOBODY%";
public static final String USER_IS_MISSING = "%MISSING%";

public static final Date TIME_REPRESENTS_FOLLOW_OPENING;
public static final Date TIME_REPRESENTS_FOLLOW_VISIBLE;
public static final Date TIME_REPRESENTS_NEVER;
public static final Date TIME_REPRESENTS_LATER;
public static final Date TIME_REPRESENTS_NOW;
public static final Date TIME_REPRESENTS_DEFAULT_TIMESTAMP;

static {
TIME_REPRESENTS_FOLLOW_OPENING = TimeHelper.convertToDate("1970-12-31 00:00 AM UTC");
TIME_REPRESENTS_FOLLOW_VISIBLE = TimeHelper.convertToDate("1970-06-22 00:00 AM UTC");
TIME_REPRESENTS_NEVER = TimeHelper.convertToDate("1970-11-27 00:00 AM UTC");
TIME_REPRESENTS_LATER = TimeHelper.convertToDate("1970-01-01 00:00 AM UTC");
TIME_REPRESENTS_NOW = TimeHelper.convertToDate("1970-02-14 00:00 AM UTC");
TIME_REPRESENTS_DEFAULT_TIMESTAMP = TimeHelper.convertToDate("2011-01-01 00:00 AM UTC");
}

public static final String ADMIN_EMAIL_TASK_QUEUE_ADDRESS_MODE = "adminEmailAddressMode";
public static final String ADMIN_EMAIL_TASK_QUEUE_GROUP_MODE = "adminEmailGroupMode";

/*
* Other Constants
*/
public static enum AdminEmailPageState { COMPOSE, SENT, TRASH, DRAFT };
public static enum StatusMessageColor { INFO, SUCCESS, WARNING, DANGER };

private Const() {
// Utility class containing constants
}

public static class SystemParams {

public static final String ENCODING = "UTF8";
Expand Down Expand Up @@ -1141,98 +1239,4 @@ public class StatusCodes {
public static final String NULL_POST_PARAMETER = "The %s POST parameter is null\n";
}

/* This section holds constants that are defined as constants primarily
* because they are repeated in many places.
*/
public static final String EOL = System.getProperty("line.separator");
public static final String HTML_BR_TAG = "<br>";

public static final String USER_NOBODY_TEXT = "-";
public static final String USER_UNKNOWN_TEXT = "Unknown user";
public static final String TEAM_OF_EMAIL_OWNER = "'s Team";
public static final String REGEXP_TEAM = String.format("^.*%s$", TEAM_OF_EMAIL_OWNER);

public static final String FEEDBACK_SESSION_QUESTIONS_HIDDEN = "Some questions may be hidden due to visibility options";
public static final String NONE_OF_THE_ABOVE = "None of the above";

public static final String INSTRUCTOR_FEEDBACK_SESSION_VISIBLE_TIME_CUSTOM = "custom";
public static final String INSTRUCTOR_FEEDBACK_SESSION_VISIBLE_TIME_ATOPEN = "atopen";
public static final String INSTRUCTOR_FEEDBACK_SESSION_VISIBLE_TIME_NEVER = "never";

public static final String INSTRUCTOR_FEEDBACK_RESULTS_VISIBLE_TIME_CUSTOM = "custom";
public static final String INSTRUCTOR_FEEDBACK_RESULTS_VISIBLE_TIME_ATVISIBLE = "atvisible";
public static final String INSTRUCTOR_FEEDBACK_RESULTS_VISIBLE_TIME_LATER = "later";
public static final String INSTRUCTOR_FEEDBACK_RESULTS_VISIBLE_TIME_NEVER = "never";
public static final String INSTRUCTOR_FEEDBACK_RESULTS_MISSING_RESPONSE = "No Response";

public static final String STUDENT_COURSE_STATUS_YET_TO_JOIN = "Yet to join";
public static final String STUDENT_COURSE_STATUS_JOINED = "Joined";
public static final String STUDENT_PROFILE_FIELD_NOT_FILLED = "Not Specified";

public static final String USER_NAME_FOR_SELF = "Myself";
public static final String USER_TEAM_FOR_INSTRUCTOR = "Instructors";
public static final String USER_NOT_IN_A_SECTION = "Not in a section";

public static String ACTION_RESULT_FAILURE = "Servlet Action Failure";
public static String ACTION_RESULT_SYSTEM_ERROR_REPORT = "System Error Report";

//for course sorting in instructorHomePage
public static final String SORT_BY_COURSE_ID = "id";
public static final String SORT_BY_COURSE_NAME = "name";
public static final String SORT_BY_COURSE_CREATION_DATE = "createdAt";
public static final String DEFAULT_SORT_CRITERIA = SORT_BY_COURSE_CREATION_DATE;

// used for instructor details single line form
public static final int LENGTH_FOR_NAME_EMAIL_INSTITUTION = 3;

public static final String DEFAULT_SECTION = "None";

public static final String EVAL_PREFIX_FOR_INSTRUCTOR_PRIVILEGES = "eval%";

/* These constants are used as variable values to mean that the variable
* is in a 'special' state.
*/
public static final int INT_UNINITIALIZED = -9999;
public static final double DOUBLE_UNINITIALIZED = -9999.0;

public static final int MAX_POSSIBLE_RECIPIENTS = -100;

public static final int POINTS_EQUAL_SHARE = 100;
public static final int POINTS_NOT_SURE = -101;
public static final int POINTS_NOT_SUBMITTED = -999;

public static final int VISIBILITY_TABLE_GIVER = 0;
public static final int VISIBILITY_TABLE_RECIPIENT = 1;

public static final String GENERAL_QUESTION = "%GENERAL%";
public static final String USER_IS_TEAM = "%TEAM%";
public static final String USER_IS_NOBODY = "%NOBODY%";
public static final String USER_IS_MISSING = "%MISSING%";

public static final Date TIME_REPRESENTS_FOLLOW_OPENING;
public static final Date TIME_REPRESENTS_FOLLOW_VISIBLE;
public static final Date TIME_REPRESENTS_NEVER;
public static final Date TIME_REPRESENTS_LATER;
public static final Date TIME_REPRESENTS_NOW;
public static final Date TIME_REPRESENTS_DEFAULT_TIMESTAMP;

static {
TIME_REPRESENTS_FOLLOW_OPENING = TimeHelper.convertToDate("1970-12-31 00:00 AM UTC");
TIME_REPRESENTS_FOLLOW_VISIBLE = TimeHelper.convertToDate("1970-06-22 00:00 AM UTC");
TIME_REPRESENTS_NEVER = TimeHelper.convertToDate("1970-11-27 00:00 AM UTC");
TIME_REPRESENTS_LATER = TimeHelper.convertToDate("1970-01-01 00:00 AM UTC");
TIME_REPRESENTS_NOW = TimeHelper.convertToDate("1970-02-14 00:00 AM UTC");
TIME_REPRESENTS_DEFAULT_TIMESTAMP = TimeHelper.convertToDate("2011-01-01 00:00 AM UTC");
}

/* Other Constants
*/
public static enum AdminEmailPageState { COMPOSE, SENT, TRASH, DRAFT };
public static enum StatusMessageColor { INFO, SUCCESS, WARNING, DANGER };
public static final String ADMIN_EMAIL_TASK_QUEUE_ADDRESS_MODE = "adminEmailAddressMode";
public static final String ADMIN_EMAIL_TASK_QUEUE_GROUP_MODE = "adminEmailGroupMode";

private Const() {
// Utility class containing constants
}
}
4 changes: 3 additions & 1 deletion src/main/java/teammates/common/util/EmailLogEntry.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package teammates.common.util;

import java.io.IOException;
import java.util.Calendar;
import java.util.TimeZone;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import teammates.logic.core.Sendgrid;
Expand All @@ -21,7 +23,7 @@ public class EmailLogEntry {
@SuppressWarnings("unused") // used by js
private String logInfoAsHtml;

public EmailLogEntry(MimeMessage msg) throws Exception {
public EmailLogEntry(MimeMessage msg) throws MessagingException, IOException {
this.receiver = msg.getRecipients(Message.RecipientType.TO)[0].toString();
this.subject = msg.getSubject();
this.content = (String) msg.getContent();
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/teammates/common/util/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,17 @@ private int compareVersionString(String s1, String s2) {
if (s2 == null) {
return -1;
}
String convertedS1 = s1;
String convertedS2 = s2;
while (convertedS1.length() < convertedS2.length()) {
convertedS1 = "0" + convertedS1; // NOPMD
}
while (convertedS2.length() < convertedS1.length()) {
convertedS2 = "0" + convertedS2; // NOPMD
String convertedS1;
String convertedS2;
if (s1.length() == s2.length()) {
convertedS1 = s1;
convertedS2 = s2;
} else if (s1.length() > s2.length()) {
convertedS1 = s1;
convertedS2 = StringHelper.generateStringOfLength(s1.length() - s2.length(), '0') + s2;
} else {
convertedS1 = StringHelper.generateStringOfLength(s2.length() - s1.length(), '0') + s1;
convertedS2 = s2;
}
return convertedS2.compareTo(convertedS1);
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/teammates/logic/automated/EmailAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ protected void logActivityFailure(HttpServletRequest req, Throwable e) {
log.severe(e.getMessage());
}

private String generateLogMessage(List<MimeMessage> emailsSent) throws Exception {
private String generateLogMessage(List<MimeMessage> emailsSent) throws MessagingException, IOException {
StringBuilder logMessage = new StringBuilder(100);
logMessage.append("Emails sent to:<br/>");

Expand All @@ -144,7 +144,8 @@ private String generateLogMessage(List<MimeMessage> emailsSent) throws Exception
return logMessage.toString();
}

private Map<String, EmailData> extractEmailDataForLogging(List<MimeMessage> emails) throws Exception {
private Map<String, EmailData> extractEmailDataForLogging(List<MimeMessage> emails)
throws MessagingException, IOException {
Map<String, EmailData> logData = new TreeMap<String, EmailData>();

for (MimeMessage email : emails) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/teammates/logic/backdoor/BackDoorLogic.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package teammates.logic.backdoor;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -405,7 +406,7 @@ private FeedbackResponseAttributes injectRealIds(FeedbackResponseAttributes resp
}
response.feedbackQuestionId = question.getId();

} catch (NumberFormatException e) { // NOPMD
} catch (NumberFormatException e) {
// Correct question ID was already attached to response.
}

Expand All @@ -432,7 +433,7 @@ private FeedbackResponseCommentAttributes injectRealIds(FeedbackResponseCommentA
responseComment.feedbackSessionName,
responseComment.courseId,
qnNumber).getId();
} catch (NumberFormatException e) { // NOPMD
} catch (NumberFormatException e) {
// Correct question ID was already attached to response.
}

Expand Down Expand Up @@ -597,7 +598,7 @@ public String isPicturePresentInGcs(String pictureKey) {
}

public void uploadAndUpdateStudentProfilePicture(String googleId,
byte[] pictureData) throws Exception {
byte[] pictureData) throws EntityDoesNotExistException, IOException {
String pictureKey = GoogleCloudStorageHelper.writeDataToGcs(googleId, pictureData, "");
updateStudentProfilePicture(googleId, pictureKey);
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/teammates/logic/backdoor/BackDoorServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import teammates.common.datatransfer.DataBundle;
import teammates.common.datatransfer.FeedbackResponseAttributes;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.common.exception.TeammatesException;
import teammates.common.util.Config;
import teammates.common.util.Const;
Expand Down Expand Up @@ -133,7 +135,7 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOEx
}

private String executeBackendAction(HttpServletRequest req, String action)
throws Exception {
throws EntityDoesNotExistException, InvalidParametersException, IOException {
// TODO: reorder in alphabetical order
BackDoorLogic backDoorLogic = new BackDoorLogic();
if (action.equals(OPERATION_DELETE_INSTRUCTOR)) {
Expand Down Expand Up @@ -273,7 +275,7 @@ private String executeBackendAction(HttpServletRequest req, String action)
FeedbackResponseAttributes fr = backDoorLogic.getFeedbackResponse(feedbackQuestionId, giverEmail, recipient);
backDoorLogic.deleteFeedbackResponse(fr);
} else {
throw new Exception("Unknown command: " + action);
throw new InvalidParametersException("Unknown command: " + action);
}
return Const.StatusCodes.BACKDOOR_STATUS_SUCCESS;
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/teammates/logic/core/TeamEvalResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,10 @@ private static double calculateFactor(double[] input) {
return factor;
}

// Suppress PMD.AvoidArrayLoops since the arrays are of different types
@SuppressWarnings("PMD.AvoidArrayLoops")
private static double[] intToDouble(int[] input) {
double[] converted = new double[input.length];
for (int i = 0; i < input.length; i++) {
converted[i] = input[i];
converted[i] = (double) input[i];
}
return converted;
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/teammates/ui/controller/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ private boolean doesUserNeedToLogin(UserType currentUser) {
return false;
}

@SuppressWarnings("PMD.EmptyIfStmt")
protected AccountAttributes authenticateAndGetNominalUser(UserType loggedInUserType) {
String paramRequestedUserId = request.getParameter(Const.ParamsNames.USER_ID);

Expand Down
Loading

0 comments on commit 64095a8

Please sign in to comment.