Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/checkstyle-checks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@
<module name="BooleanExpressionComplexity">
<property name="max" value="7"/>
</module>
<module name="NPathComplexity"/>

<!-- Misc -->
<module name="ArrayTypeStyle"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,6 @@ protected void clean(IProgressMonitor monitor) throws CoreException {
public final <T extends IResource> void handleBuildSelection(final Collection<T> 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);
Expand All @@ -232,68 +228,56 @@ public final <T extends IResource> void handleBuildSelection(final Collection<T>
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<FileSet> fileSets = configuration.getFileSets();
List<FileSet> enabledFileSets = configuration.getFileSets().stream()
.filter(FileSet::isEnabled)
.toList();

Map<ICheckConfiguration, Auditor> audits = new HashMap<>();
List<IFile> 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<ICheckConfiguration, Auditor> 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<IFile> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,34 +117,44 @@ public static void write(OutputStream out, List<Module> 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) {
CheckstylePluginException.rethrow(ex);
}
}

private static void writeModules(Module module, Branch parent, Severity parentSeverity,
List<Module> 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<Module> childs = getChildModules(module, remainingModules);

// write child modules recursively
for (Module child : childs) {
writeModules(child, moduleEl, severity, remainingModules);
}
}

/**
* Writes a module to the transformer handler.
*
* @param module
* 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<Module> remainingModules) {

// remove this module from the list of modules to write
remainingModules.remove(module);

final List<Module> 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());
Expand All @@ -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
Expand All @@ -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<String, String> entry : module.getCustomMessages().entrySet()) {
Expand All @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -394,10 +394,7 @@ private boolean updateNeeded(IResource[] oldResources, IResource[] newResources)
Collection<IProject> oldProjects = CheckstyleMarkerFilter.getProjectsAsCollection(oldResources);
Collection<IProject> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Boolean> 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.
*
Expand Down
Loading