Skip to content

Commit

Permalink
Eliminate SonarQube warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Jan 22, 2024
1 parent 5db961f commit 65d3ce5
Show file tree
Hide file tree
Showing 23 changed files with 75 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ private boolean createSymlinkToLocalFile() throws IOException {
}
catch (IOException ex) {
// creates symbolic link failed - log warning and fallback to copying content
log.warn("Unable to create symbolic link: " + ex.getMessage());
log.warn("Unable to create symbolic link: {}", ex.getMessage());
return false;
}
}
Expand All @@ -332,7 +332,7 @@ private void createSymlinkToSymlinkTarget() throws IOException {
}
catch (IOException ex) {
// creates symbolic link failed - create text file with link instead (similar to git)
log.warn("Created link textfile instead of symbolic link: " + ex.getMessage());
log.warn("Created link textfile instead of symbolic link: {}", ex.getMessage());
FileUtils.write(linkPath.toFile(), relativizedPath.toString(), StandardCharsets.UTF_8);
}
}
Expand Down Expand Up @@ -447,7 +447,7 @@ private void applyPostProcessor(Map<String, GeneratedFileContext> consolidatedFi
});

// apply post processor configured as implicit ALWAYS
consolidatedFiles.values().forEach(fileItem -> {
consolidatedFiles.values().forEach(fileItem ->
pluginManager.getAll(PostProcessorPlugin.class).stream()
.filter(implicitPlugin -> implicitPlugin.accepts(fileItem.getFileContext(), postProcessorContext))
.filter(implicitPlugin -> implicitPlugin.implicitApply(fileItem.getFileContext(), postProcessorContext) == ImplicitApplyOptions.ALWAYS)
Expand All @@ -464,8 +464,8 @@ private void applyPostProcessor(Map<String, GeneratedFileContext> consolidatedFi
}
generatedFileContext.postProcessor(implicitPlugin.getName());
});
});
});
})
);

