Skip to content

Commit

Permalink
Merge pull request #1362 from suhothayan/master
Browse files Browse the repository at this point in the history
Validate mandatory parameter mismatch in annotation.
  • Loading branch information
mohanvive authored Jul 1, 2019
2 parents 6f0ffaa + 868609d commit efff4cc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -134,8 +137,12 @@ public void parameterOverloadValidation(ParameterOverload[] parameterOverloads,
throws AnnotationValidationException {

Map<String, Parameter> parameterMap = new HashMap<>();
Set<String> mandatoryParameterSet = new HashSet<>();
for (Parameter parameter : parameters) {
parameterMap.put(parameter.name(), parameter);
if (!parameter.optional()) {
mandatoryParameterSet.add(parameter.name());
}
}

for (ParameterOverload parameterOverload : parameterOverloads) {
Expand All @@ -161,6 +168,33 @@ public void parameterOverloadValidation(ParameterOverload[] parameterOverloads,
}
}
}

if (parameterOverloads.length > 0) {
Set<String> mandatoryParameterSetViaOverload = new HashSet<>(parameterMap.keySet());
for (Iterator<String> iterator = mandatoryParameterSetViaOverload.iterator(); iterator.hasNext(); ) {
String parameter = iterator.next();
for (ParameterOverload parameterOverload : parameterOverloads) {
boolean contains = false;
for (String parameterName : parameterOverload.parameterNames()) {
if (parameter.equalsIgnoreCase(parameterName)) {
contains = true;
break;
}
}
if (!contains) {
iterator.remove();
break;
}
}
}

if (!mandatoryParameterSetViaOverload.equals(mandatoryParameterSet)) {
throw new AnnotationValidationException("Mandatory parameter information in ParameterOverload " +
"and based on 'optional' annotation is a mismatch. The parameters '" +
mandatoryParameterSetViaOverload + "' always appearing in ParameterOverload, but '" +
mandatoryParameterSet + "' are defined as not 'optional' in the annotations.");
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
defaultValue = "If z value is not given, drop the third parameter of the output.")
},
parameterOverloads = {
@ParameterOverload(),
@ParameterOverload(parameterNames = {"theta", "rho"}),
@ParameterOverload(parameterNames = {"theta", "rho", "z"})
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
type = {DataType.DOUBLE}),
@Parameter(name = "error.bound",
description = "The error bound value.",
optional = true,
defaultValue = "`support.threshold`/10",
type = {DataType.DOUBLE}),
@Parameter(name = "attribute",
description = "The attributes to group the events. If no attributes are given, " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
description = "The attribute that should be checked for the order.",
type = {DataType.STRING, DataType.DOUBLE, DataType.INT, DataType.LONG, DataType.FLOAT,
DataType.LONG},
optional = true,
dynamic = true,
defaultValue = "The concatenation of all the attributes of the event is considered."),
@Parameter(name = "order",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ public static void validateExpressionExecutors(Object objectHavingAnnotation,
for (ParameterOverload aParameterOverload : parameterOverloads) {
String[] overloadParameterNames = aParameterOverload.parameterNames();
if (overloadParameterNames.length == attributeExpressionExecutors.length &&
((overloadParameterNames.length == 0) || (overloadParameterNames.length > 0)
&& !(overloadParameterNames[overloadParameterNames.length - 1].
equals(REPETITIVE_PARAMETER_NOTATION)))) {
(overloadParameterNames.length == 0 || !overloadParameterNames[overloadParameterNames.length - 1].
equals(REPETITIVE_PARAMETER_NOTATION))) {
boolean isExpectedParameterOverload = true;
for (int i = 0; i < overloadParameterNames.length; i++) {
String overloadParameterName = overloadParameterNames[i];
Expand Down

0 comments on commit efff4cc

Please sign in to comment.