19 |
20 | ## Requirements
21 | - JDK (version 17 or later)
22 | - VS Code (version 1.30.0 or later)
23 | - [Language Support for Java by Red Hat](https://marketplace.visualstudio.com/items?itemName=redhat.java)
24 |
25 | ## Quick Start
26 | 
27 |
28 | ## Features
29 |
30 | ### Set Checkstyle Configuration File
31 |
32 | 
33 |
34 | - To set the configuration file, Just Right click the `.xml` file and select `Set the Checkstyle Configuration File`.
35 |
36 | - You can also trigger the command **Checkstyle: Set Checkstyle Configuration File** to choose the configuration file in the File Explorer. The extension will automatically detect and list the Checkstyle configuration files in your workspace. Besides that, you will also see the two built-in configurations:
37 | - **Google's Check**
38 | - **Sun's Check**
39 |
40 | ### Set Checkstyle Version
41 |
42 | .gif)
43 |
44 | - You can use the command `Checkstyle: Set the Checkstyle Version` to manually set the Checkstyle version according to your project preferences. The extension will automatically download the required jar files if they do not exist locally.
45 |
46 | > Note: If you don't know which `Checkstlye` version is used by `Maven Checkstyle Plugin`, check [this table](https://maven.apache.org/plugins/maven-checkstyle-plugin/history.html).
47 |
48 | ### Check the Style and Fix the Violations
49 |
50 | 
51 |
52 | - When editing a Java file, the extension will check the file format and provide quick fixes if possible. You can click the  button in the editor to show the available quick fixes.
53 |
54 |
55 |
56 | ## Settings
57 | | Setting Name | Description | Default Value |
58 | |---|---|---|
59 | | `java.checkstyle.version` | Specify the Checkstyle Version. | `9.3` |
60 | | `java.checkstyle.configuration` | Specify the path of the Checkstyle configuration file. The path can either be a local file path or a URL. | `""` |
61 | | `java.checkstyle.properties` | Specify the customized properties used in the Checkstyle configuration. | `{}` |
62 | | `java.checkstyle.modules` | Specify the third-party modules used for Checkstyle. | `[]` |
63 | | `java.checkstyle.autocheck` | Specify if the extension will check the format automatically or not. | `true` |
64 |
65 | > Note: You can use the `${workspaceFolder}` to represent the path of the workspace folder of the file to be checked. For example:
66 |
67 | ```json
68 | "java.checkstyle.modules": [
69 | "${workspaceFolder}/src/main/resources/sevntu-checks-1.35.0.jar"
70 | ]
71 | ```
72 | or
73 | ```json
74 | "java.checkstyle.properties": {
75 | "basedir": "${workspaceFolder}"
76 | }
77 | ```
78 |
79 | ## Release Notes
80 |
81 | Refer to [CHANGELOG.md](https://github.com/jdneo/vscode-checkstyle/blob/master/CHANGELOG.md)
82 |
--------------------------------------------------------------------------------
/docs/gifs/demo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jdneo/vscode-checkstyle/326c5b44877133088f7160660ac5a24007352c27/docs/gifs/demo.gif
--------------------------------------------------------------------------------
/docs/gifs/liveLinting.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jdneo/vscode-checkstyle/326c5b44877133088f7160660ac5a24007352c27/docs/gifs/liveLinting.gif
--------------------------------------------------------------------------------
/docs/gifs/setConfiguration.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jdneo/vscode-checkstyle/326c5b44877133088f7160660ac5a24007352c27/docs/gifs/setConfiguration.gif
--------------------------------------------------------------------------------
/docs/gifs/setVersion(lower).gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jdneo/vscode-checkstyle/326c5b44877133088f7160660ac5a24007352c27/docs/gifs/setVersion(lower).gif
--------------------------------------------------------------------------------
/docs/imgs/btn_bulb.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jdneo/vscode-checkstyle/326c5b44877133088f7160660ac5a24007352c27/docs/imgs/btn_bulb.png
--------------------------------------------------------------------------------
/docs/imgs/quick_fix.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jdneo/vscode-checkstyle/326c5b44877133088f7160660ac5a24007352c27/docs/imgs/quick_fix.png
--------------------------------------------------------------------------------
/docs/imgs/set_config.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jdneo/vscode-checkstyle/326c5b44877133088f7160660ac5a24007352c27/docs/imgs/set_config.png
--------------------------------------------------------------------------------
/docs/imgs/set_version.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jdneo/vscode-checkstyle/326c5b44877133088f7160660ac5a24007352c27/docs/imgs/set_version.png
--------------------------------------------------------------------------------
/javaConfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "projects": ["./jdtls.ext/com.shengchen.checkstyle.runner"],
3 | "targetPlatform": "./jdtls.ext/com.shengchen.checkstyle.target/target.target"
4 | }
5 |
--------------------------------------------------------------------------------
/jdtls.ext/.mvn/wrapper/maven-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jdneo/vscode-checkstyle/326c5b44877133088f7160660ac5a24007352c27/jdtls.ext/.mvn/wrapper/maven-wrapper.jar
--------------------------------------------------------------------------------
/jdtls.ext/.mvn/wrapper/maven-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.checker/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.shengchen.checkstyle
8 | parent
9 | 1.4.2
10 |
11 | com.shengchen.checkstyle.checker
12 | jar
13 | ${base.name} :: Checker
14 |
15 |
16 | com.puppycrawl.tools
17 | checkstyle
18 | ${checkstyle-version}
19 | provided
20 |
21 |
22 | com.shengchen.checkstyle
23 | com.shengchen.checkstyle.runner
24 | ${project.parent.version}
25 | provided
26 |
27 |
28 |
29 |
30 |
31 | org.apache.maven.plugins
32 | maven-checkstyle-plugin
33 |
34 |
35 |
36 |
37 | 17
38 | 17
39 |
40 |
41 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.checker/src/main/java/com/shengchen/checkstyle/checker/CheckerListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) jdneo
3 |
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * any later version.
8 |
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 |
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see .
16 | */
17 |
18 | package com.shengchen.checkstyle.checker;
19 |
20 | import com.puppycrawl.tools.checkstyle.api.AuditEvent;
21 | import com.puppycrawl.tools.checkstyle.api.AuditListener;
22 | import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
23 | import com.shengchen.checkstyle.runner.api.CheckResult;
24 |
25 | import java.io.File;
26 | import java.lang.reflect.InvocationTargetException;
27 | import java.lang.reflect.Method;
28 | import java.util.ArrayList;
29 | import java.util.HashMap;
30 | import java.util.List;
31 | import java.util.Map;
32 |
33 | public class CheckerListener implements AuditListener {
34 |
35 | private Map> fileErrors = new HashMap<>();
36 |
37 | @Override
38 | public void addError(AuditEvent error) {
39 | final SeverityLevel severity = error.getSeverityLevel();
40 | if (severity.equals(SeverityLevel.IGNORE)) {
41 | return;
42 | }
43 | fileErrors.get(error.getFileName()).add(new CheckResult(
44 | error.getLine(),
45 | this.backCompatColumnIndex(error) + 1,
46 | error.getMessage(),
47 | severity.toString().toLowerCase(),
48 | error.getSourceName().substring(error.getSourceName().lastIndexOf('.') + 1)));
49 | }
50 |
51 | @Override
52 | public void fileStarted(AuditEvent event) {
53 | fileErrors.put(event.getFileName(), new ArrayList<>());
54 | }
55 |
56 | @Override
57 | public void fileFinished(AuditEvent arg0) {
58 | return;
59 | }
60 |
61 | @Override
62 | public void auditStarted(AuditEvent arg0) {
63 | return;
64 | }
65 |
66 | @Override
67 | public void auditFinished(AuditEvent arg0) {
68 | return;
69 | }
70 |
71 | @Override
72 | public void addException(AuditEvent arg0, Throwable arg1) {
73 | return;
74 | }
75 |
76 | public Map> getResult(List filesToCheck) {
77 | final Map> result = new HashMap<>();
78 | for (final File file: filesToCheck) {
79 | final String fileName = file.getAbsolutePath();
80 | result.put(fileName, fileErrors.get(fileName));
81 | }
82 | return result;
83 | }
84 |
85 | private int backCompatColumnIndex(final AuditEvent error) {
86 | // To keep backwards compatibility:
87 | try {
88 | // Try to use LocalizedMessage for Checkstyle versions <= v8.41.1
89 | final Method getLocalizedMessage = error.getClass().getMethod("getLocalizedMessage");
90 | final Object localizedMessage = getLocalizedMessage.invoke(error);
91 | final Method getColumnCharIndex = localizedMessage.getClass().getMethod("getColumnCharIndex");
92 |
93 | return (int) getColumnCharIndex.invoke(localizedMessage);
94 | } catch (NoSuchMethodException |
95 | SecurityException |
96 | IllegalAccessException |
97 | IllegalArgumentException |
98 | InvocationTargetException e1
99 | ) {
100 | // If LocalizedMessage does not exist, Checkstyle version >= v8.42 is used
101 | return error.getViolation().getColumnCharIndex();
102 | }
103 | }
104 | }
105 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.checker/src/main/java/com/shengchen/checkstyle/checker/CheckerService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) jdneo
3 |
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * any later version.
8 |
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 |
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see .
16 | */
17 |
18 | package com.shengchen.checkstyle.checker;
19 |
20 | import com.puppycrawl.tools.checkstyle.Checker;
21 | import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
22 | import com.puppycrawl.tools.checkstyle.Main;
23 | import com.puppycrawl.tools.checkstyle.PropertiesExpander;
24 | import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
25 | import com.shengchen.checkstyle.runner.api.CheckResult;
26 | import com.shengchen.checkstyle.runner.api.ICheckerService;
27 |
28 | import java.io.File;
29 | import java.io.IOException;
30 | import java.util.List;
31 | import java.util.Map;
32 | import java.util.Properties;
33 |
34 | public class CheckerService implements ICheckerService {
35 |
36 | private Checker checker = null;
37 | private CheckerListener listener = null;
38 |
39 | public void initialize() {
40 | checker = new Checker();
41 | listener = new CheckerListener();
42 | // reset the basedir if it is set so it won't get into the plugins way
43 | // of determining workspace resources from checkstyle reported file names, see
44 | // https://sourceforge.net/tracker/?func=detail&aid=2880044&group_id=80344&atid=559497
45 | checker.setBasedir(null);
46 | checker.setModuleClassLoader(Checker.class.getClassLoader());
47 | checker.addListener(listener);
48 | }
49 |
50 | public void dispose() {
51 | if (checker != null) {
52 | if (listener != null) {
53 | checker.removeListener(listener);
54 | listener = null;
55 | }
56 | checker.destroy();
57 | checker = null;
58 | }
59 | }
60 |
61 | @SuppressWarnings("unchecked")
62 | public void setConfiguration(Map config) throws IOException, CheckstyleException {
63 | final String configurationFsPath = (String) config.get("path");
64 | final Map properties = (Map) config.get("properties");
65 | final Properties checkstyleProperties = new Properties();
66 | checkstyleProperties.putAll(properties);
67 | checker.configure(ConfigurationLoader.loadConfiguration(
68 | configurationFsPath,
69 | new PropertiesExpander(checkstyleProperties)
70 | ));
71 | }
72 |
73 | public String getVersion() throws Exception {
74 | return Main.class.getPackage().getImplementationVersion();
75 | }
76 |
77 | public Map> checkCode(List filesToCheck, String charset) throws Exception {
78 | checker.setCharset(charset);
79 | checker.process(filesToCheck);
80 | return listener.getResult(filesToCheck);
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | com.shengchen.checkstyle.runner
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.eclipse.pde.ManifestBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.pde.SchemaBuilder
20 |
21 |
22 |
23 |
24 | org.eclipse.m2e.core.maven2Builder
25 |
26 |
27 |
28 |
29 |
30 | org.eclipse.m2e.core.maven2Nature
31 | org.eclipse.pde.PluginNature
32 | org.eclipse.jdt.core.javanature
33 |
34 |
35 |
36 | 1708997989680
37 |
38 | 30
39 |
40 | org.eclipse.core.resources.regexFilterMatcher
41 | node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/.settings/org.eclipse.core.resources.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | encoding/=UTF-8
3 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/.settings/org.eclipse.m2e.core.prefs:
--------------------------------------------------------------------------------
1 | activeProfiles=
2 | eclipse.preferences.version=1
3 | resolveWorkspaceProjects=true
4 | version=1
5 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/META-INF/MANIFEST.MF:
--------------------------------------------------------------------------------
1 | Manifest-Version: 1.0
2 | Bundle-ManifestVersion: 2
3 | Bundle-Name: com.shengchen.checkstyle.runner
4 | Bundle-SymbolicName: com.shengchen.checkstyle.runner;singleton:=true
5 | Bundle-Version: 1.4.2
6 | Bundle-Activator: com.shengchen.checkstyle.runner.CheckstylePlugin
7 | Bundle-RequiredExecutionEnvironment: JavaSE-17
8 | Import-Package: org.eclipse.jdt.core,
9 | org.eclipse.jdt.launching,
10 | org.osgi.framework;version="1.3.0"
11 | Bundle-ActivationPolicy: lazy
12 | Require-Bundle: org.eclipse.jdt.core,
13 | org.eclipse.lsp4j,
14 | org.eclipse.jdt.launching,
15 | org.eclipse.jdt.ls.core,
16 | org.eclipse.core.runtime,
17 | org.eclipse.core.resources,
18 | org.eclipse.jdt.core.manipulation,
19 | org.eclipse.text
20 | Bundle-ClassPath: .
21 | Automatic-Module-Name: com.shengchen.checkstyle.runner
22 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/build.properties:
--------------------------------------------------------------------------------
1 | source.. = src/main/java
2 | output.. = target/classes
3 | bin.includes = META-INF/,\
4 | .,\
5 | plugin.xml
6 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/plugin.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.shengchen.checkstyle
8 | parent
9 | 1.4.2
10 |
11 | com.shengchen.checkstyle.runner
12 | eclipse-plugin
13 | ${base.name} :: Plugin
14 |
15 |
16 |
17 | org.eclipse.tycho
18 | tycho-maven-plugin
19 | ${tycho-version}
20 | true
21 |
22 |
23 | org.apache.maven.plugins
24 | maven-checkstyle-plugin
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/src/main/java/com/shengchen/checkstyle/quickfix/FixableCheck.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) jdneo
3 |
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * any later version.
8 |
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 |
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see .
16 | */
17 |
18 | package com.shengchen.checkstyle.quickfix;
19 |
20 | public enum FixableCheck {
21 | // Blocks
22 | NEED_BRACE_CHECK("NeedBracesCheck"), AVOID_NESTED_BLOCKS_CHECK("AvoidNestedBlocksCheck"),
23 |
24 | // Coding
25 | FINAL_LOCAL_VARIABLE_CHECK("FinalLocalVariableCheck"), DEFAULT_COMES_LAST_CHECK("DefaultComesLastCheck"),
26 | EMPTY_STATEMENT_CHECK("EmptyStatementCheck"), MISSING_SWITCH_DEFAULT_CHECK("MissingSwitchDefaultCheck"),
27 | EXPLICIT_INITIALIZATION_CHECK("ExplicitInitializationCheck"), REQUIRE_THIS_CHECK("RequireThisCheck"),
28 | SIMPLIFY_BOOLEAN_RETURN_CHECK("SimplifyBooleanReturnCheck"), STRING_LITERAL_EQUALITY("StringLiteralEqualityCheck"),
29 | MULTIPLE_VARIABLE_DECLARATIONS_CHECK("MultipleVariableDeclarationsCheck"),
30 |
31 | // Design
32 | DESIGN_FOR_EXTENSION_CHECK("DesignForExtensionCheck"), FINAL_CLASS_CHECK("FinalClassCheck"),
33 |
34 | // Modifier
35 | MODIFIER_ORDER_CHECK("ModifierOrderCheck"), REDUNDANT_MODIFIER_CHECK("RedundantModifierCheck"),
36 |
37 | // Misc
38 | FINAL_PARAMETERS_CHECK("FinalParametersCheck"), UNCOMMENTED_MAIN_CHECK("UncommentedMainCheck"),
39 | UPPER_ELL_CHECK("UpperEllCheck"), ARRAY_TYPE_STYLE_CHECK("ArrayTypeStyleCheck");
40 |
41 | private final String check;
42 |
43 | FixableCheck(final String check) {
44 | this.check = check;
45 | }
46 |
47 | /*
48 | * (non-Javadoc)
49 | *
50 | * @see java.lang.Enum#toString()
51 | */
52 | @Override
53 | public String toString() {
54 | return check;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/src/main/java/com/shengchen/checkstyle/quickfix/QuickFixService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) jdneo
3 |
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * any later version.
8 |
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 |
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see .
16 | */
17 |
18 | package com.shengchen.checkstyle.quickfix;
19 |
20 | import com.shengchen.checkstyle.quickfix.blocks.AvoidNestedBlocksQuickFix;
21 | import com.shengchen.checkstyle.quickfix.blocks.NeedBracesQuickFix;
22 | import com.shengchen.checkstyle.quickfix.coding.DefaultComesLastQuickFix;
23 | import com.shengchen.checkstyle.quickfix.coding.EmptyStatementQuickFix;
24 | import com.shengchen.checkstyle.quickfix.coding.ExplicitInitializationQuickFix;
25 | import com.shengchen.checkstyle.quickfix.coding.FinalLocalVariableQuickFix;
26 | import com.shengchen.checkstyle.quickfix.coding.MissingSwitchDefaultQuickFix;
27 | import com.shengchen.checkstyle.quickfix.coding.MultipleVariableDeclarationsQuickFix;
28 | import com.shengchen.checkstyle.quickfix.coding.RequireThisQuickFix;
29 | import com.shengchen.checkstyle.quickfix.coding.SimplifyBooleanReturnQuickFix;
30 | import com.shengchen.checkstyle.quickfix.coding.StringLiteralEqualityQuickFix;
31 | import com.shengchen.checkstyle.quickfix.design.DesignForExtensionQuickFix;
32 | import com.shengchen.checkstyle.quickfix.design.FinalClassQuickFix;
33 | import com.shengchen.checkstyle.quickfix.misc.ArrayTypeStyleQuickFix;
34 | import com.shengchen.checkstyle.quickfix.misc.FinalParametersQuickFix;
35 | import com.shengchen.checkstyle.quickfix.misc.UncommentedMainQuickFix;
36 | import com.shengchen.checkstyle.quickfix.misc.UpperEllQuickFix;
37 | import com.shengchen.checkstyle.quickfix.modifier.ModifierOrderQuickFix;
38 | import com.shengchen.checkstyle.quickfix.modifier.RedundantModifierQuickFix;
39 | import com.shengchen.checkstyle.quickfix.utils.EditUtils;
40 | import com.shengchen.checkstyle.runner.api.IQuickFixService;
41 |
42 | import org.eclipse.jdt.core.ICompilationUnit;
43 | import org.eclipse.jdt.core.JavaModelException;
44 | import org.eclipse.jdt.core.dom.ASTParser;
45 | import org.eclipse.jdt.core.dom.CompilationUnit;
46 | import org.eclipse.jdt.internal.corext.dom.IASTSharedValues;
47 | import org.eclipse.jdt.ls.core.internal.JDTUtils;
48 | import org.eclipse.jface.text.BadLocationException;
49 | import org.eclipse.jface.text.Document;
50 | import org.eclipse.jface.text.IRegion;
51 | import org.eclipse.lsp4j.WorkspaceEdit;
52 | import org.eclipse.text.edits.TextEdit;
53 |
54 | import java.util.HashMap;
55 | import java.util.List;
56 | import java.util.Map;
57 |
58 | public class QuickFixService implements IQuickFixService {
59 |
60 | private final Map quickFixMap;
61 |
62 | public QuickFixService() {
63 | quickFixMap = new HashMap<>();
64 | quickFixMap.put(FixableCheck.FINAL_LOCAL_VARIABLE_CHECK.toString(), new FinalLocalVariableQuickFix());
65 | quickFixMap.put(FixableCheck.MODIFIER_ORDER_CHECK.toString(), new ModifierOrderQuickFix());
66 | quickFixMap.put(FixableCheck.REDUNDANT_MODIFIER_CHECK.toString(), new RedundantModifierQuickFix());
67 | quickFixMap.put(FixableCheck.NEED_BRACE_CHECK.toString(), new NeedBracesQuickFix());
68 | quickFixMap.put(FixableCheck.AVOID_NESTED_BLOCKS_CHECK.toString(), new AvoidNestedBlocksQuickFix());
69 | quickFixMap.put(FixableCheck.DESIGN_FOR_EXTENSION_CHECK.toString(), new DesignForExtensionQuickFix());
70 | quickFixMap.put(FixableCheck.FINAL_CLASS_CHECK.toString(), new FinalClassQuickFix());
71 | quickFixMap.put(FixableCheck.DEFAULT_COMES_LAST_CHECK.toString(), new DefaultComesLastQuickFix());
72 | quickFixMap.put(FixableCheck.EMPTY_STATEMENT_CHECK.toString(), new EmptyStatementQuickFix());
73 | quickFixMap.put(FixableCheck.MISSING_SWITCH_DEFAULT_CHECK.toString(), new MissingSwitchDefaultQuickFix());
74 | quickFixMap.put(FixableCheck.EXPLICIT_INITIALIZATION_CHECK.toString(), new ExplicitInitializationQuickFix());
75 | quickFixMap.put(FixableCheck.REQUIRE_THIS_CHECK.toString(), new RequireThisQuickFix());
76 | quickFixMap.put(FixableCheck.FINAL_PARAMETERS_CHECK.toString(), new FinalParametersQuickFix());
77 | quickFixMap.put(FixableCheck.UNCOMMENTED_MAIN_CHECK.toString(), new UncommentedMainQuickFix());
78 | quickFixMap.put(FixableCheck.UPPER_ELL_CHECK.toString(), new UpperEllQuickFix());
79 | quickFixMap.put(FixableCheck.ARRAY_TYPE_STYLE_CHECK.toString(), new ArrayTypeStyleQuickFix());
80 | quickFixMap.put(FixableCheck.SIMPLIFY_BOOLEAN_RETURN_CHECK.toString(), new SimplifyBooleanReturnQuickFix());
81 | quickFixMap.put(FixableCheck.STRING_LITERAL_EQUALITY.toString(), new StringLiteralEqualityQuickFix());
82 | quickFixMap.put(FixableCheck.MULTIPLE_VARIABLE_DECLARATIONS_CHECK.toString(),
83 | new MultipleVariableDeclarationsQuickFix());
84 | }
85 |
86 | public BaseQuickFix getQuickFix(String sourceName) {
87 | return quickFixMap.get(sourceName);
88 | }
89 |
90 | public WorkspaceEdit quickFix(
91 | String fileToCheckUri,
92 | List offsets,
93 | List sourceNames
94 | ) throws JavaModelException, IllegalArgumentException, BadLocationException {
95 | final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(fileToCheckUri);
96 | final Document document = new Document(unit.getSource());
97 | final ASTParser astParser = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL);
98 | astParser.setKind(ASTParser.K_COMPILATION_UNIT);
99 | astParser.setSource(unit);
100 | final CompilationUnit astRoot = (CompilationUnit) astParser.createAST(null);
101 | astRoot.recordModifications();
102 |
103 | for (int i = 0; i < offsets.size(); i++) {
104 | final int offset = offsets.get(i).intValue();
105 | final BaseQuickFix quickFix = getQuickFix(sourceNames.get(i));
106 | if (quickFix != null) {
107 | final IRegion lineInfo = document.getLineInformationOfOffset(offset);
108 | astRoot.accept(quickFix.getCorrectingASTVisitor(lineInfo, offset));
109 | }
110 | }
111 |
112 | final TextEdit edit = astRoot.rewrite(document, null);
113 | return EditUtils.convertToWorkspaceEdit(unit, edit);
114 | }
115 | }
116 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/src/main/java/com/shengchen/checkstyle/quickfix/blocks/AvoidNestedBlocksQuickFix.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) jdneo
3 |
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * any later version.
8 |
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 |
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see .
16 | */
17 |
18 | package com.shengchen.checkstyle.quickfix.blocks;
19 |
20 | import com.shengchen.checkstyle.quickfix.BaseQuickFix;
21 |
22 | import org.eclipse.jdt.core.dom.ASTNode;
23 | import org.eclipse.jdt.core.dom.ASTVisitor;
24 | import org.eclipse.jdt.core.dom.Block;
25 | import org.eclipse.jdt.core.dom.SwitchStatement;
26 | import org.eclipse.jface.text.IRegion;
27 |
28 | import java.util.List;
29 |
30 | public class AvoidNestedBlocksQuickFix extends BaseQuickFix {
31 |
32 | @Override
33 | public ASTVisitor getCorrectingASTVisitor(IRegion lineInfo, int markerStartOffset) {
34 | return new ASTVisitor() {
35 |
36 | @SuppressWarnings("unchecked")
37 | @Override
38 | public boolean visit(Block node) {
39 |
40 | if (containsPosition(lineInfo, node.getStartPosition())) {
41 |
42 | if (node.getParent() instanceof Block) {
43 |
44 | final List> statements = ((Block) node.getParent()).statements();
45 | final int index = statements.indexOf(node);
46 |
47 | statements.remove(node);
48 | statements.addAll(index, ASTNode.copySubtrees(node.getAST(), node.statements()));
49 |
50 | } else if (node.getParent() instanceof SwitchStatement) {
51 |
52 | final List> statements = ((SwitchStatement) node.getParent()).statements();
53 | final int index = statements.indexOf(node);
54 |
55 | statements.remove(node);
56 | statements.addAll(index, ASTNode.copySubtrees(node.getAST(), node.statements()));
57 | }
58 | }
59 | return true;
60 | }
61 | };
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/src/main/java/com/shengchen/checkstyle/quickfix/blocks/NeedBracesQuickFix.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) jdneo
3 |
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * any later version.
8 |
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 |
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see .
16 | */
17 |
18 | package com.shengchen.checkstyle.quickfix.blocks;
19 |
20 | import com.shengchen.checkstyle.quickfix.BaseQuickFix;
21 |
22 | import org.eclipse.jdt.core.dom.AST;
23 | import org.eclipse.jdt.core.dom.ASTNode;
24 | import org.eclipse.jdt.core.dom.ASTVisitor;
25 | import org.eclipse.jdt.core.dom.Block;
26 | import org.eclipse.jdt.core.dom.DoStatement;
27 | import org.eclipse.jdt.core.dom.ForStatement;
28 | import org.eclipse.jdt.core.dom.IfStatement;
29 | import org.eclipse.jdt.core.dom.Statement;
30 | import org.eclipse.jdt.core.dom.WhileStatement;
31 | import org.eclipse.jface.text.IRegion;
32 |
33 | public class NeedBracesQuickFix extends BaseQuickFix {
34 |
35 | @Override
36 | public ASTVisitor getCorrectingASTVisitor(IRegion lineInfo, int markerStartOffset) {
37 | return new ASTVisitor() {
38 | @Override
39 | public boolean visit(IfStatement node) {
40 |
41 | final int nodePos = node.getStartPosition();
42 | final int nodeEnd = nodePos + node.getLength();
43 | if ((nodePos >= lineInfo.getOffset() && nodePos <= (lineInfo.getOffset() + lineInfo.getLength())) ||
44 | (nodePos <= lineInfo.getOffset() && nodeEnd >= lineInfo.getOffset() + lineInfo.getLength())) {
45 | bracifyIfStatement(node);
46 | }
47 |
48 | return true;
49 | }
50 |
51 | // TODO: recursively block the statement like what we did in if-block
52 | @Override
53 | public boolean visit(ForStatement node) {
54 | if (containsPosition(lineInfo, node.getStartPosition())) {
55 | final Block block = createBracifiedCopy(node.getAST(), node.getBody());
56 | node.setBody(block);
57 | }
58 |
59 | return true;
60 | }
61 |
62 | @Override
63 | public boolean visit(DoStatement node) {
64 | if (containsPosition(lineInfo, node.getStartPosition())) {
65 | final Block block = createBracifiedCopy(node.getAST(), node.getBody());
66 | node.setBody(block);
67 | }
68 |
69 | return true;
70 | }
71 |
72 | @Override
73 | public boolean visit(WhileStatement node) {
74 | if (containsPosition(lineInfo, node.getStartPosition())) {
75 | final Block block = createBracifiedCopy(node.getAST(), node.getBody());
76 | node.setBody(block);
77 | }
78 |
79 | return true;
80 | }
81 |
82 | /**
83 | * Helper method to recursively bracify a if-statement.
84 | *
85 | * @param ifStatement the if statement
86 | */
87 | private void bracifyIfStatement(IfStatement ifStatement) {
88 |
89 | // change the then statement to a block if necessary
90 | if (!(ifStatement.getThenStatement() instanceof Block)) {
91 | if (ifStatement.getThenStatement() instanceof IfStatement) {
92 | bracifyIfStatement((IfStatement) ifStatement.getThenStatement());
93 | }
94 | final Block block = createBracifiedCopy(ifStatement.getAST(), ifStatement.getThenStatement());
95 | ifStatement.setThenStatement(block);
96 | }
97 |
98 | // check the else statement if it is a block
99 | final Statement elseStatement = ifStatement.getElseStatement();
100 | if (elseStatement != null && !(elseStatement instanceof Block)) {
101 |
102 | // in case the else statement is an further if statement
103 | // (else if)
104 | // do the recursion
105 | if (elseStatement instanceof IfStatement) {
106 | bracifyIfStatement((IfStatement) elseStatement);
107 | } else {
108 | // change the else statement to a block
109 | // Block block = ifStatement.getAST().newBlock();
110 | // block.statements().add(ASTNode.copySubtree(block.getAST(),
111 | // elseStatement));
112 | final Block block = createBracifiedCopy(ifStatement.getAST(), ifStatement.getElseStatement());
113 | ifStatement.setElseStatement(block);
114 | }
115 | }
116 | }
117 |
118 | @SuppressWarnings("unchecked")
119 | private Block createBracifiedCopy(AST ast, Statement body) {
120 | final Block block = ast.newBlock();
121 | block.statements().add(ASTNode.copySubtree(block.getAST(), body));
122 | return block;
123 | }
124 | };
125 | }
126 | }
127 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/src/main/java/com/shengchen/checkstyle/quickfix/coding/DefaultComesLastQuickFix.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) jdneo
3 |
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * any later version.
8 |
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 |
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see .
16 | */
17 |
18 | package com.shengchen.checkstyle.quickfix.coding;
19 |
20 | import com.shengchen.checkstyle.quickfix.BaseQuickFix;
21 |
22 | import org.eclipse.jdt.core.dom.ASTNode;
23 | import org.eclipse.jdt.core.dom.ASTVisitor;
24 | import org.eclipse.jdt.core.dom.SwitchCase;
25 | import org.eclipse.jdt.core.dom.SwitchStatement;
26 | import org.eclipse.jface.text.IRegion;
27 |
28 | import java.util.ArrayList;
29 | import java.util.List;
30 |
31 | public class DefaultComesLastQuickFix extends BaseQuickFix {
32 |
33 | @Override
34 | public ASTVisitor getCorrectingASTVisitor(IRegion lineInfo, int markerStartOffset) {
35 | return new ASTVisitor() {
36 |
37 | @SuppressWarnings("unchecked")
38 | @Override
39 | public boolean visit(SwitchCase node) {
40 |
41 | if (containsPosition(lineInfo, node.getStartPosition())) {
42 |
43 | if (node.isDefault() && !isLastSwitchCase(node)) {
44 | final SwitchStatement switchStatement = (SwitchStatement) node.getParent();
45 |
46 | final List defaultCaseStatements = new ArrayList<>();
47 | defaultCaseStatements.add(node);
48 |
49 | // collect all statements belonging to the default case
50 | final int defaultStatementIndex = switchStatement.statements().indexOf(node);
51 | for (int i = defaultStatementIndex + 1; i < switchStatement.statements().size(); i++) {
52 | final ASTNode tmpNode = (ASTNode) switchStatement.statements().get(i);
53 |
54 | if (!(tmpNode instanceof SwitchCase)) {
55 | defaultCaseStatements.add(tmpNode);
56 | } else {
57 | break;
58 | }
59 | }
60 |
61 | // move the statements to the end of the statement list
62 | switchStatement.statements().removeAll(defaultCaseStatements);
63 | switchStatement.statements().addAll(defaultCaseStatements);
64 | }
65 | }
66 | return true;
67 | }
68 |
69 | private boolean isLastSwitchCase(SwitchCase switchCase) {
70 |
71 | final SwitchStatement switchStatement = (SwitchStatement) switchCase.getParent();
72 |
73 | // collect all statements belonging to the default case
74 | final int defaultStatementIndex = switchStatement.statements().indexOf(switchCase);
75 | for (int i = defaultStatementIndex + 1; i < switchStatement.statements().size(); i++) {
76 | final ASTNode tmpNode = (ASTNode) switchStatement.statements().get(i);
77 |
78 | if (tmpNode instanceof SwitchCase) {
79 | return false;
80 | }
81 | }
82 | return true;
83 | }
84 | };
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/src/main/java/com/shengchen/checkstyle/quickfix/coding/EmptyStatementQuickFix.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) jdneo
3 |
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * any later version.
8 |
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 |
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see .
16 | */
17 |
18 | package com.shengchen.checkstyle.quickfix.coding;
19 |
20 | import com.shengchen.checkstyle.quickfix.BaseQuickFix;
21 |
22 | import org.eclipse.jdt.core.dom.ASTVisitor;
23 | import org.eclipse.jdt.core.dom.ChildPropertyDescriptor;
24 | import org.eclipse.jdt.core.dom.EmptyStatement;
25 | import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
26 | import org.eclipse.jface.text.IRegion;
27 |
28 | public class EmptyStatementQuickFix extends BaseQuickFix {
29 |
30 | @Override
31 | public ASTVisitor getCorrectingASTVisitor(IRegion lineInfo, int markerStartOffset) {
32 | return new ASTVisitor() {
33 | @Override
34 | public boolean visit(EmptyStatement node) {
35 | if (containsPosition(lineInfo, node.getStartPosition())) {
36 |
37 | // early exit if the statement is mandatory, e.g. only
38 | // statement in a for-statement without block
39 | final StructuralPropertyDescriptor p = node.getLocationInParent();
40 | if (p.isChildProperty() && ((ChildPropertyDescriptor) p).isMandatory()) {
41 | return false;
42 | }
43 |
44 | node.delete();
45 | }
46 | return false;
47 | }
48 | };
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/src/main/java/com/shengchen/checkstyle/quickfix/coding/ExplicitInitializationQuickFix.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) jdneo
3 |
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * any later version.
8 |
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 |
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see .
16 | */
17 |
18 | package com.shengchen.checkstyle.quickfix.coding;
19 |
20 | import com.shengchen.checkstyle.quickfix.BaseQuickFix;
21 |
22 | import org.eclipse.jdt.core.dom.ASTVisitor;
23 | import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
24 | import org.eclipse.jface.text.IRegion;
25 |
26 | public class ExplicitInitializationQuickFix extends BaseQuickFix {
27 |
28 | @Override
29 | public ASTVisitor getCorrectingASTVisitor(IRegion lineInfo, int markerStartOffset) {
30 | return new ASTVisitor() {
31 |
32 | @Override
33 | public boolean visit(final VariableDeclarationFragment node) {
34 | if (containsPosition(node, markerStartOffset)) {
35 | node.getInitializer().delete();
36 | }
37 | return false;
38 | }
39 | };
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/src/main/java/com/shengchen/checkstyle/quickfix/coding/FinalLocalVariableQuickFix.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) jdneo
3 |
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * any later version.
8 |
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 |
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see .
16 | */
17 |
18 | package com.shengchen.checkstyle.quickfix.coding;
19 |
20 | import com.shengchen.checkstyle.quickfix.BaseQuickFix;
21 |
22 | import org.eclipse.jdt.core.dom.ASTVisitor;
23 | import org.eclipse.jdt.core.dom.Modifier;
24 | import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
25 | import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
26 | import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
27 | import org.eclipse.jface.text.IRegion;
28 |
29 | public class FinalLocalVariableQuickFix extends BaseQuickFix {
30 | @Override
31 | @SuppressWarnings("unchecked")
32 | public ASTVisitor getCorrectingASTVisitor(IRegion lineInfo, int markerStartOffset) {
33 | return new ASTVisitor() {
34 |
35 | @Override
36 | public boolean visit(SingleVariableDeclaration node) {
37 | if (containsPosition(node, markerStartOffset) && !Modifier.isFinal(node.getModifiers())) {
38 | final Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
39 | node.modifiers().add(finalModifier);
40 | }
41 | return true;
42 | }
43 |
44 | @Override
45 | public boolean visit(VariableDeclarationStatement node) {
46 | if (containsPosition(node, markerStartOffset) && !Modifier.isFinal(node.getModifiers())) {
47 | final Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
48 | node.modifiers().add(finalModifier);
49 | }
50 | return true;
51 | }
52 | };
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/src/main/java/com/shengchen/checkstyle/quickfix/coding/MissingSwitchDefaultQuickFix.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) jdneo
3 |
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * any later version.
8 |
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 |
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see .
16 | */
17 |
18 | package com.shengchen.checkstyle.quickfix.coding;
19 |
20 | import com.shengchen.checkstyle.quickfix.BaseQuickFix;
21 |
22 | import org.eclipse.jdt.core.dom.ASTVisitor;
23 | import org.eclipse.jdt.core.dom.SwitchCase;
24 | import org.eclipse.jdt.core.dom.SwitchStatement;
25 | import org.eclipse.jface.text.IRegion;
26 |
27 | public class MissingSwitchDefaultQuickFix extends BaseQuickFix {
28 |
29 | @Override
30 | public ASTVisitor getCorrectingASTVisitor(IRegion lineInfo, int markerStartOffset) {
31 | return new ASTVisitor() {
32 |
33 | @SuppressWarnings("unchecked")
34 | @Override
35 | public boolean visit(SwitchStatement node) {
36 | if (containsPosition(lineInfo, node.getStartPosition())) {
37 | final SwitchCase defNode = node.getAST().newSwitchCase();
38 | defNode.setExpression(null);
39 | node.statements().add(defNode);
40 | node.statements().add(node.getAST().newBreakStatement());
41 | }
42 | return true; // also visit children
43 | }
44 | };
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/src/main/java/com/shengchen/checkstyle/quickfix/coding/MultipleVariableDeclarationsQuickFix.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) jdneo
3 |
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * any later version.
8 |
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 |
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see .
16 | */
17 |
18 | package com.shengchen.checkstyle.quickfix.coding;
19 |
20 | import com.shengchen.checkstyle.quickfix.BaseQuickFix;
21 |
22 | import org.eclipse.jdt.core.dom.ASTNode;
23 | import org.eclipse.jdt.core.dom.ASTVisitor;
24 | import org.eclipse.jdt.core.dom.FieldDeclaration;
25 | import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
26 | import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
27 | import org.eclipse.jface.text.IRegion;
28 |
29 | import java.util.ArrayList;
30 | import java.util.Collection;
31 | import java.util.List;
32 |
33 | public class MultipleVariableDeclarationsQuickFix extends BaseQuickFix {
34 |
35 | @Override
36 | public ASTVisitor getCorrectingASTVisitor(IRegion lineInfo, int markerStartOffset) {
37 | return new ASTVisitor() {
38 |
39 | @SuppressWarnings("unchecked")
40 | @Override
41 | public boolean visit(FieldDeclaration node) {
42 | if (containsPosition(node, markerStartOffset) && node.fragments().size() > 1) {
43 | final Collection replacements = new ArrayList<>();
44 | for (final VariableDeclarationFragment fragment :
45 | (List) node.fragments()) {
46 | final FieldDeclaration newFieldDeclaration = node.getAST().newFieldDeclaration(copy(fragment));
47 | newFieldDeclaration.setType(copy(node.getType()));
48 | newFieldDeclaration.modifiers().addAll(copy(node.modifiers()));
49 | if (replacements.isEmpty() && node.getJavadoc() != null) {
50 | newFieldDeclaration.setJavadoc(copy(node.getJavadoc()));
51 | }
52 |
53 | replacements.add(newFieldDeclaration);
54 | }
55 |
56 | replace(node, replacements);
57 | }
58 | return true;
59 | }
60 |
61 | @SuppressWarnings("unchecked")
62 | @Override
63 | public boolean visit(VariableDeclarationStatement node) {
64 | if (containsPosition(node, markerStartOffset) && node.fragments().size() > 1) {
65 | final Collection newVariableDeclarations = new ArrayList<>();
66 | final List fragments =
67 | (List) node.fragments();
68 |
69 | /* We keep the existing statement, as statements are associated with comments */
70 | for (int i = 1; i < fragments.size(); i++) {
71 | final VariableDeclarationFragment fragment = fragments.get(i);
72 | final VariableDeclarationStatement newVariableDeclarationStatement =
73 | node.getAST().newVariableDeclarationStatement(copy(fragment));
74 | newVariableDeclarationStatement.setType(copy(node.getType()));
75 | newVariableDeclarationStatement.modifiers().addAll(copy(node.modifiers()));
76 |
77 | newVariableDeclarations.add(newVariableDeclarationStatement);
78 | }
79 |
80 | /* Remove the additional fragments that we've duplicated */
81 | while (fragments.size() > 1) {
82 | fragments.remove(1);
83 | }
84 |
85 | append(node, newVariableDeclarations);
86 | }
87 | return true;
88 | }
89 | };
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/src/main/java/com/shengchen/checkstyle/quickfix/coding/RequireThisQuickFix.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) jdneo
3 |
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * any later version.
8 |
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 |
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see .
16 | */
17 |
18 | package com.shengchen.checkstyle.quickfix.coding;
19 |
20 | import com.shengchen.checkstyle.quickfix.BaseQuickFix;
21 |
22 | import org.eclipse.jdt.core.dom.AST;
23 | import org.eclipse.jdt.core.dom.ASTNode;
24 | import org.eclipse.jdt.core.dom.ASTVisitor;
25 | import org.eclipse.jdt.core.dom.Expression;
26 | import org.eclipse.jdt.core.dom.FieldAccess;
27 | import org.eclipse.jdt.core.dom.FieldDeclaration;
28 | import org.eclipse.jdt.core.dom.MethodDeclaration;
29 | import org.eclipse.jdt.core.dom.MethodInvocation;
30 | import org.eclipse.jdt.core.dom.SimpleName;
31 | import org.eclipse.jdt.core.dom.ThisExpression;
32 | import org.eclipse.jdt.core.dom.TypeDeclaration;
33 | import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
34 | import org.eclipse.jface.text.IRegion;
35 |
36 | import java.util.List;
37 |
38 | public class RequireThisQuickFix extends BaseQuickFix {
39 |
40 | @Override
41 | public ASTVisitor getCorrectingASTVisitor(IRegion lineInfo, int markerStartOffset) {
42 | return new ASTVisitor() {
43 |
44 | @Override
45 | public boolean visit(final SimpleName node) {
46 | if (containsPosition(node, markerStartOffset)) {
47 | replace(node, findFieldReplacement(node, node, 0));
48 | }
49 | return false;
50 | }
51 |
52 | @Override
53 | public boolean visit(final MethodInvocation node) {
54 | if (containsPosition(node, markerStartOffset)) {
55 | replace(node, findMethodReplacement(node.getName(), node, node, 0));
56 | }
57 | return false;
58 | }
59 |
60 | private Expression findFieldReplacement(final SimpleName name, final ASTNode node, int typeLevel) {
61 |
62 | int level = typeLevel;
63 |
64 | final ASTNode parent = node.getParent();
65 | if (parent instanceof TypeDeclaration) {
66 | level++;
67 | final TypeDeclaration type = (TypeDeclaration) parent;
68 | for (final FieldDeclaration fieldDeclaration : type.getFields()) {
69 | @SuppressWarnings("unchecked")
70 | final List fragments = fieldDeclaration.fragments();
71 | for (final VariableDeclarationFragment fragment : fragments) {
72 | if (name.getFullyQualifiedName().equals(fragment.getName().getFullyQualifiedName())) {
73 | return createFieldAccessReplacement(level == 1 ? null : type, name);
74 | }
75 | }
76 | }
77 | }
78 | return findFieldReplacement(name, parent, level);
79 | }
80 |
81 | private FieldAccess createFieldAccessReplacement(final TypeDeclaration type, final SimpleName name) {
82 | final AST ast = name.getAST();
83 | final FieldAccess fieldAccess = ast.newFieldAccess();
84 | final ThisExpression thisExpr = ast.newThisExpression();
85 | if (type != null) {
86 | thisExpr.setQualifier(copy(type.getName()));
87 | }
88 | fieldAccess.setExpression(thisExpr);
89 | fieldAccess.setName(copy(name));
90 | return fieldAccess;
91 | }
92 |
93 | private Expression findMethodReplacement(final SimpleName name, ASTNode contextNode,
94 | final MethodInvocation node, int typeLevel) {
95 |
96 | int level = typeLevel;
97 |
98 | final ASTNode parent = contextNode.getParent();
99 | if (parent instanceof TypeDeclaration) {
100 | level++;
101 | final TypeDeclaration type = (TypeDeclaration) parent;
102 | for (final MethodDeclaration methodDeclaration : type.getMethods()) {
103 | if (name.getFullyQualifiedName().equals(methodDeclaration.getName().getFullyQualifiedName())) {
104 | return createMethodInvocationReplacement(level == 1 ? null : type, node);
105 | }
106 | }
107 | }
108 | return findMethodReplacement(name, parent, node, level);
109 | }
110 |
111 | private Expression createMethodInvocationReplacement(final TypeDeclaration type,
112 | MethodInvocation origMethodInvocation) {
113 | final AST ast = origMethodInvocation.getAST();
114 | final MethodInvocation methodInvocation = copy(origMethodInvocation);
115 | final ThisExpression thisExpr = ast.newThisExpression();
116 | if (type != null) {
117 | thisExpr.setQualifier(copy(type.getName()));
118 | }
119 | methodInvocation.setExpression(thisExpr);
120 | return methodInvocation;
121 | }
122 |
123 | };
124 | }
125 | }
126 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/src/main/java/com/shengchen/checkstyle/quickfix/coding/SimplifyBooleanReturnQuickFix.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) jdneo
3 |
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * any later version.
8 |
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 |
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see .
16 | */
17 |
18 | package com.shengchen.checkstyle.quickfix.coding;
19 |
20 | import com.shengchen.checkstyle.quickfix.BaseQuickFix;
21 |
22 | import org.eclipse.jdt.core.dom.AST;
23 | import org.eclipse.jdt.core.dom.ASTVisitor;
24 | import org.eclipse.jdt.core.dom.Block;
25 | import org.eclipse.jdt.core.dom.BooleanLiteral;
26 | import org.eclipse.jdt.core.dom.Expression;
27 | import org.eclipse.jdt.core.dom.FieldAccess;
28 | import org.eclipse.jdt.core.dom.IfStatement;
29 | import org.eclipse.jdt.core.dom.MethodInvocation;
30 | import org.eclipse.jdt.core.dom.ParenthesizedExpression;
31 | import org.eclipse.jdt.core.dom.PrefixExpression;
32 | import org.eclipse.jdt.core.dom.PrefixExpression.Operator;
33 | import org.eclipse.jdt.core.dom.QualifiedName;
34 | import org.eclipse.jdt.core.dom.ReturnStatement;
35 | import org.eclipse.jdt.core.dom.SimpleName;
36 | import org.eclipse.jdt.core.dom.Statement;
37 | import org.eclipse.jdt.core.dom.SuperFieldAccess;
38 | import org.eclipse.jdt.core.dom.SuperMethodInvocation;
39 | import org.eclipse.jdt.core.dom.ThisExpression;
40 | import org.eclipse.jface.text.IRegion;
41 |
42 | import java.util.Arrays;
43 | import java.util.Collection;
44 | import java.util.List;
45 |
46 | public class SimplifyBooleanReturnQuickFix extends BaseQuickFix {
47 |
48 | /**
49 | * If the condition is of one of these expression types, the parantheses are not
50 | * necessary when negated. I.e the replacement can be written as
51 | * !condition instead of !(condition).
52 | */
53 | private static final Collection> OMIT_PARANETHESES_CLASSES = Arrays.asList(
54 | BooleanLiteral.class, FieldAccess.class, MethodInvocation.class, QualifiedName.class, SimpleName.class,
55 | ParenthesizedExpression.class, SuperFieldAccess.class, SuperMethodInvocation.class, ThisExpression.class);
56 |
57 | @Override
58 | public ASTVisitor getCorrectingASTVisitor(IRegion lineInfo, int markerStartOffset) {
59 |
60 | return new ASTVisitor() {
61 |
62 | @Override
63 | public boolean visit(final IfStatement node) {
64 | if (containsPosition(node, markerStartOffset)) {
65 |
66 | final Boolean isThenStatementTrue = isReturnStatementTrue(node.getThenStatement());
67 |
68 | if (isThenStatementTrue == null) {
69 | // the AST structure of the if statement is not as expected
70 | return true;
71 | }
72 |
73 | final Expression condition = removeNotFromCondition(node.getExpression());
74 | final boolean isNotCondition = condition != node.getExpression();
75 |
76 | final ReturnStatement replacement;
77 | if (isThenStatementTrue ^ isNotCondition) {
78 | // create replacement: return condition;
79 | replacement = node.getAST().newReturnStatement();
80 | replacement.setExpression(copy(condition));
81 |
82 | } else {
83 | // create replacement: return !(condition);
84 | final AST ast = node.getAST();
85 | replacement = ast.newReturnStatement();
86 | final PrefixExpression not = ast.newPrefixExpression();
87 | not.setOperator(Operator.NOT);
88 | if (omitParantheses(condition)) {
89 | not.setOperand(copy(condition));
90 | } else {
91 | final ParenthesizedExpression parentheses = ast.newParenthesizedExpression();
92 | parentheses.setExpression(copy(condition));
93 | not.setOperand(parentheses);
94 | }
95 | replacement.setExpression(not);
96 | }
97 | replace(node, replacement);
98 |
99 | }
100 | return true;
101 | }
102 |
103 | private Boolean isReturnStatementTrue(final Statement node) {
104 | if (node instanceof ReturnStatement) {
105 | final Expression expression = ((ReturnStatement) node).getExpression();
106 | if (expression instanceof BooleanLiteral) {
107 | return ((BooleanLiteral) expression).booleanValue();
108 | }
109 | } else if (node instanceof Block) {
110 | // the return statement might be wrapped in a block statement
111 | @SuppressWarnings("unchecked")
112 | final List statements = ((Block) node).statements();
113 | if (statements.size() > 0) {
114 | return isReturnStatementTrue(statements.get(0));
115 | }
116 | }
117 | return null;
118 | }
119 |
120 | private Expression removeNotFromCondition(final Expression condition) {
121 | if (condition instanceof PrefixExpression) {
122 | final PrefixExpression prefix = (PrefixExpression) condition;
123 | if (PrefixExpression.Operator.NOT.equals(prefix.getOperator())) {
124 | return prefix.getOperand();
125 | }
126 | }
127 | return condition;
128 | }
129 |
130 | private boolean omitParantheses(final Expression condition) {
131 | return OMIT_PARANETHESES_CLASSES.contains(condition.getClass());
132 | }
133 |
134 | };
135 | }
136 |
137 | }
138 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/src/main/java/com/shengchen/checkstyle/quickfix/coding/StringLiteralEqualityQuickFix.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) jdneo
3 |
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * any later version.
8 |
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 |
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see .
16 | */
17 |
18 | package com.shengchen.checkstyle.quickfix.coding;
19 |
20 | import com.shengchen.checkstyle.quickfix.BaseQuickFix;
21 |
22 | import org.eclipse.jdt.core.dom.ASTNode;
23 | import org.eclipse.jdt.core.dom.ASTVisitor;
24 | import org.eclipse.jdt.core.dom.Expression;
25 | import org.eclipse.jdt.core.dom.InfixExpression;
26 | import org.eclipse.jdt.core.dom.MethodInvocation;
27 | import org.eclipse.jdt.core.dom.PrefixExpression;
28 | import org.eclipse.jdt.core.dom.StringLiteral;
29 | import org.eclipse.jface.text.IRegion;
30 |
31 | import java.lang.reflect.InvocationTargetException;
32 | import java.lang.reflect.Method;
33 | import java.util.List;
34 |
35 | public class StringLiteralEqualityQuickFix extends BaseQuickFix {
36 |
37 | @Override
38 | public ASTVisitor getCorrectingASTVisitor(IRegion lineInfo, int markerStartOffset) {
39 | return new ASTVisitor() {
40 |
41 | @SuppressWarnings("unchecked")
42 | @Override
43 | public boolean visit(InfixExpression node) {
44 |
45 | if (containsPosition(lineInfo, node.getStartPosition())) {
46 |
47 | StringLiteral literal = null;
48 | Expression otherOperand = null;
49 |
50 | if (node.getLeftOperand() instanceof StringLiteral) {
51 | literal = (StringLiteral) node.getLeftOperand();
52 | otherOperand = node.getRightOperand();
53 | } else if (node.getRightOperand() instanceof StringLiteral) {
54 | literal = (StringLiteral) node.getRightOperand();
55 | otherOperand = node.getLeftOperand();
56 | } else {
57 | return true;
58 | }
59 |
60 | Expression replacementNode = null;
61 |
62 | final MethodInvocation equalsInvocation = node.getAST().newMethodInvocation();
63 | equalsInvocation.setName(node.getAST().newSimpleName("equals")); //$NON-NLS-1$
64 | equalsInvocation.setExpression((Expression) ASTNode.copySubtree(node.getAST(), literal));
65 | equalsInvocation.arguments().add(ASTNode.copySubtree(node.getAST(), otherOperand));
66 |
67 | // if the string was compared with != create a not
68 | // expression
69 | if (node.getOperator().equals(InfixExpression.Operator.NOT_EQUALS)) {
70 | final PrefixExpression prefixExpression = node.getAST().newPrefixExpression();
71 | prefixExpression.setOperator(PrefixExpression.Operator.NOT);
72 | prefixExpression.setOperand(equalsInvocation);
73 | replacementNode = prefixExpression;
74 | } else {
75 | replacementNode = equalsInvocation;
76 | }
77 |
78 | replaceNode(node, replacementNode);
79 | }
80 | return true;
81 | }
82 |
83 | /**
84 | * Replaces the given node with the replacement node (using reflection since I
85 | * am not aware of a proper API to do this).
86 | *
87 | * @param node the node to replace
88 | * @param replacementNode the replacement
89 | */
90 | private void replaceNode(ASTNode node, ASTNode replacementNode) {
91 | try {
92 | if (node.getLocationInParent().isChildProperty()) {
93 |
94 | final String property = node.getLocationInParent().getId();
95 |
96 | final String capitalizedProperty = property.substring(0, 1).toUpperCase() +
97 | property.substring(1);
98 | final String setterMethodName = "set" + capitalizedProperty;
99 |
100 | Class> testClass = node.getClass();
101 |
102 | while (testClass != null) {
103 |
104 | try {
105 | final Method setterMethod = node.getParent().getClass().getMethod(setterMethodName,
106 | testClass);
107 | setterMethod.invoke(node.getParent(), replacementNode);
108 | break;
109 | } catch (NoSuchMethodException e) {
110 | testClass = testClass.getSuperclass();
111 | }
112 | }
113 |
114 | } else if (node.getLocationInParent().isChildListProperty()) {
115 | final Method listMethod = node.getParent().getClass()
116 | .getMethod(node.getLocationInParent().getId(), (Class>[]) null);
117 | @SuppressWarnings("unchecked")
118 | final List list = (List) listMethod.invoke(node.getParent(), (Object[]) null);
119 | list.set(list.indexOf(node), replacementNode);
120 | }
121 | } catch (InvocationTargetException e) {
122 | // TODO: log
123 | } catch (IllegalAccessException e) {
124 | // TODO: log
125 | } catch (NoSuchMethodException e) {
126 | // TODO: log
127 | }
128 | }
129 | };
130 | }
131 |
132 | }
133 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/src/main/java/com/shengchen/checkstyle/quickfix/design/DesignForExtensionQuickFix.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) jdneo
3 |
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * any later version.
8 |
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 |
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see .
16 | */
17 |
18 | package com.shengchen.checkstyle.quickfix.design;
19 |
20 | import com.shengchen.checkstyle.quickfix.BaseQuickFix;
21 | import com.shengchen.checkstyle.quickfix.modifier.ModifierOrderQuickFix;
22 |
23 | import org.eclipse.jdt.core.dom.ASTNode;
24 | import org.eclipse.jdt.core.dom.ASTVisitor;
25 | import org.eclipse.jdt.core.dom.MethodDeclaration;
26 | import org.eclipse.jdt.core.dom.Modifier;
27 | import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
28 | import org.eclipse.jface.text.IRegion;
29 |
30 | import java.util.List;
31 |
32 | public class DesignForExtensionQuickFix extends BaseQuickFix {
33 |
34 | /** The length of the javadoc comment declaration. */
35 | private static final int JAVADOC_COMMENT_LENGTH = 6;
36 |
37 | @Override
38 | public ASTVisitor getCorrectingASTVisitor(IRegion lineInfo, int markerStartOffset) {
39 | return new ASTVisitor() {
40 |
41 | @SuppressWarnings("unchecked")
42 | @Override
43 | public boolean visit(MethodDeclaration node) {
44 | // recalculate start position because optional javadoc is mixed
45 | // into the original start position
46 | final int pos = node.getStartPosition() +
47 | (node.getJavadoc() != null ? node.getJavadoc().getLength() + JAVADOC_COMMENT_LENGTH : 0);
48 | if (containsPosition(lineInfo, pos)) {
49 |
50 | if (!Modifier.isFinal(node.getModifiers())) {
51 |
52 | final Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
53 | node.modifiers().add(finalModifier);
54 |
55 | // reorder modifiers into their correct order
56 | final List reorderedModifiers = ModifierOrderQuickFix
57 | .reorderModifiers(node.modifiers());
58 | node.modifiers().clear();
59 | node.modifiers().addAll(reorderedModifiers);
60 | }
61 | }
62 | return true;
63 | }
64 | };
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/src/main/java/com/shengchen/checkstyle/quickfix/design/FinalClassQuickFix.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) jdneo
3 |
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * any later version.
8 |
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 |
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see .
16 | */
17 |
18 | package com.shengchen.checkstyle.quickfix.design;
19 |
20 | import com.shengchen.checkstyle.quickfix.BaseQuickFix;
21 | import com.shengchen.checkstyle.quickfix.modifier.ModifierOrderQuickFix;
22 |
23 | import org.eclipse.jdt.core.dom.ASTVisitor;
24 | import org.eclipse.jdt.core.dom.Modifier;
25 | import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
26 | import org.eclipse.jdt.core.dom.TypeDeclaration;
27 | import org.eclipse.jface.text.IRegion;
28 |
29 | import java.util.List;
30 |
31 | public class FinalClassQuickFix extends BaseQuickFix {
32 |
33 | /** The length of the javadoc comment declaration. */
34 | private static final int JAVADOC_COMMENT_LENGTH = 6;
35 |
36 | @Override
37 | public ASTVisitor getCorrectingASTVisitor(IRegion lineInfo, int markerStartOffset) {
38 | return new ASTVisitor() {
39 |
40 | @SuppressWarnings("unchecked")
41 | @Override
42 | public boolean visit(TypeDeclaration node) {
43 | // recalculate start position because optional javadoc is mixed
44 | // into the original start position
45 | final int pos = node.getStartPosition() +
46 | (node.getJavadoc() != null ? node.getJavadoc().getLength() + JAVADOC_COMMENT_LENGTH : 0);
47 | if (containsPosition(lineInfo, pos)) {
48 |
49 | if (!Modifier.isFinal(node.getModifiers())) {
50 |
51 | final Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
52 | node.modifiers().add(finalModifier);
53 |
54 | // reorder modifiers into their correct order
55 | final List> reorderedModifiers = ModifierOrderQuickFix.reorderModifiers(node.modifiers());
56 | node.modifiers().clear();
57 | node.modifiers().addAll(reorderedModifiers);
58 | }
59 | }
60 | return true;
61 | }
62 | };
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/src/main/java/com/shengchen/checkstyle/quickfix/misc/FinalParametersQuickFix.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) jdneo
3 |
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * any later version.
8 |
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 |
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see .
16 | */
17 |
18 | package com.shengchen.checkstyle.quickfix.misc;
19 |
20 | import com.shengchen.checkstyle.quickfix.BaseQuickFix;
21 |
22 | import org.eclipse.jdt.core.dom.ASTVisitor;
23 | import org.eclipse.jdt.core.dom.Modifier;
24 | import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
25 | import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
26 | import org.eclipse.jface.text.IRegion;
27 |
28 | public class FinalParametersQuickFix extends BaseQuickFix {
29 |
30 | @Override
31 | public ASTVisitor getCorrectingASTVisitor(IRegion lineInfo, int markerStartOffset) {
32 | return new ASTVisitor() {
33 |
34 | @SuppressWarnings("unchecked")
35 | @Override
36 | public boolean visit(SingleVariableDeclaration node) {
37 | if (containsPosition(node, markerStartOffset) && !Modifier.isFinal(node.getModifiers())) {
38 | if (!Modifier.isFinal(node.getModifiers())) {
39 | final Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
40 | node.modifiers().add(finalModifier);
41 | }
42 | }
43 | return true;
44 | }
45 | };
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/src/main/java/com/shengchen/checkstyle/quickfix/misc/UncommentedMainQuickFix.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) jdneo
3 |
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * any later version.
8 |
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 |
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see .
16 | */
17 |
18 | package com.shengchen.checkstyle.quickfix.misc;
19 |
20 | import com.shengchen.checkstyle.quickfix.BaseQuickFix;
21 |
22 | import org.eclipse.jdt.core.dom.ASTVisitor;
23 | import org.eclipse.jdt.core.dom.MethodDeclaration;
24 | import org.eclipse.jface.text.IRegion;
25 |
26 | public class UncommentedMainQuickFix extends BaseQuickFix {
27 |
28 | /** The length of the javadoc comment declaration. */
29 | private static final int JAVADOC_COMMENT_LENGTH = 6;
30 |
31 | @Override
32 | public ASTVisitor getCorrectingASTVisitor(IRegion lineInfo, int markerStartOffset) {
33 | return new ASTVisitor() {
34 |
35 | @Override
36 | public boolean visit(MethodDeclaration node) {
37 | // recalculate start position because optional javadoc is mixed
38 | // into the original start position
39 | final int pos = node.getStartPosition() +
40 | (node.getJavadoc() != null ? node.getJavadoc().getLength() + JAVADOC_COMMENT_LENGTH : 0);
41 | if (containsPosition(lineInfo, pos) && node.getName().getFullyQualifiedName().equals("main")) {
42 | node.delete();
43 | }
44 | return true;
45 | }
46 | };
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/src/main/java/com/shengchen/checkstyle/quickfix/misc/UpperEllQuickFix.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) jdneo
3 |
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * any later version.
8 |
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 |
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see .
16 | */
17 |
18 | package com.shengchen.checkstyle.quickfix.misc;
19 |
20 | import com.shengchen.checkstyle.quickfix.BaseQuickFix;
21 |
22 | import org.eclipse.jdt.core.dom.ASTVisitor;
23 | import org.eclipse.jdt.core.dom.NumberLiteral;
24 | import org.eclipse.jface.text.IRegion;
25 |
26 | public class UpperEllQuickFix extends BaseQuickFix {
27 |
28 | @Override
29 | public ASTVisitor getCorrectingASTVisitor(IRegion lineInfo, int markerStartOffset) {
30 | return new ASTVisitor() {
31 |
32 | @Override
33 | public boolean visit(NumberLiteral node) {
34 | if (containsPosition(node, markerStartOffset)) {
35 |
36 | String token = node.getToken();
37 | if (token.endsWith("l")) { //$NON-NLS-1$
38 | token = token.replace('l', 'L');
39 | node.setToken(token);
40 | }
41 | }
42 | return true;
43 | }
44 | };
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/jdtls.ext/com.shengchen.checkstyle.runner/src/main/java/com/shengchen/checkstyle/quickfix/modifier/ModifierOrderQuickFix.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) jdneo
3 |
4 | * This program is free software: you can redistribute it and/or modify
5 | * it under the terms of the GNU General Public License as published by
6 | * the Free Software Foundation, either version 3 of the License, or
7 | * any later version.
8 |
9 | * This program is distributed in the hope that it will be useful,
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | * GNU General Public License for more details.
13 |
14 | * You should have received a copy of the GNU General Public License
15 | * along with this program. If not, see .
16 | */
17 |
18 | package com.shengchen.checkstyle.quickfix.modifier;
19 |
20 | import com.shengchen.checkstyle.quickfix.BaseQuickFix;
21 |
22 | import org.eclipse.jdt.core.dom.ASTNode;
23 | import org.eclipse.jdt.core.dom.ASTVisitor;
24 | import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration;
25 | import org.eclipse.jdt.core.dom.BodyDeclaration;
26 | import org.eclipse.jdt.core.dom.FieldDeclaration;
27 | import org.eclipse.jdt.core.dom.MethodDeclaration;
28 | import org.eclipse.jdt.core.dom.Modifier;
29 | import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
30 | import org.eclipse.jdt.core.dom.TypeDeclaration;
31 | import org.eclipse.jface.text.IRegion;
32 |
33 | import java.util.ArrayList;
34 | import java.util.Arrays;
35 | import java.util.Collections;
36 | import java.util.Comparator;
37 | import java.util.Iterator;
38 | import java.util.List;
39 | import java.util.stream.Collectors;
40 |
41 | public class ModifierOrderQuickFix extends BaseQuickFix {
42 |
43 | /**
44 | * List containing modifier keywords in the order proposed by Java Language
45 | * specification, sections 8.1.1, 8.3.1 and 8.4.3.
46 | */
47 | private static final List