Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lombok Integration #42

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ class Example {
};

// Use builder to create rule
LintRule rule = new LintRule.Builder()
.setLevel(LintLevel.ERROR)
.setImplementation(lintImplementation)
.setIssueId("STRING_VALUE_NAMED_TEST")
LintRule rule = LintRule.builder()
.level(LintLevel.ERROR)
.implementation(lintImplementation)
.issueId("STRING_VALUE_NAMED_TEST")
.build();

// Create register and register rule
Expand All @@ -113,7 +113,7 @@ The classes are:

`LintImplementation<T>` - Target `WrappedObject` implementing class type, determine rules for failure, configure output
&darr;
`LintRule.Builder` &rarr; `LintRule` - Configure severity, set issue ID, explanation, description, and implementation.
`LintRule.builder()` &rarr; `LintRule` - Configure severity, set issue ID, explanation, description, and implementation.
&darr;
`LintRegister` - Register all `LintRule`s
&darr;
Expand Down Expand Up @@ -228,7 +228,7 @@ A `LintRule` can have the following properties set through the builder:
* `String issueDescription` - Short description of this lint rule.
* `String issueExplanation` - More in-depth description of lint rule.

*Note:* If the required fields are not set when `LintRule.Builder.build()` is called a `LintRuleBuilderException` will be thrown.
*Note:* If the required fields are not set when `LintRule.builder().build()` is called a `NullPointerException` will be thrown.

## LintRegister
`LintRegister` is a simple class to register as many or as few `LintRule`s as wanted.
Expand Down Expand Up @@ -331,10 +331,10 @@ class Example {
}
};

LintRule rule = new LintRule.Builder()
.setLevel(LintLevel.ERROR)
.setImplementation(lintImplementation)
.setIssueId("BOOLEAN_NAME_STARTS_WITH_HAS")
LintRule rule = LintRule.builder()
.level(LintLevel.ERROR)
.implementation(lintImplementation)
.issueId("BOOLEAN_NAME_STARTS_WITH_HAS")
.build();