// remove items that do no longer exist
List.copyOf(consolidatedFiles.values()).forEach(fileItem -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
/**
* Charset-aware TemplateSource for handlebars.
*/
@SuppressWarnings("java:S2160") // equals/hashCode is implemented in base class
class CharsetAwareTemplateSource extends AbstractTemplateSource {

private final Resource file;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.github.jknack.handlebars.EscapingStrategy;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Helper;
import com.github.jknack.handlebars.Options;
Expand Down Expand Up @@ -61,12 +60,8 @@ public Handlebars load(HandlebarsKey options) throws Exception {
// setup handlebars
TemplateLoader templateLoader = new CharsetAwareTemplateLoader(templateDirs, options.getCharset());
EscapingStrategyPlugin escapingStrategy = pluginManager.get(options.getEscapingStrategy(), EscapingStrategyPlugin.class);
Handlebars handlebars = new Handlebars(templateLoader).with(new EscapingStrategy() {
@Override
public CharSequence escape(CharSequence value) {
return escapingStrategy.escape(value, escapingStrategyContext);
}
});
Handlebars handlebars = new Handlebars(templateLoader)
.with(value -> escapingStrategy.escape(value, escapingStrategyContext));

// register helpers provided by JKnack Handlebars implementation
handlebars.registerHelpers(StringHelpers.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected String getBlockSuffix() {
return null;
}

protected int getInsertPosition(@SuppressWarnings("unused") String content) {
protected int getInsertPosition(@SuppressWarnings({ "unused", "java:S1172" }) String content) {
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

Expand All @@ -43,12 +43,12 @@
/**
* Handlebars helper that extends the each helper by iterating only on list items that match a certain condition.
*/
abstract class AbstractEachIfHelper implements HelperPlugin {
abstract class AbstractEachIfHelper implements HelperPlugin<Object> {

private final Helper<Object> delegate = new EachHelper();
private final BiFunction<Object, Options, Boolean> propertyEvaluator;
private final BiPredicate<Object, Options> propertyEvaluator;

AbstractEachIfHelper(BiFunction<Object, Options, Boolean> propertyEvaluator) {
AbstractEachIfHelper(BiPredicate<Object, Options> propertyEvaluator) {
this.propertyEvaluator = propertyEvaluator;
}

Expand Down Expand Up @@ -90,7 +90,7 @@ private Iterable<Object> filterIterable(Iterable<Object> items, String propertyN
private boolean checkProperty(Object item, String propertyName, Options options) {
Map<String, Object> propertyMap = toMap(options.propertySet(item));
Object value = MapExpander.getDeep(propertyMap, propertyName);
return propertyEvaluator.apply(value, options);
return propertyEvaluator.test(value, options);
}

private Map<String, Object> toMap(Set<Entry<String, Object>> entries) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,10 @@
*/
public final class ConfigInheritanceResolver extends AbstractConfigurableObjectTreeProcessor<Map<String, Object>> {

private static final ConfigurableProcessor<Map<String, Object>> PROCESSOR = new ConfigurableProcessor<Map<String, Object>>() {
@Override
public Map<String, Object> process(Configurable configurable, Map<String, Object> parentConfig) {
Map<String, Object> mergedConfig = MapMerger.merge(configurable.getConfig(), parentConfig);
configurable.setConfig(mergedConfig);
return mergedConfig;
}
private static final ConfigurableProcessor<Map<String, Object>> PROCESSOR = (configurable, parentConfig) -> {
Map<String, Object> mergedConfig = MapMerger.merge(configurable.getConfig(), parentConfig);
configurable.setConfig(mergedConfig);
return mergedConfig;
};

private ConfigInheritanceResolver(Set<String> ignorePropertyNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static String getCanonicalPath(File file) {
* @return Canonical path
* @deprecated use {@link FileContext#getCanonicalPath()} instead.
*/
@Deprecated
@Deprecated(forRemoval = true)
public static String getCanonicalPath(FileContext fileContext) {
return fileContext.getCanonicalPath();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
/**
* Line endings for generated files.
*/
@SuppressWarnings("java:S115") // naming convention
public enum LineEndings {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ public static <T> List<T> defaultEmptyList(List<T> list) {
if (list != null) {

// do not allow null entries in list
if (list.stream().filter(Objects::isNull).findFirst().isPresent()) {
if (list.stream().anyMatch(Objects::isNull)) {
throw new IllegalArgumentException("Null element detected in list.");
}

return list;
}
else {
return new ArrayList<T>();
return new ArrayList<>();
}
}

Expand All @@ -67,7 +67,7 @@ public static <K, V> Map<K, V> defaultEmptyMap(Map<K, V> map) {
return map;
}
else {
return new HashMap<K, V>();
return new HashMap<>();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Expands map with shortcut keys like
Expand All @@ -46,7 +48,7 @@ private MapExpander() {
* @return Value or null
*/
@SuppressWarnings("unchecked")
public static Object getDeep(Map<String, Object> map, String key) {
public static @Nullable Object getDeep(@NotNull Map<String, Object> map, @NotNull String key) {
if (map.containsKey(key)) {
return ObjectUtils.defaultIfNull(map.get(key), "");
}
Expand All @@ -67,7 +69,7 @@ public static Object getDeep(Map<String, Object> map, String key) {
* @return Expanded amp
*/
@SuppressWarnings("unchecked")
public static Map<String, Object> expand(Map<String, Object> map) {
public static @Nullable Map<String, Object> expand(@Nullable Map<String, Object> map) {
if (map == null) {
return null;
}
Expand All @@ -89,9 +91,9 @@ public static Map<String, Object> expand(Map<String, Object> map) {
return expanded;
}

private static Map.Entry<String, Object> expandEntry(Map.Entry<String, Object> entry) {
private static @NotNull Map.Entry<String, Object> expandEntry(@NotNull Map.Entry<String, Object> entry) {
if (!StringUtils.contains(entry.getKey(), ".")) {
return new MapEntry<String, Object>(entry.getKey(), expandDeep(entry.getValue()));
return new MapEntry<>(entry.getKey(), expandDeep(entry.getValue()));
}

String key = StringUtils.substringBefore(entry.getKey(), ".");
Expand All @@ -101,11 +103,11 @@ private static Map.Entry<String, Object> expandEntry(Map.Entry<String, Object> e
map.put(remaining, expandDeep(entry.getValue()));
Map<String, Object> expandedMap = expand(map);

return new MapEntry<String, Object>(key, expandedMap);
return new MapEntry<>(key, expandedMap);
}

@SuppressWarnings("unchecked")
private static Object expandDeep(Object object) {
private static @Nullable Object expandDeep(@Nullable Object object) {
if (object instanceof Map) {
return expand((Map<String, Object>)object);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ private static List<Object> mergeList(List<Object> l1, List<Object> l2) {
}
else {
mergedList = new MergingList<>();
l1.forEach(item -> mergedList.addCheckMergeToken(item));
l1.forEach(mergedList::addCheckMergeToken);
}
l2.forEach(item -> mergedList.add(item));
l2.forEach(mergedList::add);
return mergedList;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;

import org.jetbrains.annotations.NotNull;

Expand All @@ -45,7 +45,7 @@ private MapSplitter() {
*/
@SuppressWarnings("unchecked")
public static @NotNull SplitResult splitMap(Map<String, Object> map,
@NotNull Function<Map.Entry<String, Object>, Boolean> matcher) {
@NotNull Predicate<Map.Entry<String, Object>> matcher) {
Map<String, Object> matching = new HashMap<>();
Map<String, Object> unmatching = new HashMap<>();

Expand Down Expand Up @@ -75,9 +75,9 @@ else if (entry.getValue() instanceof List) {
private static void processSimpleValue(@NotNull Map.Entry<String, Object> entry,
@NotNull Map<String, Object> matching,
@NotNull Map<String, Object> unmatching,
@NotNull Function<Map.Entry<String, Object>, Boolean> matcher) {
@NotNull Predicate<Map.Entry<String, Object>> matcher) {

if (matcher.apply(entry)) {
if (matcher.test(entry)) {
matching.put(entry.getKey(), entry.getValue());
}
else {
Expand All @@ -89,7 +89,7 @@ private static void processSimpleValue(@NotNull Map.Entry<String, Object> entry,
private static void processMapValue(@NotNull Map.Entry<String, Object> entry,
@NotNull Map<String, Object> matching,
@NotNull Map<String, Object> unmatching,
@NotNull Function<Map.Entry<String, Object>, Boolean> matcher) {
@NotNull Predicate<Map.Entry<String, Object>> matcher) {

Map<String, Object> map = (Map<String, Object>)entry.getValue();
SplitResult subResult = splitMap(map, matcher);
Expand All @@ -105,7 +105,7 @@ private static void processMapValue(@NotNull Map.Entry<String, Object> entry,
private static void processListValue(@NotNull Map.Entry<String, Object> entry,
@NotNull Map<String, Object> matching,
@NotNull Map<String, Object> unmatching,
@NotNull Function<Map.Entry<String, Object>, Boolean> matcher) {
@NotNull Predicate<Map.Entry<String, Object>> matcher) {

// we cannot split up the list - so it's put to unmatched if at least one list entry is unmatched
// to make processing easy we convert to list to a map and check of any unmatched
Expand All @@ -125,12 +125,11 @@ private static void processListValue(@NotNull Map.Entry<String, Object> entry,

private static boolean listHasSubStructures(@NotNull List<Object> list) {
return list.stream()
.filter(item -> (item instanceof List) || (item instanceof Map))
.findFirst().isPresent();
.anyMatch(item -> (item instanceof List) || (item instanceof Map));
}

/**
* Result of {@link #splitMap(Map, Function)} method.
* Result of {@link #splitMap(Map, Predicate)} method.
*/
public static final class SplitResult {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class MergingList<T> extends LinkedList<T> {
}

MergingList(MergingList<T> mergingList) {
mergingList.forEach(item -> super.add(item));
mergingList.forEach(super::add);
this.mergePositionIndex = mergingList.mergePositionIndex;
}

Expand Down
Loading

0 comments on commit 65d3ce5

Please sign in to comment.