Skip to content

Commit 5c82668

Browse files
(Sonar) Fixed finding: "Utility classes should not have public constructors" (#395)
## Remediation This change fixes "Utility classes should not have public constructors" (id = [java:S1118](https://rules.sonarsource.com/java/RSPEC-1118/)) identified by Sonar. ## Details This change adds private constructors to utility classes. Utility classes are only meant to be accessed statically. Since they're not meant to be instantiated, we can use the Java's code visibility protections to hide the constructor and prevent unintended or malicious access. Our changes look something like this: ```diff public class Utils { + private Utils() {} ... ``` <details> <summary>More reading</summary> * [https://rules.sonarsource.com/java/RSPEC-1118/](https://rules.sonarsource.com/java/RSPEC-1118/) </details> 🧚🤖 Powered by Pixeebot [Feedback](https://ask.pixee.ai/feedback) | [Community](https://pixee-community.slack.com/signup#/domain-signup) | [Docs](https://docs.pixee.ai/) | Codemod ID: [sonar:java/avoid-implicit-public-constructor-s1118](https://docs.pixee.ai/codemods/java/sonar_java_avoid-implicit-public-constructor-s1118) ![](https://d1zaessa2hpsmj.cloudfront.net/pixel/v1/track?writeKey=2PI43jNm7atYvAuK7rJUz3Kcd6A&event=DRIP_PR%7Cpixee%2Fcodemodder-java%7C23119153cf3dc9489e82f2ebd71777202ecb522e) <!--{"type":"DRIP","codemod":"sonar:java/avoid-implicit-public-constructor-s1118"}--> --------- Co-authored-by: pixeebot[bot] <104101892+pixeebot[bot]@users.noreply.github.com> Co-authored-by: pixeebot[bot] <pixeebot[bot]@users.noreply.github.com>
1 parent ec403a7 commit 5c82668

File tree

6 files changed

+12
-0
lines changed

6 files changed

+12
-0
lines changed

core-codemods/src/main/java/io/codemodder/codemods/ResourceLeakFixer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
*/
3232
final class ResourceLeakFixer {
3333

34+
private ResourceLeakFixer() {}
35+
3436
private static final Logger LOG = LoggerFactory.getLogger(ResourceLeakFixer.class);
3537

3638
private static final String rootPrefix = "resource";

framework/codemodder-base/src/main/java/io/codemodder/Runner.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
/** Provides an entrypoint for CLIs built with the codemodder framework. */
88
public final class Runner {
99

10+
private Runner() {}
11+
1012
/**
1113
* Runs the codemods with a set of customized streams.
1214
*

framework/codemodder-base/src/main/java/io/codemodder/ast/ASTTransforms.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
public final class ASTTransforms {
3232
/** Add an import in alphabetical order. */
33+
private ASTTransforms() {}
34+
3335
public static void addImportIfMissing(final CompilationUnit cu, final String className) {
3436
final NodeList<ImportDeclaration> imports = cu.getImports();
3537
final ImportDeclaration newImport = new ImportDeclaration(className, false, false);

framework/codemodder-base/src/main/java/io/codemodder/ast/ASTs.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
/** A static library for querying and returning patterns over AST nodes. */
3232
public final class ASTs {
3333

34+
private ASTs() {}
35+
3436
/**
3537
* Test for this pattern: {@link AssignExpr} -&gt; {@link Expression} ({@code expr}), where
3638
* ({@code expr}) is the right hand side expression of the assignment.

framework/codemodder-base/src/main/java/io/codemodder/ast/NameResolver.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
/** Find sources of names in JavaParser ASTs. */
1111
final class NameResolver {
1212

13+
private NameResolver() {}
14+
1315
private static Optional<Node> isLocalNameSource(final Node n, final String name) {
1416
final Optional<Node> maybe =
1517
ASTs.isExpressionStmtDeclarationOf(n, name).map(Triplet::getValue2);

framework/codemodder-testutils-llm/src/main/java/io/codemodder/testutils/llm/CodemodderOpenAIKeys.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
@SuppressWarnings("unused")
88
public final class CodemodderOpenAIKeys {
99

10+
private CodemodderOpenAIKeys() {}
11+
1012
/**
1113
* Return true if and only if we have a non-empty {@code CODEMODDER_OPENAI_API_KEY} environment
1214
* variable.

0 commit comments

Comments
 (0)