Skip to content

Commit a305692

Browse files
Refactoring to reduce sonar code smell fixes (#4485)
--------- Signed-off-by: Gino Augustine <[email protected]> Co-authored-by: Vladimir Kotal <[email protected]>
1 parent 09c4287 commit a305692

File tree

69 files changed

+1015
-758
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1015
-758
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/AnalyzerGuru.java

Lines changed: 167 additions & 156 deletions
Large diffs are not rendered by default.

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/Ctags.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ public int exitValue() {
574574

575575
@Override
576576
public void destroy() {
577+
//Empty Method
577578
}
578579
};
579580

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/CtagsReader.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,9 @@ public void readLine(String tagLine) {
258258
addTag(defs, cidx.lineno, def, type, match, classInher, signature,
259259
cidx.lineStart, cidx.lineEnd);
260260

261-
String[] args;
262261
if (signature != null && !signature.equals("()") &&
263-
!signature.startsWith("() ") && (args =
264-
splitSignature(signature)) != null) {
265-
for (String arg : args) {
262+
!signature.startsWith("() ") ) {
263+
for (String arg : splitSignature(signature)) {
266264
//TODO this algorithm assumes that data types occur to
267265
// the left of the argument name, so it will not
268266
// work for languages like rust, kotlin, etc. which
@@ -287,7 +285,7 @@ public void readLine(String tagLine) {
287285
arg = a[0]; // throws away assigned value
288286
}
289287
arg = arg.trim();
290-
if (arg.length() < 1) {
288+
if (arg.isEmpty()) {
291289
continue;
292290
}
293291

@@ -433,7 +431,7 @@ private CpatIndex bestIndexOfTag(int lineno, String whole, String str) {
433431
* @return a defined instance
434432
*/
435433
private CpatIndex bestIndexOfArg(int lineno, String whole, String arg) {
436-
if (whole.length() < 1) {
434+
if (whole.isEmpty()) {
437435
return new CpatIndex(lineno, 0, 1, true);
438436
}
439437

@@ -551,9 +549,9 @@ private PatResult bestMatch(String whole, String arg, Pattern argpat) {
551549
* ending with a word character.
552550
*/
553551
private int strictIndexOf(String whole, String substr) {
554-
boolean strictLeft = substr.length() > 0 && WORD_CHAR.matcher(
552+
boolean strictLeft = !substr.isEmpty() && WORD_CHAR.matcher(
555553
String.valueOf(substr.charAt(0))).matches();
556-
boolean strictRight = substr.length() > 0 && WORD_CHAR.matcher(
554+
boolean strictRight = !substr.isEmpty() && WORD_CHAR.matcher(
557555
String.valueOf(substr.charAt(substr.length() - 1))).matches();
558556

559557
int spos = 0;
@@ -703,7 +701,7 @@ private static String[] splitSignature(String signature) {
703701
int soff = off0;
704702
int eoff = offz;
705703
if (soff >= eoff) {
706-
return null;
704+
return new String[0];
707705
}
708706

709707
// Trim outer punctuation if it exists.

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/Definitions.java

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package org.opengrok.indexer.analysis;
2525

26+
import org.jetbrains.annotations.Nullable;
2627
import org.opengrok.indexer.util.WhitelistObjectInputFilter;
2728

2829
import java.io.ByteArrayInputStream;
@@ -33,11 +34,14 @@
3334
import java.io.ObjectOutputStream;
3435
import java.io.Serializable;
3536
import java.util.ArrayList;
37+
import java.util.Collection;
3638
import java.util.HashMap;
3739
import java.util.HashSet;
3840
import java.util.List;
3941
import java.util.Map;
42+
import java.util.Optional;
4043
import java.util.Set;
44+
import java.util.stream.Collectors;
4145

4246
public class Definitions implements Serializable {
4347

@@ -60,14 +64,14 @@ public class Definitions implements Serializable {
6064
public static class LineTagMap implements Serializable {
6165

6266
private static final long serialVersionUID = 1191703801007779481L;
63-
private final Map<String, Set<Tag>> sym_tags; //NOPMD
67+
private final Map<String, Set<Tag>> symTags; //NOPMD
6468

6569
protected LineTagMap() {
66-
this.sym_tags = new HashMap<>();
70+
this.symTags = new HashMap<>();
6771
}
6872
}
6973
// line number -> tag map
70-
private final Map<Integer, LineTagMap> line_maps;
74+
private final Map<Integer, LineTagMap> lineMaps;
7175

7276
/**
7377
* Map from symbol to the line numbers on which the symbol is defined.
@@ -80,7 +84,7 @@ protected LineTagMap() {
8084

8185
public Definitions() {
8286
symbols = new HashMap<>();
83-
line_maps = new HashMap<>();
87+
lineMaps = new HashMap<>();
8488
tags = new ArrayList<>();
8589
}
8690

@@ -121,16 +125,18 @@ public boolean hasSymbol(String symbol) {
121125
* @return {@code true} if {@code symbol} is defined on the specified line
122126
*/
123127
public boolean hasDefinitionAt(String symbol, int lineNumber, String[] strs) {
124-
Set<Integer> lines = symbols.get(symbol);
125128
if (strs.length > 0) {
126129
strs[0] = "none";
127130
}
128131

129132
// Get tag info
130-
if (lines != null && lines.contains(lineNumber)) {
131-
LineTagMap lineMap = line_maps.get(lineNumber);
133+
boolean isDefinitionPresent = Optional.ofNullable(symbols.get(symbol))
134+
.filter(lines -> lines.contains(lineNumber))
135+
.isPresent();
136+
if (isDefinitionPresent) {
137+
LineTagMap lineMap = lineMaps.get(lineNumber);
132138
if (lineMap != null) {
133-
for (Tag tag : lineMap.sym_tags.get(symbol)) {
139+
for (Tag tag : lineMap.symTags.get(symbol)) {
134140
if (tag.used) {
135141
continue;
136142
}
@@ -180,20 +186,15 @@ public List<Tag> getTags() {
180186
* Get a list of all tags on given line.
181187
*
182188
* @param line line number
183-
* @return list of tags
189+
* @return list of tags or null
184190
*/
185-
public List<Tag> getTags(int line) {
186-
LineTagMap lineMap = line_maps.get(line);
187-
List<Tag> result = null;
188-
189-
if (lineMap != null) {
190-
result = new ArrayList<>();
191-
for (Set<Tag> ltags : lineMap.sym_tags.values()) {
192-
result.addAll(ltags);
193-
}
194-
}
195-
196-
return result;
191+
public @Nullable List<Tag> getTags(int line) {
192+
return Optional.ofNullable(lineMaps.get(line))
193+
.map(lineMap -> lineMap.symTags.values().stream()
194+
.flatMap(Collection::stream)
195+
.collect(Collectors.toList())
196+
)
197+
.orElse(null);
197198
}
198199

199200
/**
@@ -267,26 +268,20 @@ public void addTag(int line, String symbol, String type, String text,
267268
Tag newTag = new Tag(line, symbol, type, text, namespace, signature,
268269
lineStart, lineEnd);
269270
tags.add(newTag);
270-
Set<Integer> lines = symbols.get(symbol);
271-
if (lines == null) {
272-
lines = new HashSet<>();
273-
symbols.put(symbol, lines);
274-
}
271+
Set<Integer> lines = symbols.computeIfAbsent(symbol,
272+
k -> new HashSet<>());
275273
Integer aLine = line;
276274
lines.add(aLine);
277275

278276
// Get per line map
279-
LineTagMap lineMap = line_maps.get(aLine);
280-
if (lineMap == null) {
281-
lineMap = new LineTagMap();
282-
line_maps.put(aLine, lineMap);
283-
}
277+
LineTagMap lineMap = lineMaps.computeIfAbsent(aLine,
278+
key -> new LineTagMap());
284279

285280
// Insert sym->tag map for this line
286-
Set<Tag> ltags = lineMap.sym_tags.get(symbol);
281+
Set<Tag> ltags = lineMap.symTags.get(symbol);
287282
if (ltags == null) {
288283
ltags = new HashSet<>();
289-
lineMap.sym_tags.put(symbol, ltags);
284+
lineMap.symTags.put(symbol, ltags);
290285
}
291286
ltags.add(newTag);
292287
}

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/JFlexXrefUtils.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@
2626

2727
import java.io.IOException;
2828
import java.io.Writer;
29+
import java.util.Collection;
2930
import java.util.Comparator;
3031
import java.util.HashMap;
31-
import java.util.List;
3232
import java.util.Locale;
3333
import java.util.Map;
34+
import java.util.Optional;
3435
import java.util.Set;
3536
import java.util.SortedSet;
3637
import java.util.TreeSet;
@@ -55,6 +56,7 @@ public class JFlexXrefUtils {
5556
* Matches an HTML 5 ID or Name.
5657
*/
5758
private static final Pattern HTML5_ID_NAME = Pattern.compile("(?U)^\\S+$");
59+
private static final String A_HREF_START = "<a href=\"";
5860

5961
/**
6062
* Appends the {@code url} to the specified {@code out} {@link Writer}.
@@ -89,7 +91,7 @@ public static void appendLink(Writer out, JFlexLexer lexer, String url,
8991
}
9092

9193
url = result.getUri();
92-
out.write("<a href=\"");
94+
out.write(A_HREF_START);
9395
Util.htmlize(url, out);
9496
out.write("\">");
9597
Util.htmlize(url, out);
@@ -166,16 +168,13 @@ public static Scope maybeNewScope(boolean scopesEnabled,
166168
Scope existingScope, JFlexStackingLexer lexer, Definitions defs) {
167169
if (scopesEnabled && existingScope == null) {
168170
int line = lexer.getLineNumber();
169-
if (defs != null) {
170-
List<Tag> tags = defs.getTags(line);
171-
if (tags != null) {
172-
for (Tag tag : tags) {
173-
if (tag.type.startsWith("function") || tag.type.startsWith("method")) {
174-
return new Scope(tag.line, tag.line, tag.symbol, tag.namespace, tag.signature);
175-
}
176-
}
177-
}
178-
}
171+
return Optional.ofNullable(defs)
172+
.map(objDefs -> objDefs.getTags(line))
173+
.stream().flatMap(Collection::stream)
174+
.filter(tag -> tag.type.startsWith("function") || tag.type.startsWith("method"))
175+
.findFirst()
176+
.map(tag -> new Scope(tag.line, tag.line, tag.symbol, tag.namespace, tag.signature))
177+
.orElse(null);
179178
}
180179
return null;
181180
}
@@ -234,11 +233,11 @@ public static boolean writeSymbol(Writer out, Definitions defs,
234233
if (defs != null && defs.hasDefinitionAt(symbol, line, strs)) {
235234
// This is the definition of the symbol.
236235
String type = strs[0];
237-
String style_class = "d";
236+
String styleClass = "d";
238237

239238
XrefStyle style = XrefStyle.getStyle(type);
240239
if (style != null) {
241-
style_class = style.ssClass;
240+
styleClass = style.ssClass;
242241
}
243242

244243
// 1) Create an anchor for direct links. (Perhaps we should only
@@ -255,20 +254,20 @@ public static boolean writeSymbol(Writer out, Definitions defs,
255254
// explained by @tulinkry.
256255
if (HTML5_ID_NAME.matcher(symbol).matches()) {
257256
out.append("<a class=\"");
258-
out.append(style_class);
257+
out.append(styleClass);
259258
out.append("\" name=\"");
260259
Util.htmlize(symbol, out);
261260
out.append("\"/>");
262261
}
263262

264263
// 2) Create a link that searches for all references to this symbol.
265-
out.append("<a href=\"");
264+
out.append(A_HREF_START);
266265
out.append(urlPrefix);
267266
out.append(QueryParameters.REFS_SEARCH_PARAM_EQ);
268267
Util.qurlencode(symbol, out);
269268
appendProject(out, project);
270269
out.append("\" class=\"");
271-
out.append(style_class);
270+
out.append(styleClass);
272271
out.append(" intelliWindow-symbol\"");
273272
out.append(" data-definition-place=\"def\"");
274273
out.append(">");
@@ -281,7 +280,7 @@ public static boolean writeSymbol(Writer out, Definitions defs,
281280
// that is defined more than once in this file. In either case, we
282281
// can't generate a direct link to the definition, so generate a
283282
// link to search for all definitions of that symbol instead.
284-
out.append("<a href=\"");
283+
out.append(A_HREF_START);
285284
out.append(urlPrefix);
286285
out.append(QueryParameters.DEFS_SEARCH_PARAM_EQ);
287286
Util.qurlencode(symbol, out);
@@ -307,11 +306,11 @@ public static boolean writeSymbol(Writer out, Definitions defs,
307306
public static void writeSameFileLinkSymbol(Writer out, String symbol)
308307
throws IOException {
309308
// This is a reference to a symbol defined exactly once in this file.
310-
String style_class = "d";
309+
String styleClass = "d";
311310

312311
// Generate a direct link to the symbol definition.
313312
out.append("<a class=\"");
314-
out.append(style_class);
313+
out.append(styleClass);
315314
out.append(" intelliWindow-symbol\" href=\"#");
316315
Util.uriEncode(symbol, out);
317316
out.append("\"");

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/hcl/HCLLexer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public abstract class HCLLexer extends JFlexSymbolMatcher
4949

5050
private Stack<HCLLexerData> data;
5151

52-
public HCLLexer() {
52+
protected HCLLexer() {
5353
dataHead = new HCLLexerData();
5454
}
5555

@@ -113,7 +113,7 @@ public void hereOp(String capture) throws IOException {
113113
* @return true if a Here state was pushed
114114
*/
115115
public boolean maybeHereStart() throws IOException {
116-
if (dataHead.hereSettings != null && dataHead.hereSettings.size() > 0) {
116+
if (dataHead.hereSettings != null && !dataHead.hereSettings.isEmpty()) {
117117
HereDocSettings settings = dataHead.hereSettings.peek();
118118
yypush(settings.state);
119119
disjointSpan(HtmlConsts.STRING_CLASS);
@@ -141,7 +141,7 @@ public boolean maybeHereEnd(String capture) throws IOException {
141141

142142
offer(capture);
143143

144-
if (dataHead.hereSettings.size() > 0) {
144+
if (!dataHead.hereSettings.isEmpty()) {
145145
settings = dataHead.hereSettings.peek();
146146
yybegin(settings.state);
147147
if (didZspan) {

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/yaml/YamlAnalyzerFactory.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,8 @@ public class YamlAnalyzerFactory extends FileAnalyzerFactory {
3535
"YML"
3636
};
3737

38-
public static final YamlAnalyzerFactory DEFAULT_INSTANCE =
39-
new YamlAnalyzerFactory();
4038

41-
42-
private YamlAnalyzerFactory() {
39+
public YamlAnalyzerFactory() {
4340
super(null, null, SUFFIXES, null, null, "text/plain",
4441
AbstractAnalyzer.Genre.PLAIN, NAME);
4542
}

0 commit comments

Comments
 (0)