Skip to content

Commit 220ce88

Browse files
authored
Merge pull request #220 from emouty/cleanup-warns
Remove deprecated apis usages and fix some warns
2 parents e0b846e + 61b60f1 commit 220ce88

27 files changed

+215
-270
lines changed

build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ changelog {
118118
}
119119

120120
tasks {
121+
withType<JavaCompile> {
122+
options.compilerArgs.add("-Xlint:deprecation")
123+
}
121124
wrapper {
122125
gradleVersion = providers.gradleProperty("gradleVersion").get()
123126
}

src/main/java/com/intellij/plugins/bodhi/pmd/ConfigOption.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import net.sourceforge.pmd.lang.LanguageRegistry;
44

5+
import java.util.Objects;
6+
57
/**
68
* Configuration options enumeration. Separation between key for persistent state and description to show in the UI.
79
*/
@@ -26,7 +28,7 @@ public enum ConfigOption {
2628
private final String defaultValue;
2729

2830
private static String latestSupportJavaVersionByPmd() {
29-
return LanguageRegistry.PMD.getLanguageById("java").getLatestVersion().getVersion();
31+
return Objects.requireNonNull(LanguageRegistry.PMD.getLanguageById("java")).getLatestVersion().getVersion();
3032
}
3133

3234
public static ConfigOption fromKey(String key) {

src/main/java/com/intellij/plugins/bodhi/pmd/PMDConfigurable.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.intellij.plugins.bodhi.pmd;
22

33
import com.intellij.openapi.options.Configurable;
4-
import com.intellij.openapi.options.ConfigurationException;
54
import com.intellij.openapi.project.Project;
65
import org.jetbrains.annotations.NonNls;
76
import org.jetbrains.annotations.Nullable;
@@ -10,12 +9,12 @@
109

1110
public class PMDConfigurable implements Configurable {
1211
private PMDConfigurationForm form;
13-
private PMDProjectComponent component;
12+
private final PMDProjectComponent component;
1413
private final Project project;
1514

1615
public PMDConfigurable(Project project) {
17-
this.project=project;
18-
component = project.getComponent(PMDProjectComponent.class);
16+
this.project = project;
17+
this.component = project.getService(PMDProjectComponent.class);
1918
}
2019

2120
public String getDisplayName() {
@@ -39,7 +38,7 @@ public boolean isModified() {
3938
return form != null && form.isModified(component);
4039
}
4140

42-
public void apply() throws ConfigurationException {
41+
public void apply() {
4342
if (form != null) {
4443
form.getDataFromUi(component);
4544
}

src/main/java/com/intellij/plugins/bodhi/pmd/PMDConfigurationForm.java

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.intellij.openapi.fileChooser.FileChooser;
55
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
66
import com.intellij.openapi.project.Project;
7+
import com.intellij.openapi.project.ProjectUtil;
8+
import com.intellij.openapi.ui.ComboBox;
79
import com.intellij.openapi.ui.DialogBuilder;
810
import com.intellij.openapi.ui.DialogWrapper;
911
import com.intellij.openapi.vfs.VfsUtilCore;
@@ -18,17 +20,18 @@
1820
import org.jetbrains.annotations.NotNull;
1921

2022
import javax.swing.*;
21-
import javax.swing.event.*;
23+
import javax.swing.event.ChangeEvent;
24+
import javax.swing.event.ChangeListener;
25+
import javax.swing.event.ListSelectionEvent;
26+
import javax.swing.event.ListSelectionListener;
2227
import javax.swing.table.DefaultTableModel;
2328
import javax.swing.table.TableModel;
2429
import java.awt.*;
25-
import java.awt.event.ActionEvent;
26-
import java.awt.event.ActionListener;
2730
import java.awt.event.KeyEvent;
2831
import java.io.File;
2932
import java.io.IOException;
30-
import java.util.List;
3133
import java.util.*;
34+
import java.util.List;
3235

3336
import static com.intellij.plugins.bodhi.pmd.actions.PreDefinedMenuGroup.RULESETS_FILENAMES_KEY;
3437
import static com.intellij.plugins.bodhi.pmd.actions.PreDefinedMenuGroup.RULESETS_PROPERTY_FILE;
@@ -174,7 +177,7 @@ private void modifyRuleSet(final String defaultValue, AnActionEvent e) {
174177
DialogBuilder db = new DialogBuilder(PMDUtil.getProjectComponent(e).getCurrentProject());
175178
db.addOkAction();
176179
db.addCancelAction();
177-
db.setTitle("Choose Custom RuleSet from Drop-down, File or paste URL");
180+
db.setTitle("Choose Custom RuleSet from Drop-down, File or Paste URL");
178181
final BrowsePanel panel = new BrowsePanel(defaultValue, db, project);
179182
db.show();
180183
//If ok is selected add the selected ruleset
@@ -189,7 +192,7 @@ private void modifyRuleSet(final String defaultValue, AnActionEvent e) {
189192
ruleSetPathJList.setSelectedIndex(listModel.getSize());
190193
}
191194
String err;
192-
if ((err = PMDResultCollector.isValidRuleSet(rulesPath)).length() > 0) {
195+
if (!(err = PMDResultCollector.isValidRuleSet(rulesPath)).isEmpty()) {
193196
String message = "The selected file/URL is not valid for PMD 7.";
194197
if (err.contains("XML validation errors occurred")) {
195198
message += " XML validation errors occurred.";
@@ -209,7 +212,7 @@ private void modifyRuleSet(final String defaultValue, AnActionEvent e) {
209212
listModel.set(selectedIndex, rulesPath.trim()); // trigger menu update
210213
return;
211214
}
212-
if (defaultValue != null && defaultValue.trim().length() > 0 && selectedIndex >= 0) {
215+
if (defaultValue != null && !defaultValue.trim().isEmpty() && selectedIndex >= 0) {
213216
listModel.set(selectedIndex, rulesPath);
214217
return;
215218
}
@@ -321,10 +324,10 @@ private void validateJavaVersion(String versionInput, int row, int column, Objec
321324
if (versionInput.equals(orig)) {
322325
return;
323326
}
324-
Language java = LanguageRegistry.PMD.getLanguageById("java");
327+
Language java = Objects.requireNonNull(LanguageRegistry.PMD.getLanguageById("java"));
325328
boolean isRegistered = java.hasVersion(versionInput);
326329
if (isRegistered) {
327-
String registeredVersion = java.getVersion(versionInput).getVersion();
330+
String registeredVersion = Objects.requireNonNull(java.getVersion(versionInput)).getVersion();
328331
optionsTable.setToolTipText("Java version " + registeredVersion);
329332
}
330333
else {
@@ -352,23 +355,21 @@ private void validateStatUrl(String urlInput, int row, int column, Object orig,
352355
return;
353356
}
354357
if (!urlInput.isEmpty()) {
355-
if (!PMDUtil.isValidUrl(urlInput)) {
356-
optionsTable.setToolTipText("Previous input - Invalid URL: '" + urlInput + "'");
357-
super.setValueAt(orig, row, column);
358-
isModified = origIsMod;
359-
}
360-
else {
358+
if (PMDUtil.isValidUrl(urlInput)) {
361359
String content = "{\"test connection\"}\n";
362360
String exportMsg = PMDJsonExportingRenderer.tryJsonExport(content, urlInput);
363361
if (!exportMsg.isEmpty()) {
364362
optionsTable.setToolTipText("Previous input - Failure for '" + urlInput + "': " + exportMsg);
365363
super.setValueAt(orig, row, column);
366364
isModified = origIsMod;
367-
}
368-
else {
365+
} else {
369366
isModified = true;
370367
optionsTable.setToolTipText(STAT_URL_MSG_SUCCESS);
371368
}
369+
} else {
370+
optionsTable.setToolTipText("Previous input - Invalid URL: '" + urlInput + "'");
371+
super.setValueAt(orig, row, column);
372+
isModified = origIsMod;
372373
}
373374
}
374375
}
@@ -476,44 +477,46 @@ public BrowsePanel(String defaultValue, final DialogBuilder db, final Project pr
476477
final Vector<String> elements = new Vector<>();
477478
elements.add(defaultValue);
478479
Set<String> ruleSetNames = PMDUtil.getValidKnownCustomRules().keySet();
479-
for (String ruleSetName : ruleSetNames) {
480-
elements.add(ruleSetName);
481-
}
480+
elements.addAll(ruleSetNames);
481+
482482
ComboBoxModel<String> model = new DefaultComboBoxModel<>(elements);
483483
model.setSelectedItem(defaultValue);
484-
pathComboBox = new JComboBox<>(model);
484+
pathComboBox = new ComboBox<>(model);
485485
pathComboBox.setEditable(true);
486486
pathComboBox.setMinimumSize(new Dimension(200, 26));
487487
pathComboBox.setMaximumSize(new Dimension(800, 28));
488488
pathComboBox.setPreferredSize(new Dimension(230, 28));
489489
add(pathComboBox);
490490
add(Box.createHorizontalStrut(5));
491+
JButton open = getJButton(project);
492+
add(open);
493+
add(Box.createVerticalGlue());
494+
db.setCenterPanel(this);
495+
}
496+
497+
private @NotNull JButton getJButton(Project project) {
491498
JButton open = new JButton("Browse");
492499
open.setPreferredSize(new Dimension(80, 20));
493-
open.addActionListener(new ActionListener() {
494-
public void actionPerformed(ActionEvent e) {
495-
final VirtualFile toSelect = project.getBaseDir();
496-
// file system access takes some time, IntelliJ sometimes gives an exception that
497-
// and EDT thread should not take long. Should be solved by using a BGT thread, but how?
498-
final FileChooserDescriptor descriptor = new FileChooserDescriptor(true, false, false, false, false, false);
499-
descriptor.withFileFilter(virtualFile -> virtualFile.getName().endsWith(".xml"));
500-
501-
final VirtualFile chosen = FileChooser.chooseFile(descriptor, BrowsePanel.this, project, toSelect);
502-
if (chosen != null) {
503-
final File newConfigFile = VfsUtilCore.virtualToIoFile(chosen);
504-
String ioFile = newConfigFile.getAbsolutePath();
505-
final Vector<String> elem = new Vector<>();
506-
elem.add(ioFile);
507-
ComboBoxModel<String> newModel = new DefaultComboBoxModel<>(elem);
508-
pathComboBox.setModel(newModel);
509-
pathComboBox.setSelectedItem(ioFile);
510-
pathComboBox.setEditable(false);
511-
}
500+
open.addActionListener(e -> {
501+
final VirtualFile toSelect = ProjectUtil.guessProjectDir(project);
502+
// file system access takes some time, IntelliJ sometimes gives an exception that
503+
// and EDT thread should not take long. Should be solved by using a BGT thread, but how?
504+
final FileChooserDescriptor descriptor = new FileChooserDescriptor(true, false, false, false, false, false);
505+
descriptor.withFileFilter(virtualFile -> virtualFile.getName().endsWith(".xml"));
506+
507+
final VirtualFile chosen = FileChooser.chooseFile(descriptor, this, project, toSelect);
508+
if (chosen != null) {
509+
final File newConfigFile = VfsUtilCore.virtualToIoFile(chosen);
510+
String ioFile = newConfigFile.getAbsolutePath();
511+
final Vector<String> elem = new Vector<>();
512+
elem.add(ioFile);
513+
ComboBoxModel<String> newModel = new DefaultComboBoxModel<>(elem);
514+
pathComboBox.setModel(newModel);
515+
pathComboBox.setSelectedItem(ioFile);
516+
pathComboBox.setEditable(false);
512517
}
513518
});
514-
add(open);
515-
add(Box.createVerticalGlue());
516-
db.setCenterPanel(this);
519+
return open;
517520
}
518521

519522
public String getText() {

src/main/java/com/intellij/plugins/bodhi/pmd/PMDInvoker.java

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@
1818
import com.intellij.openapi.wm.ToolWindowManager;
1919
import com.intellij.plugins.bodhi.pmd.core.PMDProgressRenderer;
2020
import com.intellij.plugins.bodhi.pmd.core.PMDResultCollector;
21-
import com.intellij.plugins.bodhi.pmd.handlers.PMDCheckinHandler;
22-
import com.intellij.plugins.bodhi.pmd.tree.*;
21+
import com.intellij.plugins.bodhi.pmd.tree.PMDRootNode;
22+
import com.intellij.plugins.bodhi.pmd.tree.PMDRuleSetEntryNode;
23+
import com.intellij.plugins.bodhi.pmd.tree.PMDRuleSetNode;
2324
import org.apache.commons.logging.Log;
2425
import org.apache.commons.logging.LogFactory;
2526
import org.jetbrains.annotations.NotNull;
2627

2728
import java.io.File;
2829
import java.util.LinkedList;
2930
import java.util.List;
31+
import java.util.Objects;
3032

3133
import static com.intellij.plugins.bodhi.pmd.filter.VirtualFileFilters.*;
3234

@@ -74,42 +76,36 @@ public static PMDInvoker getInstance() {
7476
*
7577
* @param actionEvent The action event that triggered run
7678
* @param ruleSetPaths The ruleSetPath(s) for rules to run
77-
* @param isCustomRuleSet Is it a custom ruleset or not.
7879
*/
79-
public void runPMD(AnActionEvent actionEvent, String ruleSetPaths, boolean isCustomRuleSet) {
80+
public void runPMD(AnActionEvent actionEvent, String ruleSetPaths) {
8081
//If no ruleSetPath is selected, nothing to do
81-
if (ruleSetPaths == null || ruleSetPaths.length() == 0) {
82+
if (ruleSetPaths == null || ruleSetPaths.isEmpty()) {
8283
return;
8384
}
8485
//Show the tool window
8586
PMDUtil.getProjectComponent(actionEvent).setupToolWindow();
8687

87-
Project project = actionEvent.getData(PlatformDataKeys.PROJECT);
88-
PMDProjectComponent projectComponent = project.getComponent(PMDProjectComponent.class);
88+
Project project = Objects.requireNonNull(actionEvent.getData(PlatformDataKeys.PROJECT));
89+
PMDProjectComponent projectComponent = project.getService(PMDProjectComponent.class);
8990
PMDResultPanel resultPanel = projectComponent.getResultPanel();
9091
PMDRootNode rootNode = resultPanel.getRootNode();
9192

92-
List<File> files = new LinkedList<File>();
93+
List<File> files = new LinkedList<>();
9394
if (actionEvent.getPlace().equals(ActionPlaces.PROJECT_VIEW_POPUP)
9495
|| actionEvent.getPlace().equals(ActionPlaces.SCOPE_VIEW_POPUP)
9596
|| actionEvent.getPlace().equals(ActionPlaces.CHANGES_VIEW_POPUP)
9697
|| actionEvent.getPlace().equals(ActionPlaces.MAIN_MENU)
9798
) {
9899

99100
//If selected by right-click on file/folder (s)
100-
VirtualFile[] selectedFiles;
101-
switch (actionEvent.getPlace()) {
102-
case ActionPlaces.CHANGES_VIEW_POPUP:
103-
selectedFiles = actionEvent.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY);
104-
break;
105-
case ActionPlaces.MAIN_MENU:
101+
@SuppressWarnings("SwitchStatementWithTooFewBranches")
102+
VirtualFile[] selectedFiles = switch (actionEvent.getPlace()) {
103+
case ActionPlaces.MAIN_MENU -> {
106104
VirtualFile[] contentRoots = ProjectRootManager.getInstance(project).getContentRoots();
107-
selectedFiles = VfsUtil.getCommonAncestors(contentRoots);
108-
break;
109-
default:
110-
selectedFiles = actionEvent.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY);
111-
break;
112-
}
105+
yield VfsUtil.getCommonAncestors(contentRoots);
106+
}
107+
default -> actionEvent.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY);
108+
};
113109

114110
if (selectedFiles == null || selectedFiles.length == 0) {
115111
//toolWindow.displayErrorMessage("Please select a file to process first");
@@ -137,25 +133,24 @@ public void runPMD(AnActionEvent actionEvent, String ruleSetPaths, boolean isCus
137133
}
138134

139135
//Got the files, start processing now
140-
processFiles(project, ruleSetPaths, files, isCustomRuleSet, projectComponent);
136+
processFiles(project, ruleSetPaths, files, projectComponent);
141137
}
142138

143139
/**
144140
* Runs PMD on given files.
145141
* @param project the project
146142
* @param ruleSetPaths The ruleSetPath(s) of rules to run
147143
* @param files The files on which to run
148-
* @param isCustomRuleSet Is it a custom ruleset or not.
149144
* @param projectComponent
150145
*/
151-
public void processFiles(Project project, final String ruleSetPaths, final List<File> files, final boolean isCustomRuleSet, final PMDProjectComponent projectComponent) {
146+
public void processFiles(Project project, final String ruleSetPaths, final List<File> files, final PMDProjectComponent projectComponent) {
152147
ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(PMDProjectComponent.TOOL_ID);
153148
if (toolWindow != null) {
154149
toolWindow.activate(null);
155150
}
156151

157152
//Save all files
158-
ApplicationManager.getApplication().saveAll();
153+
ApplicationManager.getApplication().saveSettings();
159154

160155
//Run PMD asynchronously
161156
ProgressManager.getInstance().run(new Task.Backgroundable(project, "Running PMD", true) {

0 commit comments

Comments
 (0)