This commit is contained in:
2026-03-18 00:39:22 -07:00
commit c699640518
27 changed files with 3576 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
// Nik Johnson
// 2-24-2024
// TA: Andy Ruan
import java.util.*;
// checks lines for the characters "System.out.println("")"
public class BlankPrintlnCheck implements Check {
// if the line being checked has a blank println statement, return a new error (code 3)
// takes line and line number
// returns error inside optional
public Optional<Error> lint(String line, int lineNumber) {
if (line.contains("System.out.println(\"\")")) {
return Optional.of(new Error(3, lineNumber, "Line contains blank print statement"));
}
return Optional.empty();
}
}

23
linter/BreakCheck.java Normal file
View File

@@ -0,0 +1,23 @@
// Nik Johnson
// 2-24-2024
// TA: Andy Ruan
import java.util.*;
// checks Strings for the characters "break" that come BEFORE the characters "//"
public class BreakCheck implements Check {
// if the line being checked has "break" in it
// after removing all single line comments, return a new error (code 2)
// takes line and line number
// returns error inside optional
public Optional<Error> lint(String line, int lineNumber) {
String input = line;
String output = input.replaceAll("//.*", "");
if (output.contains("break")) {
return Optional.of(new Error(2, lineNumber, "we cant use break in this class bro"));
}
return Optional.empty();
}
}

11
linter/Check.java Normal file
View File

@@ -0,0 +1,11 @@
import java.util.*;
// A common interface for linters that can check a single line of code
// Every Check will check for its own type of error on a single line of code.
public interface Check {
// Checks for this Check's error condition on this line with this line number.
// If an error exists on this line, returns an Optional with an Error present
// indicating an error occurred. If no errors are present, returns an empty Optional.
public Optional<Error> lint(String line, int lineNumber);
}

43
linter/Error.java Normal file
View File

@@ -0,0 +1,43 @@
// Nik Johnson
// 2-24-2024
// TA: Andy Ruan
/*
Defining an object called Error, which has the fields:
code: the errors code, represented by an int
lineNumber: the line where the error occurred
message: a description of the error
*/
public class Error {
private int code;
private int lineNumber;
private String message;
// construct an error with its fields
public Error(int code, int lineNumber, String message) {
this.code = code;
this.lineNumber = lineNumber;
this.message = message;
}
// format an error and its information nicely
public String toString() {
return ("(Line: " + lineNumber + ") " + "has error code " + code + "\n" + message);
}
// return line where the error occurs
public int getLineNumber() {
return this.lineNumber;
}
// return the error code (a number)
public int getCode() {
return this.code;
}
// return the errors description
public String getMessage() {
return this.message;
}
}

39
linter/Linter.java Normal file
View File

@@ -0,0 +1,39 @@
// Nik Johnson
// 2-24-2024
// TA: Andy Ruan
import java.util.*;
import java.io.*;
// file-based linting object, linting based on checks defined in a list
public class Linter {
private List<Check> checks;
// constructor, takes list of checks to perform
public Linter(List<Check> checks) {
this.checks = checks;
}
// compile list of errors based on output of check classes
// takes filename to perform checks on
// return list of errors present in that file
public List<Error> lint(String fileName) throws FileNotFoundException {
File toBeChecked = new File(fileName);
Scanner fileScanner = new Scanner(toBeChecked);
List<Error> errors = new ArrayList<>();
int lineNumber = 1;
while (fileScanner.hasNext()) {
String line = fileScanner.nextLine();
for (Check check : checks) {
Optional<Error> error = check.lint(line, lineNumber);
if (error.isPresent()) {
errors.add(error.get());
}
}
lineNumber++;
}
fileScanner.close();
return errors;
}
}

22
linter/LinterMain.java Normal file
View File

@@ -0,0 +1,22 @@
// Nik Johnson
// 2-24-2024
// TA: Andy Ruan
import java.util.*;
import java.io.*;
public class LinterMain {
public static final String FILE_NAME = "TestFile.txt";
public static void main(String[] args) throws FileNotFoundException {
List<Check> checks = new ArrayList<>();
checks.add(new LongLineCheck());
checks.add(new BreakCheck());
checks.add(new BlankPrintlnCheck());
Linter linter = new Linter(checks);
List<Error> errors = linter.lint(FILE_NAME);
for (Error e : errors) {
System.out.println(e);
}
}
}

21
linter/LongLineCheck.java Normal file
View File

@@ -0,0 +1,21 @@
// Nik Johnson
// 2-24-2024
// TA: Andy Ruan
import java.util.*;
// long line checker
public class LongLineCheck implements Check {
// if the line being checked is 100 characters or longer, return a new error (code 1)
// takes line and line number
// returns error inside optional
public Optional<Error> lint(String line, int lineNumber) {
if (line.length() >= 100) {
return Optional.of(new Error(1, lineNumber,
"Line length exceeded maximum length of 100 characters"));
}
return Optional.empty();
}
}

10
linter/TestFile.java Normal file
View File

@@ -0,0 +1,10 @@
public class TestFile {
public static void main(String[] args) {
System.out.println("This is a really really really long line that should fail the long line check");
while (true) {
System.out.println("");
break;
}
// break
}
}

89
linter/TestFile.txt Normal file
View File

@@ -0,0 +1,89 @@
package com.example;
public class Example {
public static void main(String[] args) {
int x = 5;
while (x < 10) {
System.out.println("Hello, world!");
break;
}
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue;
} else {
System.out.println(i);
}
}
try {
throw new Exception("Test");
} catch (Exception e) {
System.out.println("Caught an exception: " + e.getMessage());
} finally {
System.out.println("Finally block executed.");
}
for (int j = 0; j < 100; j++) {
if (j % 2 == 0) {
break;
} else {
System.out.println(j);
}
}
while (x > 5) {
System.out.println("Hello, world!");
break;
}
if (x % 2 == 0) {
continue;
} else {
System.out.println(x);
}
for (int k = 0; k < 1000; k++) {
if (k % 2 == 0) {
break;
} else {
System.out.println(k);
}
}
while (x > 5) {
System.out.println("Hello, world!");
break;
}
if (x % 2 == 0) {
continue;
} else {
System.out.println(x);
}
for (int l = 0; l < 10000; l++) {
if (l % 2 == 0) {
break;
} else {
System.out.println(l);
}
}
while (x > 5) {
System.out.println("Hello, world!");
break;
}
if (x % 2 == 0) {
continue;
} else {
System.out.println(x);
}
for (int m = 0; m < 100000; m++) {
if (m % 2 == 0) {
break;
} else {
System.out.println(m);
}
}
while (x > 5) {
System.out.println("Hello, world!");
break;
}
if (x % 2 == 0) {
continue;
} else {
System.out.println(x);
}
}
}