Skip to content

Commit

Permalink
fix lints
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonwatson committed Aug 31, 2023
1 parent 59565a3 commit e4c44ba
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 91 deletions.
13 changes: 6 additions & 7 deletions checker/src/main/java/org/frc5572/robotools/Checks.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package org.frc5572.robotools;

import org.frc5572.robotools.checks.*;
import org.frc5572.robotools.checks.Check;
import org.frc5572.robotools.checks.IOCheck;

public class Checks {

Check warning on line 6 in checker/src/main/java/org/frc5572/robotools/Checks.java

View workflow job for this annotation

GitHub Actions / testtool

[testtool] checker/src/main/java/org/frc5572/robotools/Checks.java#L6 <com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck>

Missing a Javadoc comment.
Raw output
/github/workspace/./checker/src/main/java/org/frc5572/robotools/Checks.java:6:1: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck)

private static final Check[] CHECKS = new Check[] {
new ExampleCheck(),
new IOCheck(),
};

private static final Check[] CHECKS = new Check[] {new IOCheck(),};

Check warning on line 8 in checker/src/main/java/org/frc5572/robotools/Checks.java

View workflow job for this annotation

GitHub Actions / testtool

[testtool] checker/src/main/java/org/frc5572/robotools/Checks.java#L8 <com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck>

',' is not followed by whitespace.
Raw output
/github/workspace/./checker/src/main/java/org/frc5572/robotools/Checks.java:8:69: warning: ',' is not followed by whitespace. (com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck)

