Skip to content

Commit c86a135

Browse files
committed
spider: SAST (SonarLint) Fixes
- Use isEmpty vs length checks. - Use instanceof assignment where applicable. - Use text blocks vs concatenation where applicable. - Use computeIfAbsent vs get/put where applicable. - Use charset constants from JRE vs string literals. - Use constants from base classes vs extended classes. - Remove useless conditions/assignments. Signed-off-by: kingthorin <[email protected]>
1 parent cf0d606 commit c86a135

20 files changed

+131
-134
lines changed

addOns/spider/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## Unreleased
8-
8+
### Changed
9+
- Maintenance changes.
910

1011
## [0.15.0] - 2025-06-20
1112
### Changed

addOns/spider/src/main/java/org/zaproxy/addon/spider/DomainsAlwaysInScopeTableModel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public Object getValueAt(int rowIndex, int columnIndex) {
7979

8080
@Override
8181
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
82-
if (columnIndex == 0 && aValue instanceof Boolean) {
83-
domainsInScope.get(rowIndex).setEnabled((Boolean) aValue);
82+
if (columnIndex == 0 && aValue instanceof Boolean val) {
83+
domainsInScope.get(rowIndex).setEnabled(val);
8484
fireTableCellUpdated(rowIndex, columnIndex);
8585
}
8686
}

addOns/spider/src/main/java/org/zaproxy/addon/spider/ExtensionSpider2.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.awt.Dimension;
2323
import java.awt.EventQueue;
24+
import java.awt.event.InputEvent;
2425
import java.awt.event.KeyEvent;
2526
import java.text.MessageFormat;
2627
import java.util.ArrayList;
@@ -489,8 +490,8 @@ private String createDisplayName(Target target, Object[] customConfigurations) {
489490
} else if (target.getStartNode() == null) {
490491
if (customConfigurations != null) {
491492
for (Object customConfiguration : customConfigurations) {
492-
if (customConfiguration instanceof URI) {
493-
return abbreviateDisplayName(((URI) customConfiguration).toString());
493+
if (customConfiguration instanceof URI uri) {
494+
return abbreviateDisplayName(uri.toString());
494495
}
495496
}
496497
}
@@ -508,8 +509,8 @@ private String createDisplayName(Target target, Object[] customConfigurations) {
508509
private HttpPrefixFetchFilter getUriPrefixFecthFilter(Object[] customConfigurations) {
509510
if (customConfigurations != null) {
510511
for (Object customConfiguration : customConfigurations) {
511-
if (customConfiguration instanceof HttpPrefixFetchFilter) {
512-
return (HttpPrefixFetchFilter) customConfiguration;
512+
if (customConfiguration instanceof HttpPrefixFetchFilter prefixFetchFilter) {
513+
return prefixFetchFilter;
513514
}
514515
}
515516
}
@@ -630,8 +631,8 @@ protected String getTargetUriOutOfScope(Target target, Object[] contextSpecificO
630631
if (node == null) {
631632
continue;
632633
}
633-
if (node instanceof StructuralSiteNode) {
634-
SiteNode siteNode = ((StructuralSiteNode) node).getSiteNode();
634+
if (node instanceof StructuralSiteNode ssNode) {
635+
SiteNode siteNode = ssNode.getSiteNode();
635636
if (!siteNode.isIncludedInScope()) {
636637
return node.getURI().toString();
637638
}
@@ -645,10 +646,10 @@ protected String getTargetUriOutOfScope(Target target, Object[] contextSpecificO
645646
}
646647
if (contextSpecificObjects != null) {
647648
for (Object obj : contextSpecificObjects) {
648-
if (obj instanceof URI) {
649-
String uri = ((URI) obj).toString();
650-
if (!isTargetUriInScope(uri)) {
651-
return uri;
649+
if (obj instanceof URI uri) {
650+
String uriStr = uri.toString();
651+
if (!isTargetUriInScope(uriStr)) {
652+
return uriStr;
652653
}
653654
}
654655
}
@@ -758,7 +759,7 @@ private ZapMenuItem getMenuItemCustomScan() {
758759
"menu.tools.spider",
759760
getView()
760761
.getMenuShortcutKeyStroke(
761-
KeyEvent.VK_S, KeyEvent.ALT_DOWN_MASK, false));
762+
KeyEvent.VK_S, InputEvent.ALT_DOWN_MASK, false));
762763
menuItemCustomScan.setEnabled(Control.getSingleton().getMode() != Mode.safe);
763764

764765
menuItemCustomScan.addActionListener(e -> showSpiderDialog((Target) null));

addOns/spider/src/main/java/org/zaproxy/addon/spider/OptionsSpiderPanel.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -390,11 +390,7 @@ private JCheckBox getChkProcessForm() {
390390
new ChangeListener() {
391391
@Override
392392
public void stateChanged(ChangeEvent ev) {
393-
if (chkProcessForm.isSelected()) {
394-
chkPostForm.setEnabled(true);
395-
} else {
396-
chkPostForm.setEnabled(false);
397-
}
393+
chkPostForm.setEnabled(chkProcessForm.isSelected());
398394
}
399395
});
400396
}

addOns/spider/src/main/java/org/zaproxy/addon/spider/SpiderAPI.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public ApiResponse handleApiAction(String name, JSONObject params) throws ApiExc
231231
String url = ApiUtils.getOptionalStringParam(params, PARAM_URL);
232232
if (params.containsKey(PARAM_MAX_CHILDREN)) {
233233
String maxChildrenStr = params.getString(PARAM_MAX_CHILDREN);
234-
if (maxChildrenStr != null && maxChildrenStr.length() > 0) {
234+
if (maxChildrenStr != null && !maxChildrenStr.isEmpty()) {
235235
try {
236236
maxChildren = Integer.parseInt(maxChildrenStr);
237237
} catch (NumberFormatException e) {
@@ -276,7 +276,7 @@ public ApiResponse handleApiAction(String name, JSONObject params) throws ApiExc
276276
}
277277
if (params.containsKey(PARAM_MAX_CHILDREN)) {
278278
String maxChildrenStr = params.getString(PARAM_MAX_CHILDREN);
279-
if (maxChildrenStr != null && maxChildrenStr.length() > 0) {
279+
if (maxChildrenStr != null && !maxChildrenStr.isEmpty()) {
280280
try {
281281
maxChildren = Integer.parseInt(maxChildrenStr);
282282
} catch (NumberFormatException e) {

addOns/spider/src/main/java/org/zaproxy/addon/spider/SpiderDialog.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public void init(Target target) {
151151
this.addCheckBoxField(1, FIELD_LOGOUT_AVOIDANCE, getSpiderParam().isLogoutAvoidance());
152152
this.addPadding(1);
153153

154-
if (!getBoolValue(FIELD_PROCESS_FORMS)) {
154+
if (!getBoolValue(FIELD_PROCESS_FORMS).booleanValue()) {
155155
setFieldValue(FIELD_POST_FORMS, false);
156156
getField(FIELD_POST_FORMS).setEnabled(false);
157157
}
@@ -169,7 +169,7 @@ public void actionPerformed(ActionEvent e) {
169169
new ActionListener() {
170170
@Override
171171
public void actionPerformed(ActionEvent e) {
172-
if (getBoolValue(FIELD_PROCESS_FORMS)) {
172+
if (getBoolValue(FIELD_PROCESS_FORMS).booleanValue()) {
173173
getField(FIELD_POST_FORMS).setEnabled(true);
174174
} else {
175175
setFieldValue(FIELD_POST_FORMS, false);
@@ -238,7 +238,7 @@ public void targetSelected(String field, Target node) {
238238
}
239239
}
240240
this.setComboFields(FIELD_CONTEXT, ctxNames, "");
241-
this.getField(FIELD_CONTEXT).setEnabled(ctxNames.size() > 0);
241+
this.getField(FIELD_CONTEXT).setEnabled(!ctxNames.isEmpty());
242242
}
243243

244244
private Context getSelectedContext() {
@@ -325,7 +325,7 @@ public void save() {
325325
} catch (Exception e1) {
326326
// Ignore - will have been checked in validateParams
327327
}
328-
if (this.getBoolValue(FIELD_ADVANCED)) {
328+
if (this.getBoolValue(FIELD_ADVANCED).booleanValue()) {
329329
// Set the advanced options
330330
spiderParam.setMaxDepth(this.getIntValue(FIELD_MAX_DEPTH));
331331
spiderParam.setMaxDuration(this.getIntValue(FIELD_MAX_DURATION));
@@ -351,7 +351,7 @@ public void save() {
351351
if (startUri != null) {
352352
contextSpecificObjects.add(startUri);
353353

354-
if (getBoolValue(FIELD_SUBTREE_ONLY)) {
354+
if (getBoolValue(FIELD_SUBTREE_ONLY).booleanValue()) {
355355
contextSpecificObjects.add(new HttpPrefixFetchFilter(startUri));
356356
}
357357
}
@@ -428,7 +428,7 @@ public String validateFields() {
428428
}
429429
}
430430

431-
if (getBoolValue(FIELD_SUBTREE_ONLY) && noStartUri) {
431+
if (getBoolValue(FIELD_SUBTREE_ONLY).booleanValue() && noStartUri) {
432432
return Constant.messages.getString("spider.custom.noStartSubtreeOnly.error");
433433
}
434434

addOns/spider/src/main/java/org/zaproxy/addon/spider/SpiderMessagesTable.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,14 @@ protected Component doHighlight(Component component, ComponentAdapter adapter) {
131131

132132
boolean processed = cell.isSuccessful();
133133
Icon icon = getProcessedIcon(processed);
134-
if (component instanceof IconAware) {
135-
((IconAware) component).setIcon(icon);
136-
} else if (component instanceof JLabel) {
137-
((JLabel) component).setIcon(icon);
134+
if (component instanceof IconAware iconAware) {
135+
iconAware.setIcon(icon);
136+
} else if (component instanceof JLabel label) {
137+
label.setIcon(icon);
138138
}
139139

140-
if (component instanceof JLabel) {
141-
((JLabel) component).setText(processed ? "" : cell.getLabel());
140+
if (component instanceof JLabel label) {
141+
label.setText(processed ? "" : cell.getLabel());
142142
}
143143

144144
return component;

addOns/spider/src/main/java/org/zaproxy/addon/spider/SpiderMessagesTableModel.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,8 @@ private ProcessedCellItem getProcessedCellItem(boolean processed, String reasonN
149149
if (processed) {
150150
return SUCCESSFULLY_PROCESSED_CELL_ITEM;
151151
}
152-
ProcessedCellItem processedCellItem = cacheProcessedCellItems.get(reasonNotProcessed);
153-
if (processedCellItem == null) {
154-
processedCellItem = new ProcessedCellItem(processed, reasonNotProcessed);
155-
cacheProcessedCellItems.put(reasonNotProcessed, processedCellItem);
156-
}
157-
return processedCellItem;
152+
return cacheProcessedCellItems.computeIfAbsent(
153+
reasonNotProcessed, k -> new ProcessedCellItem(processed, k));
158154
}
159155

160156
@Override

addOns/spider/src/main/java/org/zaproxy/addon/spider/SpiderScanController.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,14 @@ public int startScan(String name, Target target, User user, Object[] contextSpec
107107
if (obj instanceof SpiderParam) {
108108
LOGGER.debug("Setting custom spider params");
109109
spiderParams = (SpiderParam) obj;
110-
} else if (obj instanceof SpiderParser) {
111-
customSpiderParsers.add((SpiderParser) obj);
112-
} else if (obj instanceof FetchFilter) {
113-
customFetchFilters.add((FetchFilter) obj);
114-
} else if (obj instanceof ParseFilter) {
115-
customParseFilters.add((ParseFilter) obj);
116-
} else if (obj instanceof URI) {
117-
startUri = (URI) obj;
110+
} else if (obj instanceof SpiderParser parser) {
111+
customSpiderParsers.add(parser);
112+
} else if (obj instanceof FetchFilter fetchFilter) {
113+
customFetchFilters.add(fetchFilter);
114+
} else if (obj instanceof ParseFilter parseFilter) {
115+
customParseFilters.add(parseFilter);
116+
} else if (obj instanceof URI uri) {
117+
startUri = uri;
118118
} else {
119119
LOGGER.error(
120120
"Unexpected contextSpecificObject: {}",

addOns/spider/src/main/java/org/zaproxy/addon/spider/SpiderTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public SpiderTask(Spider parent, SpiderResourceFound resourceFound, URI uri) {
117117
if (resourceFound.getMessage() != null
118118
&& parent.getSpiderParam().isSendRefererHeader()) {
119119
requestHeader.setHeader(
120-
HttpRequestHeader.REFERER,
120+
HttpHeader.REFERER,
121121
resourceFound.getMessage().getRequestHeader().getURI().toString());
122122
}
123123
HttpMessage msg = new HttpMessage(requestHeader);

0 commit comments

Comments
 (0)