From ff96476dd4108c31fa61e4d50c73430d2042b46c Mon Sep 17 00:00:00 2001 From: Santanu Sinha Date: Thu, 17 Aug 2023 20:54:02 +0530 Subject: [PATCH 1/3] Some cleanups for sonar --- .github/workflows/build.yml | 2 + .../io/appform/hope/core/BinaryOperator.java | 2 +- .../hope/core/exceptions/HopeException.java | 4 +- .../hope/core/functions/FunctionRegistry.java | 9 +-- .../hope/core/functions/impl/str/Length.java | 4 +- .../core/functions/impl/str/LowerCase.java | 4 +- .../hope/core/functions/impl/str/SubStr.java | 4 +- .../core/functions/impl/str/UpperCase.java | 4 +- .../appform/hope/core/utils/Converters.java | 80 ++++++++++--------- .../appform/hope/core/visitors/Evaluator.java | 35 ++------ .../hope/core/visitors/EvaluatorTest.java | 4 +- hope-lang/pom.xml | 2 +- .../io/appform/hope/lang/utils/TypeUtils.java | 3 +- .../appform/hope/lang/HopeLangEngineTest.java | 55 +++++-------- .../hope/lang/LibraryFunctionsTest.java | 13 ++- lombok.config | 3 + pom.xml | 12 ++- 17 files changed, 112 insertions(+), 128 deletions(-) create mode 100644 lombok.config diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0b297b9..af614fe 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -36,6 +36,8 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=santanusinha_hope + - name: Create dependency tree + run: mvn dependency:tree -DoutputFile=dependencies.txt - uses: stefanzweifel/git-auto-commit-action@v4 with: commit_message: Comitting files changed by Github Actions diff --git a/hope-core/src/main/java/io/appform/hope/core/BinaryOperator.java b/hope-core/src/main/java/io/appform/hope/core/BinaryOperator.java index ae7e285..d919515 100644 --- a/hope-core/src/main/java/io/appform/hope/core/BinaryOperator.java +++ b/hope-core/src/main/java/io/appform/hope/core/BinaryOperator.java @@ -28,7 +28,7 @@ public abstract class BinaryOperator extends Evaluatable { protected final T lhs; protected final T rhs; - public BinaryOperator(T lhs, T rhs) { + protected BinaryOperator(T lhs, T rhs) { this.lhs = lhs; this.rhs = rhs; } diff --git a/hope-core/src/main/java/io/appform/hope/core/exceptions/HopeException.java b/hope-core/src/main/java/io/appform/hope/core/exceptions/HopeException.java index 406526b..6f50ff0 100644 --- a/hope-core/src/main/java/io/appform/hope/core/exceptions/HopeException.java +++ b/hope-core/src/main/java/io/appform/hope/core/exceptions/HopeException.java @@ -25,11 +25,11 @@ @EqualsAndHashCode(callSuper = true) @ToString(callSuper = true) public abstract class HopeException extends RuntimeException { - public HopeException(String message) { + protected HopeException(String message) { super(message); } - public HopeException(String message, Throwable cause) { + protected HopeException(String message, Throwable cause) { super(message, cause); } } diff --git a/hope-core/src/main/java/io/appform/hope/core/functions/FunctionRegistry.java b/hope-core/src/main/java/io/appform/hope/core/functions/FunctionRegistry.java index ff90aaf..d2fa955 100644 --- a/hope-core/src/main/java/io/appform/hope/core/functions/FunctionRegistry.java +++ b/hope-core/src/main/java/io/appform/hope/core/functions/FunctionRegistry.java @@ -70,7 +70,7 @@ public synchronized void discover(List packages) { .addAll(packages.stream() .flatMap(packagePath -> ClasspathHelper.forPackage(packagePath) .stream()) - .collect(Collectors.toList())) + .toList()) .build(); Reflections reflections = new Reflections( new ConfigurationBuilder() @@ -133,9 +133,6 @@ private static List constructors(Class final Constructor[] declaredConstructors = (Constructor[]) type.getDeclaredConstructors(); FunctionImplementation annotation = type.getAnnotation(FunctionImplementation.class); -/* Preconditions.checkArgument( - declaredConstructors != null && declaredConstructors.length == 1, - "Function " + annotation.value() + " must have only one constructor");*/ return Arrays.stream(declaredConstructors) .map(declaredConstructor -> { final Class[] declaredParamTypes = declaredConstructor @@ -145,13 +142,13 @@ private static List constructors(Class ? parameterType.getComponentType() .isAssignableFrom(Value.class) : parameterType.isAssignableFrom(Value.class)) - .collect(Collectors.toList()); + .toList(); Preconditions.checkArgument( paramTypes.size() == declaredParamTypes.length, "Non value parameter types declared for constructor in function '" + annotation.value() + "'. Param types: " + Arrays.stream(declaredParamTypes) .map(Class::getSimpleName) - .collect(Collectors.toList())); + .toList()); final boolean variantArgs = paramTypes.stream() .anyMatch(Class::isArray); Preconditions.checkArgument(!variantArgs || paramTypes.size() == 1, diff --git a/hope-core/src/main/java/io/appform/hope/core/functions/impl/str/Length.java b/hope-core/src/main/java/io/appform/hope/core/functions/impl/str/Length.java index 9dbc529..2e540b9 100644 --- a/hope-core/src/main/java/io/appform/hope/core/functions/impl/str/Length.java +++ b/hope-core/src/main/java/io/appform/hope/core/functions/impl/str/Length.java @@ -26,7 +26,7 @@ * Returns {@link NumericValue} length of provided evaluated {@link StringValue} parameter. */ @FunctionImplementation("str.len") -public class Length extends HopeFunction { +public class Length extends HopeFunction { private final Value arg; public Length(Value arg) { @@ -34,7 +34,7 @@ public Length(Value arg) { } @Override - public Value apply(Evaluator.EvaluationContext evaluationContext) { + public NumericValue apply(Evaluator.EvaluationContext evaluationContext) { return new NumericValue((double)Converters.stringValue(evaluationContext, arg, "").length()); } } diff --git a/hope-core/src/main/java/io/appform/hope/core/functions/impl/str/LowerCase.java b/hope-core/src/main/java/io/appform/hope/core/functions/impl/str/LowerCase.java index 1f092ac..f0503da 100644 --- a/hope-core/src/main/java/io/appform/hope/core/functions/impl/str/LowerCase.java +++ b/hope-core/src/main/java/io/appform/hope/core/functions/impl/str/LowerCase.java @@ -25,7 +25,7 @@ * Returns lower case {@link StringValue} of provided {@link StringValue} parameter. */ @FunctionImplementation("str.lower") -public class LowerCase extends HopeFunction { +public class LowerCase extends HopeFunction { private final Value arg; public LowerCase(Value arg) { @@ -33,7 +33,7 @@ public LowerCase(Value arg) { } @Override - public Value apply(Evaluator.EvaluationContext evaluationContext) { + public StringValue apply(Evaluator.EvaluationContext evaluationContext) { return new StringValue(Converters.stringValue(evaluationContext, arg, "").toLowerCase()); } } diff --git a/hope-core/src/main/java/io/appform/hope/core/functions/impl/str/SubStr.java b/hope-core/src/main/java/io/appform/hope/core/functions/impl/str/SubStr.java index a4f444f..eae1f37 100644 --- a/hope-core/src/main/java/io/appform/hope/core/functions/impl/str/SubStr.java +++ b/hope-core/src/main/java/io/appform/hope/core/functions/impl/str/SubStr.java @@ -27,7 +27,7 @@ * to {@link io.appform.hope.core.values.NumericValue} end (exclusive). */ @FunctionImplementation("str.substr") -public class SubStr extends HopeFunction { +public class SubStr extends HopeFunction { private final Value arg; private final Value start; private final Value end; @@ -43,7 +43,7 @@ public SubStr(Value arg, Value start, Value end) { } @Override - public Value apply(Evaluator.EvaluationContext evaluationContext) { + public StringValue apply(Evaluator.EvaluationContext evaluationContext) { final String argValue = Converters.stringValue(evaluationContext, arg, ""); int startIndex = Converters.numericValue(evaluationContext, start, 0).intValue(); final int endIndex; diff --git a/hope-core/src/main/java/io/appform/hope/core/functions/impl/str/UpperCase.java b/hope-core/src/main/java/io/appform/hope/core/functions/impl/str/UpperCase.java index f4e1a1e..f3e5795 100644 --- a/hope-core/src/main/java/io/appform/hope/core/functions/impl/str/UpperCase.java +++ b/hope-core/src/main/java/io/appform/hope/core/functions/impl/str/UpperCase.java @@ -25,7 +25,7 @@ * Returns upper case {@link StringValue} of provided {@link StringValue} parameter. */ @FunctionImplementation("str.upper") -public class UpperCase extends HopeFunction { +public class UpperCase extends HopeFunction { private final Value arg; public UpperCase(Value arg) { @@ -33,7 +33,7 @@ public UpperCase(Value arg) { } @Override - public Value apply(Evaluator.EvaluationContext evaluationContext) { + public StringValue apply(Evaluator.EvaluationContext evaluationContext) { return new StringValue(Converters.stringValue(evaluationContext, arg, "").toUpperCase()); } } diff --git a/hope-core/src/main/java/io/appform/hope/core/utils/Converters.java b/hope-core/src/main/java/io/appform/hope/core/utils/Converters.java index 2829b34..39263d5 100644 --- a/hope-core/src/main/java/io/appform/hope/core/utils/Converters.java +++ b/hope-core/src/main/java/io/appform/hope/core/utils/Converters.java @@ -33,7 +33,6 @@ import java.util.*; import java.util.function.Function; import java.util.function.Supplier; -import java.util.stream.Collectors; import java.util.stream.StreamSupport; /** @@ -51,7 +50,8 @@ private Converters() { * @param evaluationContext Current eval context * @param node Node to be evaluated * @param defaultValue Default value if eval fails - * @return Evaluated value on success, defaultValue or excption in case of failure depending on {@link ErrorHandlingStrategy} + * @return Evaluated value on success, defaultValue or exception in case of failure depending on + * {@link ErrorHandlingStrategy} */ public static String stringValue( Evaluator.EvaluationContext evaluationContext, @@ -59,7 +59,7 @@ public static String stringValue( String defaultValue) { final ErrorHandlingStrategy errorHandlingStrategy = evaluationContext.getEvaluator() .getErrorHandlingStrategy(); - return node.accept(new VisitorAdapter( + return node.accept(new VisitorAdapter<>( () -> errorHandlingStrategy.handleIllegalEval("String value eval", defaultValue)) { @Override public String visit(JsonPathValue jsonPathValue) { @@ -88,7 +88,7 @@ public String visit(StringValue stringValue) { return pathValue.accept(this); } final JsonPointerValue pointerValue = stringValue.getPointerValue(); - if(null != pointerValue) { + if (null != pointerValue) { return pointerValue.accept(this); } final FunctionValue functionValue = stringValue.getFunction(); @@ -112,7 +112,8 @@ public String visit(FunctionValue functionValue) { * @param evaluationContext Current eval context * @param node Node to be evaluated * @param defaultValue Default value if eval fails - * @return Evaluated value on success, defaultValue or excption in case of failure depending on {@link ErrorHandlingStrategy} + * @return Evaluated value on success, defaultValue or exception in case of failure depending on + * {@link ErrorHandlingStrategy} */ public static Number numericValue( Evaluator.EvaluationContext evaluationContext, @@ -120,8 +121,8 @@ public static Number numericValue( Number defaultValue) { final ErrorHandlingStrategy errorHandlingStrategy = evaluationContext.getEvaluator() .getErrorHandlingStrategy(); - return node.accept(new VisitorAdapter(() -> errorHandlingStrategy.handleIllegalEval("Number eval", - defaultValue)) { + return node.accept(new VisitorAdapter<>(() -> errorHandlingStrategy.handleIllegalEval("Number eval", + defaultValue)) { @Override public Number visit(JsonPathValue jsonPathValue) { return extractNodeValue(jsonPathValue, @@ -151,7 +152,7 @@ public Number visit(NumericValue numericValue) { return pathValue.accept(this); } final JsonPointerValue pointerValue = numericValue.getPointerValue(); - if(null != pointerValue) { + if (null != pointerValue) { return pointerValue.accept(this); } final FunctionValue functionValue = numericValue.getFunction(); @@ -176,7 +177,8 @@ public Number visit(FunctionValue functionValue) { * @param evaluationContext Current eval context * @param node Node to be evaluated * @param defaultValue Default value if eval fails - * @return Evaluated value on success, defaultValue or excption in case of failure depending on {@link ErrorHandlingStrategy} + * @return Evaluated value on success, defaultValue or exception in case of failure depending on + * {@link ErrorHandlingStrategy} */ public static Boolean booleanValue( Evaluator.EvaluationContext evaluationContext, @@ -184,7 +186,7 @@ public static Boolean booleanValue( boolean defaultValue) { final ErrorHandlingStrategy errorHandlingStrategy = evaluationContext.getEvaluator() .getErrorHandlingStrategy(); - return node.accept(new VisitorAdapter( + return node.accept(new VisitorAdapter<>( () -> errorHandlingStrategy.handleIllegalEval("Boolean eval", defaultValue)) { @Override public Boolean visit(JsonPathValue jsonPathValue) { @@ -215,7 +217,7 @@ public Boolean visit(BooleanValue booleanValue) { return pathValue.accept(this); } final JsonPointerValue pointerValue = booleanValue.getPointerValue(); - if(null != pointerValue) { + if (null != pointerValue) { return pointerValue.accept(this); } final FunctionValue functionValue = booleanValue.getFunction(); @@ -240,7 +242,8 @@ public Boolean visit(FunctionValue functionValue) { * @param evaluationContext Current eval context * @param node Node to be evaluated * @param defaultValue Default value if eval fails - * @return Evaluated array on success, defaultValue or excption in case of failure depending on {@link ErrorHandlingStrategy} + * @return Evaluated array on success, defaultValue or exception in case of failure depending on + * {@link ErrorHandlingStrategy} */ public static List explodeArray( Evaluator.EvaluationContext evaluationContext, @@ -248,7 +251,7 @@ public static List explodeArray( List defaultValue) { final ErrorHandlingStrategy errorHandlingStrategy = evaluationContext.getEvaluator() .getErrorHandlingStrategy(); - return node.accept(new VisitorAdapter>(() -> defaultValue) { + return node.accept(new VisitorAdapter<>(() -> defaultValue) { @Override public List visit(JsonPathValue jsonPathValue) { final JsonNode value = evaluationContext.getJsonContext() @@ -260,13 +263,10 @@ public List visit(JsonPathValue jsonPathValue) { } if (value.isArray()) { return StreamSupport.stream( - Spliterators.spliteratorUnknownSize( - ArrayNode.class.cast(value) - .elements(), - Spliterator.ORDERED), + Spliterators.spliteratorUnknownSize(value.elements(), Spliterator.ORDERED), false) .map(Converters::jsonNodeToValue) - .collect(Collectors.toList()); + .toList(); } return errorHandlingStrategy.handleTypeMismatch( jsonPathValue.getPath(), @@ -287,13 +287,10 @@ public List visit(JsonPointerValue jsonPointerValue) { } if (value.isArray()) { return StreamSupport.stream( - Spliterators.spliteratorUnknownSize( - ArrayNode.class.cast(value) - .elements(), - Spliterator.ORDERED), + Spliterators.spliteratorUnknownSize(value.elements(), Spliterator.ORDERED), false) .map(Converters::jsonNodeToValue) - .collect(Collectors.toList()); + .toList(); } return errorHandlingStrategy.handleTypeMismatch( jsonPointerValue.getPointer(), @@ -312,7 +309,7 @@ public List visit(ArrayValue arrayValue) { return pathValue.accept(this); } final JsonPointerValue pointerValue = arrayValue.getPointerValue(); - if(null != pointerValue) { + if (null != pointerValue) { return pointerValue.accept(this); } final FunctionValue functionValue = arrayValue.getFunction(); @@ -336,7 +333,8 @@ public List visit(FunctionValue functionValue) { * @param evaluationContext Current eval context * @param value Value that evaluates to an array * @param defaultValue Default value if eval fails - * @return Evaluated list on success, defaultValue or excption in case of failure depending on {@link ErrorHandlingStrategy} + * @return Evaluated list on success, defaultValue or exception in case of failure depending on + * {@link ErrorHandlingStrategy} */ public static List flattenArray( Evaluator.EvaluationContext evaluationContext, @@ -354,13 +352,14 @@ public static List flattenArray( * @param evaluationContext Current eval context * @param node Node to be evaluated * @param defaultValue Default value if eval fails - * @return provided json path on success, defaultValue or excption in case of failure depending on {@link ErrorHandlingStrategy} + * @return provided json path on success, defaultValue or exception in case of failure depending on + * {@link ErrorHandlingStrategy} */ public static String jsonPathValue( Evaluator.EvaluationContext evaluationContext, TreeNode node, String defaultValue) { - return node.accept(new VisitorAdapter(() -> defaultValue) { + return node.accept(new VisitorAdapter<>(() -> defaultValue) { @Override public String visit(JsonPathValue jsonPathValue) { final String path = jsonPathValue.getPath(); @@ -385,13 +384,14 @@ public String visit(FunctionValue functionValue) { * @param evaluationContext Current eval context * @param node Node to be evaluated * @param defaultValue Default value if eval fails - * @return provided json path on success, defaultValue or excption in case of failure depending on {@link ErrorHandlingStrategy} + * @return provided json path on success, defaultValue or exception in case of failure depending on + * {@link ErrorHandlingStrategy} */ public static String jsonPointerValue( Evaluator.EvaluationContext evaluationContext, TreeNode node, String defaultValue) { - return node.accept(new VisitorAdapter(() -> defaultValue) { + return node.accept(new VisitorAdapter<>(() -> defaultValue) { @Override public String visit(JsonPointerValue jsonPointerValue) { final String pointer = jsonPointerValue.getPointer(); @@ -405,7 +405,9 @@ public String visit(JsonPointerValue jsonPointerValue) { @Override public String visit(FunctionValue functionValue) { - return jsonPointerValue(evaluationContext, function(functionValue).apply(evaluationContext), defaultValue); + return jsonPointerValue(evaluationContext, + function(functionValue).apply(evaluationContext), + defaultValue); } }); } @@ -417,7 +419,8 @@ public String visit(FunctionValue functionValue) { * @param evaluationContext Current eval context * @param node Node to be evaluated * @param defaultValue Default value if eval fails - * @return Evaluated object on success, defaultValue or excption in case of failure depending on {@link ErrorHandlingStrategy} + * @return Evaluated object on success, defaultValue or exception in case of failure depending on + * {@link ErrorHandlingStrategy} */ public static Object objectValue( Evaluator.EvaluationContext evaluationContext, @@ -425,8 +428,8 @@ public static Object objectValue( Object defaultValue) { final ErrorHandlingStrategy errorHandlingStrategy = evaluationContext.getEvaluator() .getErrorHandlingStrategy(); - return node.accept(new VisitorAdapter(() -> errorHandlingStrategy.handleIllegalEval("Object eval", - defaultValue)) { + return node.accept(new VisitorAdapter<>(() -> errorHandlingStrategy.handleIllegalEval("Object eval", + defaultValue)) { @Override public Object visit(JsonPathValue jsonPathValue) { final JsonNode value = nodeForJsonPath(jsonPathValue, evaluationContext); @@ -496,7 +499,7 @@ public static T handleValue( final ErrorHandlingStrategy errorHandlingStrategy = evaluationContext.getEvaluator() .getErrorHandlingStrategy(); return node.accept( - new VisitorAdapter(() -> handler.handleObject( + new VisitorAdapter<>(() -> handler.handleObject( errorHandlingStrategy.handleIllegalEval("Object eval", defaultValue))) { @Override public T visit(JsonPathValue jsonPathValue) { @@ -563,6 +566,7 @@ public T visit(FunctionValue functionValue) { }); } + @SuppressWarnings("java:S3740") private static HopeFunction function(FunctionValue functionValue) { final List parameters = functionValue.getParameters(); return createFunction(functionValue.getName(), @@ -570,6 +574,7 @@ private static HopeFunction function(FunctionValue functionValue) { parameters); } + @SuppressWarnings({"java:S3740", "java:S3878", "java:S3776"}) private static HopeFunction createFunction( String name, FunctionRegistry.ConstructorMeta selectedConstructor, @@ -607,11 +612,10 @@ private static Value jsonNodeToValue(JsonNode node) { } if (node.isArray()) { return new ArrayValue(StreamSupport.stream( - Spliterators.spliteratorUnknownSize(ArrayNode.class.cast(node) - .elements(), Spliterator.ORDERED), + Spliterators.spliteratorUnknownSize(node.elements(), Spliterator.ORDERED), false) .map(child -> jsonNodeToValue(node)) - .collect(Collectors.toList())); + .toList()); } throw new UnsupportedOperationException(node.getNodeType() .name() + " is not supported"); @@ -704,7 +708,7 @@ private static List arrayToObjectList( return values .stream() .map(value -> objectValue(evaluationContext, value, defaultValue)) - .collect(Collectors.toList()); + .toList(); } } diff --git a/hope-core/src/main/java/io/appform/hope/core/visitors/Evaluator.java b/hope-core/src/main/java/io/appform/hope/core/visitors/Evaluator.java index b4320b1..70f5942 100644 --- a/hope-core/src/main/java/io/appform/hope/core/visitors/Evaluator.java +++ b/hope-core/src/main/java/io/appform/hope/core/visitors/Evaluator.java @@ -17,11 +17,7 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.jayway.jsonpath.Configuration; -import com.jayway.jsonpath.DocumentContext; -import com.jayway.jsonpath.JsonPath; -import com.jayway.jsonpath.Option; -import com.jayway.jsonpath.ParseContext; +import com.jayway.jsonpath.*; import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider; import io.appform.hope.core.Evaluatable; import io.appform.hope.core.VisitorAdapter; @@ -29,27 +25,14 @@ import io.appform.hope.core.combiners.OrCombiner; import io.appform.hope.core.exceptions.errorstrategy.DefaultErrorHandlingStrategy; import io.appform.hope.core.exceptions.errorstrategy.ErrorHandlingStrategy; -import io.appform.hope.core.operators.And; -import io.appform.hope.core.operators.Equals; -import io.appform.hope.core.operators.Greater; -import io.appform.hope.core.operators.GreaterEquals; -import io.appform.hope.core.operators.Lesser; -import io.appform.hope.core.operators.LesserEquals; -import io.appform.hope.core.operators.Not; -import io.appform.hope.core.operators.NotEquals; -import io.appform.hope.core.operators.Or; +import io.appform.hope.core.operators.*; import io.appform.hope.core.utils.Converters; -import io.appform.hope.core.values.JsonPointerValue; import lombok.Builder; import lombok.Data; import lombok.Getter; import lombok.val; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.OptionalInt; +import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -75,9 +58,9 @@ public Evaluator() { public Evaluator(ErrorHandlingStrategy errorHandlingStrategy) { this.errorHandlingStrategy = errorHandlingStrategy; parseContext = JsonPath.using(Configuration.builder() - .jsonProvider(new JacksonJsonNodeJsonProvider()) - .options(Option.SUPPRESS_EXCEPTIONS) - .build()); + .jsonProvider(new JacksonJsonNodeJsonProvider()) + .options(Option.SUPPRESS_EXCEPTIONS) + .build()); } @@ -113,6 +96,7 @@ public static class EvaluationContext { private final Map jsonPointerEvalCache = new HashMap<>(128); } + @SuppressWarnings("java:S5411") public static class LogicEvaluator extends VisitorAdapter { private final EvaluationContext evaluationContext; @@ -213,11 +197,6 @@ public Boolean visit(Not not) { return !operand; } - @Override - public Boolean visit(JsonPointerValue jsonPointerValue) { - return null; - } - } } diff --git a/hope-core/src/test/java/io/appform/hope/core/visitors/EvaluatorTest.java b/hope-core/src/test/java/io/appform/hope/core/visitors/EvaluatorTest.java index 2b4b91e..9a195f5 100644 --- a/hope-core/src/test/java/io/appform/hope/core/visitors/EvaluatorTest.java +++ b/hope-core/src/test/java/io/appform/hope/core/visitors/EvaluatorTest.java @@ -26,12 +26,12 @@ /** * */ -public class EvaluatorTest { +class EvaluatorTest { final JsonNode node = NullNode.getInstance(); @Test - public void basicTest() { + void basicTest() { final Evaluator evaluator = new Evaluator(); assertTrue( evaluator.evaluate( diff --git a/hope-lang/pom.xml b/hope-lang/pom.xml index 9ee2cdb..a0cc1d6 100644 --- a/hope-lang/pom.xml +++ b/hope-lang/pom.xml @@ -41,7 +41,7 @@ com.helger.maven ph-javacc-maven-plugin - 4.1.2 + 4.1.4 jjc1 diff --git a/hope-lang/src/main/java/io/appform/hope/lang/utils/TypeUtils.java b/hope-lang/src/main/java/io/appform/hope/lang/utils/TypeUtils.java index 50727c3..ac8e8f0 100644 --- a/hope-lang/src/main/java/io/appform/hope/lang/utils/TypeUtils.java +++ b/hope-lang/src/main/java/io/appform/hope/lang/utils/TypeUtils.java @@ -20,7 +20,6 @@ import io.appform.hope.core.values.FunctionValue; import java.util.List; -import java.util.stream.Collectors; /** * A bunch of utils used by parser @@ -42,7 +41,7 @@ public static FunctionValue function(FunctionRegistry functionRegistry, String n final List matchingConstructors = constructors.stream() .filter(constructorMeta -> constructorMeta.getParamTypes() .size() == numProvidedParams) - .collect(Collectors.toList()); + .toList(); Preconditions.checkArgument(!matchingConstructors.isEmpty(), String.format("No matching function named %s that accepts %d params.", name, numProvidedParams)); diff --git a/hope-lang/src/test/java/io/appform/hope/lang/HopeLangEngineTest.java b/hope-lang/src/test/java/io/appform/hope/lang/HopeLangEngineTest.java index f9c711a..f636b70 100644 --- a/hope-lang/src/test/java/io/appform/hope/lang/HopeLangEngineTest.java +++ b/hope-lang/src/test/java/io/appform/hope/lang/HopeLangEngineTest.java @@ -37,47 +37,20 @@ class HopeLangEngineTest { @Test void testFuncIntFailNoExceptNoNode() throws Exception { - final ObjectMapper mapper = new ObjectMapper(); - final JsonNode node = mapper.readTree("{ \"x\" : true }"); - final HopeLangEngine hopeLangParser = HopeLangEngine.builder() - .errorHandlingStrategy(new InjectValueErrorHandlingStrategy()) - .build(); - - final Evaluatable operator = hopeLangParser.parse("\"$.x\" == \"true\""); - - //NOTE::THIS IS HOW THE BEHAVIOUR IS FOR EQUALS/NOT_EQUALS: - //BASICALLY THE NODE WILL EVALUATE TO NULL AND WILL MISMATCH EVERYTHING - assertFalse(hopeLangParser.evaluate(operator, node)); + testRuleFalse(""" + "$.x" == true"""); } @Test void testFuncIntFailNoExceptNoNodeSQ() throws Exception { - final ObjectMapper mapper = new ObjectMapper(); - final JsonNode node = mapper.readTree("{ \"x\" : true }"); - final HopeLangEngine hopeLangParser = HopeLangEngine.builder() - .errorHandlingStrategy(new InjectValueErrorHandlingStrategy()) - .build(); - - final Evaluatable operator = hopeLangParser.parse("\"$.x\" == \"true\""); - - //NOTE::THIS IS HOW THE BEHAVIOUR IS FOR EQUALS/NOT_EQUALS: - //BASICALLY THE NODE WILL EVALUATE TO NULL AND WILL MISMATCH EVERYTHING - assertFalse(hopeLangParser.evaluate(operator, node)); + testRuleFalse(""" + '$.x' == true"""); } @Test void testFuncIntFailNoExceptNoNodeJPtr() throws Exception { - final ObjectMapper mapper = new ObjectMapper(); - final JsonNode node = mapper.readTree("{ \"x\" : true }"); - final HopeLangEngine hopeLangParser = HopeLangEngine.builder() - .errorHandlingStrategy(new InjectValueErrorHandlingStrategy()) - .build(); - - final Evaluatable operator = hopeLangParser.parse("\"$.x\" == \"true\""); - - //NOTE::THIS IS HOW THE BEHAVIOUR IS FOR EQUALS/NOT_EQUALS: - //BASICALLY THE NODE WILL EVALUATE TO NULL AND WILL MISMATCH EVERYTHING - assertFalse(hopeLangParser.evaluate(operator, node)); + testRuleFalse(""" + "/x" == true"""); } @Test @@ -91,7 +64,7 @@ void testEvaluateFirst() throws JsonProcessingException { hopeLangParser.parse("\"$.x\" == \"E\""), hopeLangParser.parse("\"$.x\" == \"A\""), hopeLangParser.parse("\"$.y\" == \"U\"") - ); + ); Integer matchedRuleIndex = hopeLangParser.evaluateFirst(evaluatables, node).orElse(-1); Assertions.assertEquals(1, matchedRuleIndex); } @@ -107,4 +80,18 @@ void testBlah() throws Exception { JsonNode node = new ObjectMapper().readTree("{}"); assertTrue(hope.evaluate("ss.blah() == \"blah\"", node)); } + + private static void testRuleFalse(String hopeLangExpression) throws JsonProcessingException { + final ObjectMapper mapper = new ObjectMapper(); + final JsonNode node = mapper.readTree(""); + final HopeLangEngine hopeLangParser = HopeLangEngine.builder() + .errorHandlingStrategy(new InjectValueErrorHandlingStrategy()) + .build(); + + final Evaluatable operator = hopeLangParser.parse(hopeLangExpression); + + //NOTE::THIS IS HOW THE BEHAVIOUR IS FOR EQUALS/NOT_EQUALS: + //BASICALLY THE NODE WILL EVALUATE TO NULL AND WILL MISMATCH EVERYTHING + assertFalse(hopeLangParser.evaluate(operator, node)); + } } \ No newline at end of file diff --git a/hope-lang/src/test/java/io/appform/hope/lang/LibraryFunctionsTest.java b/hope-lang/src/test/java/io/appform/hope/lang/LibraryFunctionsTest.java index c151381..0ba5253 100644 --- a/hope-lang/src/test/java/io/appform/hope/lang/LibraryFunctionsTest.java +++ b/hope-lang/src/test/java/io/appform/hope/lang/LibraryFunctionsTest.java @@ -16,7 +16,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.NullNode; +import io.appform.hope.core.Evaluatable; import io.appform.hope.core.exceptions.errorstrategy.InjectValueErrorHandlingStrategy; import io.appform.hope.core.exceptions.impl.HopeMissingValueError; import io.appform.hope.core.exceptions.impl.HopeTypeMismatchError; @@ -42,7 +42,6 @@ class LibraryFunctionsTest { final ObjectMapper mapper = new ObjectMapper(); - final JsonNode node = NullNode.getInstance(); final FunctionRegistry functionRegistry; LibraryFunctionsTest() { @@ -80,9 +79,11 @@ void testMissingValueError(final String json, final String rule) { val parser = new HopeParser(new StringReader(rule)); val operator = parser.parse(functionRegistry); - assertThrows(HopeMissingValueError.class, () -> new Evaluator().evaluate(operator, node)); + assertThrows(HopeMissingValueError.class, () -> evaluate(operator, node)); } + + @ParameterizedTest @MethodSource("rulesWrongTypes") @SneakyThrows @@ -91,7 +92,7 @@ void testWongTypeError(final String json, final String rule) { val parser = new HopeParser(new StringReader(rule)); val operator = parser.parse(functionRegistry); - assertThrows(HopeTypeMismatchError.class, () -> new Evaluator().evaluate(operator, node)); + assertThrows(HopeTypeMismatchError.class, () -> evaluate(operator, node)); } private static Stream rulesWrongTypes() { @@ -236,5 +237,9 @@ private static Stream rules() { ); } + + private static void evaluate(Evaluatable operator, JsonNode node) { + new Evaluator().evaluate(operator, node); + } } diff --git a/lombok.config b/lombok.config new file mode 100644 index 0000000..b832adb --- /dev/null +++ b/lombok.config @@ -0,0 +1,3 @@ +lombok.addLombokGeneratedAnnotation = true +lombok.accessors.chain = true +lombok.anyConstructor.addConstructorProperties = true \ No newline at end of file diff --git a/pom.xml b/pom.xml index 9ff6b89..6265cff 100644 --- a/pom.xml +++ b/pom.xml @@ -97,6 +97,10 @@ false santanusinha https://sonarcloud.io + + **/io/appform/hope/lang/jmh_generated/*.java, + **/io/appform/hope/lang/parser/*.java + @@ -308,8 +312,8 @@ maven-compiler-plugin 3.8.1 - 11 - 11 + 17 + 17 @@ -333,6 +337,10 @@ + + From 76cd5ae263cbcbded8f7621f29856575000d940a Mon Sep 17 00:00:00 2001 From: santanusinha Date: Thu, 17 Aug 2023 15:25:56 +0000 Subject: [PATCH 2/3] Comitting files changed by Github Actions Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- dependencies.txt | 14 +++++++------- hope-core/dependencies.txt | 14 +++++++------- hope-lang/dependencies.txt | 16 ++++++++-------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/dependencies.txt b/dependencies.txt index 269fd5a..108140e 100644 --- a/dependencies.txt +++ b/dependencies.txt @@ -1,4 +1,4 @@ -io.appform.hope:hope:pom:2.0.1 +io.appform.hope:hope:pom:2.0.3 +- org.openjdk.jmh:jmh-core:jar:1.35:test | +- net.sf.jopt-simple:jopt-simple:jar:5.0.4:test | \- org.apache.commons:commons-math3:jar:3.2:test @@ -11,14 +11,14 @@ io.appform.hope:hope:pom:2.0.1 | +- org.checkerframework:checker-qual:jar:3.12.0:provided | +- com.google.errorprone:error_prone_annotations:jar:2.7.1:provided | \- com.google.j2objc:j2objc-annotations:jar:1.3:provided -+- com.fasterxml.jackson.core:jackson-core:jar:2.13.1:compile -+- com.fasterxml.jackson.core:jackson-databind:jar:2.13.1:compile -| \- com.fasterxml.jackson.core:jackson-annotations:jar:2.13.1:compile ++- com.fasterxml.jackson.core:jackson-core:jar:2.13.4:compile ++- com.fasterxml.jackson.core:jackson-databind:jar:2.13.4.1:compile +| \- com.fasterxml.jackson.core:jackson-annotations:jar:2.13.4:compile +- com.jayway.jsonpath:json-path:jar:2.7.0:compile -| +- net.minidev:json-smart:jar:2.4.7:compile -| | \- net.minidev:accessors-smart:jar:2.4.7:compile -| | \- org.ow2.asm:asm:jar:9.1:compile | \- org.slf4j:slf4j-api:jar:1.7.33:compile ++- net.minidev:json-smart:jar:2.4.11:compile +| \- net.minidev:accessors-smart:jar:2.4.11:compile +| \- org.ow2.asm:asm:jar:9.3:compile +- org.junit.jupiter:junit-jupiter-api:jar:5.8.2:test | +- org.opentest4j:opentest4j:jar:1.2.0:test | +- org.junit.platform:junit-platform-commons:jar:1.8.2:test diff --git a/hope-core/dependencies.txt b/hope-core/dependencies.txt index 736524e..19f62ce 100644 --- a/hope-core/dependencies.txt +++ b/hope-core/dependencies.txt @@ -1,4 +1,4 @@ -io.appform.hope:hope-core:jar:2.0.1 +io.appform.hope:hope-core:jar:2.0.3 +- org.reflections:reflections:jar:0.9.12:compile | \- org.javassist:javassist:jar:3.26.0-GA:compile +- org.openjdk.jmh:jmh-core:jar:1.35:test @@ -13,14 +13,14 @@ io.appform.hope:hope-core:jar:2.0.1 | +- org.checkerframework:checker-qual:jar:3.12.0:provided | +- com.google.errorprone:error_prone_annotations:jar:2.7.1:provided | \- com.google.j2objc:j2objc-annotations:jar:1.3:provided -+- com.fasterxml.jackson.core:jackson-core:jar:2.13.1:compile -+- com.fasterxml.jackson.core:jackson-databind:jar:2.13.1:compile -| \- com.fasterxml.jackson.core:jackson-annotations:jar:2.13.1:compile ++- com.fasterxml.jackson.core:jackson-core:jar:2.13.4:compile ++- com.fasterxml.jackson.core:jackson-databind:jar:2.13.4.1:compile +| \- com.fasterxml.jackson.core:jackson-annotations:jar:2.13.4:compile +- com.jayway.jsonpath:json-path:jar:2.7.0:compile -| +- net.minidev:json-smart:jar:2.4.7:compile -| | \- net.minidev:accessors-smart:jar:2.4.7:compile -| | \- org.ow2.asm:asm:jar:9.1:compile | \- org.slf4j:slf4j-api:jar:1.7.33:compile ++- net.minidev:json-smart:jar:2.4.11:compile +| \- net.minidev:accessors-smart:jar:2.4.11:compile +| \- org.ow2.asm:asm:jar:9.3:compile +- org.junit.jupiter:junit-jupiter-api:jar:5.8.2:test | +- org.opentest4j:opentest4j:jar:1.2.0:test | +- org.junit.platform:junit-platform-commons:jar:1.8.2:test diff --git a/hope-lang/dependencies.txt b/hope-lang/dependencies.txt index 33f4fb7..cea1445 100644 --- a/hope-lang/dependencies.txt +++ b/hope-lang/dependencies.txt @@ -1,5 +1,5 @@ -io.appform.hope:hope-lang:jar:2.0.1 -+- io.appform.hope:hope-core:jar:2.0.1:compile +io.appform.hope:hope-lang:jar:2.0.3 ++- io.appform.hope:hope-core:jar:2.0.3:compile | \- org.reflections:reflections:jar:0.9.12:compile | \- org.javassist:javassist:jar:3.26.0-GA:compile +- ch.qos.logback:logback-classic:jar:1.2.10:test @@ -17,13 +17,13 @@ io.appform.hope:hope-lang:jar:2.0.1 | +- org.checkerframework:checker-qual:jar:3.12.0:provided | +- com.google.errorprone:error_prone_annotations:jar:2.7.1:provided | \- com.google.j2objc:j2objc-annotations:jar:1.3:provided -+- com.fasterxml.jackson.core:jackson-core:jar:2.13.1:compile -+- com.fasterxml.jackson.core:jackson-databind:jar:2.13.1:compile -| \- com.fasterxml.jackson.core:jackson-annotations:jar:2.13.1:compile ++- com.fasterxml.jackson.core:jackson-core:jar:2.13.4:compile ++- com.fasterxml.jackson.core:jackson-databind:jar:2.13.4.1:compile +| \- com.fasterxml.jackson.core:jackson-annotations:jar:2.13.4:compile +- com.jayway.jsonpath:json-path:jar:2.7.0:compile -| \- net.minidev:json-smart:jar:2.4.7:compile -| \- net.minidev:accessors-smart:jar:2.4.7:compile -| \- org.ow2.asm:asm:jar:9.1:compile ++- net.minidev:json-smart:jar:2.4.11:compile +| \- net.minidev:accessors-smart:jar:2.4.11:compile +| \- org.ow2.asm:asm:jar:9.3:compile +- org.junit.jupiter:junit-jupiter-api:jar:5.8.2:test | +- org.opentest4j:opentest4j:jar:1.2.0:test | +- org.junit.platform:junit-platform-commons:jar:1.8.2:test From 8591c9106a563a099c3056f4e6ccc756ca2165ff Mon Sep 17 00:00:00 2001 From: Santanu Sinha Date: Thu, 17 Aug 2023 20:58:10 +0530 Subject: [PATCH 3/3] Added exclusion for generated classes --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 6265cff..6582ecd 100644 --- a/pom.xml +++ b/pom.xml @@ -338,9 +338,9 @@ - +