From 78e17e1575a26161df5b05536678c81e10bf86ae Mon Sep 17 00:00:00 2001 From: Calixte Bonsart Date: Tue, 21 Apr 2026 16:18:08 -0400 Subject: [PATCH] Issue #451: add NPathComplexity to checkstyle config --- config/checkstyle-checks.xml | 1 + .../core/builder/CheckstyleBuilder.java | 84 ++++++++----------- .../core/config/ConfigurationWriter.java | 66 ++++++++------- .../LeftCurlyTransformer.java | 8 +- .../ui/stats/views/AbstractStatsView.java | 9 +- .../internal/CheckstyleMarkerFilter.java | 78 +++++++++-------- 6 files changed, 112 insertions(+), 134 deletions(-) diff --git a/config/checkstyle-checks.xml b/config/checkstyle-checks.xml index d5402dbd9..9e9f0fe18 100644 --- a/config/checkstyle-checks.xml +++ b/config/checkstyle-checks.xml @@ -612,6 +612,7 @@ + diff --git a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/builder/CheckstyleBuilder.java b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/builder/CheckstyleBuilder.java index 836a42a00..59e0d730a 100644 --- a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/builder/CheckstyleBuilder.java +++ b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/builder/CheckstyleBuilder.java @@ -220,10 +220,6 @@ protected void clean(IProgressMonitor monitor) throws CoreException { public final void handleBuildSelection(final Collection resources, final IProjectConfiguration configuration, final IProgressMonitor monitor, final IProject project, final int kind) throws CoreException { - - // System.out.println(new java.util.Date() + " kind: " + kind + " files: - // " + resources.size()); - // on full build remove all previous checkstyle markers if (kind == IncrementalProjectBuilder.FULL_BUILD) { project.deleteMarkers(CheckstyleMarker.MARKER_ID, false, IResource.DEPTH_INFINITE); @@ -232,68 +228,56 @@ public final void handleBuildSelection(final Collection boolean backgroundFullBuild = CheckstylePluginPrefs .getBoolean(CheckstylePluginPrefs.PREF_BACKGROUND_FULL_BUILD); - try { - - // - // Build a set of auditors from the file sets of this project - // configuration. - // File sets that share the same check configuration merge into - // one Auditor. - // - List fileSets = configuration.getFileSets(); + List enabledFileSets = configuration.getFileSets().stream() + .filter(FileSet::isEnabled) + .toList(); - Map audits = new HashMap<>(); + List files = resources.stream() + .filter(resource -> resource instanceof IFile) + .map(resource -> (IFile) resource) + .toList(); - for (FileSet fileSet : fileSets) { - - // skip not enabled filesets - if (!fileSet.isEnabled()) { - continue; - } + // + // Build a set of auditors from the file sets of this project + // configuration. + // File sets that share the same check configuration merge into + // one Auditor. + // + Map audits = new HashMap<>(); + try { + for (FileSet fileSet : enabledFileSets) { ICheckConfiguration checkConfig = fileSet.getCheckConfig(); if (checkConfig == null) { throw new CheckstylePluginException( NLS.bind(Messages.errorNoCheckConfig, project.getName())); } - // get an already created audit from the map - Auditor audit = audits.get(checkConfig); - - // create the audit with the file sets check configuration - if (audit == null) { - - audit = new Auditor(checkConfig); - audits.put(checkConfig, audit); - } + Auditor audit = audits.computeIfAbsent(checkConfig, Auditor::new); // check which files belong to the file set - for (IResource resource : resources) { + List filesInFileset = files.stream() + .filter(fileSet::includesFile) + .toList(); - if (resource instanceof IFile) { - IFile file = (IFile) resource; + for (IFile file : filesInFileset) { + boolean hasCompileErrors = IMarker.SEVERITY_ERROR == file.findMaxProblemSeverity( + IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, true, IResource.DEPTH_ZERO); - // if file set includes file add to the audit - if (fileSet.includesFile(file)) { - boolean hasCompileErrors = IMarker.SEVERITY_ERROR == file.findMaxProblemSeverity( - IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, true, IResource.DEPTH_ZERO); + // remove markers on this file + file.deleteMarkers(CheckstyleMarker.MARKER_ID, false, IResource.DEPTH_ZERO); - // remove markers on this file - file.deleteMarkers(CheckstyleMarker.MARKER_ID, false, IResource.DEPTH_ZERO); - - // avoid checkstyle parser errors being shown - if (hasCompileErrors) { - continue; - } + // avoid checkstyle parser errors being shown + if (hasCompileErrors) { + continue; + } - audit.addFile(file); + audit.addFile(file); - // remove markers from package to prevent - // packagehtml messages from accumulatin - file.getParent().deleteMarkers(CheckstyleMarker.MARKER_ID, false, - IResource.DEPTH_ZERO); - } - } + // remove markers from package to prevent + // packagehtml messages from accumulatin + file.getParent().deleteMarkers(CheckstyleMarker.MARKER_ID, false, + IResource.DEPTH_ZERO); } } diff --git a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/config/ConfigurationWriter.java b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/config/ConfigurationWriter.java index ad0834479..2e4a9f8de 100644 --- a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/config/ConfigurationWriter.java +++ b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/config/ConfigurationWriter.java @@ -117,7 +117,7 @@ public static void write(OutputStream out, List modules, ICheckConfigura throw new CheckstylePluginException(Messages.errorMoreThanOneRootModule); } - writeModule(rootModules.get(0), doc, null, modules); + writeModules(rootModules.get(0), doc, null, modules); out.write(XMLUtil.toByteArray(doc)); } catch (IOException ex) { @@ -125,6 +125,27 @@ public static void write(OutputStream out, List modules, ICheckConfigura } } + private static void writeModules(Module module, Branch parent, Severity parentSeverity, + List remainingModules) { + Element moduleEl = writeModule(module, parent); + + Severity severity = parentSeverity; + if (module.getSeverity() != null && Severity.INHERIT != module.getSeverity()) { + // set the parent severity for child modules + severity = module.getSeverity(); + } + + // remove this module from the list of modules to write + remainingModules.remove(module); + + final List childs = getChildModules(module, remainingModules); + + // write child modules recursively + for (Module child : childs) { + writeModules(child, moduleEl, severity, remainingModules); + } + } + /** * Writes a module to the transformer handler. * @@ -132,19 +153,8 @@ public static void write(OutputStream out, List modules, ICheckConfigura * the module to write * @param parent * the parent element - * @param parentSeverity - * the severity of the parent module - * @param remainingModules - * the list of remaining (possibly child) modules */ - private static void writeModule(Module module, Branch parent, Severity parentSeverity, - List remainingModules) { - - // remove this module from the list of modules to write - remainingModules.remove(module); - - final List childs = getChildModules(module, remainingModules); - + private static Element writeModule(Module module, Branch parent) { // Start the module Element moduleEl = parent.addElement(XMLTags.MODULE_TAG); moduleEl.addAttribute(XMLTags.NAME_TAG, module.getMetaData().identity().internalName()); @@ -158,15 +168,11 @@ private static void writeModule(Module module, Branch parent, Severity parentSev } // Write severity only if it differs from the parents severity - Severity severity = parentSeverity; if (module.getSeverity() != null && Severity.INHERIT != module.getSeverity()) { Element propertyEl = moduleEl.addElement(XMLTags.PROPERTY_TAG); propertyEl.addAttribute(XMLTags.NAME_TAG, XMLTags.SEVERITY_TAG); propertyEl.addAttribute(XMLTags.VALUE_TAG, module.getSeverity().toXmlValue()); - - // set the parent severity for child modules - severity = module.getSeverity(); } // write module id @@ -178,17 +184,16 @@ private static void writeModule(Module module, Branch parent, Severity parentSev } // write properties of the module - for (ConfigProperty property : module.getProperties()) { - - // write property only if it differs from the default value - String value = Strings.emptyToNull(property.getValue()); - if (value != null && !Objects.equals(value, property.getMetaData().getDefaultValue())) { - - Element propertyEl = moduleEl.addElement(XMLTags.PROPERTY_TAG); - propertyEl.addAttribute(XMLTags.NAME_TAG, property.getMetaData().getName()); - propertyEl.addAttribute(XMLTags.VALUE_TAG, property.getValue()); - } - } + module.getProperties().stream() + .filter(property -> property.getValue() != null) + .filter(property -> !property.getValue().isEmpty()) + .filter(property -> !Objects.equals(property.getValue(), + property.getMetaData().getDefaultValue())) + .forEach(property -> { + Element propertyEl = moduleEl.addElement(XMLTags.PROPERTY_TAG); + propertyEl.addAttribute(XMLTags.NAME_TAG, property.getMetaData().getName()); + propertyEl.addAttribute(XMLTags.VALUE_TAG, property.getValue()); + }); // write custom messages for (Map.Entry entry : module.getCustomMessages().entrySet()) { @@ -214,10 +219,7 @@ private static void writeModule(Module module, Branch parent, Severity parentSev metaEl.addAttribute(XMLTags.VALUE_TAG, module.getLastEnabledSeverity().toXmlValue()); } - // write child modules recursively - for (Module child : childs) { - writeModule(child, moduleEl, severity, remainingModules); - } + return moduleEl; } /** diff --git a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/LeftCurlyTransformer.java b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/LeftCurlyTransformer.java index b4b24ef05..ffc91d57f 100644 --- a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/LeftCurlyTransformer.java +++ b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/ctransformerclasses/LeftCurlyTransformer.java @@ -39,20 +39,16 @@ public FormatterConfiguration transformRule() { + "LITERAL_FINALLY, LITERAL_FOR, LITERAL_IF, LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_WHILE"; } final StringTokenizer token = new StringTokenizer(tokens, ", "); - String tok; String option = getAttribute("option"); - if (option == null) { - option = "eol"; - } - if ("eol".equals(option)) { + if (option == null || "eol".equals(option)) { option = "end_of_line"; } else if ("nl".equals(option) || "nlow".equals(option)) { option = "next_line"; } while (token.hasMoreTokens()) { - tok = token.nextToken(); + String tok = token.nextToken(); if ("CLASS_DEF".equals(tok)) { userFormatterSetting("brace_position_for_anonymous_type_declaration", option); userFormatterSetting("brace_position_for_enum_constant", option); diff --git a/net.sf.eclipsecs.ui/src/net/sf/eclipsecs/ui/stats/views/AbstractStatsView.java b/net.sf.eclipsecs.ui/src/net/sf/eclipsecs/ui/stats/views/AbstractStatsView.java index 156ea1181..9b6b73aef 100644 --- a/net.sf.eclipsecs.ui/src/net/sf/eclipsecs/ui/stats/views/AbstractStatsView.java +++ b/net.sf.eclipsecs.ui/src/net/sf/eclipsecs/ui/stats/views/AbstractStatsView.java @@ -381,10 +381,10 @@ private boolean updateNeeded(IResource[] oldResources, IResource[] newResources) || onResource == CheckstyleMarkerFilter.ON_WORKING_SET) { return false; } - if (newResources == null || newResources.length < 1) { + if (newResources.length == 0) { return false; } - if (oldResources == null || oldResources.length < 1) { + if (oldResources == null || oldResources.length == 0) { return true; } if (Arrays.equals(oldResources, newResources)) { @@ -394,10 +394,7 @@ private boolean updateNeeded(IResource[] oldResources, IResource[] newResources) Collection oldProjects = CheckstyleMarkerFilter.getProjectsAsCollection(oldResources); Collection newProjects = CheckstyleMarkerFilter.getProjectsAsCollection(newResources); - if (oldProjects.size() == newProjects.size()) { - return !newProjects.containsAll(oldProjects); - } - return true; + return oldProjects.size() != newProjects.size() || !newProjects.containsAll(oldProjects); } return true; diff --git a/net.sf.eclipsecs.ui/src/net/sf/eclipsecs/ui/stats/views/internal/CheckstyleMarkerFilter.java b/net.sf.eclipsecs.ui/src/net/sf/eclipsecs/ui/stats/views/internal/CheckstyleMarkerFilter.java index a0ff650a5..f92fb4f4b 100644 --- a/net.sf.eclipsecs.ui/src/net/sf/eclipsecs/ui/stats/views/internal/CheckstyleMarkerFilter.java +++ b/net.sf.eclipsecs.ui/src/net/sf/eclipsecs/ui/stats/views/internal/CheckstyleMarkerFilter.java @@ -26,6 +26,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.List; +import java.util.Optional; import org.eclipse.core.resources.IMarker; import org.eclipse.core.resources.IProject; @@ -359,54 +360,51 @@ public void restoreState(IDialogSettings dialogSettings) { resetState(); IDialogSettings settings = dialogSettings.getSection(TAG_DIALOG_SECTION); - if (settings != null) { - - String setting = settings.get(TAG_ENABLED); - if (setting != null) { - mEnabled = Boolean.parseBoolean(setting); - } - - setting = settings.get(TAG_ON_RESOURCE); - if (setting != null) { - try { - mOnResource = Integer.parseInt(setting); - } catch (NumberFormatException ex) { - // ignore and use default value - } - } - - setting = settings.get(TAG_WORKING_SET); - if (setting != null) { - setWorkingSet(PlatformUI.getWorkbench().getWorkingSetManager() - .getWorkingSet(setting)); - } - - setting = settings.get(TAG_SELECT_BY_SEVERITY); - if (setting != null) { - mSelectBySeverity = Boolean.parseBoolean(setting); - } + if (settings == null) { + return; + } - setting = settings.get(TAG_SEVERITY); - if (setting != null) { - try { - mSeverity = Integer.parseInt(setting); - } catch (NumberFormatException ex) { - // ignore and use default value - } + findBooleanSetting(settings, TAG_ENABLED) + .ifPresent(setting -> mEnabled = setting); + findBooleanSetting(settings, TAG_SELECT_BY_SEVERITY) + .ifPresent(setting -> mSelectBySeverity = setting); + findBooleanSetting(settings, TAG_SELECT_BY_REGEX) + .ifPresent(setting -> mFilterByRegex = setting); + + String setting = settings.get(TAG_ON_RESOURCE); + if (setting != null) { + try { + mOnResource = Integer.parseInt(setting); + } catch (NumberFormatException ex) { + // ignore and use default value } + } - setting = settings.get(TAG_SELECT_BY_REGEX); - if (setting != null) { - mFilterByRegex = Boolean.parseBoolean(setting); + setting = settings.get(TAG_WORKING_SET); + if (setting != null) { + setWorkingSet(PlatformUI.getWorkbench().getWorkingSetManager() + .getWorkingSet(setting)); + } + setting = settings.get(TAG_SEVERITY); + if (setting != null) { + try { + mSeverity = Integer.parseInt(setting); + } catch (NumberFormatException ex) { + // ignore and use default value } + } - String[] regex = settings.getArray(TAG_REGULAR_EXPRESSIONS); - if (regex != null) { - mFilterRegex = Arrays.asList(regex); - } + String[] regex = settings.getArray(TAG_REGULAR_EXPRESSIONS); + if (regex != null) { + mFilterRegex = Arrays.asList(regex); } } + private static Optional findBooleanSetting(IDialogSettings dialogSettings, + String key) { + return Optional.ofNullable(dialogSettings.get(key)).map(Boolean::parseBoolean); + } + /** * Saves the state of the filter into the given dialog settings. *