Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring to reduce sonar code smell fixes #4485

Merged
merged 18 commits into from
Nov 30, 2023
Merged

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ public int exitValue() {

@Override
public void destroy() {
//Empty Method
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,9 @@ public void readLine(String tagLine) {
addTag(defs, cidx.lineno, def, type, match, classInher, signature,
cidx.lineStart, cidx.lineEnd);

String[] args;
if (signature != null && !signature.equals("()") &&
!signature.startsWith("() ") && (args =
splitSignature(signature)) != null) {
for (String arg : args) {
!signature.startsWith("() ") ) {
for (String arg : splitSignature(signature)) {
//TODO this algorithm assumes that data types occur to
// the left of the argument name, so it will not
// work for languages like rust, kotlin, etc. which
Expand All @@ -287,7 +285,7 @@ public void readLine(String tagLine) {
arg = a[0]; // throws away assigned value
}
arg = arg.trim();
if (arg.length() < 1) {
if (arg.isEmpty()) {
continue;
}

Expand Down Expand Up @@ -433,7 +431,7 @@ private CpatIndex bestIndexOfTag(int lineno, String whole, String str) {
* @return a defined instance
*/
private CpatIndex bestIndexOfArg(int lineno, String whole, String arg) {
if (whole.length() < 1) {
if (whole.isEmpty()) {
return new CpatIndex(lineno, 0, 1, true);
}

Expand Down Expand Up @@ -551,9 +549,9 @@ private PatResult bestMatch(String whole, String arg, Pattern argpat) {
* ending with a word character.
*/
private int strictIndexOf(String whole, String substr) {
boolean strictLeft = substr.length() > 0 && WORD_CHAR.matcher(
boolean strictLeft = !substr.isEmpty() && WORD_CHAR.matcher(
String.valueOf(substr.charAt(0))).matches();
boolean strictRight = substr.length() > 0 && WORD_CHAR.matcher(
boolean strictRight = !substr.isEmpty() && WORD_CHAR.matcher(
String.valueOf(substr.charAt(substr.length() - 1))).matches();

int spos = 0;
Expand Down Expand Up @@ -703,7 +701,7 @@ private static String[] splitSignature(String signature) {
int soff = off0;
int eoff = offz;
if (soff >= eoff) {
return null;
return new String[0];
}

// Trim outer punctuation if it exists.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.opengrok.indexer.analysis;

import org.jetbrains.annotations.Nullable;
import org.opengrok.indexer.util.WhitelistObjectInputFilter;

import java.io.ByteArrayInputStream;
Expand All @@ -33,11 +34,14 @@
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

public class Definitions implements Serializable {

Expand All @@ -60,14 +64,14 @@ public class Definitions implements Serializable {
public static class LineTagMap implements Serializable {

private static final long serialVersionUID = 1191703801007779481L;
private final Map<String, Set<Tag>> sym_tags; //NOPMD
private final Map<String, Set<Tag>> symTags; //NOPMD

protected LineTagMap() {
this.sym_tags = new HashMap<>();
this.symTags = new HashMap<>();
}
}
// line number -> tag map
private final Map<Integer, LineTagMap> line_maps;
private final Map<Integer, LineTagMap> lineMaps;

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

public Definitions() {
symbols = new HashMap<>();
line_maps = new HashMap<>();
lineMaps = new HashMap<>();
tags = new ArrayList<>();
}

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

// Get tag info
if (lines != null && lines.contains(lineNumber)) {
LineTagMap lineMap = line_maps.get(lineNumber);
boolean isDefinitionPresent = Optional.ofNullable(symbols.get(symbol))
.filter(lines -> lines.contains(lineNumber))
.isPresent();
if (isDefinitionPresent) {
LineTagMap lineMap = lineMaps.get(lineNumber);
if (lineMap != null) {
for (Tag tag : lineMap.sym_tags.get(symbol)) {
for (Tag tag : lineMap.symTags.get(symbol)) {
if (tag.used) {
continue;
}
Expand Down Expand Up @@ -180,20 +186,15 @@ public List<Tag> getTags() {
* Get a list of all tags on given line.
*
* @param line line number
* @return list of tags
* @return list of tags or null
*/
public List<Tag> getTags(int line) {
LineTagMap lineMap = line_maps.get(line);
List<Tag> result = null;

if (lineMap != null) {
result = new ArrayList<>();
for (Set<Tag> ltags : lineMap.sym_tags.values()) {
result.addAll(ltags);
}
}

return result;
public @Nullable List<Tag> getTags(int line) {
return Optional.ofNullable(lineMaps.get(line))
.map(lineMap -> lineMap.symTags.values().stream()
.flatMap(Collection::stream)
.collect(Collectors.toList())
)
.orElse(null);
}

/**
Expand Down Expand Up @@ -267,26 +268,20 @@ public void addTag(int line, String symbol, String type, String text,
Tag newTag = new Tag(line, symbol, type, text, namespace, signature,
lineStart, lineEnd);
tags.add(newTag);
Set<Integer> lines = symbols.get(symbol);
if (lines == null) {
lines = new HashSet<>();
symbols.put(symbol, lines);
}
Set<Integer> lines = symbols.computeIfAbsent(symbol,
k -> new HashSet<>());
Integer aLine = line;
lines.add(aLine);

// Get per line map
LineTagMap lineMap = line_maps.get(aLine);
if (lineMap == null) {
lineMap = new LineTagMap();
line_maps.put(aLine, lineMap);
}
LineTagMap lineMap = lineMaps.computeIfAbsent(aLine,
key -> new LineTagMap());

// Insert sym->tag map for this line
Set<Tag> ltags = lineMap.sym_tags.get(symbol);
Set<Tag> ltags = lineMap.symTags.get(symbol);
if (ltags == null) {
ltags = new HashSet<>();
lineMap.sym_tags.put(symbol, ltags);
lineMap.symTags.put(symbol, ltags);
}
ltags.add(newTag);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@

import java.io.IOException;
import java.io.Writer;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
Expand All @@ -55,6 +56,7 @@ public class JFlexXrefUtils {
* Matches an HTML 5 ID or Name.
*/
private static final Pattern HTML5_ID_NAME = Pattern.compile("(?U)^\\S+$");
private static final String A_HREF_START = "<a href=\"";

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

url = result.getUri();
out.write("<a href=\"");
out.write(A_HREF_START);
Util.htmlize(url, out);
out.write("\">");
Util.htmlize(url, out);
Expand Down Expand Up @@ -166,16 +168,13 @@ public static Scope maybeNewScope(boolean scopesEnabled,
Scope existingScope, JFlexStackingLexer lexer, Definitions defs) {
if (scopesEnabled && existingScope == null) {
int line = lexer.getLineNumber();
if (defs != null) {
List<Tag> tags = defs.getTags(line);
if (tags != null) {
for (Tag tag : tags) {
if (tag.type.startsWith("function") || tag.type.startsWith("method")) {
return new Scope(tag.line, tag.line, tag.symbol, tag.namespace, tag.signature);
}
}
}
}
return Optional.ofNullable(defs)
.map(objDefs -> objDefs.getTags(line))
.stream().flatMap(Collection::stream)
.filter(tag -> tag.type.startsWith("function") || tag.type.startsWith("method"))
.findFirst()
.map(tag -> new Scope(tag.line, tag.line, tag.symbol, tag.namespace, tag.signature))
.orElse(null);
}
return null;
}
Expand Down Expand Up @@ -234,11 +233,11 @@ public static boolean writeSymbol(Writer out, Definitions defs,
if (defs != null && defs.hasDefinitionAt(symbol, line, strs)) {
// This is the definition of the symbol.
String type = strs[0];
String style_class = "d";
String styleClass = "d";

XrefStyle style = XrefStyle.getStyle(type);
if (style != null) {
style_class = style.ssClass;
styleClass = style.ssClass;
}

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

// 2) Create a link that searches for all references to this symbol.
out.append("<a href=\"");
out.append(A_HREF_START);
out.append(urlPrefix);
out.append(QueryParameters.REFS_SEARCH_PARAM_EQ);
Util.qurlencode(symbol, out);
appendProject(out, project);
out.append("\" class=\"");
out.append(style_class);
out.append(styleClass);
out.append(" intelliWindow-symbol\"");
out.append(" data-definition-place=\"def\"");
out.append(">");
Expand All @@ -281,7 +280,7 @@ public static boolean writeSymbol(Writer out, Definitions defs,
// that is defined more than once in this file. In either case, we
// can't generate a direct link to the definition, so generate a
// link to search for all definitions of that symbol instead.
out.append("<a href=\"");
out.append(A_HREF_START);
out.append(urlPrefix);
out.append(QueryParameters.DEFS_SEARCH_PARAM_EQ);
Util.qurlencode(symbol, out);
Expand All @@ -307,11 +306,11 @@ public static boolean writeSymbol(Writer out, Definitions defs,
public static void writeSameFileLinkSymbol(Writer out, String symbol)
throws IOException {
// This is a reference to a symbol defined exactly once in this file.
String style_class = "d";
String styleClass = "d";

// Generate a direct link to the symbol definition.
out.append("<a class=\"");
out.append(style_class);
out.append(styleClass);
out.append(" intelliWindow-symbol\" href=\"#");
Util.uriEncode(symbol, out);
out.append("\"");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public abstract class HCLLexer extends JFlexSymbolMatcher

private Stack<HCLLexerData> data;

public HCLLexer() {
protected HCLLexer() {
dataHead = new HCLLexerData();
}

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

offer(capture);

if (dataHead.hereSettings.size() > 0) {
if (!dataHead.hereSettings.isEmpty()) {
settings = dataHead.hereSettings.peek();
yybegin(settings.state);
if (didZspan) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ public class YamlAnalyzerFactory extends FileAnalyzerFactory {
"YML"
};

public static final YamlAnalyzerFactory DEFAULT_INSTANCE =
new YamlAnalyzerFactory();


private YamlAnalyzerFactory() {
public YamlAnalyzerFactory() {
super(null, null, SUFFIXES, null, null, "text/plain",
AbstractAnalyzer.Genre.PLAIN, NAME);
}
Expand Down
Loading
Loading