diff --git a/.gitignore b/.gitignore index 7a28623d77dde33709f918e67b23b8932ffc80ec..76f27ae0ada5595d6236b979f1d885c485b5fdaa 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ build .venv __pycache__ devkitdependencies +.gradle component/DevKitTester/devkit_tester/config/log.ini component/DevKitTester/devkit_tester/config/devkit_distribute.yaml diff --git a/ci/maven3/settings.xml b/ci/maven3/settings.xml index 9b916d37386dccce0e4d1c98db4590da81fe845e..66617be45530ed9ee6e345c9300a373e8f0c5f7a 100644 --- a/ci/maven3/settings.xml +++ b/ci/maven3/settings.xml @@ -19,30 +19,6 @@ specific language governing permissions and limitations under the License. --> - @@ -158,16 +134,9 @@ under the License. --> - maven-default-http-blocker - external:http:* - Pseudo repository to mirror external repositories initially using HTTP. - http://0.0.0.0/ - true - - - jboss + huaweicloud * - https://repository.jboss.org/nexus/content/repositories/thirdparty-releases/ + https://mirrors.huaweicloud.com/repository/maven/ diff --git a/component/CodeInspector/code_inspector/build.gradle b/component/CodeInspector/code_inspector/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..00a8ffffa0c9347f112e7a6b6e59cbf4d4cdb4a6 --- /dev/null +++ b/component/CodeInspector/code_inspector/build.gradle @@ -0,0 +1,75 @@ +plugins { + id "application" + id 'distribution' //打包tar包用到的插件 +} + +application { + mainClass = 'com.huawei.devkit.code.inspector.CodeInspector' +} + +startScripts { + doLast { + unixScript.text = unixScript.text + .replace("DEFAULT_JVM_OPTS=\"\"", + "DEFAULT_JVM_OPTS=\"-DCODE_INSPECTOR_APP_HOME=\$APP_HOME " + + "-Dlog4j.configuration=file:\${APP_HOME}/config/log4j.properties\"" + ) + windowsScript.delete() + } +} + +distributions { + main { + var version = "1.0.0" + distributionBaseName = "code-inspector-${version}" + contents { + into("/code-inspector-${version}") { + from("src/main/content") { + include(["logs", "data", "config", "config/*.xml", "config/*.properties"]) + exclude("**/.gitkeep") + } + } + } + } +} + +distZip { + enabled = false +} + +distTar { + compression = Compression.GZIP + archiveExtension = "tar.gz" +} + + +compileJava { + sourceCompatibility JavaVersion.VERSION_11 + targetCompatibility JavaVersion.VERSION_11 +} + +repositories { + maven { + url "https://repo.huaweicloud.com/repository/maven/" + } +} + +dependencies { + implementation 'commons-codec:commons-codec:1.13' + implementation('com.puppycrawl.tools:checkstyle:10.17.0') { + exclude module: 'commons-codec' + } + implementation 'org.xerial:sqlite-jdbc:3.45.1.0' + implementation 'org.mybatis:mybatis:3.5.13' + implementation "org.slf4j:slf4j-api:1.7.32" + implementation "org.slf4j:slf4j-log4j12:1.7.32" + testImplementation 'org.mockito:mockito-core:4.11.0' + compileOnly 'org.projectlombok:lombok:1.18.28' + annotationProcessor 'org.projectlombok:lombok:1.18.28' + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0' + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0' +} + +test { + useJUnitPlatform() +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/main/content/config/devkit_checkstyle.xml b/component/CodeInspector/code_inspector/src/main/content/config/devkit_checkstyle.xml new file mode 100644 index 0000000000000000000000000000000000000000..aa6bd878b4486197222011aae873b5d521848e31 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/main/content/config/devkit_checkstyle.xml @@ -0,0 +1,745 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/main/content/config/log4j.properties b/component/CodeInspector/code_inspector/src/main/content/config/log4j.properties new file mode 100644 index 0000000000000000000000000000000000000000..b83e3c1b3ce7a7c5fbbfb52a7e66104287273f94 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/main/content/config/log4j.properties @@ -0,0 +1,21 @@ +# +# Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. +# +log4j.rootLogger=DEBUG,console,rollingFile +# console +log4j.appender.console=org.apache.log4j.ConsoleAppender +log4j.appender.console.Threshold=WARN +log4j.appender.console.ImmediateFlush=true +log4j.appender.console.Target=System.err +log4j.appender.console.layout=org.apache.log4j.PatternLayout +log4j.appender.console.layout.ConversionPattern=[%d{YYYY-MM-dd HH:mm:ss}] [%p] - %l - %m%n +# rollingFile +log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender +log4j.appender.rollingFile.Threshold=INFO +log4j.appender.rollingFile.ImmediateFlush=true +log4j.appender.rollingFile.Append=true +log4j.appender.rollingFile.File=${CODE_INSPECTOR_APP_HOME}/logs/code-inspector.log +log4j.appender.rollingFile.MaxFileSize=10MB +log4j.appender.rollingFile.MaxBackupIndex=50 +log4j.appender.rollingFile.layout=org.apache.log4j.PatternLayout +log4j.appender.rollingFile.layout.ConversionPattern=[%d{YYYY-MM-dd HH:mm:ss}] [%p] - %l - %m%n diff --git a/component/CodeInspector/code_inspector/src/main/content/data/.gitkeep b/component/CodeInspector/code_inspector/src/main/content/data/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/component/CodeInspector/code_inspector/src/main/content/logs/.gitkeep b/component/CodeInspector/code_inspector/src/main/content/logs/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/CodeInspector.java b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/CodeInspector.java new file mode 100644 index 0000000000000000000000000000000000000000..f0a6774ca7b9f0460739e5c0cb8846ee758b3eee --- /dev/null +++ b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/CodeInspector.java @@ -0,0 +1,43 @@ +package com.huawei.devkit.code.inspector; + +import com.huawei.devkit.code.inspector.entity.CliOptions; +import com.huawei.devkit.code.inspector.perload.DataBasePreLoad; +import com.huawei.devkit.code.inspector.utils.PropertiesUtils; +import com.huawei.devkit.code.inspector.wrappers.CheckStyleWrapper; +import lombok.extern.slf4j.Slf4j; +import picocli.CommandLine; + +import java.util.Locale; +import java.util.Properties; + +@Slf4j +public class CodeInspector { + + public static void main(String[] args) { + try { + final CliOptions cliOptions = new CliOptions(); + final CommandLine commandLine = new CommandLine(cliOptions); + commandLine.setUsageHelpWidth(CliOptions.HELP_WIDTH); + commandLine.setCaseInsensitiveEnumValuesAllowed(true); + CommandLine.ParseResult parseResult = commandLine.parseArgs(args); + if (!CommandLine.printHelpIfRequested(parseResult)) { + log.info("start enter log"); + Properties properties = PropertiesUtils.loadProperties("config.properties"); + PropertiesUtils.configAndUpdate(properties); + if (cliOptions.getConfigurationFile() == null || cliOptions.getConfigurationFile().isEmpty()) { + cliOptions.setConfigurationFile(PropertiesUtils.ROOT_DIR + "/config/devkit_checkstyle.xml"); + } + DataBasePreLoad.preload(properties); + Locale.setDefault(Locale.SIMPLIFIED_CHINESE); + CheckStyleWrapper.checkStyle(cliOptions); + } + } catch (CommandLine.ParameterException ex) { + log.error("error", ex); + System.err.println(ex.getMessage()); + System.err.println("Usage: code-inspector [OPTIONS]... FILES..."); + System.err.println("Try 'code-inspector --help' for more information."); + } catch (Exception ex) { + log.error("error", ex); + } + } +} diff --git a/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/dao/DataBaseMapper.java b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/dao/DataBaseMapper.java new file mode 100644 index 0000000000000000000000000000000000000000..ff685c57c8fba4d5392ce2ac3fdb5c92b8bb5e88 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/dao/DataBaseMapper.java @@ -0,0 +1,15 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +package com.huawei.devkit.code.inspector.dao; + +/** + * DataBaseMapper + * + * @since 2024-07-11 + */ +public interface DataBaseMapper { + void createTable(); + +} diff --git a/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/dao/RuleViolationInfoMapper.java b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/dao/RuleViolationInfoMapper.java new file mode 100644 index 0000000000000000000000000000000000000000..e0a8a581983cc03320801e3ea36135552042b9d2 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/dao/RuleViolationInfoMapper.java @@ -0,0 +1,19 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +package com.huawei.devkit.code.inspector.dao; + +import com.huawei.devkit.code.inspector.entity.RuleViolationInfo; + +import java.util.List; + +/** + * RuleViolationInfoMapper + * + * @since 2024-07-11 + */ +public interface RuleViolationInfoMapper { + + int addRuleViolationInfos(List infoList); +} diff --git a/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/entity/CliOptions.java b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/entity/CliOptions.java new file mode 100644 index 0000000000000000000000000000000000000000..63ecf30b8e2fcefb06bfa23994a02d4e2b53d315 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/entity/CliOptions.java @@ -0,0 +1,123 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +package com.huawei.devkit.code.inspector.entity; + +import lombok.Data; +import picocli.CommandLine; +import picocli.CommandLine.Command; + +import java.io.File; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** + * CliOptions + * + * @since 2024-07-15 + */ +@Data +@Command(name = "code_inspector", version = "1.0.0", mixinStandardHelpOptions = true) +public class CliOptions { + /** + * Width of CLI help option. + */ + public static final int HELP_WIDTH = 120; + + /** + * The default number of threads to use for checker and the tree walker. + */ + private static final int DEFAULT_THREAD_COUNT = 1; + + /** + * Default output format. + */ + private static final OutputStyle DEFAULT_OUTPUT_FORMAT = OutputStyle.PLAIN; + + + /** + * The checker threads number. + * This option has been skipped for CLI options intentionally. + */ + public static final int CHECKER_THREADS_NUMBER = DEFAULT_THREAD_COUNT; + + /** + * The tree walker threads number. + */ + public static final int TREE_WALKER_THREADS_NUMBER = DEFAULT_THREAD_COUNT; + + /** + * List of file to validate. + */ + @CommandLine.Parameters(arity = "1..*", description = "One or more source files to verify") + private List files; + + /** + * Config file location. + */ + @CommandLine.Option(names = "-c", description = "Specifies the location of the file that defines" + + " the configuration modules. The location can either be a filesystem location" + + ", or a name passed to the ClassLoader.getResource() method.") + private String configurationFile; + + /** + * Output file location. + */ + @CommandLine.Option(names = "-o", description = "Sets the output file. Defaults to stdout.") + private Path outputPath; + + /** + * Properties file location. + */ + private File propertiesFile; + + /** + * Output format. + */ + @CommandLine.Option(names = "-f", + description = "Specifies the output format. Valid values: " + + "${COMPLETION-CANDIDATES} for XMLLogger, SarifLogger, CustomJsonFormatterLogger," + + "and DefaultLogger respectively. Defaults to ${DEFAULT-VALUE}.") + private OutputStyle format = DEFAULT_OUTPUT_FORMAT; + + /** + * Option that allows users to specify a list of paths to exclude. + */ + @CommandLine.Option(names = {"-e", "--exclude"}, + description = "Directory/file to exclude from CodeInspector. The path can be the " + + "full, absolute path, or relative to the current path. Multiple " + + "excludes are allowed.") + private List exclude = new ArrayList<>(); + + /** + * Option that allows users to specify a regex of paths to exclude. + */ + @CommandLine.Option(names = {"-x", "--exclude-regexp"}, + description = "Directory/file pattern to exclude from CodeInspector. Multiple " + + "excludes are allowed.") + private List excludeRegex = new ArrayList<>(); + + /** + * Switch whether to execute ignored modules or not. + */ + private boolean executeIgnoredModules; + + /** + * Gets the list of exclusions provided through the command line arguments. + * + * @return List of exclusion patterns. + */ + public List getExclusions() { + final List result = exclude.stream() + .map(File::getAbsolutePath) + .map(Pattern::quote) + .map(pattern -> Pattern.compile("^" + pattern + "$")) + .collect(Collectors.toCollection(ArrayList::new)); + result.addAll(excludeRegex); + return result; + } +} diff --git a/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/entity/OutputStyle.java b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/entity/OutputStyle.java new file mode 100644 index 0000000000000000000000000000000000000000..3130558698ad3e27380640e1022911bdb6bcd3a5 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/entity/OutputStyle.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +package com.huawei.devkit.code.inspector.entity; + +import com.huawei.devkit.code.inspector.listener.CustomJsonFormatterLogger; +import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean; +import com.puppycrawl.tools.checkstyle.DefaultLogger; +import com.puppycrawl.tools.checkstyle.XMLLogger; +import com.puppycrawl.tools.checkstyle.api.AuditListener; + +import java.io.IOException; +import java.io.OutputStream; +import java.util.Locale; + +/** + * OutputStyle + * + * @since 2024-07-15 + */ +public enum OutputStyle { + /** + * XML output format. + */ + XML, + /** + * Json output format + */ + JSON, + /** + * Plain output format. + */ + PLAIN; + + /** + * Returns a new AuditListener for this OutputFormat. + * + * @param out the output stream + * @param options the output stream options + * @return a new AuditListener for this OutputFormat + * @throws IOException if there is any IO exception during logger initialization + */ + public AuditListener createListener( + OutputStream out, + AbstractAutomaticBean.OutputStreamOptions options) throws IOException { + final AuditListener result; + if (this == XML) { + result = new XMLLogger(out, options); + } else if (this == JSON) { + result = new CustomJsonFormatterLogger(out, options); + } else { + result = new DefaultLogger(out, options); + } + return result; + } + + /** + * Returns the name in lowercase. + * + * @return the enum name in lowercase + */ + @Override + public String toString() { + return name().toLowerCase(Locale.ROOT); + } +} diff --git a/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/entity/RuleViolationInfo.java b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/entity/RuleViolationInfo.java new file mode 100644 index 0000000000000000000000000000000000000000..a663ce800ba0f8debbd0b5dec8a6a58edf32539a --- /dev/null +++ b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/entity/RuleViolationInfo.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +package com.huawei.devkit.code.inspector.entity; + +import lombok.Builder; +import lombok.Data; + +/** + * RuleViolationInfo + * + * @since 2024-07-11 + */ +@Data +@Builder +public class RuleViolationInfo { + private int id; + private String ruleId; + private int filePathHash; + private String filePath; + private String line; + private int lineno; + private String message; + private String mergeId; + private boolean shielded; + private boolean commitRequestToShield; + private long time; + private int level; +} diff --git a/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/listener/CustomJsonFormatterLogger.java b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/listener/CustomJsonFormatterLogger.java new file mode 100644 index 0000000000000000000000000000000000000000..e65175d1f24adf3d2eeaab362e7d977703a0bb4c --- /dev/null +++ b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/listener/CustomJsonFormatterLogger.java @@ -0,0 +1,185 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +package com.huawei.devkit.code.inspector.listener; + +import com.huawei.devkit.code.inspector.utils.PropertiesUtils; +import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean.OutputStreamOptions; +import com.puppycrawl.tools.checkstyle.SarifLogger; +import com.puppycrawl.tools.checkstyle.api.AuditEvent; +import com.puppycrawl.tools.checkstyle.api.AuditListener; +import lombok.extern.slf4j.Slf4j; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; + +/** + * DataBaseListener + * + * @since 2024-07-11 + */ +@Slf4j +public class CustomJsonFormatterLogger implements AuditListener { + /** + * The placeholder for message. + */ + private static final String MESSAGE_PLACEHOLDER = "${message}"; + + /** + * The placeholder for severity level. + */ + private static final String SEVERITY_LEVEL_PLACEHOLDER = "${severityLevel}"; + + /** + * The placeholder for uri. + */ + private static final String URI_PLACEHOLDER = "${uri}"; + + /** + * The placeholder for line. + */ + private static final String LINE_PLACEHOLDER = "${line}"; + + /** + * The placeholder for column. + */ + private static final String COLUMN_PLACEHOLDER = "${column}"; + + /** + * The placeholder for rule id. + */ + private static final String RULE_ID_PLACEHOLDER = "${ruleId}"; + + /** + * The placeholder for results. + */ + private static final String RESULTS_PLACEHOLDER = "${results}"; + /** + * Helper writer that allows easy encoding and printing. + */ + private final PrintWriter writer; + + /** + * Close output stream in auditFinished. + */ + private final boolean closeStream; + /** + * Content for the entire report. + */ + private final String report; + + /** + * Content for result representing an error with source line and column. + */ + private final String resultLineColumn; + + /** + * Content for result representing an error with source line only. + */ + private final String resultLineOnly; + + /** + * Content for result representing an error with filename only and without source location. + */ + private final String resultFileOnly; + + /** + * Content for result representing an error without filename or location. + */ + private final String resultErrorOnly; + + private final List results = new ArrayList<>(); + + public CustomJsonFormatterLogger(OutputStream outputStream, + OutputStreamOptions outputStreamOptions) throws IOException { + writer = new PrintWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)); + closeStream = outputStreamOptions == OutputStreamOptions.CLOSE; + report = PropertiesUtils.readResource("templates/CustomReport.template"); + resultLineColumn = + PropertiesUtils.readResource("templates/ResultLineColumn.template"); + resultLineOnly = + PropertiesUtils.readResource("templates/ResultLineOnly.template"); + resultFileOnly = + PropertiesUtils.readResource("templates/ResultFileOnly.template"); + resultErrorOnly = + PropertiesUtils.readResource("templates/ResultErrorOnly.template"); + + } + + + @Override + public void auditStarted(AuditEvent event) { + + } + + @Override + public void auditFinished(AuditEvent event) { + final String rendered = report.replace(RESULTS_PLACEHOLDER, String.join(",\n", results)); + writer.print(rendered); + if (closeStream) { + writer.close(); + } else { + writer.flush(); + } + } + + @Override + public void fileStarted(AuditEvent event) { + + } + + @Override + public void fileFinished(AuditEvent event) { + + } + + @Override + public void addError(AuditEvent event) { + if (event.getColumn() > 0) { + results.add(resultLineColumn + .replace(SEVERITY_LEVEL_PLACEHOLDER, event.getSeverityLevel().getName()) + .replace(URI_PLACEHOLDER, event.getFileName()) + .replace(COLUMN_PLACEHOLDER, Integer.toString(event.getColumn())) + .replace(LINE_PLACEHOLDER, Integer.toString(event.getLine())) + .replace(MESSAGE_PLACEHOLDER, SarifLogger.escape(event.getMessage())) + .replace(RULE_ID_PLACEHOLDER, + event.getModuleId() != null ? event.getModuleId() : event.getViolation().getKey()) + ); + } else { + results.add(resultLineOnly + .replace(SEVERITY_LEVEL_PLACEHOLDER, event.getSeverityLevel().getName()) + .replace(URI_PLACEHOLDER, event.getFileName()) + .replace(LINE_PLACEHOLDER, Integer.toString(event.getLine())) + .replace(MESSAGE_PLACEHOLDER, SarifLogger.escape(event.getMessage())) + .replace(RULE_ID_PLACEHOLDER, + event.getModuleId() != null ? event.getModuleId() : event.getViolation().getKey()) + ); + } + } + + @Override + public void addException(AuditEvent event, Throwable throwable) { + final StringWriter stringWriter = new StringWriter(); + final PrintWriter printer = new PrintWriter(stringWriter); + throwable.printStackTrace(printer); + if (event.getFileName() == null) { + results.add(resultErrorOnly + .replace(SEVERITY_LEVEL_PLACEHOLDER, event.getSeverityLevel().getName()) + .replace(MESSAGE_PLACEHOLDER, SarifLogger.escape(stringWriter.toString())) + ); + } else { + results.add(resultFileOnly + .replace(SEVERITY_LEVEL_PLACEHOLDER, event.getSeverityLevel().getName()) + .replace(URI_PLACEHOLDER, event.getFileName()) + .replace(MESSAGE_PLACEHOLDER, SarifLogger.escape(stringWriter.toString())) + ); + } + } +} diff --git a/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/listener/DataBaseLogger.java b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/listener/DataBaseLogger.java new file mode 100644 index 0000000000000000000000000000000000000000..0382b164bdf07f56c24ce622c0087e182d9dc716 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/listener/DataBaseLogger.java @@ -0,0 +1,100 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +package com.huawei.devkit.code.inspector.listener; + +import com.google.common.io.Files; +import com.huawei.devkit.code.inspector.dao.RuleViolationInfoMapper; +import com.huawei.devkit.code.inspector.entity.RuleViolationInfo; +import com.huawei.devkit.code.inspector.perload.DataBasePreLoad; +import com.puppycrawl.tools.checkstyle.api.AuditEvent; +import com.puppycrawl.tools.checkstyle.api.AuditListener; +import com.puppycrawl.tools.checkstyle.api.SeverityLevel; +import lombok.extern.slf4j.Slf4j; +import org.apache.ibatis.session.SqlSession; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * DataBaseListener + * + * @since 2024-07-11 + */ +@Slf4j +public class DataBaseLogger implements AuditListener { + + private final long time; + private final String mergeId; + private final List infoList; + + public DataBaseLogger(String mergeId) { + this.infoList = new ArrayList<>(); + this.mergeId = mergeId; + this.time = System.currentTimeMillis(); + } + + @Override + public void auditStarted(AuditEvent event) { + + } + + @Override + public void auditFinished(AuditEvent event) { + Map> collected = this.infoList.stream() + .collect(Collectors.groupingBy(RuleViolationInfo::getFilePath)); + for (Map.Entry> entry : collected.entrySet()) { + this.filledLine(entry.getKey(), entry.getValue()); + } + try (SqlSession sqlSession = DataBasePreLoad.getSqlSession()) { + RuleViolationInfoMapper mapper = sqlSession.getMapper(RuleViolationInfoMapper.class); + for (int i = 0; i < this.infoList.size(); i += 100) { + int endIndex = Math.min(i + 100, this.infoList.size()); + mapper.addRuleViolationInfos(this.infoList.subList(i, endIndex)); + } + sqlSession.commit(); + } + } + + @Override + public void fileStarted(AuditEvent event) { + + } + + @Override + public void fileFinished(AuditEvent event) { + + } + + @Override + public void addError(AuditEvent event) { + RuleViolationInfo ruleViolation = RuleViolationInfo.builder().filePath(event.getFileName()) + .lineno(event.getLine()).time(this.time).filePathHash(event.getFileName().hashCode()) + .message(event.getMessage()).ruleId(event.getViolation().getModuleId()) + .mergeId(this.mergeId).shielded(false).commitRequestToShield(false) + .level(SeverityLevel.ERROR == event.getSeverityLevel() ? 1 : 0).build(); + this.infoList.add(ruleViolation); + } + + @Override + public void addException(AuditEvent event, Throwable throwable) { + log.error("entry", throwable); + } + + private void filledLine(String file, List infoList) { + try { + List lines = Files.readLines(new File(file), StandardCharsets.UTF_8); + for (RuleViolationInfo info : infoList) { + info.setLine(lines.get(info.getLineno() - 1)); + } + } catch (IOException ex) { + log.error("filled line", ex); + } + } +} diff --git a/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/perload/DataBasePreLoad.java b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/perload/DataBasePreLoad.java new file mode 100644 index 0000000000000000000000000000000000000000..b270463cbc98b27b54acd276783ea940e0a3d4b5 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/perload/DataBasePreLoad.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +package com.huawei.devkit.code.inspector.perload; + +import com.huawei.devkit.code.inspector.dao.DataBaseMapper; +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +/** + * DataBasePreLoad + * + * @since 2024-07-11 + */ +public class DataBasePreLoad { + public static SqlSession getSqlSession() { + return sqlSessionFactory.openSession(); + } + + /** + * 预处理 + * + * @param properties 属性 + * @throws IOException 异常 + */ + public static void preload(Properties properties) throws IOException { + try (InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");) { + sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, properties); + } + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + createTable(sqlSession); + } + } + + private static void createTable(SqlSession sqlSession) { + DataBaseMapper mapper = sqlSession.getMapper(DataBaseMapper.class); + mapper.createTable(); + sqlSession.commit(); + } + + + private static SqlSessionFactory sqlSessionFactory; +} diff --git a/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/utils/PropertiesUtils.java b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/utils/PropertiesUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..d12eddca07fd196a70d298b78da999eebdd067ec --- /dev/null +++ b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/utils/PropertiesUtils.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +package com.huawei.devkit.code.inspector.utils; + +import lombok.extern.slf4j.Slf4j; +import org.apache.ibatis.io.Resources; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.Properties; + +/** + * PropertiesUtils + * + * @since 2024-07-11 + */ +@Slf4j +public class PropertiesUtils { + + public static String ROOT_DIR; + + public static Properties loadProperties(String propertiesFile) { + Properties properties = new Properties(); + try (InputStream in = Resources.getResourceAsStream(propertiesFile)) { + properties.load(in); + } catch (IOException ex) { + log.error("load properties", ex); + } + return properties; + } + + public static void configAndUpdate(Properties properties) { + String rootDir = System.getProperty("CODE_INSPECTOR_APP_HOME"); + if (rootDir != null && !rootDir.isEmpty()) { + ROOT_DIR = rootDir; + properties.setProperty("root.dir", ROOT_DIR); + return; + } + String mode = properties.getProperty("mode"); + if ("dev".equals(mode)) { + ROOT_DIR = properties.getProperty("root.dir"); + } else { + ROOT_DIR = System.getProperty("user.dir"); + properties.setProperty("root.dir", ROOT_DIR); + } + } + + public static String readResource(String name) throws IOException { + try (InputStream inputStream = Resources.getResourceAsStream(name); + ByteArrayOutputStream result = new ByteArrayOutputStream()) { + if (inputStream == null) { + throw new IOException("Cannot find the resource " + name); + } + final byte[] buffer = new byte[1024]; + int length = inputStream.read(buffer); + while (length != -1) { + result.write(buffer, 0, length); + length = inputStream.read(buffer); + } + return result.toString(StandardCharsets.UTF_8); + } + } + +} diff --git a/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/wrappers/CheckStyleWrapper.java b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/wrappers/CheckStyleWrapper.java new file mode 100644 index 0000000000000000000000000000000000000000..d811d1a19c89898b5e86f6481c183a8505612184 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/main/java/com/huawei/devkit/code/inspector/wrappers/CheckStyleWrapper.java @@ -0,0 +1,302 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +package com.huawei.devkit.code.inspector.wrappers; + +import com.huawei.devkit.code.inspector.entity.CliOptions; +import com.huawei.devkit.code.inspector.entity.OutputStyle; +import com.huawei.devkit.code.inspector.listener.DataBaseLogger; +import com.puppycrawl.tools.checkstyle.AbstractAutomaticBean; +import com.puppycrawl.tools.checkstyle.Checker; +import com.puppycrawl.tools.checkstyle.ConfigurationLoader; +import com.puppycrawl.tools.checkstyle.Definitions; +import com.puppycrawl.tools.checkstyle.LocalizedMessage; +import com.puppycrawl.tools.checkstyle.Main; +import com.puppycrawl.tools.checkstyle.ModuleFactory; +import com.puppycrawl.tools.checkstyle.PackageObjectFactory; +import com.puppycrawl.tools.checkstyle.PropertiesExpander; +import com.puppycrawl.tools.checkstyle.ThreadModeSettings; +import com.puppycrawl.tools.checkstyle.api.AuditEvent; +import com.puppycrawl.tools.checkstyle.api.AuditListener; +import com.puppycrawl.tools.checkstyle.api.CheckstyleException; +import com.puppycrawl.tools.checkstyle.api.Configuration; +import com.puppycrawl.tools.checkstyle.api.RootModule; +import com.puppycrawl.tools.checkstyle.utils.ChainedPropertyUtil; +import lombok.extern.slf4j.Slf4j; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.LinkedList; +import java.util.List; +import java.util.Properties; +import java.util.regex.Pattern; + +/** + * CheckStyleWrapper + * + * @since 2024-07-11 + */ + +@Slf4j +public class CheckStyleWrapper { + + /** + * A key pointing to the error counter + * message in the "messages.properties" file. + */ + public static final String ERROR_COUNTER = "Main.errorCounter"; + /** + * A key pointing to the load properties exception + * message in the "messages.properties" file. + */ + public static final String LOAD_PROPERTIES_EXCEPTION = "Main.loadProperties"; + + + /** + * Loops over the files specified checking them for errors. The exit code + * is the number of errors found in all the files. + * + * @throws IOException if there is a problem with files access + **/ + public static void checkStyle(CliOptions cliOptions) throws IOException { + int errorCounter = 0; + try { + final List filesToProcess = getFilesToProcess(cliOptions); + errorCounter = runCheckstyle(cliOptions, filesToProcess); + } catch (CheckstyleException ex) { + errorCounter = 1; + log.error(ex.getMessage(), ex); + } finally { + if (errorCounter > 0) { + final LocalizedMessage errorCounterViolation = new LocalizedMessage( + Definitions.CHECKSTYLE_BUNDLE, Main.class, + ERROR_COUNTER, String.valueOf(errorCounter)); + log.error(errorCounterViolation.getMessage()); + } + } + } + + /** + * Determines the files to process. + * + * @param options the user-specified options + * @return list of files to process + */ + private static List getFilesToProcess(CliOptions options) { + final List patternsToExclude = options.getExclusions(); + + final List result = new LinkedList<>(); + for (File file : options.getFiles()) { + result.addAll(listFiles(file, patternsToExclude)); + } + return result; + } + + /** + * Traverses a specified node looking for files to check. Found files are added to + * a specified list. Subdirectories are also traversed. + * + * @param node the node to process + * @param patternsToExclude The list of patterns to exclude from searching or being added as + * files. + * @return found files + */ + private static List listFiles(File node, List patternsToExclude) { + // could be replaced with org.apache.commons.io.FileUtils.list() method + // if only we add commons-io library + final List result = new LinkedList<>(); + + if (node.canRead() && !isPathExcluded(node.getAbsolutePath(), patternsToExclude)) { + if (node.isDirectory()) { + final File[] files = node.listFiles(); + // listFiles() can return null, so we need to check it + if (files != null) { + for (File element : files) { + result.addAll(listFiles(element, patternsToExclude)); + } + } + } else if (node.isFile()) { + result.add(node); + } + } + return result; + } + + /** + * Checks if a directory/file {@code path} should be excluded based on if it matches one of the + * patterns supplied. + * + * @param path The path of the directory/file to check + * @param patternsToExclude The collection of patterns to exclude from searching + * or being added as files. + * @return True if the directory/file matches one of the patterns. + */ + private static boolean isPathExcluded(String path, Iterable patternsToExclude) { + boolean result = false; + + for (Pattern pattern : patternsToExclude) { + if (pattern.matcher(path).find()) { + result = true; + break; + } + } + + return result; + } + + + /** + * Executes required Checkstyle actions based on passed parameters. + * + * @param options user-specified options + * @param filesToProcess the list of files whose style to check + * @return number of violations of ERROR level + * @throws IOException when output file could not be found + * @throws CheckstyleException when properties file could not be loaded + */ + private static int runCheckstyle(CliOptions options, List filesToProcess) + throws CheckstyleException, IOException { + final Properties props; + + if (options.getPropertiesFile() == null) { + props = System.getProperties(); + } else { + props = loadProperties(options.getPropertiesFile()); + } + + // create a configuration + final ThreadModeSettings multiThreadModeSettings = + new ThreadModeSettings(CliOptions.CHECKER_THREADS_NUMBER, + CliOptions.TREE_WALKER_THREADS_NUMBER); + + final ConfigurationLoader.IgnoredModulesOptions ignoredModulesOptions; + if (options.isExecuteIgnoredModules()) { + ignoredModulesOptions = ConfigurationLoader.IgnoredModulesOptions.EXECUTE; + } else { + ignoredModulesOptions = ConfigurationLoader.IgnoredModulesOptions.OMIT; + } + + final Configuration config = ConfigurationLoader.loadConfiguration( + options.getConfigurationFile(), new PropertiesExpander(props), + ignoredModulesOptions, multiThreadModeSettings); + + // create RootModule object and run it + final int errorCounter; + final ClassLoader moduleClassLoader = Checker.class.getClassLoader(); + final RootModule rootModule = getRootModule(config.getName(), moduleClassLoader); + + try { + final AuditListener listener = createListener(options.getFormat(), options.getOutputPath()); + rootModule.setModuleClassLoader(moduleClassLoader); + rootModule.configure(config); + DataBaseLogger baseListener = new DataBaseLogger(""); + rootModule.addListener(listener); + rootModule.addListener(baseListener); + + // run RootModule + errorCounter = rootModule.process(filesToProcess); + } finally { + rootModule.destroy(); + } + + return errorCounter; + } + + /** + * Loads properties from a File. + * + * @param file the properties file + * @return the properties in file + * @throws CheckstyleException when could not load properties file + */ + private static Properties loadProperties(File file) + throws CheckstyleException { + final Properties properties = new Properties(); + + try (InputStream stream = Files.newInputStream(file.toPath())) { + properties.load(stream); + } catch (final IOException ex) { + final LocalizedMessage loadPropertiesExceptionMessage = new LocalizedMessage( + Definitions.CHECKSTYLE_BUNDLE, CheckStyleWrapper.class, + LOAD_PROPERTIES_EXCEPTION, file.getAbsolutePath()); + throw new CheckstyleException(loadPropertiesExceptionMessage.getMessage(), ex); + } + + return ChainedPropertyUtil.getResolvedProperties(properties); + } + + /** + * Creates a new instance of the root module that will control and run + * Checkstyle. + * + * @param name The name of the module. This will either be a short name that + * will have to be found or the complete package name. + * @param moduleClassLoader Class loader used to load the root module. + * @return The new instance of the root module. + * @throws CheckstyleException if no module can be instantiated from name + */ + private static RootModule getRootModule(String name, ClassLoader moduleClassLoader) + throws CheckstyleException { + final ModuleFactory factory = new PackageObjectFactory( + Checker.class.getPackage().getName(), moduleClassLoader); + + return (RootModule) factory.createModule(name); + } + + /** + * This method creates in AuditListener an open stream for validation data, it must be + * closed by {@link RootModule} (default implementation is {@link Checker}) by calling + * {@link AuditListener#auditFinished(AuditEvent)}. + * + * @param format format of the audit listener + * @param outputLocation the location of output + * @return a fresh new {@code AuditListener} + * @throws IOException when provided output location is not found + */ + private static AuditListener createListener(OutputStyle format, Path outputLocation) + throws IOException { + final OutputStream out = getOutputStream(outputLocation); + final AbstractAutomaticBean.OutputStreamOptions closeOutputStreamOption = + getOutputStreamOptions(outputLocation); + return format.createListener(out, closeOutputStreamOption); + } + + /** + * Create output stream or return System.out. + * + * @param outputPath output location + * @return output stream + * @throws IOException might happen + */ + private static OutputStream getOutputStream(Path outputPath) throws IOException { + final OutputStream result; + if (outputPath == null) { + result = System.out; + } else { + result = Files.newOutputStream(outputPath); + } + return result; + } + + /** + * Create {@link AbstractAutomaticBean.OutputStreamOptions} for the given location. + * + * @param outputPath output location + * @return output stream options + */ + private static AbstractAutomaticBean.OutputStreamOptions getOutputStreamOptions(Path outputPath) { + final AbstractAutomaticBean.OutputStreamOptions result; + if (outputPath == null) { + result = AbstractAutomaticBean.OutputStreamOptions.NONE; + } else { + result = AbstractAutomaticBean.OutputStreamOptions.CLOSE; + } + return result; + } + +} diff --git a/component/CodeInspector/code_inspector/src/main/resources/config.properties b/component/CodeInspector/code_inspector/src/main/resources/config.properties new file mode 100644 index 0000000000000000000000000000000000000000..74074368b8f5ce4f9a903d3adbd09b7dc6701cd2 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/main/resources/config.properties @@ -0,0 +1,4 @@ +# +#mode=product +mode=dev +root.dir=src/main/content \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/main/resources/log4j.properties b/component/CodeInspector/code_inspector/src/main/resources/log4j.properties new file mode 100644 index 0000000000000000000000000000000000000000..d72be5ffaef89c7a6293e00fcc0ec25304a87ab4 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/main/resources/log4j.properties @@ -0,0 +1,11 @@ +# +# Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. +# +log4j.rootLogger=DEBUG,console +# console +log4j.appender.console=org.apache.log4j.ConsoleAppender +log4j.appender.console.Threshold=INFO +log4j.appender.console.ImmediateFlush=true +log4j.appender.console.Target=System.err +log4j.appender.console.layout=org.apache.log4j.PatternLayout +log4j.appender.console.layout.ConversionPattern=[%d{YYYY-MM-dd HH:mm:ss}] [%p] - %l - %m%n diff --git a/component/CodeInspector/code_inspector/src/main/resources/mappers/DataBaseMapper.xml b/component/CodeInspector/code_inspector/src/main/resources/mappers/DataBaseMapper.xml new file mode 100644 index 0000000000000000000000000000000000000000..2a41e456ca512a8d7d79150acfa4ce5c0f5d7ca6 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/main/resources/mappers/DataBaseMapper.xml @@ -0,0 +1,28 @@ + + + + + + + create table if not exists rule_violation_info + ( + id integer primary key autoincrement, + rule_id int, + file_path_hash int, + file_path varchar(256), + line varchar(256), + lineno int, + level int, + merge_id varchar(50), + message varchar(256), + shielded boolean, + commit_request_to_shield boolean, + time long + ) + + + \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/main/resources/mappers/RuleViolationInfoMapper.xml b/component/CodeInspector/code_inspector/src/main/resources/mappers/RuleViolationInfoMapper.xml new file mode 100644 index 0000000000000000000000000000000000000000..201054a0bfbcbd57bc82da31d85e481319c98a30 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/main/resources/mappers/RuleViolationInfoMapper.xml @@ -0,0 +1,18 @@ + + + + + + + insert into rule_violation_info (rule_id, file_path_hash, file_path, line, lineno,level, merge_id, message, + shielded, commit_request_to_shield, time) values + + (#{item.ruleId}, #{item.filePathHash}, #{item.filePath}, #{item.line}, #{item.level}, #{item.lineno}, + #{item.mergeId}, #{item.message}, #{item.shielded}, #{item.commitRequestToShield}, #{item.time}) + + + \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/main/resources/mybatis-config.xml b/component/CodeInspector/code_inspector/src/main/resources/mybatis-config.xml new file mode 100644 index 0000000000000000000000000000000000000000..01b181f26d1696efd12554da922e9b76c14864d6 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/main/resources/mybatis-config.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/main/resources/templates/CustomReport.template b/component/CodeInspector/code_inspector/src/main/resources/templates/CustomReport.template new file mode 100644 index 0000000000000000000000000000000000000000..0e81f0cd46dbd11cfcf17b7f70d214e498bbefaf --- /dev/null +++ b/component/CodeInspector/code_inspector/src/main/resources/templates/CustomReport.template @@ -0,0 +1,5 @@ +{ + result: [ +${results} + ] +} diff --git a/component/CodeInspector/code_inspector/src/main/resources/templates/ResultErrorOnly.template b/component/CodeInspector/code_inspector/src/main/resources/templates/ResultErrorOnly.template new file mode 100644 index 0000000000000000000000000000000000000000..700f7373069e3b624075b20ff69042ec580ecec7 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/main/resources/templates/ResultErrorOnly.template @@ -0,0 +1,4 @@ + { + "level": "${severityLevel}", + "message": "${message}" + } \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/main/resources/templates/ResultFileOnly.template b/component/CodeInspector/code_inspector/src/main/resources/templates/ResultFileOnly.template new file mode 100644 index 0000000000000000000000000000000000000000..56875eb07954c22feb531ecad405d71f2c1d3e2d --- /dev/null +++ b/component/CodeInspector/code_inspector/src/main/resources/templates/ResultFileOnly.template @@ -0,0 +1,5 @@ + { + "level": "${severityLevel}", + "locations": "${uri}", + "message": "${message}" + } \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/main/resources/templates/ResultLineColumn.template b/component/CodeInspector/code_inspector/src/main/resources/templates/ResultLineColumn.template new file mode 100644 index 0000000000000000000000000000000000000000..fe24b2b4f61698bf7ec1037fd512e1feed6f9f8d --- /dev/null +++ b/component/CodeInspector/code_inspector/src/main/resources/templates/ResultLineColumn.template @@ -0,0 +1,8 @@ + { + "level": "${severityLevel}", + "uri": "${uri}", + "startColumn": ${column}, + "startLine": ${line}, + "message": "${message}", + "ruleId": "${ruleId}" + } \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/main/resources/templates/ResultLineOnly.template b/component/CodeInspector/code_inspector/src/main/resources/templates/ResultLineOnly.template new file mode 100644 index 0000000000000000000000000000000000000000..9d8ebff6b2f30c15f32cb2174b7ebd3a4b46418b --- /dev/null +++ b/component/CodeInspector/code_inspector/src/main/resources/templates/ResultLineOnly.template @@ -0,0 +1,7 @@ + { + "level": "${severityLevel}", + "uri": "${uri}", + "startLine": ${line}, + "message": "${message}", + "ruleId": "${ruleId}" + } \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/java/com/huawei/devkit/code/inspector/AnnotationsTest.java b/component/CodeInspector/code_inspector/src/test/java/com/huawei/devkit/code/inspector/AnnotationsTest.java new file mode 100644 index 0000000000000000000000000000000000000000..a15936475e0dc9e03df5ac521dd9def1bbe7d498 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/java/com/huawei/devkit/code/inspector/AnnotationsTest.java @@ -0,0 +1,35 @@ +package com.huawei.devkit.code.inspector; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Objects; + +/** + * AnnotationsTest + * + * @since 2024-07-18 + */ +public class AnnotationsTest { + @Test + void testAnnotationLocation() { + String root = System.getProperty("user.dir"); + String filePath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("case/annotations/AnnotationLocationCase.java")).getPath(); + String configPath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("single_rules/annotations/AnnotationLocation.xml")).getPath(); + String[] args = new String[]{"-c", configPath, "-o", root + "/testAnnotationLocation.out", "-f", "json", filePath}; + Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + } + + @Test + void testSuppressWarnings() { + String root = System.getProperty("user.dir"); + String filePath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("case/annotations/SuppressWarningsCase.java")).getPath(); + String configPath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("single_rules/annotations/SuppressWarnings.xml")).getPath(); + String[] args = new String[]{"-c", configPath, "-o", root + "/testAnnotationLocation.out", "-f", "json", filePath}; + Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + } +} diff --git a/component/CodeInspector/code_inspector/src/test/java/com/huawei/devkit/code/inspector/HeadersAndImportsTest.java b/component/CodeInspector/code_inspector/src/test/java/com/huawei/devkit/code/inspector/HeadersAndImportsTest.java new file mode 100644 index 0000000000000000000000000000000000000000..a4f3a82ac9d9e895fc91daa5c270246cc25f4d13 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/java/com/huawei/devkit/code/inspector/HeadersAndImportsTest.java @@ -0,0 +1,64 @@ +package com.huawei.devkit.code.inspector; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Objects; + +public class HeadersAndImportsTest { + + @Test + void testRegexpHeader() { + String root = System.getProperty("user.dir"); + String filePath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("case/headers_and_imports/RegexpHeaderCase.java")).getPath(); + String configPath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("single_rules/headers_and_imports/RegexpHeader.xml")).getPath(); + String[] args = new String[]{"-c", configPath, "-o", root + "/testRegexpHeader.out", "-f", "json", filePath}; + Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + } + + @Test + void testAvoidStarImport() { + String root = System.getProperty("user.dir"); + String filePath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("case/headers_and_imports/AvoidStarImportCase.java")).getPath(); + String configPath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("single_rules/headers_and_imports/AvoidStarImport.xml")).getPath(); + String[] args = new String[]{"-c", configPath, "-o", root + "/testAvoidStarImport.out", "-f", "json", filePath}; + Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + } + + @Test + void testCustomImportOrderRules() { + String root = System.getProperty("user.dir"); + String filePath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("case/headers_and_imports/CustomImportOrderCase.java")).getPath(); + String configPath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("single_rules/headers_and_imports/CustomImportOrder.xml")).getPath(); + String[] args = new String[]{"-c", configPath, "-o", root + "/testCustomImportOrderRules.out", "-f", "json", filePath}; + Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + } + + @Test + void testUnusedImports() { + String root = System.getProperty("user.dir"); + String filePath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("case/headers_and_imports/UnusedImportsCase.java")).getPath(); + String configPath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("single_rules/headers_and_imports/UnusedImports.xml")).getPath(); + String[] args = new String[]{"-c", configPath, "-o", root + "/testUnusedImports.out", "-f", "json", filePath}; + Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + } + + @Test + void testRedundantImport() { + String root = System.getProperty("user.dir"); + String filePath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("case/headers_and_imports/RedundantImportCase.java")).getPath(); + String configPath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("single_rules/headers_and_imports/RedundantImport.xml")).getPath(); + String[] args = new String[]{"-c", configPath, "-o", root + "/testRedundantImport.out", "-f", "json", filePath}; + Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + } +} diff --git a/component/CodeInspector/code_inspector/src/test/java/com/huawei/devkit/code/inspector/JavadocTest.java b/component/CodeInspector/code_inspector/src/test/java/com/huawei/devkit/code/inspector/JavadocTest.java new file mode 100644 index 0000000000000000000000000000000000000000..18d0aa5cc4ff3341b0f96e0d0610b56857a7efb0 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/java/com/huawei/devkit/code/inspector/JavadocTest.java @@ -0,0 +1,196 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +package com.huawei.devkit.code.inspector; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Objects; + +/** + * JavadocTest + * + * @since 2024-07-16 + */ +public class JavadocTest { + @Test + void testAtclauseOrder() { + String root = System.getProperty("user.dir"); + String filePath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("case/javadoc/AtclauseOrderCase.java")).getPath(); + String configPath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("single_rules/javadoc/AtclauseOrder.xml")).getPath(); + String[] args = new String[]{"-c", configPath, "-o", root + "/testAtclauseOrder.out", "-f", "json", filePath}; + Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + } + + @Test + void testInvalidJavadocPosition() { + String root = System.getProperty("user.dir"); + String filePath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("case/javadoc/InvalidJavadocPositionCase.java")).getPath(); + String configPath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("single_rules/javadoc/InvalidJavadocPosition.xml")).getPath(); + String[] args = new String[]{"-c", configPath, "-o", root + "/testInvalidJavadocPosition.out", "-f", "json", filePath}; + Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + } + + @Test + void testJavadocBlockTagLocation() { + String root = System.getProperty("user.dir"); + String filePath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("case/javadoc/JavadocBlockTagLocationCase.java")).getPath(); + String configPath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("single_rules/javadoc/JavadocBlockTagLocation.xml")).getPath(); + String[] args = new String[]{"-c", configPath, "-o", root + "/testJavadocBlockTagLocation.out", "-f", "json", filePath}; + Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + } + + @Test + void testJavadocContentLocation() { + String root = System.getProperty("user.dir"); + String filePath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("case/javadoc/JavadocContentLocationCase.java")).getPath(); + String configPath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("single_rules/javadoc/JavadocContentLocation.xml")).getPath(); + String[] args = new String[]{"-c", configPath, "-o", root + "/testJavadocContentLocation.out", "-f", "json", filePath}; + Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + } + + @Test + void testJavadocMethod() { + String root = System.getProperty("user.dir"); + String filePath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("case/javadoc/JavadocMethodCase.java")).getPath(); + String configPath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("single_rules/javadoc/JavadocMethod.xml")).getPath(); + String[] args = new String[]{"-c", configPath, "-o", root + "/testJavadocMethod.out", "-f", "json", filePath}; + Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + } + + @Test + void testJavadocMissingLeadingAsterisk() { + String root = System.getProperty("user.dir"); + String filePath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("case/javadoc/JavadocMissingLeadingAsteriskCase.java")).getPath(); + String configPath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("single_rules/javadoc/JavadocMissingLeadingAsterisk.xml")).getPath(); + String[] args = new String[]{"-c", configPath, "-o", root + "/testJavadocMissingLeadingAsterisk.out", "-f", "json", filePath}; + Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + } + + @Test + void testJavadocMissingWhitespaceAfterAsterisk() { + String root = System.getProperty("user.dir"); + String filePath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("case/javadoc/JavadocMissingWhitespaceAfterAsteriskCase.java")).getPath(); + String configPath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("single_rules/javadoc/JavadocMissingWhitespaceAfterAsterisk.xml")).getPath(); + String[] args = new String[]{"-c", configPath, "-o", root + "/testJavadocMissingWhitespaceAfterAsterisk.out", "-f", "json", filePath}; + Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + } + + @Test + void testJavadocParagraph() { + String root = System.getProperty("user.dir"); + String filePath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("case/javadoc/JavadocParagraphCase.java")).getPath(); + String configPath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("single_rules/javadoc/JavadocParagraph.xml")).getPath(); + String[] args = new String[]{"-c", configPath, "-o", root + "/testJavadocParagraph.out", "-f", "json", filePath}; + Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + } + + + @Test + void testJavadocTagContinuationIndentation() { + String root = System.getProperty("user.dir"); + String filePath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("case/javadoc/JavadocTagContinuationIndentationCase.java")).getPath(); + String configPath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("single_rules/javadoc/JavadocTagContinuationIndentation.xml")).getPath(); + String[] args = new String[]{"-c", configPath, "-o", root + "/testJavadocTagContinuationIndentation.out", "-f", "json", filePath}; + Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + } + + @Test + void testJavadocVariable() { + String root = System.getProperty("user.dir"); + String filePath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("case/javadoc/JavadocVariableCase.java")).getPath(); + String configPath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("single_rules/javadoc/JavadocVariable.xml")).getPath(); + String[] args = new String[]{"-c", configPath, "-o", root + "/testJavadocVariable.out", "-f", "json", filePath}; + Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + } + + @Test + void testMissingJavadocMethod() { + String root = System.getProperty("user.dir"); + String filePath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("case/javadoc/MissingJavadocMethodCase.java")).getPath(); + String configPath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("single_rules/javadoc/MissingJavadocMethod.xml")).getPath(); + String[] args = new String[]{"-c", configPath, "-o", root + "/testMissingJavadocMethod.out", "-f", "json", filePath}; + Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + } + + + @Test + void testMissingJavadocType() { + String root = System.getProperty("user.dir"); + String filePath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("case/javadoc/MissingJavadocTypeCase.java")).getPath(); + String configPath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("single_rules/javadoc/MissingJavadocType.xml")).getPath(); + String[] args = new String[]{"-c", configPath, "-o", root + "/testMissingJavadocType.out", "-f", "json", filePath}; + Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + } + + @Test + void testNonEmptyAtclauseDescription() { + String root = System.getProperty("user.dir"); + String filePath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("case/javadoc/NonEmptyAtclauseDescriptionCase.java")).getPath(); + String configPath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("single_rules/javadoc/NonEmptyAtclauseDescription.xml")).getPath(); + String[] args = new String[]{"-c", configPath, "-o", root + "/testNonEmptyAtclauseDescription.out", "-f", "json", filePath}; + Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + } + + @Test + void testRequireEmptyLineBeforeBlockTagGroup() { + String root = System.getProperty("user.dir"); + String filePath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("case/javadoc/RequireEmptyLineBeforeBlockTagGroupCase.java")).getPath(); + String configPath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("single_rules/javadoc/RequireEmptyLineBeforeBlockTagGroup.xml")).getPath(); + String[] args = new String[]{"-c", configPath, "-o", root + "/testRequireEmptyLineBeforeBlockTagGroup.out", "-f", "json", filePath}; + Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + } + + @Test + void testSingleLineJavadoc() { + String root = System.getProperty("user.dir"); + String filePath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("case/javadoc/SingleLineJavadocCase.java")).getPath(); + String configPath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("single_rules/javadoc/SingleLineJavadoc.xml")).getPath(); + String[] args = new String[]{"-c", configPath, "-o", root + "/testSingleLineJavadoc.out", "-f", "json", filePath}; + Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + } + + @Test + void testWriteTag() { + String root = System.getProperty("user.dir"); + String filePath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("case/javadoc/WriteTagCase.java")).getPath(); + String configPath = Objects.requireNonNull(this.getClass().getClassLoader() + .getResource("single_rules/javadoc/WriteTag.xml")).getPath(); + String[] args = new String[]{"-c", configPath, "-o", root + "/testWriteTag.out", "-f", "json", filePath}; + Assertions.assertDoesNotThrow(() -> CodeInspector.main(args)); + } + +} diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/annotations/AnnotationLocationCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/annotations/AnnotationLocationCase.java new file mode 100644 index 0000000000000000000000000000000000000000..2d587e39fe0961fa9b5549810c0ea3b7019b68be --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/annotations/AnnotationLocationCase.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +package com.huawei.devkit.code.inspector; + +import org.junit.jupiter.api.Test; + +import javax.annotation.Nullable; + +@SuppressWarnings("unchecked") +public class AnnotationLocationCase { + @Nullable private String aab; + + @Test void test01() { + System.out.println();System.out.println();System.out.println();System.out.println();System.out.println();System.out.println();System.out.println(); + } + + + @Test + void test02(@Nullable String demo) { + System.out.println(demo); + } + + void test03(@Nullable String demo) { + System.out.println(demo); + } +} diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/annotations/SuppressWarningsCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/annotations/SuppressWarningsCase.java new file mode 100644 index 0000000000000000000000000000000000000000..dd798b1dde2f31dc1d3f287c71fd67738049a089 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/annotations/SuppressWarningsCase.java @@ -0,0 +1,26 @@ +// ok below, since we are only checking for '^unchecked$|^unused$' +@SuppressWarnings("") +class SuppressWarningsCase { + // ok below as VARIABLE_DEF is not configured in tokens to check + @SuppressWarnings("") + final int num1 = 1; + @SuppressWarnings("all") + final int num2 = 2; + @SuppressWarnings("unused") + final int num3 = 3; + + // ok below as PARAMETER_DEF is not configured in tokens to check + void foo1(@SuppressWarnings("unused") int param) {} + + // ok below, since we are only checking for '^unchecked$|^unused$' + @SuppressWarnings("all") + void foo2(int param) {} + + // violation below, 'The warning 'unused' cannot be suppressed at this location' + @SuppressWarnings(true?"all":"unchecked") + void foo4(int param) {} +} + +// violation below, 'The warning 'unchecked' cannot be suppressed at this location' +@SuppressWarnings("unchecked") +class Test2 {} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/block_checks/AvoidNestedBlocksCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/block_checks/AvoidNestedBlocksCase.java new file mode 100644 index 0000000000000000000000000000000000000000..0180a161f461a920544d186ed4c53d92184fba84 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/block_checks/AvoidNestedBlocksCase.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +public class AvoidNestedBlocksCase { + public void foo() { + int myInteger = 0; + { // violation + myInteger = 2; + } + System.out.println("myInteger = " + myInteger); + + switch (a) { + case 1: { // violation + System.out.println("Case 1"); + break; + } + case 2: + System.out.println("Case 2"); + break; + } + } +} diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/block_checks/EmptyCatchBlockCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/block_checks/EmptyCatchBlockCase.java new file mode 100644 index 0000000000000000000000000000000000000000..90e5084afdf19a69a51112e062d48a8e018f75b8 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/block_checks/EmptyCatchBlockCase.java @@ -0,0 +1,31 @@ +public class EmptyCatchBlockCase { + private void exampleMethod1() { + try { + throw new RuntimeException(); + } catch (RuntimeException expected) { + } // violation above + } + + private void exampleMethod2() { + try { + throw new RuntimeException(); + } catch (Exception ignore) { + // no handling + } // ok, catch block has comment + } + + private void exampleMethod3 () { + try { + throw new RuntimeException(); + } catch (RuntimeException o) { + } // violation above + } + + private void exampleMethod4 () { + try { + throw new RuntimeException(); + } catch (RuntimeException ex) { + // This is expected + } + } +} diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/block_checks/LeftCurlyCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/block_checks/LeftCurlyCase.java new file mode 100644 index 0000000000000000000000000000000000000000..da78ff3ad526557dd9a8a9f98a9cd02b222cad46 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/block_checks/LeftCurlyCase.java @@ -0,0 +1,16 @@ +class LeftCurlyCase +{ // violation, ''{' at column 1 should be on the previous line.' + private interface TestInterface + { // violation, ''{' at column 3 should be on the previous line.' + } + + private + class + MyClass { // OK + } + + enum Colors {RED, // OK + BLUE, + GREEN; + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/block_checks/NeedBracesCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/block_checks/NeedBracesCase.java new file mode 100644 index 0000000000000000000000000000000000000000..5639ad1f2d975e299199553d2a1b6eb0c9580933 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/block_checks/NeedBracesCase.java @@ -0,0 +1,35 @@ +class NeedBracesCase { + String obj = new String(); + String value = new String(); + int counter = 1; + int count = 0; + int num = 12; + String o = "O"; + public boolean Example1() { + if (obj.equals(num)) return true; + // violation above, ''if' construct must use '{}'s.' + if (true) { + count = 2; + } else + // violation above, ''else' construct must use '{}'s.' + return false; + for (int i = 0; i < 5; i++) { + ++count;} + do // violation, ''do' construct must use '{}'s.' + ++count; + while (false); + for (int j = 0; j < 10; j++); + // violation above, ''for' construct must use '{}'s.' + for(int i = 0; i < 10; value.charAt(12)); + // violation above, ''for' construct must use '{}'s.' + while (counter < 10) + // violation above, ''while' construct must use '{}'s.' + ++count; + while (value.charAt(12) < 5); + // violation above, ''while' construct must use '{}'s.' + switch (num) { + case 1: counter++; break; + } + return true; + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/block_checks/RightCurlyAloneCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/block_checks/RightCurlyAloneCase.java new file mode 100644 index 0000000000000000000000000000000000000000..4706b9c45a83e89fa11349de08badf6de888718e --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/block_checks/RightCurlyAloneCase.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + + +public class RightCurlyAloneCase { + + public void test() { + + boolean foo = false; + if (foo) { + bar(); + } else { bar(); } + // violation above, 'should be alone on a line.' + + if (foo) { + bar(); + } else { + bar(); + } + + try { + bar(); + } catch (Exception e) { + // OK above because config is set to token METHOD_DEF and LITERAL_ELSE + bar(); + } + + } + + private void bar() { + } + + public void violate() { Object bar = "bar"; } + // violation above, 'should be alone on a line.' + + public void ok() { + bar(); + } +} + diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/block_checks/RightCurlySameCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/block_checks/RightCurlySameCase.java new file mode 100644 index 0000000000000000000000000000000000000000..d15f5090ad5ca0d2155aa7bd591ccd13bd67feb6 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/block_checks/RightCurlySameCase.java @@ -0,0 +1,52 @@ + +public class RightCurlySameCase{ + + public void test() { + boolean foo = false; + try (Foo foo = new Foo(); Bar bar = new Bar()) { + bar(); + } + if (foo) { + bar(); + } // violation, 'should be on the same line' + // as the next part of a multi-block statement (one that directly + // contains multiple blocks: if/else-if/else, do/while or try/catch/finally). + else { + bar(); + } + + if (foo) { + bar(); + } else { + bar(); + } + + if (foo) { bar(); } int i = 0; + // violation above, 'should be alone on a line.' + + if (foo) { bar(); } + i = 0; + + try { + bar(); + } // violation, 'should be on the same line' + // as the next part of a multi-block statement (one that directly + // contains multiple blocks: if/else-if/else, do/while or try/catch/finally). + catch (Exception e) { + bar(); + } + + try { + bar(); + } catch (Exception e) { + bar(); + } finally { + bar(); } + + } + + private void bar() { + } + + public void testSingleLine() { bar(); } // OK, because singleline is allowed +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/class_design/OneTopLevelClassCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/class_design/OneTopLevelClassCase.java new file mode 100644 index 0000000000000000000000000000000000000000..b0bda9fd37161b8acfbb76a56da70af6b912bb0e --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/class_design/OneTopLevelClassCase.java @@ -0,0 +1,15 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +public class OneTopLevelClassCase { // OK, first top-level class + // methods +} + +class Foo2 { // violation, second top-level class + // methods +} + +record Foo3 () {// violation, third top-level "class" + // methods +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/class_design/ThrowsCountCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/class_design/ThrowsCountCase.java new file mode 100644 index 0000000000000000000000000000000000000000..16a0d8325ec0e494857d9937322b51acd3ec8381 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/class_design/ThrowsCountCase.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +class Test { + public void myFunction() throws CloneNotSupportedException, + ArrayIndexOutOfBoundsException, + StringIndexOutOfBoundsException, + IllegalStateException, + NullPointerException { // violation, max allowed is 4 + // body + } + + public void myFunc() throws ArithmeticException, + NumberFormatException { // ok + // body + } + + private void privateFunc() throws CloneNotSupportedException, + ClassNotFoundException, + IllegalAccessException, + ArithmeticException, + ClassCastException { // ok, private methods are ignored + // body + } + +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/class_design/VisibilityModifierCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/class_design/VisibilityModifierCase.java new file mode 100644 index 0000000000000000000000000000000000000000..7b35bfe7e1a739b2217f52362ca1c0db99a943e9 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/class_design/VisibilityModifierCase.java @@ -0,0 +1,40 @@ +class Example4 { + private int myPrivateField1; + + int field1; // violation, must have visibility modifier 'must be private' + + protected String field2; // violation, protected not allowed 'must be private' + + // violation below, not final nor matching pattern 'must be private' + public int field3 = 42; + + // violation below, doesn't match the pattern 'must be private' + public long serialVersionUID = 1L; + + public static final int field4 = 42; + + // violation below, public immutable fields are not allowed 'must be private' + public final int field5 = 42; + + // violation below, public immutable fields are not allowed 'must be private' + public final java.lang.String notes = null; + + // violation below, HashSet is mutable 'must be private' + public final Set mySet1 = new HashSet<>(); + + // violation below, immutable type not in config 'must be private' + public final ImmutableSet mySet2 = null; + + // violation below, immutable type not in config 'must be private' + public final ImmutableMap objects1 = null; + + @java.lang.Deprecated + String annotatedString; // violation, annotation not configured 'must be private' + + @Deprecated + // violation below, annotation not configured 'must be private' + String shortCustomAnnotated; + + @com.google.common.annotations.VisibleForTesting + public String testString = ""; +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/ConstructorsDeclarationGroupingCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/ConstructorsDeclarationGroupingCase.java new file mode 100644 index 0000000000000000000000000000000000000000..699e52db765fa7d7b1caee3c327efb25d0df3d9f --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/ConstructorsDeclarationGroupingCase.java @@ -0,0 +1,31 @@ +public class ConstructorsDeclarationGroupingCase { + + int x; + + ConstructorsDeclarationGroupingCase() {} + + ConstructorsDeclarationGroupingCase(String s){} + + void foo() {} + + ConstructorsDeclarationGroupingCase(int x) {} // violation + + ConstructorsDeclarationGroupingCase(String s, int x) {} // violation + + private enum ExampleEnum { + + ONE, TWO, THREE; + + ExampleEnum() {} + + ExampleEnum(int x) {} + + final int x = 10; + + ExampleEnum(String str) {} // violation + + void foo() {} + } + + ConstructorsDeclarationGroupingCase(float f) {} // violation +} diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/CovariantEqualsCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/CovariantEqualsCase.java new file mode 100644 index 0000000000000000000000000000000000000000..ad191b510545e89c7a666da2eb36af91ea25d952 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/CovariantEqualsCase.java @@ -0,0 +1,19 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +class CovariantEqualsCase { + public boolean equals(CovariantEqualsCase i) { // violation + return false; + } +} + +class CovariantEqualsCase01 { + public boolean equals(CovariantEqualsCase01 i) { // no violation + return false; + } + + public boolean equals(Object i) { + return false; + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/DeclarationOrderCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/DeclarationOrderCase.java new file mode 100644 index 0000000000000000000000000000000000000000..b9ec34055f1f4653b71563920e1b49e01abe53cd --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/DeclarationOrderCase.java @@ -0,0 +1,20 @@ +public class DeclarationOrderCase { + + public int a; + protected int b; + public int c; // violation, variable access definition in wrong order + + DeclarationOrderCase() { + this.a = 0; + } + + public void foo() { + // This method does nothing + } + + DeclarationOrderCase(int a) { // OK, validation of constructors ignored + this.a = a; + } + + private String name; // violation, instance variable declaration in wrong order +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/DefaultComesLastCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/DefaultComesLastCase.java new file mode 100644 index 0000000000000000000000000000000000000000..1ebf9c967a89c2de0d5dc31ba09953764d08607e --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/DefaultComesLastCase.java @@ -0,0 +1,36 @@ +public class DefaultComesLastCase { + public void test(int i){ + switch (i) { + case 1: + break; + case 2: + break; + default: // OK + break; + } + + switch (i) { + case 1: + break; + case 2: + break; // OK, no default + } + + switch (i) { + case 1: + break; + default: // violation, 'default' before 'case' + break; + case 2: + break; + } + + switch (i) { + case 1: + default: // violation, 'default' before 'case' + break; + case 2: + break; + } + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/EmptyStatementCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/EmptyStatementCase.java new file mode 100644 index 0000000000000000000000000000000000000000..cebd0585ad8c625a855a2296116956ffb9a26064 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/EmptyStatementCase.java @@ -0,0 +1,15 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +public class EmptyStatementCase { + public void foo() { + int i = 5; + if(i > 3); // violation + i++; + for (i = 0; i < 5; i++); // violation + i++; + while (i > 10) + i++; + } +} diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/EqualsAvoidNullCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/EqualsAvoidNullCase.java new file mode 100644 index 0000000000000000000000000000000000000000..4d33dc4394ce3af1aea2dfb00a4a8d3d1ecae403 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/EqualsAvoidNullCase.java @@ -0,0 +1,9 @@ +public class EqualsAvoidNullCase { + public void test(){ + String nullString = null; + nullString.equals("My_Sweet_String"); // violation + "My_Sweet_String".equals(nullString); // OK + nullString.equalsIgnoreCase("My_Sweet_String"); // violation + "My_Sweet_String".equalsIgnoreCase(nullString); // OK + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/EqualsHashCodeCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/EqualsHashCodeCase.java new file mode 100644 index 0000000000000000000000000000000000000000..61a93455e4adc6292b1dd0c4f669a2abc47b95f6 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/EqualsHashCodeCase.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +class EqualsHashCodeCase { + public int hashCode() { // violation, no valid 'equals' + return 0; + } + public boolean equals(String o) { return false; } +} + +class ExampleNoHashCode { + public boolean equals(Object o) { // violation, no 'hashCode' + return false; + } + public boolean equals(String o) { return false; } +} + +class ExampleBothMethods1 { + public int hashCode() { return 0; } + public boolean equals(Object o) { // ok, both methods exist + return false; + } + public boolean equals(String o) { return false; } +} + +class ExampleBothMethods2 { + public int hashCode() { return 0; } + public boolean equals(java.lang.Object o) { // ok, both methods exist + return false; + } +} + +class ExampleNoValidHashCode { + public static int hashCode(int i) { return 0; } + public boolean equals(Object o) { // violation, no valid 'hashCode' + return false; + } +} + +class ExampleNoValidEquals { + public int hashCode() { // violation, no valid 'equals' + return 0; + } + public static boolean equals(Object o, Object o2) { return false; } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/FallThroughCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/FallThroughCase.java new file mode 100644 index 0000000000000000000000000000000000000000..d24803ecbb0b04c6b43acaac66e90b04856f356a --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/FallThroughCase.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +class FallThroughCase { + public void foo() throws Exception { + int i = 0; + while (i >= 0) { + switch (i) { + case 1: + i++; + case 2: // violation 'Fall\ through from previous branch of the switch' + i++; + break; + case 3: + i++; + return; + case 4: + i++; + throw new Exception(); + case 5: + i++; // no break by design + case 6: // violation 'Fall\ through from previous branch of the switch' + case 7: + i++; + continue; + case 11: // violation 'Fall\ through from the last branch of the switch' + i++; + } + } + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/HiddenFieldCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/HiddenFieldCase.java new file mode 100644 index 0000000000000000000000000000000000000000..094bd5e0daf7df999d885961228719aed9ee6cd0 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/HiddenFieldCase.java @@ -0,0 +1,21 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +public class HiddenFieldCase { + + private String field; + private String testField; + + public HiddenFieldCase(String testField) { // OK, 'testField' param doesn't hide any field + } + public void method(String param) { // OK + String field = param; // violation, 'field' variable hides 'field' field + } + public void setTestField(String testField) { // OK, 'testField' param doesn't hide any field + this.field = field; + } + public HiddenFieldCase setField(String field) { // OK, 'field' param doesn't hide any field + this.field = field; + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/IllegalCatchCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/IllegalCatchCase.java new file mode 100644 index 0000000000000000000000000000000000000000..fbeb73df84e25d956c7bbd6b7e8ab559de462e3e --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/IllegalCatchCase.java @@ -0,0 +1,29 @@ +public class IllegalCatchCase { + public void test(){ + try { + // some code here + } catch (Exception e) { // violation + + } + + try { + // some code here + } catch (ArithmeticException e) { // OK + + } catch (RuntimeException e) { // violation, catching Exception is illegal and order of catch blocks doesn't matter + + } + + try { + // some code here + } catch (NullPointerException e) { // violation, catching Exception is illegal + + } + + try { + // some code here + } catch (ArithmeticException e) { // OK + + } + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/IllegalInstantiationCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/IllegalInstantiationCase.java new file mode 100644 index 0000000000000000000000000000000000000000..b479c3c3fba959ccb22e02fa5231a9e335f06dbe --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/IllegalInstantiationCase.java @@ -0,0 +1,21 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +public class IllegalInstantiationCase { + public class Boolean { + boolean a; + + public Boolean (boolean a) { this.a = a; } + } + + public void myTest (boolean a, int b) { + Boolean c = new Boolean(a); // OK + java.lang.Boolean d = new java.lang.Boolean(a); // violation, instantiation of + // java.lang.Boolean should be avoided + + Integer e = new Integer(b); // violation, instantiation of + // java.lang.Integer should be avoided + Integer f = Integer.valueOf(b); // OK + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/InnerAssignment.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/InnerAssignment.java new file mode 100644 index 0000000000000000000000000000000000000000..ed3629eac7c26a94002a291eff0f326babce70d8 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/InnerAssignment.java @@ -0,0 +1,17 @@ +public class InnerAssignment { + public void test(){ + String line; + while ((line = bufferedReader.readLine()) != null) { // OK + // process the line + } + + for (;(line = bufferedReader.readLine()) != null;) { // OK + // process the line + } + + do { + // process the line + } + while ((line = bufferedReader.readLine()) != null); // OK + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/MagicNumberCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/MagicNumberCase.java new file mode 100644 index 0000000000000000000000000000000000000000..7f5f984a8a31247d2bf35080872c5fbeb91f0979 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/MagicNumberCase.java @@ -0,0 +1,17 @@ +@MyAnnotation(6) // violation +class MagicNumberCase { + private int field = 7; // violation + + void foo() { + int i = i + 1; // no violation + int j = j + 8; // violation + } + + public int hashCode() { + return 10; // violation + } +} +@interface anno { + int value() default 10; // no violation +} + \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/MissingSwitchDefaultCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/MissingSwitchDefaultCase.java new file mode 100644 index 0000000000000000000000000000000000000000..6abc494290d1f77160cd56f924745f9bccd83222 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/MissingSwitchDefaultCase.java @@ -0,0 +1,22 @@ +public class MissingSwitchDefaultCase { + public void test(int i, String o){ + switch (i) { + case 1: + break; + case 2: + break; + default: // OK + break; + } + switch (o) { + case String s: // type pattern + System.out.println(s); + break; + case Integer i: // type pattern + System.out.println("Integer"); + break; + default: // will not compile without default label, thanks to type pattern label usage + break; + } + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/ModifiedControlVariableCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/ModifiedControlVariableCase.java new file mode 100644 index 0000000000000000000000000000000000000000..85e393ef7ab014ae223400a550338e340da7aadf --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/ModifiedControlVariableCase.java @@ -0,0 +1,11 @@ +public class ModifiedControlVariableCase { + public void test(){ + for(int i=0;i < 8;i++) { + i++; // violation, control variable modified + } + String args1[]={"Coding", "block"}; + for (String arg: args1) { + arg = arg.trim(); // ok + } + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/MultipleStringLiterals.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/MultipleStringLiterals.java new file mode 100644 index 0000000000000000000000000000000000000000..4bd5b224e8201af90b08236b9228da3ac849696c --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/MultipleStringLiterals.java @@ -0,0 +1,15 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +public class MultipleStringLiterals { + String a = "StringContents"; + String a1 = "unchecked"; + @SuppressWarnings("unchecked") // OK, duplicate strings are ignored in annotations + public void myTest() { + String a2 = "StringContents"; // OK, two occurrences are allowed + String a3 = "DoubleString" + "DoubleString"; // OK, two occurrences are allowed + String a4 = "SingleString"; // OK + String a5 = ", " + ", " + ", "; // violation, three occurrences are NOT allowed + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/MultipleVariableDeclarations.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/MultipleVariableDeclarations.java new file mode 100644 index 0000000000000000000000000000000000000000..aeab699846562fb90b3c7760bb1982274d79d44a --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/MultipleVariableDeclarations.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +public class MultipleVariableDeclarations { + public void myTest() { + int mid; + int high; + // ... + + int lower, higher; // violation + // ... + + int value, + index; // violation + // ... + + int place = mid, number = high; // violation + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/NestedForDepth.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/NestedForDepth.java new file mode 100644 index 0000000000000000000000000000000000000000..3b97aa2e63f69442f5e47d6f7248aa60ff7fe1f8 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/NestedForDepth.java @@ -0,0 +1,15 @@ +public class NestedForDepth { + public void test(){ + for(int i=0; i<10; i++) { + for(int j=0; j ++a == 12; // violation + IntBinaryOperator obj2 = (int a, int b) -> { + a++; // violation + b += 12; // violation + return a + b; + }; + IntPredicate obj3 = a -> { + int b = a; // ok + return ++b == 12; + }; +} diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/SimplifyBooleanExpressionCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/SimplifyBooleanExpressionCase.java new file mode 100644 index 0000000000000000000000000000000000000000..ba82338f7203ad2948447a2e4b951c73262764c2 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/SimplifyBooleanExpressionCase.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +public class SimplifyBooleanExpressionCase { + + public void bar() { + boolean a, b; + Foo c, d, e; + + if (!false) {}; // violation, can be simplified to true + + if (a == true) {}; // violation, can be simplified to a + if (a == b) {}; // OK + if (a == false) {}; // violation, can be simplified to !a + if (!(a != true)) {}; // violation, can be simplified to a + + e = (a || b) ? c : d; // OK + e = (a || false) ? c : d; // violation, can be simplified to a + e = (a && b) ? c : d; // OK + + int s = 12; + boolean m = s > 1 ? true : false; // violation, can be simplified to s > 1 + boolean f = c == null ? false : c.someMethod(); // OK + } + +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/SimplifyBooleanReturnCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/SimplifyBooleanReturnCase.java new file mode 100644 index 0000000000000000000000000000000000000000..2fd8516aa625b19b6fb5b3bb06aaa109f8d3ae40 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/SimplifyBooleanReturnCase.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +public class SimplifyBooleanReturnCase { + + private boolean cond; + private Foo a; + private Foo b; + + public boolean check1() { + if (cond) { // violation, can be simplified + return true; + } + else { + return false; + } + } + + // Ok, simplified version of check1() + public boolean check2() { + return cond; + } + + // violations, can be simplified + public boolean check3() { + if (cond == true) { // can be simplified to "if (cond)" + return false; + } + else { + return true; // can be simplified to "return !cond" + } + } + + // Ok, can be simplified but doesn't return a Boolean + public Foo choose1() { + if (cond) { + return a; + } + else { + return b; + } + } + + // Ok, simplified version of choose1() + public Foo choose2() { + return cond ? a: b; + } + +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/StringLiteralEqualityCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/StringLiteralEqualityCase.java new file mode 100644 index 0000000000000000000000000000000000000000..5b0e488a013b7d99289cb3be9fe7f9f9944a6945 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/StringLiteralEqualityCase.java @@ -0,0 +1,17 @@ +public class StringLiteralEqualityCase { + public void test(){ + String status = "pending"; + + if (status == "done") {} // violation + + while (status != "done") {} // violation + + boolean flag = (status == "done"); // violation + + boolean flag = (status.equals("done")); // OK + + String name = "X"; + if (name == getName()) {} + // OK, limitation that check cannot tell runtime type returned from method call + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/SuperCloneCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/SuperCloneCase.java new file mode 100644 index 0000000000000000000000000000000000000000..64bdcc4a0cdc3436235c1796fc5b84ad0d652356 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/SuperCloneCase.java @@ -0,0 +1,23 @@ +class SuperCloneCase { + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } +} + +class SuperCloneB { + private int b; + + // violation below, "Method 'clone' should call 'super.clone'." + public SuperCloneB clone() { + SuperCloneB other = new SuperCloneB(); + other.b = this.b; + return other; + } +} + +class SuperCloneC { + + public SuperCloneC clone() throws CloneNotSupportedException { + return (SuperCloneC) super.clone(); + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/SuperFinalizeCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/SuperFinalizeCase.java new file mode 100644 index 0000000000000000000000000000000000000000..56cae17f828aca319a6fa4c8d6463a9db0daa09d --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/SuperFinalizeCase.java @@ -0,0 +1,15 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +public class SuperFinalizeCase { + protected void finalize() throws Throwable { + System.out.println("In finalize block"); + super.finalize(); // OK, calls super.finalize() + } +} +public class SuperFinalizeCase01 { + protected void finalize() throws Throwable { // violation + System.out.println("In finalize block"); + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/UnnecessarySemicolonAfterOuterTypeDeclarationCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/UnnecessarySemicolonAfterOuterTypeDeclarationCase.java new file mode 100644 index 0000000000000000000000000000000000000000..a3de063a3488c6a291d54095c1b4ec1395009533 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/UnnecessarySemicolonAfterOuterTypeDeclarationCase.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +class UnnecessarySemicolonAfterOuterTypeDeclarationCase { + class Nested { + + }; // OK, nested type declarations are ignored + +}; // violation + +interface B { + +}; // violation + +enum C { + +}; // violation + +@interface D { + +}; // violation \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/UnnecessarySemicolonAfterTypeMemberDeclarationCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/UnnecessarySemicolonAfterTypeMemberDeclarationCase.java new file mode 100644 index 0000000000000000000000000000000000000000..03cba00c26470591bc0a20b56f9b327767acb560 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/UnnecessarySemicolonAfterTypeMemberDeclarationCase.java @@ -0,0 +1,25 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +class UnnecessarySemicolonAfterTypeMemberDeclarationCase { + ; // violation, standalone semicolon + {}; // violation, extra semicolon after init block + static {}; // violation, extra semicolon after static init block + UnnecessarySemicolonAfterTypeMemberDeclarationCase(){}; // violation, extra semicolon after constructor definition + void method() {}; // violation, extra semicolon after method definition + int field = 10;; // violation, extra semicolon after field declaration + + { + ; // no violation, it is empty statement inside init block + } + + static { + ; // no violation, it is empty statement inside static init block + } + + void anotherMethod() { + ; // no violation, it is empty statement + if(true); // no violation, it is empty statement + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/UnnecessarySemicolonInTryWithResourcesCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/UnnecessarySemicolonInTryWithResourcesCase.java new file mode 100644 index 0000000000000000000000000000000000000000..0c77de0eec9690c41986b515ff2f4128780b865f --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/UnnecessarySemicolonInTryWithResourcesCase.java @@ -0,0 +1,15 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +class UnnecessarySemicolonInTryWithResourcesCase { + void method() throws IOException { + try(Reader r1 = new PipedReader();){} // violation + try(Reader r4 = new PipedReader();Reader r5 = new PipedReader() + ;){} // violation + try(Reader r6 = new PipedReader(); + Reader r7 + = new PipedReader(); + ){} + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/UnusedLocalVariableCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/UnusedLocalVariableCase.java new file mode 100644 index 0000000000000000000000000000000000000000..6f8232f006f468c28d5fc1f9ca8b6a9fe68b0b38 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/UnusedLocalVariableCase.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +class UnusedLocalVariableCase { + { + int k = 12; // violation, assign and update but never use 'k' + k++; + } + + int a; + UnusedLocalVariableCase(int a) { // ok, as 'a' is a constructor parameter not a local variable + this.a = 12; + } + + void method(int b) { + int[] arr = {1, 2, 3}; // violation, unused local variable 'arr' + int[] anotherArr = {1}; // ok, 'anotherArr' is accessed + anotherArr[0] = 4; + } + + String convertValue(String newValue) { + String s = newValue.toLowerCase(); // violation, unused local variable 's' + return newValue.toLowerCase(); + } + + void read() throws IOException { + BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + String s; // violation, unused local variable 's' + while ((s = reader.readLine()) != null) {} + try (BufferedReader reader1 = // ok, 'reader1' is a resource + new BufferedReader(new FileReader("abc.txt"))) {} + try { + } catch (Exception e) { // ok, 'e' is an exception parameter + } + } + + void loops() { + int j = 12; + for (int i = 0; j < 11; i++) { // violation, unused local variable 'i' + } + for (int p = 0; j < 11; p++) // ok, 'p' is used + p /= 2; + } + + void lambdas() { + Predicate obj = (String str) -> true; // ok, 'str' is a lambda parameter + obj.test("Test"); + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/coding/VariableDeclarationUsageDistanceCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/coding/VariableDeclarationUsageDistanceCase.java new file mode 100644 index 0000000000000000000000000000000000000000..5c962a13084e7a90e8ee4c67136469b239f426e8 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/coding/VariableDeclarationUsageDistanceCase.java @@ -0,0 +1,25 @@ +public class VariableDeclarationUsageDistanceCase { + + public void foo1() { + int num; // violation, distance = 4 + final double PI; // OK, final variables not checked + System.out.println("Statement 1"); + System.out.println("Statement 2"); + System.out.println("Statement 3"); + num = 1; + PI = 3.14; + } + + public void foo2() { + int a; // OK, used in different scope + int b; // OK, used in different scope + int count = 0; // OK, used in different scope + + { + System.out.println("Inside inner scope"); + a = 1; + b = 2; + count++; + } + } +} diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/headers_and_imports/AvoidStarImportCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/headers_and_imports/AvoidStarImportCase.java new file mode 100644 index 0000000000000000000000000000000000000000..98cc5c953127ce46f6e6e93b37e7e4d06b5c65f8 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/headers_and_imports/AvoidStarImportCase.java @@ -0,0 +1,8 @@ +import java.util.Scanner; +import java.io.*; // violation +import static java.lang.Math.*; // violation +import java.util.*; // violation +import java.net.*; // violation + +public class AvoidStarImportCase { +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/headers_and_imports/CustomImportOrderCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/headers_and_imports/CustomImportOrderCase.java new file mode 100644 index 0000000000000000000000000000000000000000..72480ec2c3baa6d73b1fe27e1b7e6166722dbcb8 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/headers_and_imports/CustomImportOrderCase.java @@ -0,0 +1,9 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +import org.apache.commons.io.FileUtils; // OK + + +public class ImportOrderRulesCase { +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/headers_and_imports/RedundantImportCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/headers_and_imports/RedundantImportCase.java new file mode 100644 index 0000000000000000000000000000000000000000..e6c42c315c8ddf227ef27ebae9841219a0ea28c5 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/headers_and_imports/RedundantImportCase.java @@ -0,0 +1,14 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +import static com.puppycrawl.tools.checkstyle.checks.imports.redundantimport.Example1.*; // OK, static import +import static java.lang.Integer.MAX_VALUE; // OK, static import + +// violation below, 'Redundant import from the same package' +import com.puppycrawl.tools.checkstyle.checks.imports.redundantimport.Example1; +import java.lang.String; // violation, "Redundant import from the java.lang package" +import java.util.Scanner; +import java.util.Scanner; // violation 'Duplicate import to line 18 - java.util.Scanner' + +public class RedundantImportCase{ } \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/headers_and_imports/RegexpHeaderCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/headers_and_imports/RegexpHeaderCase.java new file mode 100644 index 0000000000000000000000000000000000000000..5c237ede15be26df484117d6fd7baca7df75f8aa --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/headers_and_imports/RegexpHeaderCase.java @@ -0,0 +1,9 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +package demo; + +import java.lang.System; +public class RegexpHeaderCase { +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/headers_and_imports/UnusedImportsCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/headers_and_imports/UnusedImportsCase.java new file mode 100644 index 0000000000000000000000000000000000000000..f8bccea4ec72bc769798dfbc076204be5b58bea3 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/headers_and_imports/UnusedImportsCase.java @@ -0,0 +1,27 @@ +// limitation as it match field name in code +import java.awt.Component; //OK + +// no ability to recognize what import is not used +import static java.util.Map.copyOf; //OK +import static java.util.Arrays.copyOf; //OK + +import java.lang.String; // violation + +import java.util.Stack; +import java.util.Map; // violation + +import java.util.List; // violation +import java.util.function.Function; + +import static java.lang.Integer.parseInt; // violation + +/** + * {@link List} + */ +class UnusedImportsCase{ + Stack stack = new Stack(); + private Object Component; + int[] arr = {0,0}; + int[] array = copyOf(arr , 1); + Function stringToInteger = Integer::parseInt; +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/AtclauseOrderCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/AtclauseOrderCase.java new file mode 100644 index 0000000000000000000000000000000000000000..a860fcd95c009115060b523df224df4fb4d487fa --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/AtclauseOrderCase.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +/** + * Some javadoc. + * + * @author Some javadoc. + * @version Some javadoc. + * @param Some javadoc. + * @return Some javadoc. + * @throws Some javadoc. + * @exception Some javadoc. + * @see Some javadoc. + * @since Some javadoc. + * @serial Some javadoc. + * @serialField + * @serialData + */ +public class Example1 {} + +class Valid1 implements Serializable {} + +/** + * Some javadoc. + * + * @since Some javadoc. + * @version Some javadoc. // violation + * @deprecated + * @see Some javadoc. // violation + */ +class Invalid1 implements Serializable {} + +/** + * Some javadoc. + * + * @since Some javadoc. + * @version Some javadoc. // violation + * @deprecated + * @see Some javadoc. // violation + * @author max // violation + */ +enum Test1 {} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/InvalidJavadocPositionCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/InvalidJavadocPositionCase.java new file mode 100644 index 0000000000000000000000000000000000000000..040678708a84bf0e17ad5f1a7477346692972da5 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/InvalidJavadocPositionCase.java @@ -0,0 +1,13 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +@SuppressWarnings("serial") +// violation below, 'Javadoc comment is placed in the wrong location' +/** + * This comment looks like Javadoc but it is at an invalid location. + * Therefore, the text will not get into TestClass.html. + * And, the check will produce a violation. + */ +public class InvalidJavadocPositionCase { +} diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocBlockTagLocationCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocBlockTagLocationCase.java new file mode 100644 index 0000000000000000000000000000000000000000..c354dbda033443c97b8d16d7733fb2203bee55ab --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocBlockTagLocationCase.java @@ -0,0 +1,27 @@ +/** + * Some javadoc. + * + * Some javadoc. @author + * @version Some javadoc. + * Some javadoc. @param + * @return Some javadoc. + * @throws Some javadoc. + * @exception Some javadoc. + * @see Some javadoc. + * @since Some javadoc. + * @serial Some javadoc. + * @serialField + * @serialData + */ +public class JavadocBlockTagLocationCase { + + /** + * + * obj @param + * xx @return + */ + @Override + public boolean equals(Object obj) { + return super.equals(obj); + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocContentLocationCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocContentLocationCase.java new file mode 100644 index 0000000000000000000000000000000000000000..cd0bea1bb73f9ad6ee8715b0e580d1e77958de1b --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocContentLocationCase.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +class JavadocContentLocationCase { + + // violation below 'Javadoc content should start from the next line.' + /** This comment causes a violation because it starts from the first line + * and spans several lines. + */ + private int field1; + + /** + * This comment is OK because it starts from the second line. + */ + private int field12; + + /** This comment is OK because it is on the single-line. */ + private int field3; + + private int field4; +} diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocMethodCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocMethodCase.java new file mode 100644 index 0000000000000000000000000000000000000000..fbaecabdf81de616d58ac03442168f074521a598 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocMethodCase.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +public class JavadocMethodCase { + + /** + * + */ + Test(int x) { // violation, param tag missing for x + } + + /** + * + */ + public int foo(int p1) { // violation, param tag missing for p1 + return p1; // violation, return tag missing + } + + /** + * + * @param p1 The first number + */ + @Deprecated + private int boo(int p1) { + return p1; // ok, only public methods checked + } + + /** + * + */ + void bar(int p1) { // violation, param tag missing for p1 + } // ok, no return tag for void method +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocMissingLeadingAsteriskCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocMissingLeadingAsteriskCase.java new file mode 100644 index 0000000000000000000000000000000000000000..f606695006e84bcee78729f8105040f08017c62b --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocMissingLeadingAsteriskCase.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +/** + * Valid Java-style comment. + * + *
+ *   int value = 0;
+ * 
+ */ +class JavadocMissingLeadingAsteriskCase {} + +/** Valid Scala-style comment. + * Some description here. + **/ +class ScalaStyle {} + +/** ** + * Asterisks on first and last lines are optional. + * */ +class Asterisks {} + +/** No asterisks are required for single-line comments. */ +class SingleLine {} + +/** + + */ // violation above, 'Javadoc line should start with leading asterisk' +class BlankLine {} + +/** Wrapped + single-line comment */ +// violation above, 'Javadoc line should start with leading asterisk' +class Wrapped {} + +/** + *
+ int value; // violation, 'Javadoc line should start with leading asterisk'
+ * 
+ */ +class Example1 {} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocMissingWhitespaceAfterAsteriskCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocMissingWhitespaceAfterAsteriskCase.java new file mode 100644 index 0000000000000000000000000000000000000000..eb9b4196792b2bc471b76e3372ae3d0e9d51e7c7 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocMissingWhitespaceAfterAsteriskCase.java @@ -0,0 +1,22 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +/** This is valid single-line Javadoc. */ +class JavadocMissingWhitespaceAfterAsteriskCase { + /** + *This is invalid Javadoc. + */ + int invalidJavaDoc; + /** + * This is valid Javadoc. + */ + void validJavaDocMethod() { + } + /**This is invalid single-line Javadoc. */ + void invalidSingleLineJavaDocMethod() { + } + /** This is valid single-line Javadoc. */ + void validSingleLineJavaDocMethod() { + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocParagraphCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocParagraphCase.java new file mode 100644 index 0000000000000000000000000000000000000000..3bed5387eff2fec1db3a8300892a2efc170b9d33 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocParagraphCase.java @@ -0,0 +1,18 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +/** + * No tag (ok). + * + *

Tag immediately before the text (ok). + *

No blank line before the tag (violation). + * + *

+ * New line after tag (violation). + * + *

Whitespace after tag (violation). + * + */ +public class JavadocParagraphCase { +} diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocTagContinuationIndentationCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocTagContinuationIndentationCase.java new file mode 100644 index 0000000000000000000000000000000000000000..241eeb648a510efc6752f9af7366ea3bff0b3c31 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocTagContinuationIndentationCase.java @@ -0,0 +1,12 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +/** + * @tag comment + * Indentation spacing is 1. Line with violation + * Indentation spacing is 2. Line with violation + * Indentation spacing is 4. OK + */ +public class JavadocTagContinuationIndentationCase { +} diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocVariableCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocVariableCase.java new file mode 100644 index 0000000000000000000000000000000000000000..d191c7f8a8bcb4b3be7b9e13c814877b08d64cab --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/JavadocVariableCase.java @@ -0,0 +1,18 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +import java.util.logging.Logger; + +public class JavadocVariableCase { + private int a; + + /** + * Some description here + */ + private int b; + public Logger logger; + protected int c;// violation + public int d; // violation + /*package*/ int e; +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/MissingJavadocMethodCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/MissingJavadocMethodCase.java new file mode 100644 index 0000000000000000000000000000000000000000..3fb3b9076dead58839127d37879e274de711877b --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/MissingJavadocMethodCase.java @@ -0,0 +1,14 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +public class MissingJavadocMethodCase { + private String text; + + public void test() {} // violation, method is missing javadoc + public String getText() { return text; } // OK + public void setText(String text) { this.text = text; } // OK + private void test1() {} // OK + void test2() {} // OK + protected void test3() {} // violation, method is missing javadoc +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/MissingJavadocTypeCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/MissingJavadocTypeCase.java new file mode 100644 index 0000000000000000000000000000000000000000..5883e9d2a2181426ab8e8d8b0a17b0bb40930b81 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/MissingJavadocTypeCase.java @@ -0,0 +1,8 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +public class MissingJavadocTypeCase {} // violation +private class PublicClass {} +protected class PublicClass {} // violation +class PackagePrivateClass {} diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/NonEmptyAtclauseDescriptionCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/NonEmptyAtclauseDescriptionCase.java new file mode 100644 index 0000000000000000000000000000000000000000..cf0c23570f54bfba8b6cbb5af61a28c7f91e6c6f --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/NonEmptyAtclauseDescriptionCase.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +class NonEmptyAtclauseDescriptionCase +{ + // Violation for param "b" and at tags "deprecated", "throws" and "return". + /** + * Some summary. + * + * @param a Some description + * @param b + * @deprecated + * @throws Exception + * @return + */ + public int method(String a, int b) throws Exception { + return 1; + } + + // Violation for param "b" and at tags "deprecated", "throws" and "return". + /** + * Some summary. + * + * @param a Some description + * @param b + * @deprecated + * @throws Exception + * @return + */ + public int method(String a, int b) throws Exception { + return 1; + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/RequireEmptyLineBeforeBlockTagGroupCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/RequireEmptyLineBeforeBlockTagGroupCase.java new file mode 100644 index 0000000000000000000000000000000000000000..a9d36bada72ff0a4ea1cb2c268effd6119315e53 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/RequireEmptyLineBeforeBlockTagGroupCase.java @@ -0,0 +1,10 @@ +public class RequireEmptyLineBeforeBlockTagGroupCase { + /** + * testMethod's javadoc. + * @return something (violation) + */ + public boolean test() { + return false; + } +} + diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/SingleLineJavadocCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/SingleLineJavadocCase.java new file mode 100644 index 0000000000000000000000000000000000000000..985bd41c6fe5fe84b702b9cd1b99d6f18b82618a --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/SingleLineJavadocCase.java @@ -0,0 +1,32 @@ +public class SingleLineJavadocCase { + /** @see Math */ // violation, javadoc should be multiline + public int foo() { + return 42; + } + + /** + * @return 42 + */ // ok + public int bar() { + return 42; + } + + /** {@link #equals(Object)} */ // ok + public int baz() { + return 42; + } + + /** + *

the answer to the ultimate question + */ // ok + public int magic() { + return 42; + } + + /** + *

the answer to the ultimate question

+ */ // ok + public int foobar() { + return 42; + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/WriteTagCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/WriteTagCase.java new file mode 100644 index 0000000000000000000000000000000000000000..0ca813f3649d3d06606596caf773156d3874eee8 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/javadoc/WriteTagCase.java @@ -0,0 +1,11 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. + */ + +/** + * Some class + */ +public class WriteTagCase { // violation as required tag is missed + /** some doc */ + void foo() {} // OK, as methods are not checked by default +} diff --git a/component/CodeInspector/code_inspector/src/test/resources/case/whitespace/EmptyForInitializerPadCase.java b/component/CodeInspector/code_inspector/src/test/resources/case/whitespace/EmptyForInitializerPadCase.java new file mode 100644 index 0000000000000000000000000000000000000000..1360859b1e355a875d82e6067a04ff26e60bd5fc --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/case/whitespace/EmptyForInitializerPadCase.java @@ -0,0 +1,11 @@ +class EmptyForInitializerPadCase { + int i = 0; + void example() { + for ( ; i < 1; i++ ); // violation '';' is preceded with whitespace' + for (; i < 2; i++ ); + for (;i<2;i++); + for ( ;i<2;i++); // violation '';' is preceded with whitespace' + for ( + ; i < 2; i++ ); + } +} \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/annotations/AnnotationLocation.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/annotations/AnnotationLocation.xml new file mode 100644 index 0000000000000000000000000000000000000000..1fbf9f20f96b8ef7bbeb969de73dcdc38478e177 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/annotations/AnnotationLocation.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/annotations/SuppressWarnings.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/annotations/SuppressWarnings.xml new file mode 100644 index 0000000000000000000000000000000000000000..23033278ca641ef53d2661c0a47078ea170e5ed2 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/annotations/SuppressWarnings.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/block_checks/AvoidNestedBlocks.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/block_checks/AvoidNestedBlocks.xml new file mode 100644 index 0000000000000000000000000000000000000000..fa7d214ad29b59bdea3c6202be23a5a9620f21a7 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/block_checks/AvoidNestedBlocks.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/block_checks/EmptyBlock.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/block_checks/EmptyBlock.xml new file mode 100644 index 0000000000000000000000000000000000000000..b635734eb5248a020cd808b59f89fe6abb2c44a1 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/block_checks/EmptyBlock.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/block_checks/LeftCurly.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/block_checks/LeftCurly.xml new file mode 100644 index 0000000000000000000000000000000000000000..3e79ccd4e70ce2e40ccfdfd4833464332130f71f --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/block_checks/LeftCurly.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/block_checks/NeedBraces.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/block_checks/NeedBraces.xml new file mode 100644 index 0000000000000000000000000000000000000000..8d05ca11620ce3381dc8fe7ff1787a121164caf1 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/block_checks/NeedBraces.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/block_checks/RightCurly.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/block_checks/RightCurly.xml new file mode 100644 index 0000000000000000000000000000000000000000..d360f11602472b05a8d17fbf4b47eadafcef8029 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/block_checks/RightCurly.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/class_design/OneTopLevelClass.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/class_design/OneTopLevelClass.xml new file mode 100644 index 0000000000000000000000000000000000000000..97faf3ea9879c2715e4997214bad0c5c177e36d5 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/class_design/OneTopLevelClass.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/class_design/ThrowsCount.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/class_design/ThrowsCount.xml new file mode 100644 index 0000000000000000000000000000000000000000..8d09a68cd0aee84f39ec84529be9f0ab16338022 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/class_design/ThrowsCount.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/class_design/VisibilityModifier.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/class_design/VisibilityModifier.xml new file mode 100644 index 0000000000000000000000000000000000000000..71ffa952e29be6a60427d34da15cc01365f56e43 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/class_design/VisibilityModifier.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/ConstructorsDeclarationGrouping.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/ConstructorsDeclarationGrouping.xml new file mode 100644 index 0000000000000000000000000000000000000000..d0b5703b9b0c03fe1dcdd5c6e5c1969b9c4e7c55 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/ConstructorsDeclarationGrouping.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/CovariantEquals.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/CovariantEquals.xml new file mode 100644 index 0000000000000000000000000000000000000000..07bf2e4b7917c506af87d39f9768b4bf4fb6c4dc --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/CovariantEquals.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/DeclarationOrder.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/DeclarationOrder.xml new file mode 100644 index 0000000000000000000000000000000000000000..bdd048e477b00a0c22bdb8fd48868f5587b92fe3 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/DeclarationOrder.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/DefaultComesLast.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/DefaultComesLast.xml new file mode 100644 index 0000000000000000000000000000000000000000..82175b495d9bdd8451919509cedb780c24647ada --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/DefaultComesLast.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/EmptyStatement.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/EmptyStatement.xml new file mode 100644 index 0000000000000000000000000000000000000000..df04bd44e10dea77565b6daedd2c063b787ed172 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/EmptyStatement.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/EqualsAvoidNull.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/EqualsAvoidNull.xml new file mode 100644 index 0000000000000000000000000000000000000000..d8b94c14bfba788b872ec30004a91338f562ac5b --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/EqualsAvoidNull.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/EqualsHashCode.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/EqualsHashCode.xml new file mode 100644 index 0000000000000000000000000000000000000000..feff21d1730664077adb38c93e1bd58ad9b0f0d0 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/EqualsHashCode.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/FallThrough.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/FallThrough.xml new file mode 100644 index 0000000000000000000000000000000000000000..bfd6c91bf186961a9b877183b5be85eb5fe87f16 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/FallThrough.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/HiddenField.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/HiddenField.xml new file mode 100644 index 0000000000000000000000000000000000000000..ccd32bd068e886fe3e6b1adef916c7134208155c --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/HiddenField.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/IllegalCatch.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/IllegalCatch.xml new file mode 100644 index 0000000000000000000000000000000000000000..e14351f309763e3699ef91ef56931422bfdde321 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/IllegalCatch.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/IllegalInstantiation.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/IllegalInstantiation.xml new file mode 100644 index 0000000000000000000000000000000000000000..c41f1b1aa0d75205fc5a78ce117fc20266513a3a --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/IllegalInstantiation.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/IllegalThrows.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/IllegalThrows.xml new file mode 100644 index 0000000000000000000000000000000000000000..fc9d765efb09f1ffa26b465d2367a3d1737edc53 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/IllegalThrows.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/InnerAssignment.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/InnerAssignment.xml new file mode 100644 index 0000000000000000000000000000000000000000..dc38e559f9b58de3ae83afd72c8145b9aee63950 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/InnerAssignment.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/MagicNumber.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/MagicNumber.xml new file mode 100644 index 0000000000000000000000000000000000000000..07049c09ba24abd7443382a5e21108df2fad190f --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/MagicNumber.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/MissingSwitchDefault.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/MissingSwitchDefault.xml new file mode 100644 index 0000000000000000000000000000000000000000..756748fc0cda8722e1088ba5f7cef44863281ea7 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/MissingSwitchDefault.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/ModifiedControlVariable.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/ModifiedControlVariable.xml new file mode 100644 index 0000000000000000000000000000000000000000..3055d0900103e629b384ded451d322a78481fe78 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/ModifiedControlVariable.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/MultipleStringLiterals.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/MultipleStringLiterals.xml new file mode 100644 index 0000000000000000000000000000000000000000..8a34207a146f9e26264ae7227780b063eff4d09c --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/MultipleStringLiterals.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/MultipleVariableDeclarations.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/MultipleVariableDeclarations.xml new file mode 100644 index 0000000000000000000000000000000000000000..d61b18f03c6bae2df96318022f7cf30e847b44f6 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/MultipleVariableDeclarations.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/NestedForDepth.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/NestedForDepth.xml new file mode 100644 index 0000000000000000000000000000000000000000..c1ba77eadc3a88246f59114517df35483838d797 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/NestedForDepth.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/NestedIfDepth.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/NestedIfDepth.xml new file mode 100644 index 0000000000000000000000000000000000000000..99e5699c56075bd9d12e0ce44ac7fd7060af58b1 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/NestedIfDepth.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/NestedTryDepth.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/NestedTryDepth.xml new file mode 100644 index 0000000000000000000000000000000000000000..8b65abbe8a1d017c9c3a0f664a2a2662b261f955 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/NestedTryDepth.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/NoClone.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/NoClone.xml new file mode 100644 index 0000000000000000000000000000000000000000..25f7977489743a75b7c3d057cc3f33a37243e2c4 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/NoClone.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/NoFinalizer.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/NoFinalizer.xml new file mode 100644 index 0000000000000000000000000000000000000000..3d9e362ff93cdf7b837b1acec44bf261e5eacb95 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/NoFinalizer.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/OneStatementPerLine.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/OneStatementPerLine.xml new file mode 100644 index 0000000000000000000000000000000000000000..fee402cae0fb417099de6b962af38d0c0d986045 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/OneStatementPerLine.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/OverloadMethodsDeclarationOrder.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/OverloadMethodsDeclarationOrder.xml new file mode 100644 index 0000000000000000000000000000000000000000..246ecf855c9aaf0288241505382b97f49331c0d6 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/OverloadMethodsDeclarationOrder.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/ParameterAssignment.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/ParameterAssignment.xml new file mode 100644 index 0000000000000000000000000000000000000000..3db3cfb5c1724bf264487acfb2cc17cda1125b11 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/ParameterAssignment.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/SimplifyBooleanExpression.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/SimplifyBooleanExpression.xml new file mode 100644 index 0000000000000000000000000000000000000000..26d981650317b21052d95b5b27224c91e8b9ff96 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/SimplifyBooleanExpression.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/SimplifyBooleanReturn.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/SimplifyBooleanReturn.xml new file mode 100644 index 0000000000000000000000000000000000000000..57404fa151580e4be3de9ea2e53c6c9e3a747452 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/SimplifyBooleanReturn.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/StringLiteralEquality.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/StringLiteralEquality.xml new file mode 100644 index 0000000000000000000000000000000000000000..799fd74bea05388fa951562ac04a5aa546f804b5 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/StringLiteralEquality.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/SuperClone.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/SuperClone.xml new file mode 100644 index 0000000000000000000000000000000000000000..fab5cf4caf707b8607f7aa51a20a908916b1e765 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/SuperClone.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/UnnecessarySemicolonAfterOuterTypeDeclaration.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/UnnecessarySemicolonAfterOuterTypeDeclaration.xml new file mode 100644 index 0000000000000000000000000000000000000000..5a82a23b125039fad5291a25820b0a02b18b5cbb --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/UnnecessarySemicolonAfterOuterTypeDeclaration.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/UnnecessarySemicolonAfterTypeMemberDeclaration.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/UnnecessarySemicolonAfterTypeMemberDeclaration.xml new file mode 100644 index 0000000000000000000000000000000000000000..bb12170233d8519091701081105198d561cf3307 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/UnnecessarySemicolonAfterTypeMemberDeclaration.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/UnnecessarySemicolonInTryWithResources.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/UnnecessarySemicolonInTryWithResources.xml new file mode 100644 index 0000000000000000000000000000000000000000..650e24b81c3965014b10420f876e8edb85e502ac --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/UnnecessarySemicolonInTryWithResources.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/UnusedLocalVariable.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/UnusedLocalVariable.xml new file mode 100644 index 0000000000000000000000000000000000000000..a12652430ee1910d556078857c795b1fa39ef7d9 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/UnusedLocalVariable.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/VariableDeclarationUsageDistance.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/VariableDeclarationUsageDistance.xml new file mode 100644 index 0000000000000000000000000000000000000000..b7dad9f33cee392398b028d6655885de3587c31e --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/coding/VariableDeclarationUsageDistance.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/headers_and_imports/AvoidStarImport.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/headers_and_imports/AvoidStarImport.xml new file mode 100644 index 0000000000000000000000000000000000000000..fda29427f8897d11b4bb8000f90fc93b48546770 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/headers_and_imports/AvoidStarImport.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/headers_and_imports/CustomImportOrder.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/headers_and_imports/CustomImportOrder.xml new file mode 100644 index 0000000000000000000000000000000000000000..68c90e0f9fcc21057abf292770193f8c70bf7e33 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/headers_and_imports/CustomImportOrder.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/headers_and_imports/ImportOrder.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/headers_and_imports/ImportOrder.xml new file mode 100644 index 0000000000000000000000000000000000000000..6d81b43547c9cbc10b146ca98137ef74e19dbfc8 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/headers_and_imports/ImportOrder.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/headers_and_imports/RedundantImport.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/headers_and_imports/RedundantImport.xml new file mode 100644 index 0000000000000000000000000000000000000000..be9a535d6ad5ff2155c3973433bc50440074003f --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/headers_and_imports/RedundantImport.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/headers_and_imports/RegexpHeader.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/headers_and_imports/RegexpHeader.xml new file mode 100644 index 0000000000000000000000000000000000000000..f5f0b3c0672a0a24ca0c7ffe5a638570d02272cb --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/headers_and_imports/RegexpHeader.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/headers_and_imports/UnusedImports.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/headers_and_imports/UnusedImports.xml new file mode 100644 index 0000000000000000000000000000000000000000..abddcdc38384ce96a2a97aecafe71773f3234649 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/headers_and_imports/UnusedImports.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/AtclauseOrder.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/AtclauseOrder.xml new file mode 100644 index 0000000000000000000000000000000000000000..896f145c4c5703f798b3257cf2fea19df81b5c20 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/AtclauseOrder.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/InvalidJavadocPosition.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/InvalidJavadocPosition.xml new file mode 100644 index 0000000000000000000000000000000000000000..42db154bf0a487665e43a5fa47d72a81a706a455 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/InvalidJavadocPosition.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocBlockTagLocation.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocBlockTagLocation.xml new file mode 100644 index 0000000000000000000000000000000000000000..eacf1d0c1dd48a7debb306564f6278a4dfd07f52 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocBlockTagLocation.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocContentLocation.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocContentLocation.xml new file mode 100644 index 0000000000000000000000000000000000000000..4d675bfe6a91a68eee3f0480f46dd9fa13792415 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocContentLocation.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocMethod.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocMethod.xml new file mode 100644 index 0000000000000000000000000000000000000000..d5ba2c9f53b52eaeabea83d6694f0350153f45e9 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocMethod.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocMissingLeadingAsterisk.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocMissingLeadingAsterisk.xml new file mode 100644 index 0000000000000000000000000000000000000000..aa2b6fc2496b6ce80b6e345b128770bf5b9a4642 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocMissingLeadingAsterisk.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocMissingWhitespaceAfterAsterisk.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocMissingWhitespaceAfterAsterisk.xml new file mode 100644 index 0000000000000000000000000000000000000000..b060f9d1096f71101fa8a566f9de6b63ac544557 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocMissingWhitespaceAfterAsterisk.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocParagraph.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocParagraph.xml new file mode 100644 index 0000000000000000000000000000000000000000..9d24ef2068930b0285a2c622dc77e577a93d1bbf --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocParagraph.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocTagContinuationIndentation.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocTagContinuationIndentation.xml new file mode 100644 index 0000000000000000000000000000000000000000..cca92b4d81ba338b40e215b63aba677426889875 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocTagContinuationIndentation.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocVariable.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocVariable.xml new file mode 100644 index 0000000000000000000000000000000000000000..b4e710e43bc8f6f1a1184ebe3116203613b8178c --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/JavadocVariable.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/MissingJavadocMethod.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/MissingJavadocMethod.xml new file mode 100644 index 0000000000000000000000000000000000000000..1a804863498c113de0623e535810b20e6ffa3ed2 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/MissingJavadocMethod.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/MissingJavadocType.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/MissingJavadocType.xml new file mode 100644 index 0000000000000000000000000000000000000000..54e44963a228f5f28ca109e8876c0f02a80d8f86 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/MissingJavadocType.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/NonEmptyAtclauseDescription.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/NonEmptyAtclauseDescription.xml new file mode 100644 index 0000000000000000000000000000000000000000..a5e7b6b0e2dbaf8b81664a3c5e61bea1c0168fe8 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/NonEmptyAtclauseDescription.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/RequireEmptyLineBeforeBlockTagGroup.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/RequireEmptyLineBeforeBlockTagGroup.xml new file mode 100644 index 0000000000000000000000000000000000000000..73602fdb9b05762b1e231b4942209e155d4ec8ba --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/RequireEmptyLineBeforeBlockTagGroup.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/SingleLineJavadoc.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/SingleLineJavadoc.xml new file mode 100644 index 0000000000000000000000000000000000000000..debae0a0a674364ccdc6b8ae46dcbc1ec05dfbf9 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/SingleLineJavadoc.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/WriteTag.xml b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/WriteTag.xml new file mode 100644 index 0000000000000000000000000000000000000000..c4a21c72d1420117611d7bb11f37976d2b7164a0 --- /dev/null +++ b/component/CodeInspector/code_inspector/src/test/resources/single_rules/javadoc/WriteTag.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/component/CodeInspector/settings.gradle b/component/CodeInspector/settings.gradle new file mode 100644 index 0000000000000000000000000000000000000000..9123454cfc1d1366f02da8b19bfa4b7b28c5f46e --- /dev/null +++ b/component/CodeInspector/settings.gradle @@ -0,0 +1,11 @@ +/* + * This file was generated by the Gradle 'init' task. + * + * The settings file is used to specify which projects to include in your build. + * For more detailed information on multi-project builds, please refer to https://docs.gradle.org/8.8/userguide/multi_project_builds.html in the Gradle documentation. + */ + +rootProject.name = 'CodeInspector' + +include "code_inspector" +project(":code_inspector").name="code-inspector" \ No newline at end of file