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

Enable checkstyle #29

Merged
merged 4 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
id 'maven-publish'
id 'signing'
id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
id 'checkstyle'
}

group = 'ch.geowerkstatt.ilivalidator.extensions.functions'
Expand Down Expand Up @@ -53,7 +54,7 @@ publishing {
developers {
developer {
id = 'GeoWerkstatt'
name = 'GeoWerkstatt GmbHH'
name = 'GeoWerkstatt GmbH'
email = '[email protected]'
}
}
Expand Down
207 changes: 207 additions & 0 deletions config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">

<!--

Checkstyle configuration that checks the sun coding conventions from:

- the Java Language Specification at
https://docs.oracle.com/javase/specs/jls/se11/html/index.html

- the Sun Code Conventions at https://www.oracle.com/java/technologies/javase/codeconventions-contents.html

- the Javadoc guidelines at
https://www.oracle.com/technical-resources/articles/java/javadoc-tool.html

- the JDK Api documentation https://docs.oracle.com/en/java/javase/11/

- some best practices

Checkstyle is very configurable. Be sure to read the documentation at
https://checkstyle.org (or in your downloaded distribution).

Most Checks are configurable, be sure to consult the documentation.

To completely disable a check, just comment it out or delete it from the file.
To suppress certain violations please review suppression filters.

Finally, it is worth reading the documentation.

-->

<module name="Checker">
<!--
If you set the basedir property below, then all reported file
names will be relative to the specified directory. See
https://checkstyle.org/config.html#Checker

<property name="basedir" value="${basedir}"/>
-->
<property name="severity" value="error"/>

<property name="fileExtensions" value="java, properties, xml"/>

<!-- Excludes all 'module-info.java' files -->
<!-- See https://checkstyle.org/filefilters/index.html -->
<module name="BeforeExecutionExclusionFileFilter">
<property name="fileNamePattern" value="module\-info\.java$"/>
</module>

<!-- https://checkstyle.org/filters/suppressionfilter.html -->
<module name="SuppressionFilter">
<property name="file" value="${config_loc}/suppressions.xml"/>
<property name="optional" value="true"/>
</module>

<!-- Checks that a package-info.java file exists for each package. -->
<!-- See https://checkstyle.org/checks/javadoc/javadocpackage.html#JavadocPackage -->
<!-- <module name="JavadocPackage"/> -->

<!-- Checks whether files end with a new line. -->
<!-- See https://checkstyle.org/checks/misc/newlineatendoffile.html -->
<module name="NewlineAtEndOfFile"/>

<!-- Checks that property files contain the same keys. -->
<!-- See https://checkstyle.org/checks/misc/translation.html -->
<module name="Translation"/>

<!-- Checks for Size Violations. -->
<!-- See https://checkstyle.org/checks/sizes/index.html -->
<module name="FileLength"/>
<module name="LineLength">
<property name="fileExtensions" value="java"/>
<property name="max" value="200"/>
</module>

<!-- Checks for whitespace -->
<!-- See https://checkstyle.org/checks/whitespace/index.html -->
<module name="FileTabCharacter"/>

<!-- Miscellaneous other checks. -->
<!-- See https://checkstyle.org/checks/misc/index.html -->
<module name="RegexpSingleline">
<property name="format" value="\s+$"/>
<property name="minimum" value="0"/>
<property name="maximum" value="0"/>
<property name="message" value="Line has trailing spaces."/>
</module>

<!-- Checks for Headers -->
<!-- See https://checkstyle.org/checks/header/index.html -->
<!-- <module name="Header"> -->
<!-- <property name="headerFile" value="${checkstyle.header.file}"/> -->
<!-- <property name="fileExtensions" value="java"/> -->
<!-- </module> -->

<module name="TreeWalker">

<!-- Checks for Javadoc comments. -->
<!-- See https://checkstyle.org/checks/javadoc/index.html -->
<module name="InvalidJavadocPosition"/>
<module name="JavadocMethod">
<property name="allowMissingParamTags" value="true"/>
<property name="allowMissingReturnTag" value="true"/>
</module>
<module name="JavadocType"/>
<module name="JavadocVariable">
<property name="scope" value="public"/>
</module>
<module name="JavadocStyle"/>
<module name="MissingJavadocMethod"/>

<!-- Checks for Naming Conventions. -->
<!-- See https://checkstyle.org/checks/naming/index.html -->
<module name="ConstantName"/>
<module name="LocalFinalVariableName"/>
<module name="LocalVariableName"/>
<module name="MemberName"/>
<module name="MethodName"/>
<module name="PackageName"/>
<module name="ParameterName"/>
<module name="StaticVariableName"/>
<module name="TypeName"/>

<!-- Checks for imports -->
<!-- See https://checkstyle.org/checks/imports/index.html -->
<module name="AvoidStarImport">
<property name="excludes" value="java.util"/>
</module>
<module name="IllegalImport"/> <!-- defaults to sun.* packages -->
<module name="RedundantImport"/>
<module name="UnusedImports">
<property name="processJavadoc" value="false"/>
</module>

<!-- Checks for Size Violations. -->
<!-- See https://checkstyle.org/checks/sizes/index.html -->
<module name="MethodLength"/>
<module name="ParameterNumber"/>

<!-- Checks for whitespace -->
<!-- See https://checkstyle.org/checks/whitespace/index.html -->
<module name="EmptyForIteratorPad"/>
<module name="GenericWhitespace"/>
<module name="MethodParamPad"/>
<module name="NoWhitespaceAfter"/>
<module name="NoWhitespaceBefore"/>
<module name="OperatorWrap"/>
<module name="ParenPad"/>
<module name="TypecastParenPad"/>
<module name="WhitespaceAfter"/>
<module name="WhitespaceAround"/>

<!-- Modifier Checks -->
<!-- See https://checkstyle.org/checks/modifier/index.html -->
<module name="ModifierOrder"/>
<module name="RedundantModifier"/>

<!-- Checks for blocks. You know, those {}'s -->
<!-- See https://checkstyle.org/checks/blocks/index.html -->
<module name="AvoidNestedBlocks"/>
<module name="EmptyBlock"/>
<module name="LeftCurly"/>
<module name="NeedBraces"/>
<module name="RightCurly"/>

<!-- Checks for common coding problems -->
<!-- See https://checkstyle.org/checks/coding/index.html -->
<module name="EmptyStatement"/>
<module name="EqualsHashCode"/>
<!-- <module name="HiddenField" /> -->
<module name="IllegalInstantiation"/>
<module name="InnerAssignment"/>
<!-- <module name="MagicNumber"/> -->
<module name="MissingSwitchDefault"/>
<module name="MultipleVariableDeclarations"/>
<module name="SimplifyBooleanExpression"/>
<module name="SimplifyBooleanReturn"/>

<!-- Checks for class design -->
<!-- See https://checkstyle.org/checks/design/index.html -->
<module name="DesignForExtension"/>
<module name="FinalClass"/>
<module name="HideUtilityClassConstructor"/>
<module name="InterfaceIsType"/>
<module name="VisibilityModifier">
<property name="protectedAllowed" value="true"/>
</module>

<!-- Miscellaneous other checks. -->
<!-- See https://checkstyle.org/checks/misc/index.html -->
<module name="ArrayTypeStyle"/>
<!-- <module name="FinalParameters"/> -->
<module name="TodoComment"/>
<module name="UpperEll"/>

<!-- https://checkstyle.org/filters/suppressionxpathfilter.html -->
<module name="SuppressionXpathFilter">
<property name="file" value="${org.checkstyle.sun.suppressionxpathfilter.config}"
default="checkstyle-xpath-suppressions.xml" />
<property name="optional" value="true"/>
</module>

</module>

</module>
12 changes: 12 additions & 0 deletions config/checkstyle/suppressions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
"https://checkstyle.org/dtds/suppressions_1_2.dtd">

<!-- https://checkstyle.org/filters/suppressionfilter.html -->
<suppressions>

<!-- Disable some checks for test classes -->
<suppress checks="MissingJavadoc\w+Check|LineLengthCheck|VisibilityModifierCheck" files="[\\/]test[\\/]java[\\/]" />

</suppressions>
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public abstract class BaseInterlisFunction implements InterlisFunction {
protected Validator validator;

@Override
public void init(TransferDescription td, Settings settings, IoxValidationConfig validationConfig, ObjectPool objectPool, LogEventFactory logEventFactory) {
public final void init(TransferDescription td, Settings settings, IoxValidationConfig validationConfig, ObjectPool objectPool, LogEventFactory logEventFactory) {
this.logger = logEventFactory;
this.logger.setValidationConfig(validationConfig);
this.td = td;
Expand All @@ -27,10 +27,10 @@ public void init(TransferDescription td, Settings settings, IoxValidationConfig
}

@Override
public Value evaluate(String validationKind, String usageScope, IomObject mainObj, Value[] actualArguments) {
public final Value evaluate(String validationKind, String usageScope, IomObject mainObj, Value[] actualArguments) {

for (Value arg : actualArguments) {
if (arg.skipEvaluation()){
if (arg.skipEvaluation()) {
return arg;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,21 @@
import java.util.Iterator;
import java.util.function.Function;

public class EvaluationHelper {
public final class EvaluationHelper {

private EvaluationHelper() {
// Utility class
}

/**
* Parse the {@code argPath} into a {@link PathEl} array.
*
* @param validator the {@link Validator} instance.
* @param contextClass the {@link Viewable} definition where the {@code argPath} starts.
* @param argPath the path string to parse. see {@link Value#getValue()}.
*
* @return the parsed {@link PathEl} array or {@code null} if the {@code argPath} could not be parsed.
*/
public static PathEl[] getAttributePathEl(Validator validator, Viewable<Element> contextClass, Value argPath) {
try {
ObjectPath objectPath = validator.parseObjectOrAttributePath(contextClass, argPath.getValue());
Expand All @@ -29,6 +43,10 @@ public static PathEl[] getAttributePathEl(Validator validator, Viewable<Element>
return null;
}

/**
* Get the {@link Viewable} (e.g. the class definition) from the {@link TransferDescription}.
* If the {@code iomObject} is {@code null}, the {@code argObjects} is used to retrieve the {@link Viewable}.
*/
public static Viewable getContextClass(TransferDescription td, IomObject iomObject, Value argObjects) {
if (iomObject != null) {
return (Viewable) td.getElement(iomObject.getobjecttag());
Expand All @@ -44,6 +62,9 @@ public static Viewable getContextClass(TransferDescription td, IomObject iomObje
return null;
}

/**
* Get the collection of {@link IomObject} inside {@code argObjects} by following the provided {@code attributePath}.
*/
public static Collection<IomObject> evaluateAttributes(Validator validator, Value argObjects, PathEl[] attributePath) {
Collection<IomObject> attributes = new ArrayList<>();

Expand All @@ -57,6 +78,9 @@ public static Collection<IomObject> evaluateAttributes(Validator validator, Valu
return attributes;
}

/**
* Applies the given function to each item in the collection and sums up the results.
*/
public static Double sum(Collection<IomObject> items, Function<IomObject, Double> func) {
return items
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
import ch.interlis.iox_j.jts.Iox2jtsext;
import ch.interlis.iox_j.validator.Value;

import java.util.ArrayList;
import java.util.Collection;

public class GetAreaIoxPlugin extends BaseInterlisFunction {
public final class GetAreaIoxPlugin extends BaseInterlisFunction {

@Override
public String getQualifiedIliName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@
import ch.interlis.iom.IomObject;
import ch.interlis.iox_j.validator.Value;

import java.security.acl.Group;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;

public class GetInGroupsIoxPlugin extends BaseInterlisFunction {
public final class GetInGroupsIoxPlugin extends BaseInterlisFunction {

private static Map<GroupKey, List<IomObject>> groups = new HashMap<>();

Expand Down Expand Up @@ -43,37 +37,40 @@ public Value evaluateInternal(String validationKind, String usageScope, IomObjec
PathEl[] attributePath = EvaluationHelper.getAttributePathEl(validator, contextClass, argPath);

GroupKey key = new GroupKey(inputObjects, validator.getValueFromObjectPath(null, contextObject, attributePath, null));
if(!groups.containsKey(key)){
FillGroups(inputObjects, attributePath);
if (!groups.containsKey(key)) {
fillGroups(inputObjects, attributePath);
}

return new Value(groups.get(key));
}

private void FillGroups(Collection<IomObject> inputObjects, PathEl[] attributePath) {
private void fillGroups(Collection<IomObject> inputObjects, PathEl[] attributePath) {
for (IomObject object: inputObjects) {
GroupKey key = new GroupKey(inputObjects, validator.getValueFromObjectPath(null, object, attributePath, null));
if(!groups.containsKey(key)){
if (!groups.containsKey(key)) {
groups.put(key, new ArrayList<>());
}

groups.get(key).add(object);
}
}

private static class GroupKey
{
private static final class GroupKey {
private Collection<IomObject> inputObjects;
private Value groupId;
public GroupKey(Collection<IomObject> inputObjects, Value groupId){
GroupKey(Collection<IomObject> inputObjects, Value groupId) {
this.inputObjects = inputObjects;
this.groupId = groupId;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
GroupKey that = (GroupKey) o;
return inputObjects.equals(that.inputObjects) && groupId.compareTo(that.groupId) == 0;
}
Expand Down
Loading
Loading