Signed-off-by: Carpenter Lee <hooleeucas@163.com>master
@@ -52,7 +52,7 @@ final class ParamFlowChecker { | |||
} | |||
// Get parameter value. If value is null, then pass. | |||
Object value = valueAt(args, paramIdx); | |||
Object value = args[paramIdx]; | |||
if (value == null) { | |||
return true; | |||
} | |||
@@ -64,18 +64,6 @@ final class ParamFlowChecker { | |||
return passLocalCheck(resourceWrapper, rule, count, value); | |||
} | |||
private static Object valueAt(Object[] args, int paramIdx) { | |||
Object value = null; | |||
if (paramIdx < 0) { | |||
if (-paramIdx <= args.length) { | |||
return args[args.length + paramIdx]; | |||
} | |||
} else { | |||
value = args[paramIdx]; | |||
} | |||
return value; | |||
} | |||
private static boolean passLocalCheck(ResourceWrapper resourceWrapper, ParamFlowRule rule, int count, | |||
Object value) { | |||
try { | |||
@@ -47,9 +47,8 @@ public class ParamFlowSlot extends AbstractLinkedProcessorSlot<DefaultNode> { | |||
private final Object LOCK = new Object(); | |||
@Override | |||
public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, boolean prioritized, Object... args) | |||
throws Throwable { | |||
public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, | |||
boolean prioritized, Object... args) throws Throwable { | |||
if (!ParamFlowRuleManager.hasRules(resourceWrapper.getName())) { | |||
fireEntry(context, resourceWrapper, node, count, prioritized, args); | |||
return; | |||
@@ -73,6 +72,16 @@ public class ParamFlowSlot extends AbstractLinkedProcessorSlot<DefaultNode> { | |||
} | |||
for (ParamFlowRule rule : rules) { | |||
int paramIdx = rule.getParamIdx(); | |||
if (paramIdx < 0) { | |||
if (-paramIdx <= args.length) { | |||
rule.setParamIdx(args.length + paramIdx); | |||
} else { | |||
// illegal index, give it a illegal positive value, latter rule check will pass | |||
rule.setParamIdx(-paramIdx); | |||
} | |||
} | |||
// Initialize the parameter metrics. | |||
initHotParamMetricsFor(resourceWrapper, rule.getParamIdx()); | |||
@@ -105,7 +114,7 @@ public class ParamFlowSlot extends AbstractLinkedProcessorSlot<DefaultNode> { | |||
* Package-private for test. | |||
* | |||
* @param resourceWrapper resource to init | |||
* @param index index to initialize, which must be valid | |||
* @param index index to initialize, which must be valid | |||
*/ | |||
void initHotParamMetricsFor(ResourceWrapper resourceWrapper, /*@Valid*/ int index) { | |||
ParameterMetric metric; | |||
@@ -39,6 +39,28 @@ public class ParamFlowSlotTest { | |||
private final ParamFlowSlot paramFlowSlot = new ParamFlowSlot(); | |||
@Test | |||
public void testNegativeParamIdx() throws Throwable { | |||
String resourceName = "testNegativeParamIdx"; | |||
ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN); | |||
ParamFlowRule rule = new ParamFlowRule(resourceName) | |||
.setCount(1) | |||
.setParamIdx(-1); | |||
ParamFlowRuleManager.loadRules(Collections.singletonList(rule)); | |||
paramFlowSlot.entry(null, resourceWrapper, null, 1, false, "abc", "def", "ghi"); | |||
assertEquals(2, rule.getParamIdx().longValue()); | |||
rule.setParamIdx(-100); | |||
ParamFlowRuleManager.loadRules(Collections.singletonList(rule)); | |||
paramFlowSlot.entry(null, resourceWrapper, null, 1, false, "abc", "def", "ghi"); | |||
assertEquals(100, rule.getParamIdx().longValue()); | |||
rule.setParamIdx(0); | |||
ParamFlowRuleManager.loadRules(Collections.singletonList(rule)); | |||
paramFlowSlot.entry(null, resourceWrapper, null, 1, false, "abc", "def", "ghi"); | |||
assertEquals(0, rule.getParamIdx().longValue()); | |||
} | |||
@Test | |||
public void testEntryWhenParamFlowRuleNotExists() throws Throwable { | |||
String resourceName = "testEntryWhenParamFlowRuleNotExists"; | |||