diff --git a/README.md b/README.md new file mode 100644 index 0000000..2086ff2 --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +# BYU C S 240 Code Style/Quality Checker +This repository holds the source code for the "Code Quality Checker" for the BYU C S 240 class that is not part of the base checkstyle repository + +The code is based on [checkstyle](https://github.com/checkstyle/checkstyle) and includes the [checkstyle built-in checks](https://checkstyle.org/checks.html). Checkstyle also allows for [writing your own custom checks](https://checkstyle.org/writingchecks.html); most of the code in this repository is either a custom check or part of a test for a custom check. + +Checkstyle is based on the [Visitor Pattern](https://en.wikipedia.org/wiki/Visitor_pattern), often representing Java code in an [Abstract Syntax Tree (AST)](https://en.wikipedia.org/wiki/Abstract_syntax_tree). Because of this, any code must compile in order to be used as input for the checker. (Note to future TA's: if C S 236 still had you building a Datalog parser, an AST is somewhat similar to the list of tokens except it's a tree instead of a list of tokens) + + +## Building +This repository uses Maven for dependencies and building so you can build it with `mvn package` after which a jar will be in the `target` directory as `checkstyle-.jar`, like `checkstyle-1.0.8.jar`. + + +## Running +`java -jar path/to/jar/checkstyle-.jar -c .xml Contains custom checks for a particular quality rubric category + │ │ └─ .java Contains code necessary for checking + │ └─ resources + │ ├─ checkstyle_packages.xml Informs Checkstyle where to find custom check classes + │ └─ cs240_checks.xml Informs Checkstyle which checks to run. Used as an argument when running checkstyle. Named similarly + │ to google_checks.xml and sun_checks.xml, which also appear in the jar. + └─ test + │ ├─ java/edu.byu.cs240.checkstyle Root folder for tests for the custom checkstyle checks + │ ├─ CheckTest.java Base abstract class for writing tests for custom checks + │ └─ Contains tests for custom checks for a particular quality rubric category. Most extend CheckTest + │ └─ Test.java Tests for .java inside main. Uses input files from test/resources + └─ resources + └─ edu.byu.cs240.checkstyle Root folder for code input files used to test the checks. Generally nonsense code + │ with specific problems we expect the check to find. Many contain no errors (for + │ the specified check) to make sure it doesn't flag in unexpected cases it shouldn't + └─ .testInputs. Test input files for a specific check +``` \ No newline at end of file diff --git a/src/main/java/edu/byu/cs240/checkstyle/readability/NestingDepth.java b/src/main/java/edu/byu/cs240/checkstyle/readability/NestingDepth.java index 57a7a7d..5d26a53 100644 --- a/src/main/java/edu/byu/cs240/checkstyle/readability/NestingDepth.java +++ b/src/main/java/edu/byu/cs240/checkstyle/readability/NestingDepth.java @@ -9,6 +9,12 @@ import java.util.List; import java.util.Set; +/** + * Reports uses of nested code structures that in most styles would indent the code (if/else, loops, try/catch, switch) + * farther than a configurable number of levels + * + * @author Michael Davenport + */ @FileStatefulCheck public class NestingDepth extends AbstractCheck { private static final Set SLIST_EXCLUDED_PARENTS = Set.of(TokenTypes.METHOD_DEF, TokenTypes.LITERAL_CATCH, TokenTypes.CASE_GROUP); @@ -64,6 +70,13 @@ public void leaveToken(DetailAST ast) { } } + /** + * Determines if the provided token should be excluded as a nesting level. Generally this is because it is + * directly tied to another token that was included. For example, "else if" would double count (the else block + * and the if statement are separate) if it wasn't excluded here. + * @param ast AST token to check + * @return true if ast should be excluded, false otherwise + */ private boolean isExcluded(DetailAST ast) { return isElseIf(ast) || isSListExcluded(ast) || isElseWithBracesOfIfWithoutBraces(ast) || isBlockWithBraces(ast); } diff --git a/src/main/java/edu/byu/cs240/checkstyle/readability/UnusedMethodReporter.java b/src/main/java/edu/byu/cs240/checkstyle/readability/UnusedMethodReporter.java index 7167c80..4e62968 100644 --- a/src/main/java/edu/byu/cs240/checkstyle/readability/UnusedMethodReporter.java +++ b/src/main/java/edu/byu/cs240/checkstyle/readability/UnusedMethodReporter.java @@ -1,7 +1,6 @@ package edu.byu.cs240.checkstyle.readability; import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck; -import com.puppycrawl.tools.checkstyle.api.DetailAST; import com.puppycrawl.tools.checkstyle.api.FileText; import java.io.File; diff --git a/src/main/java/edu/byu/cs240/checkstyle/readability/UnusedMethodWalker.java b/src/main/java/edu/byu/cs240/checkstyle/readability/UnusedMethodWalker.java index 0a088bd..6927463 100644 --- a/src/main/java/edu/byu/cs240/checkstyle/readability/UnusedMethodWalker.java +++ b/src/main/java/edu/byu/cs240/checkstyle/readability/UnusedMethodWalker.java @@ -5,7 +5,6 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes; import edu.byu.cs240.checkstyle.util.TreeUtils; -import java.lang.invoke.MethodHandle; import java.util.*; /**