Automatically Adding a Private Constructor with Sensei

Automatically Adding a Private Constructor with Sensei
In a utility class, when the fields and methods are static, there is no obvious reason why I would ever instantiate it.
e.g. UtilityClass utility = new UtilityClass();
The code below is a simple implementation of a Utility class.
public class UtilityClass {
public static final Boolean ULTIMATE_TRUTH = true;
public static boolean getTrue(){
return ULTIMATE_TRUTH;
}
}
This is the type of coding pattern that Static Analysis tools can pick up, but they often don't supply the ability to fix the issue.
I can use Sensei to identify the coding pattern, and automatically generate a private constructor to make it impossible for me to instantiate the class.
Searching for the Class
I will add a new recipe on the Utility class called:
- Static Classes: create private constructor.

And initially, I'll create a simple matcher to search for a class.
search:
class: {}
This will match any class, which is good enough to let me get started writing a Quick Fix. Once I have a Quick Fix that works, I'll refine the search to make it highlight when there is more likely to be a class that requires a private constructor.

Quick Fix
For the Quick Fix, I will want to generate a private constructor.
In the example class this would look like:
private UtilityClass(){}
To add the above code to my class, my Quick Fix will add a Method, and the name of the method will be a Mustache template that uses the name of the class.
availableFixes:
- name: "add private constructor"
actions:
- addMethod:
method: "private {{{ name }}}(){}"
In the GUI Editor, I use the Show Variables to create the Mustache template and then edit the field to add the private modifier, brackets, and braces to make it syntactically correct.

This would now allow me to add a private constructor to any class.
The QuickFix preview helps me because I can see the generated code as I write the Mustache template.

Now that I know I can fix the problem. I'll refine the search conditions to show the recipe when it is most appropriate.
Search for Missing Constructors
Ideally, I don't want to create a recipe that flags an error against every class. So I'll add some additional conditions in the search so that we only match on classes that do not have a constructor.
search:
class:
without:
child:
method:
constructor: true
The YAML is slightly different from the GUI.
In the GUI I configure it to look for a class without a child method which is a constructor 'yes'. We use 'yes' in the GUI instead of 'true' to make the GUI a little more human friendly.

This recipe will now only reveal itself for any class without a constructor.
Narrow Search for Likely Culprits
So I might want to go further and look for the presence of static methods or fields.
I look for any class without a constructor and which has either all public static fields or all public static methods.
search:
class:
with:
anyOf:
- child:
method:
allOf:
- modifier: "public"
- modifier: "static"
- child:
field:
allOf:
- modifier: "static"
- modifier: "public"
without:
child:
method:
constructor: true
Since Sensei is used to help me, as a programmer, in the IDE, rather than to statically analyze the code and report all errors, this filter is good enough to rule out most classes in my code base where I might have a good reason to have a default public constructor.
In some projects, this might be a step too far because the utility classes could have private methods, so I might choose to look for the presence of 'any' public static methods, rather than 'all'.
- child:
field:
anyOf:
- modifier: "static"
- modifier: "public"
Hints
Sensei is not designed to replace a Static Analysis tool. Sensei can augment a Static Analysis tool for common issues associated with your coding process, or technology. By replicating enough of the matching to highlight an issue, and supporting the development process by generating the QuickFix code.
What I'm trying to do is create a simple enough recipe that includes all the situations I need it, but filter it so that it doesn't get suggested in every class.
When working on recipes I try to de-risk them, in this case, I wasn't sure if I could create the private constructor so I created the QuickFix first. Then refactored the search conditions to make them more specific.
Sometimes when working on recipes I'm not sure how to perform the search, so I work on that first.
I find recipes easier to create when I build them incrementally, switching between refactoring of the QuickFix and the search.
---
You can install Sensei from within IntelliJ using "Preferences \ Plugins" (Mac) or "Settings \ Plugins" (Windows) then just search for "sensei secure code"
The source code and recipes for this can be found in the `sensei-blog-examples` repository in the Secure Code Warrior GitHub account, in the `pojoexamples` module.
https://github.com/securecodewarrior/sensei-blog-examples
Gouvernez le développement assisté par IA avant la mise en production
Évaluez les risques liés à l'IA, appliquez des politiques de codage sécurisé dès le commit et accélérez la livraison sécurisée tout au long de votre cycle de développement.
Explorer plus d'articles
Accédez à du contenu expert sur le codage sécurisé, la gouvernance de l'IA et la gestion des risques logiciels.
%252520%252520(3).avif)
Supercharged Security Awareness: How Tournaments are Inspiring Developers at Erste Group
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.

Security as culture: How Blue Prism cultivates world-class secure developers
Learn how Blue Prism, the global leader in intelligent automation for the enterprise, used Secure Code Warrior's agile learning platform to create a security-first culture with their developers, achieve their business goals, and ship secure code at speed

Une culture de la sécurité : comment Sage a bâti son programme de champions
Découvrez comment Sage a renforcé sa sécurité grâce à une approche flexible axée sur les relations humaines, formant plus de 200 champions de la sécurité et obtenant une réduction mesurable des risques.

Mois de la sensibilisation à la cybersécurité 2026 : comprendre la sécurité de l'IA est essentiel à la défense de l'entreprise
Nous avons atteint un point où la plupart des grandes entreprises d'IA ont publiquement appelé à un ralentissement mondial du développement de l'IA afin de permettre aux garde-fous de sécurité de rattraper leur retard. Pourtant, la réalité est que de graves problèmes de sécurité se sont déjà concrétisés. Cela place d'innombrables responsables de la sécurité et équipes de direction dans une situation d'urgence, alors que les coûts financiers et les risques liés à la mise en œuvre de l'IA ne cessent de s'accumuler.

Levier 8 : Valoriser votre programme
Un programme de codage sécurisé a besoin de plus qu'un contenu de qualité pour réussir. Le levier 8 montre comment l'image de marque transforme la participation en une expérience convoitée, axée sur le statut.
.avif)
Chaque employé est désormais en première ligne de la cybersécurité IA
Le paysage technologique des entreprises évolue à une vitesse que peu avaient anticipée avant qu'il ne soit trop tard. Nous avons officiellement franchi le cap du code écrit par l'humain et de l'assistance basique par copilote pour entrer dans l'ère du cycle de vie de développement agentique (ADLC). Si les agents IA autonomes promettent une efficacité sans précédent dans de multiples fonctions, ils apportent également une toute nouvelle catégorie de risques en matière de sécurité et de réglementation.
Sécurisez le développement piloté par l'IA avant le déploiement
Identifiez les risques de développement, appliquez vos politiques et prévenez les vulnérabilités tout au long du cycle de vie de vos logiciels.
