Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.selesse.jxlint.model.rules;
  
  
  import java.io.File;
 import java.net.URL;
 import java.util.List;

A lint rule model object. Should be extended to create rules. A full implementation is completed by extending this class, and implementing the methods the compiler complains about. A sample implementation is demonstrated below.

 public class MyRule extends LintRule {
     @Override
     public MyRule() {
         super("My name", "My summary", "My detailed description", Severity.ERROR, Category.LINT);
     }

     @Override
     public List<File> getFilesToValidate() {
         return FileUtils.allFiles(getSourceDirectory());
     }

     @Override
     public List<LintError> getLintErrors(File file) {
         List<LintError> lintErrorList = Lists.newArrayList();

         List<String> fileContents = Files.readLines(file, Charset.defaultCharset());

         for (String line : fileContents) {
             if (line.contains("Hello, world!")) {
                 // There is no error, we passed the rule!
                 return lintErrorList;
             }
         }

         lintErrorList.add(LintError.with(this, file).andErrorMessage("Must say hello world!").create());
         return lintErrorList;
     }
 }
 
 
 
 public abstract class LintRule {
     private String name;
     private String summary;
     private String detailedDescription;
     private Severity severity;
     private Enum<?> category;
     private boolean enabled = true;
     protected List<LintErrorlintErrors;
 
     public LintRule(String nameString summaryString detailedDescriptionSeverity severityEnum<?> category) {
         this. = name;
         this. = summary;
         this. = detailedDescription;
         this. = severity;
         this. = category;
         this. = Lists.newArrayList();
     }
 
     public LintRule(String nameString summaryString detailedDescriptionSeverity severity,
                     Enum<?> categoryboolean isEnabledByDefault) {
         this(namesummarydetailedDescriptionseveritycategory);
         this. = isEnabledByDefault;
     }

    
Return a list of java.io.Files to perform this rule's validation on. Several utility methods have been created in com.selesse.jxlint.utils.FileUtils to make this easy and are sampled below.
FileUtils.allFiles(getSourceDirectory()); // All the files in directory we're validating (recursive)
      FileUtils.allFilesWithExtension(getSourceDirectory, "txt"); // All .txt files in directory we're validating
 
 
     public abstract List<FilegetFilesToValidate();

    
Goes through every file and calls getLintErrors(java.io.File) on it. If there is an error, it is added to lintErrors.
 
     public void validate() {
         try {
             for (File file : getFilesToValidate()) {
                 List<LintErrorfileLintErrors = getLintErrors(file);
                 .addAll(fileLintErrors);
             }
         }
         catch (Exception e) {
             throw new RuntimeException("The \"" + this.getClass().getSimpleName() + "\" rule threw an exception " +
                     "when trying to validate:\n" + Joiner.on("\n").join(e.getStackTrace()));
         }
     }

    
Get a java.util.List of LintErrors from a file. The list should be empty if it passed the validation. This is the function that actually performs the validation for a given file.
    public abstract List<LintErrorgetLintErrors(File file);

    
Checks to see if a particular file passes this rule.
    public boolean passesValidation(File file) {
        List<LintErrorfileLintErrors = getLintErrors(file);
        return fileLintErrors.size() == 0;
    }

    
Gets the name of the rule. This name is used with command line switches and in displaying summary information. Can contain spaces, but should be relatively short.
    public String getName() {
        return ;
    }
    public void setName(String name) {
        this. = name;
    }

    
Gets a one or two line summary of the rule. This is used when displaying summary information via getSummaryOutput().
    public String getSummary() {
        return ;
    }
    public void setSummary(String summary) {
        this. = summary;
    }

    
Gets a detailed description of the rule in question. Should be as specific as possible and contain examples. Used in getDetailedOutput().
    public String getDetailedDescription() {
        return ;
    }

    
Returns a String containing the file contents of "/src/main/resources/doc/rules/{ThisClass}.md". If no such file exists, the empty string is returned.
    protected String getMarkdownDescription() {
        String markdownDescription = "";
        try {
            URL documentationUrl = Resources.getResource("doc/rules/" + this.getClass().getSimpleName() + ".md");
            List<StringfileContents = Resources.readLines(documentationUrl.);
            markdownDescription = Joiner.on("\n").join(fileContents);
        }
        catch (Exception e) {
            ..println("Error: could not read Markdown description for " + this.getClass().getSimpleName());
            e.printStackTrace();
        }
        return markdownDescription;
    }
    public void setDetailedDescription(String detailedDescription) {
        this. = detailedDescription;
    }
    public Severity getSeverity() {
        return ;
    }
    public void setSeverity(Severity severity) {
        this. = severity;
    }
    public Enum<?> getCategory() {
        return ;
    }
    public void setCategory(Enum<?> category) {
        this. = category;
    }
    public boolean isEnabled() {
        return ;
    }
    public void setEnabled(boolean enabled) {
        this. = enabled;
    }

    
Return a short summary output:
     "Rule Name" : Summary information
     "Rule that is not enabled by default"* : This rule is not enabled by default.
 
    public String getSummaryOutput() {
        return String.format("\"%s\"%s : %s"getName(), isEnabled() ? "" : "*"getSummary());
    }

    
Return a detailed output of this rule. Prints all of its relevant information.
    public String getDetailedOutput() {
        List<StringdetailedOutput = Lists.newArrayList(
                getName(),
                // Underline the name with "-"s. Hacky-ish, but works well.
                new String(new char[getName().length()]).replace("\0""-"),
                "Summary: " + getSummary(),
                isEnabled() ? "" : "\n** Disabled by default **\n",
                "Severity: " + EnumUtils.toHappyString(getSeverity()),
                "Category: " + getCategory(),
                "",
                getDetailedDescription()
        );
        return Joiner.on("\n").join(detailedOutput);
    }
    @Override
    public int hashCode() {
        int result =  != null ? .hashCode() : 0;
        result = 31 * result + ( != null ? .hashCode() : 0);
        result = 31 * result + ( != null ? .hashCode() : 0);
        result = 31 * result + ( != null ? .hashCode() : 0);
        result = 31 * result + ( != null ? .hashCode() : 0);
        result = 31 * result + ( ? 1 : 0);
        result = 31 * result + ( != null ? .hashCode() : 0);
        return result;
    }

    
Ad-hoc equals. If the object is a LintRule, we compare getName() with the object's getName, in a case-insensitive way.
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof LintRule) {
            return ((LintRuleobj).getName().equalsIgnoreCase(getName());
        }
        return super.equals(obj);
    }

    
Returns true if this rule's name is a member of this list of strings. It's like java.util.List.contains(java.lang.Object), but on the LintRule rather than the list. Does a case-insensitive string comparison.
    public boolean hasNameInList(List<StringruleStrings) {
        for (String string : ruleStrings) {
            if (string.equalsIgnoreCase(getName())) {
                return true;
            }
        }
        return false;
    }
    public List<LintErrorgetLintErrors() {
        return ;
    }

    
Get the source/root directory. This is the directory that was passed to the program, i.e. "java -jar myjar.jar sourceDirectory".
    public File getSourceDirectory() {
        return LintRulesImpl.getInstance().getSourceDirectory();
    }
New to GrepCode? Check out our FAQ X