LintRegister register = new LintRegister();
Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ dependencies {
implementation group: 'commons-io', name: 'commons-io', version: '2.6'
implementation group: 'org.json', name: 'json', version: '20180813'
implementation group: 'com.j2html', name: 'j2html', version: '1.4.0'
compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.8'
annotationProcessor 'org.projectlombok:lombok:1.18.8'

testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.4.2'
testImplementation group: 'org.mockito', name: 'mockito-core', version: '2.27.0'
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/zachary_moore/filters/FilterMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import lombok.experimental.UtilityClass;

public class FilterMapper {

private FilterMapper() {}
@UtilityClass
public class FilterMapper {

private static Filters filters = new Filters();
private Filters filters = new Filters();

/**
* Filter JSONFile down to a list of types determined by given {@link LintImplementation}
* @param jsonFile {@link JSONFile} to filter
* @param lintImplementation {@link LintImplementation} that will be used to determine what types we should filter JSONFile down to
* @return {@link java.util.ArrayList} of type {@link LintImplementation#getClazz()}
*/
public static List<?> filter(JSONFile jsonFile, LintImplementation lintImplementation) {
public List<?> filter(JSONFile jsonFile, LintImplementation lintImplementation) {
if (lintImplementation.getClazz() == String.class) {
return filters.filterToStrings(jsonFile);
} else if (lintImplementation.getClazz() == Byte.class) {
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/zachary_moore/lint/LintImplementation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.zachary_moore.lint;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;


/**
* Abstract class to represent a Lint implementation
* @param <T> Type of Class to build a LintRule for
Expand All @@ -9,6 +14,7 @@ public abstract class LintImplementation<T> extends BaseJSONAnalyzer {
/**
* Message reported when a lint issue is found, can be set at runtime or statically
*/
@Setter(AccessLevel.PROTECTED)
private String reportMessage;

/**
Expand Down Expand Up @@ -37,10 +43,6 @@ public String report(T t) throws NoReportSetException {
return reportMessage;
}

protected void setReportMessage(String reportMessage) {
this.reportMessage = reportMessage;
}

public static class NoReportSetException extends Exception {
private NoReportSetException(String errorMessage) {
super(errorMessage);
Expand Down
17 changes: 8 additions & 9 deletions src/main/java/com/zachary_moore/lint/LintRegister.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;


/**
* Class to be passed around to represent all {@link LintRule} to be checked
*/

@Getter
@NoArgsConstructor
public class LintRegister {

/**
* List to hold all of our {@link LintRule}
*/
private final List<LintRule> lintRules;

public LintRegister() {
lintRules = new ArrayList<>();
}
private final List<LintRule> lintRules = new ArrayList<>();

/**
* Register new LintRule into {@link LintRegister}
Expand All @@ -25,8 +28,4 @@ public LintRegister() {
public void register(LintRule ...toRegister) {
lintRules.addAll(Arrays.asList(toRegister));
}

public List<LintRule> getLintRules() {
return lintRules;
}
}
116 changes: 21 additions & 95 deletions src/main/java/com/zachary_moore/lint/LintRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,38 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;


/**
* Class to represent a lint rule configuration
*/
@Builder
@RequiredArgsConstructor
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class LintRule {

@Getter
@NonNull
private final LintLevel level;

@NonNull
private final LintImplementation implementation;

@Getter
@NonNull
@EqualsAndHashCode.Include
private final String issueId;

@Getter
private final String issueDescription;
private final String issueExplanation;

private LintRule(LintLevel level,
LintImplementation implementation,
String issueId,
String issueDescription,
String issueExplanation) {
this.level = level;
this.implementation = implementation;
this.issueId = issueId;
this.issueDescription = issueDescription;
this.issueExplanation = issueExplanation;
}
@Getter
private final String issueExplanation;

/**
* Given a list of files lint each file based on setup config
Expand Down Expand Up @@ -59,87 +68,4 @@ public Map<JSONFile, List<String>> lint(JSONFile ...jsonFiles) throws LintImplem

return reportMessages;
}

public LintLevel getLevel() {
return level;
}

public String getIssueId() {
return issueId;
}

public String getIssueDescription() {
return issueDescription;
}

public String getIssueExplanation() {
return issueExplanation;
}

@Override
public int hashCode() {
return this.issueId.hashCode();
}

/**
* Builder pattern class for building our {@link LintRule}
*/
public static class Builder {

private LintLevel level;
private LintImplementation implementation;
private String issueId;
private String issueDescription;
private String issueExplanation;

public Builder setLevel(LintLevel level) {
this.level = level;
return this;
}

public Builder setImplementation(LintImplementation implementation) {
this.implementation = implementation;
return this;
}

public Builder setIssueId(String issueId) {
this.issueId = issueId;
return this;
}

public Builder setIssueDescription(String issueDescription) {
this.issueDescription = issueDescription;
return this;
}

public Builder setIssueExplanation(String issueExplanation) {
this.issueExplanation = issueExplanation;
return this;
}

/**
* Builds a {@link LintRule}
* @return new {@link LintRule}
* @throws LintRuleBuilderException if either {@link LintLevel}, IssueID, or {@link LintImplementation} is not set
*/
public LintRule build() throws LintRuleBuilderException {
if (level == null
|| implementation == null
|| issueId == null) {
throw new LintRuleBuilderException("Must set LintLevel, LintImplementation and IssueId in a lint rule");
}

return new LintRule(level,
implementation,
issueId,
issueDescription,
issueExplanation);
}

public static class LintRuleBuilderException extends Exception {
private LintRuleBuilderException(String errorMessage) {
super(errorMessage);
}
}
}
}
14 changes: 4 additions & 10 deletions src/main/java/com/zachary_moore/objects/JSONArray.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.zachary_moore.objects;

import lombok.Getter;
import org.json.JSONException;
import org.json.JSONObject;

Expand All @@ -9,7 +10,10 @@

public class JSONArray extends org.json.JSONArray implements WrappedObject {

@Getter
private final String originatingKey;

@Getter
private final WrappedObject parentObject;

private List<Object> contents;
Expand All @@ -26,16 +30,6 @@ public class JSONArray extends org.json.JSONArray implements WrappedObject {
this.parentObject = parentObject;
}

@Override
public String getOriginatingKey() {
return originatingKey;
}

@Override
public WrappedObject getParentObject() {
return parentObject;
}

@Override
public boolean isPrimitive() {
return false;
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/com/zachary_moore/objects/JSONFile.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.zachary_moore.objects;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.json.JSONException;
import org.json.JSONTokener;

Expand All @@ -8,10 +10,14 @@
import java.io.FileReader;
import java.io.IOException;

@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class JSONFile {

private org.json.JSONObject jsonObject;
private org.json.JSONArray jsonArray;

@Getter
@EqualsAndHashCode.Include
private String filePath;

public JSONFile(File file) throws IOException {
Expand Down Expand Up @@ -39,13 +45,4 @@ public Object getObject() {
throw new RuntimeException("Could not parse either a JSONArray or JSONObject from file");
}
}

public String getFilePath() {
return filePath;
}

@Override
public int hashCode() {
return this.filePath.hashCode();
}
}
17 changes: 5 additions & 12 deletions src/main/java/com/zachary_moore/objects/JSONObject.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.zachary_moore.objects;

import org.json.JSONException;

import java.util.HashMap;
import java.util.Map;
import lombok.Getter;
import org.json.JSONException;

public class JSONObject extends org.json.JSONObject implements WrappedObject {

@Getter
private final String originatingKey;

@Getter
private final WrappedObject parentObject;

private final org.json.JSONObject clonedObject;
Expand All @@ -25,16 +28,6 @@ public class JSONObject extends org.json.JSONObject implements WrappedObject {
this.clonedObject = clone;
}

@Override
public String getOriginatingKey() {
return originatingKey;
}

@Override
public WrappedObject getParentObject() {
return parentObject;
}

@Override
public boolean isPrimitive() {
return false;
Expand Down