Skip to content

Commit

Permalink
Prefer methods with string parameters over specific type ones in brid…
Browse files Browse the repository at this point in the history
…ge to simplify bridging with other languages

Only accepting string is enough in most of the cases.
  • Loading branch information
rabelenda-abstracta committed May 3, 2024
1 parent 0cd35b5 commit b6cc111
Showing 1 changed file with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.lang.reflect.Parameter;
import java.time.Duration;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -36,8 +35,30 @@ public BridgedObjectConstruct(BridgedObjectConstructor constructor, String tag,
this.constructor = constructor;
this.tag = tag;
this.builders = builders;
builders.sort(
Comparator.<BuilderMethod, Integer>comparing(b -> b.getParameters().length).reversed());
builders.sort((b1, b2) -> {
int ret = compareMoreParametersFirst(b1, b2);
return ret != 0 ? ret : compareFirstStringParameters(b1, b2);
});
}

private int compareMoreParametersFirst(BuilderMethod b1, BuilderMethod b2) {
return b2.getParameters().length - b1.getParameters().length;
}

// prefer methods with string parameters (JMeter expressions) over specific types
private int compareFirstStringParameters(BuilderMethod b1, BuilderMethod b2) {
Parameter[] params1 = b1.getParameters();
Parameter[] params2 = b2.getParameters();
for (int i = 0; i < params1.length; i++) {
if (params1[i].getType().equals(String.class) && !params2[i].getType()
.equals(String.class)) {
return -1;
} else if (params2[i].getType().equals(String.class) && !params1[i].getType()
.equals(String.class)) {
return 1;
}
}
return 0;
}

private static Map<Class<?>, Function<String, Object>> solveParsers() {
Expand Down

0 comments on commit b6cc111

Please sign in to comment.