(chores): reduce cognitive complexity in camel-console#21086
(chores): reduce cognitive complexity in camel-console#21086orpiske wants to merge 17 commits intoapache:mainfrom
Conversation
|
🌟 Thank you for your contribution to the Apache Camel project! 🌟 🤖 CI automation will test this PR automatically. 🐫 Apache Camel Committers, please review the following items:
|
core/camel-console/src/main/java/org/apache/camel/impl/console/BeanDevConsole.java
Outdated
Show resolved
Hide resolved
core/camel-console/src/main/java/org/apache/camel/impl/console/BeanDevConsole.java
Outdated
Show resolved
Hide resolved
core/camel-console/src/main/java/org/apache/camel/impl/console/BeanDevConsole.java
Outdated
Show resolved
Hide resolved
core/camel-console/src/main/java/org/apache/camel/impl/console/BeanDevConsole.java
Show resolved
Hide resolved
core/camel-console/src/main/java/org/apache/camel/impl/console/BrowseDevConsole.java
Outdated
Show resolved
Hide resolved
| if (endpoint instanceof BrowsableEndpoint be | ||
| && (filter == null || PatternHelper.matchPattern(endpoint.getEndpointUri(), filter))) { | ||
| if (dump) { | ||
| List<Exchange> list = freshSize ? be.getExchanges(Integer.MAX_VALUE, null) : be.getExchanges(max, null); | ||
| int queueSize = list != null ? list.size() : 0; | ||
| int begin = 0; | ||
| if (list != null && pos > 0) { | ||
| begin = Math.max(0, list.size() - pos); | ||
| list = list.subList(begin, list.size()); | ||
| } | ||
| if (list != null) { | ||
| JsonObject jo = new JsonObject(); | ||
| jo.put("endpointUri", endpoint.getEndpointUri()); | ||
| jo.put("queueSize", queueSize); | ||
| jo.put("limit", max); | ||
| jo.put("position", begin); | ||
| if (!list.isEmpty()) { | ||
| long ts = list.get(0).getMessage().getHeader(Exchange.MESSAGE_TIMESTAMP, 0L, long.class); | ||
| if (ts > 0) { | ||
| jo.put("firstTimestamp", ts); | ||
| } | ||
| if (list.size() > 1) { | ||
| ts = list.get(list.size() - 1).getMessage().getHeader(Exchange.MESSAGE_TIMESTAMP, 0L, | ||
| long.class); | ||
| if (ts > 0) { | ||
| jo.put("lastTimestamp", ts); | ||
| } | ||
| } | ||
| } | ||
| arr.add(jo); | ||
| JsonArray arr2 = new JsonArray(); | ||
| for (Exchange e : list) { | ||
| arr2.add(MessageHelper.dumpAsJSonObject(e.getMessage(), false, false, includeBody, true, true, true, | ||
| maxChars)); | ||
| } | ||
| if (!arr2.isEmpty()) { | ||
| jo.put("messages", arr2); | ||
| } | ||
| } | ||
| } else { | ||
| BrowsableEndpoint.BrowseStatus status = be.getBrowseStatus(Integer.MAX_VALUE); |
There was a problem hiding this comment.
it is exchanging sonar warnign about cyclomatic complexity to too many break or continue statement
| if (mcc == null) { | ||
| return sb.toString(); | ||
| } | ||
|
|
||
| for (Route route : getCamelContext().getRoutes()) { | ||
| String id = route.getId(); | ||
| ManagedConsumerMBean mc = mcc.getManagedConsumer(id); | ||
| if (mc == null) { | ||
| continue; | ||
| } | ||
|
|
||
| if (!sb.isEmpty()) { | ||
| sb.append("\n"); | ||
| } | ||
|
|
||
| appendBasicConsumerInfoText(sb, id, mc); | ||
| appendScheduledPollConsumerText(sb, mcc); | ||
| appendTimerConsumerText(sb, mc, route); | ||
| } | ||
|
|
||
| return sb.toString(); |
There was a problem hiding this comment.
to keep happy-path first, avoid continue and reduce amount of code:
if (mcc != null) {
for (Route route : getCamelContext().getRoutes()) {
String id = route.getId();
ManagedConsumerMBean mc = mcc.getManagedConsumer(id);
if (mc != null) {
if (!sb.isEmpty()) {
sb.append("\n");
}
appendBasicConsumerInfoText(sb, id, mc);
appendScheduledPollConsumerText(sb, mcc);
appendTimerConsumerText(sb, mc, route);
}
}
}
| if (!"TimerConsumer".equals(mc.getServiceType())) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| MBeanServer ms = ManagementFactory.getPlatformMBeanServer(); | ||
| ObjectName on = getCamelContext().getManagementStrategy().getManagementObjectNameStrategy() | ||
| .getObjectNameForConsumer(getCamelContext(), route.getConsumer()); | ||
|
|
||
| if (!ms.isRegistered(on)) { | ||
| return; | ||
| } | ||
|
|
||
| appendTimerAttributesText(sb, ms, on); | ||
| } catch (Exception e) { | ||
| // ignore | ||
| } |
There was a problem hiding this comment.
to avoid negation, reduce amount of code and reduce amount of return:
if ("TimerConsumer".equals(mc.getServiceType())) {
try {
MBeanServer ms = ManagementFactory.getPlatformMBeanServer();
ObjectName on = getCamelContext().getManagementStrategy().getManagementObjectNameStrategy()
.getObjectNameForConsumer(getCamelContext(), route.getConsumer());
if (ms.isRegistered(on)) {
appendTimerAttributesText(sb, ms, on);
}
} catch (Exception e) {
// ignore
}
}
…in BeanDevConsole
- BeanDevConsole: Add back comments explaining serialization behavior - BrowseDevConsole: Reduce continue statements by extracting filtering to getBrowsableEndpoints() helper and using if/else structure
eba549e to
bc43dee
Compare
Summary
Changes
Test plan
mvn verifyin core/camel-console