/** Run through all checks */
public static void process(CompilationData data) {
for(Check c : CHECKS) {
for (Check c : CHECKS) {
c.check(data);
}
}
Expand Down
47 changes: 32 additions & 15 deletions checker/src/main/java/org/frc5572/robotools/CompilationData.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic;

import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.LineMap;
import com.sun.source.tree.Tree;
Expand All @@ -15,17 +14,23 @@
import com.sun.source.util.TaskEvent;
import com.sun.source.util.Trees;

/**
* Wrapper around a TypeElement. Includes helpers for printing errors, warnings etc.
*/
public class CompilationData {

/** Type Information for entire classpath. */
public Types types;
private Trees trees;
private SourcePositions positions;
private CompilationUnitTree compilationUnitTree;
/** Element being processed */
public TypeElement element;
private Messager messager;

public CompilationData(Types types, Trees trees, SourcePositions positions, CompilationUnitTree compilationUnitTree,
TypeElement element, Messager messager) {
/** Basic constructor. */
public CompilationData(Types types, Trees trees, SourcePositions positions,
CompilationUnitTree compilationUnitTree, TypeElement element, Messager messager) {
this.types = types;
this.trees = trees;
this.positions = positions;
Expand All @@ -34,27 +39,34 @@ public CompilationData(Types types, Trees trees, SourcePositions positions, Comp
this.messager = messager;
}

/** Constructor from Javac Plugin context. */
public CompilationData(JavacTask task, TaskEvent event) {
this(task.getTypes(), Trees.instance(task), Trees.instance(task).getSourcePositions(), event.getCompilationUnit(), event.getTypeElement(), null);
this(task.getTypes(), Trees.instance(task), Trees.instance(task).getSourcePositions(),
event.getCompilationUnit(), event.getTypeElement(), null);
}

/** Constructor from Annotation Processor context. */
public CompilationData(ProcessingEnvironment processingEnv, TypeElement element) {
this(processingEnv.getTypeUtils(), null, null, null, element, processingEnv.getMessager());
}

/** Show error */
public void error(Element element, Object object) {
echo(element, object.toString(), Diagnostic.Kind.ERROR, "error", "Error");
}

/** Show warning */
public void warn(Element element, Object object) {
echo(element, object.toString(), Diagnostic.Kind.MANDATORY_WARNING, "warning", "Warning");
}

/** Show note (this doesn't work in VS Code yet) */
public void note(Element element, Object object) {
echo(element, object.toString(), Diagnostic.Kind.NOTE, "notice", "Note");
}

private void echo(Element element, String errString, Diagnostic.Kind kind, String ghString, String humanString) {
private void echo(Element element, String errString, Diagnostic.Kind kind, String ghString,
String humanString) {
if (compilationUnitTree == null) {
messager.printMessage(kind, errString, element);
} else {
Expand All @@ -63,24 +75,25 @@ private void echo(Element element, String errString, Diagnostic.Kind kind, Strin
long pos = positions.getStartPosition(compilationUnitTree, tree);
long row = linemap.getLineNumber(pos);
String name = compilationUnitTree.getSourceFile().toUri().toString().split("/src/")[1];
System.out.println("::" + ghString + " file=src/" + name + ",line=" + row + "::" + errString);
System.out
.println("::" + ghString + " file=src/" + name + ",line=" + row + "::" + errString);
}
}

private boolean _implements(String qualifiedName, TypeElement elem) {
if(elem.getQualifiedName().toString().equals(qualifiedName)) {
if (elem.getQualifiedName().toString().equals(qualifiedName)) {
return true;
}
Element superClass = types.asElement(elem.getSuperclass());
if(superClass instanceof TypeElement) {
if(_implements(qualifiedName, (TypeElement) superClass)) {
if (superClass instanceof TypeElement) {
if (_implements(qualifiedName, (TypeElement) superClass)) {
return true;
}
}
for(var iface : elem.getInterfaces()) {
for (var iface : elem.getInterfaces()) {
Element interface_ = types.asElement(iface);
if(interface_ instanceof TypeElement) {
if(_implements(qualifiedName, (TypeElement) interface_)) {
if (interface_ instanceof TypeElement) {
if (_implements(qualifiedName, (TypeElement) interface_)) {
return true;
}
}
Expand All @@ -89,30 +102,34 @@ private boolean _implements(String qualifiedName, TypeElement elem) {
}

private boolean _extends(String qualifiedName, TypeElement elem) {
if(elem.getQualifiedName().toString().equals(qualifiedName)) {
if (elem.getQualifiedName().toString().equals(qualifiedName)) {
return true;
}
Element superClass = types.asElement(elem.getSuperclass());
if(superClass instanceof TypeElement) {
if(_extends(qualifiedName, (TypeElement) superClass)) {
if (superClass instanceof TypeElement) {
if (_extends(qualifiedName, (TypeElement) superClass)) {
return true;
}
}
return false;
}

/** Get if this type implements an interface by name. */
public boolean implementsInterface(String qualifiedName) {
return _implements(qualifiedName, this.element);
}

/** Get if the specified type implements an interface by name. */
public boolean implementsInterface(TypeElement e, String qualifiedName) {
return _implements(qualifiedName, e);
}

/** Get if this type extends a class by name. */
public boolean extendsClass(String qualifiedName) {
return _extends(qualifiedName, this.element);
}

/** Get if the specified type extends a class by name. */
public boolean extendsClass(TypeElement e, String qualifiedName) {
return _extends(qualifiedName, e);
}
Expand Down
13 changes: 0 additions & 13 deletions checker/src/main/java/org/frc5572/robotools/GetIdentifier.java

This file was deleted.

13 changes: 11 additions & 2 deletions checker/src/main/java/org/frc5572/robotools/RobotPlugin.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package org.frc5572.robotools;

import javax.lang.model.util.Types;

import com.sun.source.util.JavacTask;
import com.sun.source.util.Plugin;
import com.sun.source.util.SourcePositions;
import com.sun.source.util.TaskEvent;
import com.sun.source.util.TaskListener;
import com.sun.source.util.Trees;

/**
* Javac plugin has source info, so it's used for Github Action annotations.
*/
public class RobotPlugin implements Plugin {

/**
* Name used in build.gradle
*/
@Override
public String getName() {
return "rchk";
}

/**
* Function run when loaded
*/
@Override
public void init(JavacTask task, String... arg1) {
Types types = task.getTypes();
Expand All @@ -25,7 +33,8 @@ public void init(JavacTask task, String... arg1) {
@Override
public void finished(TaskEvent e) {
if (e.getKind() == TaskEvent.Kind.ANALYZE) {
CompilationData data = new CompilationData(types, trees, positions, e.getCompilationUnit(), e.getTypeElement(), null);
CompilationData data = new CompilationData(types, trees, positions,
e.getCompilationUnit(), e.getTypeElement(), null);
Checks.process(data);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.frc5572.robotools;

import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
Expand All @@ -14,6 +13,9 @@
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;

/**
* Annotation processor for checks. Used by VS Code.
*/
@SupportedAnnotationTypes("*")
@SupportedSourceVersion(SourceVersion.RELEASE_11)
@SupportedOptions("frc_check.skip")
Expand All @@ -31,16 +33,18 @@ private void processTypeElement(TypeElement typeElement) {

private boolean hasProcessed;

/** Initialization function */
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
System.out.println(processingEnv.getOptions().get("frc_check.skip"));
hasProcessed = false;
}

/** Process all elements. */
@Override
public boolean process(Set<? extends TypeElement> arg0, RoundEnvironment roundEnv) {
if(hasProcessed) {
if (hasProcessed) {
return false;
}
hasProcessed = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import org.frc5572.robotools.CompilationData;

/** A code checker */
@FunctionalInterface
public interface Check {


/** Check if code is fine. Returns true if error is fatal. */
public boolean check(CompilationData data);

}

This file was deleted.

Loading

0 comments on commit e4c44ba

Please sign in to comment.