<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Secure Code Warrior</title>
    <description>The latest articles on DEV Community by Secure Code Warrior (@securecodewarrior).</description>
    <link>https://dev.to/securecodewarrior</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F3261%2F8c250a65-534f-469d-a4d2-2d7370e1b042.png</url>
      <title>DEV Community: Secure Code Warrior</title>
      <link>https://dev.to/securecodewarrior</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/securecodewarrior"/>
    <language>en</language>
    <item>
      <title>Try This Online Java Gotchas Quiz</title>
      <dc:creator>Alan Richardson</dc:creator>
      <pubDate>Fri, 19 Feb 2021 11:07:48 +0000</pubDate>
      <link>https://dev.to/securecodewarrior/try-this-online-java-gotchas-quiz-4kce</link>
      <guid>https://dev.to/securecodewarrior/try-this-online-java-gotchas-quiz-4kce</guid>
      <description>&lt;p&gt;A previous blog post described the Java Gotcha "Bitwise vs Boolean Operator".&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/securecodewarrior/java-gotchas-bitwise-vs-boolean-operators-11km"&gt;Java Gotchas - Bitwise vs Boolean Operator&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We added a variant of this, and some other Java Gotchas into a fun little Quiz called "&lt;a href="//scw.typeform.com/to/rgw7q7OE"&gt;Challenge The Sensei&lt;/a&gt;".&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://scw.typeform.com/to/rgw7q7OE"&gt;scw.typeform.com/to/rgw7q7OE&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you've read the blog post above then you'll be in a good place to ace at least one of the questions.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;But your friends might not, so if you find the quiz fun, you can share it with them and see if they score as well as you.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Since we don't want to just quiz you. We want to try and use this to help educate and codify the knowledge. So we have created a Github repo that has runnable code examples for the problem, and the solution.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/SecureCodeWarrior/challenge-the-sensei"&gt;github.com/SecureCodeWarrior/challenge-the-sensei&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is a &lt;a href="https://plugins.jetbrains.com/plugin/14015-sensei-by-secure-code-warrior-"&gt;Sensei&lt;/a&gt; enabled repo.&lt;/p&gt;

&lt;p&gt;When you clone the repo and load it into IntelliJ, assuming you have the Secure Code Warrior &lt;a href="https://plugins.jetbrains.com/plugin/14015-sensei-by-secure-code-warrior-"&gt;Sensei&lt;/a&gt; IntelliJ Plugin installed, then it will automatically see that you have a &lt;code&gt;.sensei&lt;/code&gt; folder, and load the Sensei recipes.&lt;/p&gt;

&lt;p&gt;When browsing the code in the IDE you should see IntelliJ prompt you that the error exists in the code, and this should make it easier to see the gotcha in the code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hover over the highlighted code then you'll see a prompt telling you about the error&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;Show Context Action&lt;/code&gt; key: &lt;code&gt;alt+enter&lt;/code&gt; (Windows) &lt;code&gt;option+enter&lt;/code&gt; (macOS) and we may have a QuickFix available that can fix the code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sensei recipes have been added for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/SecureCodeWarrior/challenge-the-sensei/blob/master/src/main/java/com/securecodewarrior/bitwiseoperators"&gt;Bitwise operators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/SecureCodeWarrior/challenge-the-sensei/blob/master/src/main/java/com/securecodewarrior/splitipaddress"&gt;Split IP Address&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/SecureCodeWarrior/challenge-the-sensei/blob/master/src/test/java/com/securecodewarrior/assertorder"&gt;Assert Order&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We are adding more recipes and more explanatory text to cover the rest of the code in the future... but don't let that stop you from having a look at the code and spotting the error yourself.&lt;/p&gt;

&lt;p&gt;And remember to try the quiz and "&lt;a href="https://scw.typeform.com/to/rgw7q7OE"&gt;Challenge the Sensei&lt;/a&gt;"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://scw.typeform.com/to/rgw7q7OE"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m3-VpP5W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ka6obg1imsnuqbkc4i94.gif" alt="Challenge the Sensei" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Running IntelliJ Inspections From Continuous Integration</title>
      <dc:creator>Alan Richardson</dc:creator>
      <pubDate>Mon, 15 Feb 2021 14:06:05 +0000</pubDate>
      <link>https://dev.to/securecodewarrior/running-intellij-inspections-from-continuous-integration-472a</link>
      <guid>https://dev.to/securecodewarrior/running-intellij-inspections-from-continuous-integration-472a</guid>
      <description>&lt;p&gt;IntelliJ IDEA offers functionality to help improve our coding, within the IDE when writing code as Intentions. Intentions can be used in batch to inspect code for patterns throughout the source and even extending to Command-Line analysis or added to Continuous Integration. This post covers the out of the box IntelliJ functionality and expanding with custom Intentions created in Sensei.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/Gk0ASNhKH2Q"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  IntelliJ Inspections
&lt;/h2&gt;

&lt;p&gt;The Inspections feature of IntelliJ drives the display of many of the errors that are reported dynamically in the IDE when coding e.g.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;detecting abstract classes that can be converted to interfaces,&lt;/li&gt;
&lt;li&gt;identifying redundant class fields which can be local,&lt;/li&gt;
&lt;li&gt;warning about uses of deprecated methods,&lt;/li&gt;
&lt;li&gt;etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These Inspections highlight code that matches in the IDE as Intention Actions which often have an associated QuickFix.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Vw7LM-FY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/wid0dpawgi0pylfsbmz9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Vw7LM-FY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/wid0dpawgi0pylfsbmz9.png" alt="Intention Actions" width="800" height="195"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The real-time IDE highlighting when code matches an Inspection can help us improve our coding dynamically. After identifying the issue in the code, using IntelliJ Intention Actions to QuickFix the code can reinforce better patterns.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inspections Profile
&lt;/h2&gt;

&lt;p&gt;Inspections can run as a batch from within the IDE, and from the Command Line or in a Continuous Integration process.&lt;/p&gt;

&lt;p&gt;The key to working with IntelliJ inspections in a batch is through the use of an Inspections Profile.&lt;/p&gt;

&lt;p&gt;IntelliJ has two default Inspection Profiles: one stored in the Project, and one stored in the IDE.&lt;/p&gt;

&lt;p&gt;New Inspection Profiles can be created to configure specific plugins or use-cases e.g.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run Checkstyle real-time scan only&lt;/li&gt;
&lt;li&gt;Run a specific set of Sensei rules&lt;/li&gt;
&lt;li&gt;Run the HTML checks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Inspections in a profile can be enabled or disabled from the IntelliJ Preferences. The Preferences dialog is also an easy way to learn the range of Inspections available.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--62S9EPSj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/iqmsu8m0kznz7gbp9096.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--62S9EPSj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/iqmsu8m0kznz7gbp9096.png" alt="Configure Inspection Preferences" width="800" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The ‘tool’ icon allows you to duplicate a Profile and create a new Profile to collect a specific set of rules.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Lsbw8KZ4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/njmfqi40wd3lejreh10r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Lsbw8KZ4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/njmfqi40wd3lejreh10r.png" alt="Duplicate Sensei Profile" width="403" height="207"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Running an Inspection Profile in the IDE
&lt;/h2&gt;

&lt;p&gt;Inspection Profiles can be run from within the IDE using the &lt;code&gt;Analyze \ Inspect Code...&lt;/code&gt; menu.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lxvJph5N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/a26tcvg25pjuiaxvm9e8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lxvJph5N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/a26tcvg25pjuiaxvm9e8.png" alt="Analyze Inspect Code" width="470" height="145"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Analyze functionality allows you to control the scope that the Inspection will run against e.g. the whole project, including or excluding test sources, or against a specific set of files.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Boxk_N68--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/35h9wmnnjmvyvs3i5wir.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Boxk_N68--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/35h9wmnnjmvyvs3i5wir.png" alt="Specify Inspection Scope" width="496" height="323"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also manage the Inspection Profiles from here to create or configure a particular profile.&lt;/p&gt;

&lt;p&gt;Clicking &lt;code&gt;[OK]&lt;/code&gt; on the “Specify Inspection Scope” dialog will trigger IntelliJ into running all the selected Inspections in the profile across the defined scope.&lt;/p&gt;

&lt;p&gt;IntelliJ will report the results of running the Inspections in the &lt;code&gt;Inspection Results&lt;/code&gt; tab.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BKq4ynln--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/oocl2ytwk2yhcpr3ffld.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BKq4ynln--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/oocl2ytwk2yhcpr3ffld.png" alt="Inspection Results" width="800" height="228"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://www.securecodewarrior.com/sensei"&gt;Sensei&lt;/a&gt; plugin from Secure Code Warrior allows you to create custom code matching recipes. Sensei tightly integrates with IntelliJ to make these custom recipes as natural to use as the IntelliJ Intention Actions. Meaning they are loaded into IntelliJ as Inspections and can be grouped, enabled, and disabled using Inspection Profiles. Creating a custom Inspection Profile and then using the Analyze Inspect Code functionality is the recommended way of running Sensei recipes in bulk across a project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running an Inspection Profile from the Command Line
&lt;/h2&gt;

&lt;p&gt;IntelliJ has the ability to run inspections from the command line as documented by JetBrains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.jetbrains.com/help/idea/working-with-the-ide-features-from-command-line.html"&gt;https://www.jetbrains.com/help/idea/working-with-the-ide-features-from-command-line.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I primarily use Mac OS, and can run a single instance of IntelliJ from the command line with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;open -na "IntelliJ IDEA CE.app"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To support easier execution I add this to a shell command script.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;vi /usr/local/bin/idea&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The contents of the script are from the official documentation provided by IntelliJ.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/sh

open -na "IntelliJ IDEA CE.app" --args "$@"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I then made this executable to allow me to simplify the command line inspection process.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;chmod 755 /usr/local/bin/idea&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The official intellij docs describe the general form of the inspection command as:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;idea inspect &amp;lt;project&amp;gt; &amp;lt;inspection-profile&amp;gt; &amp;lt;output&amp;gt; [&amp;lt;options&amp;gt;]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In practice, I fully qualify the paths:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;idea inspect /Users/user/GitHub/sensei-blog-examples /Users/user/GitHub/sensei-blog-examples/.idea/inspectionProfiles/senseiprofile.xml /Users/user/GitHub/sensei-blog-examples/scan-results
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This runs all the Inspections I added to the &lt;code&gt;senseiprofile&lt;/code&gt; and reports the results in the &lt;code&gt;scan-results&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--i-iAFeoP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/dl1ijtfkmhfhx3rg2h8e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--i-iAFeoP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/dl1ijtfkmhfhx3rg2h8e.png" alt="After Scan Results" width="484" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Viewing Inspection Results
&lt;/h2&gt;

&lt;p&gt;We can report these results from within Continuous Integration, as we’ll see later.&lt;/p&gt;

&lt;p&gt;We can also view them within IntelliJ itself using the &lt;code&gt;Analyse \ View Offline Inspection Results…&lt;/code&gt; feature.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TwUmRo1k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/x1rp302i7zccy82xeq9n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TwUmRo1k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/x1rp302i7zccy82xeq9n.png" alt="View Offline Inspection Results" width="324" height="147"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will load the results into the &lt;code&gt;Inspection Results&lt;/code&gt; tab.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Y78hjNqO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/fxd2udodbsew8nadaee9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y78hjNqO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/fxd2udodbsew8nadaee9.png" alt="Offline Inspection Results" width="800" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is officially documented on the JetBrains site:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.jetbrains.com/help/idea/command-line-code-inspector.html#inspection-results"&gt;https://www.jetbrains.com/help/idea/command-line-code-inspector.html#inspection-results&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This might be used during a code review process if the command line execution was incorporated into a Continuous Integration process and the reviewers wanted to check the full source context of any of the Inspection result entries.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inspection Profiles in Continuous Integration
&lt;/h2&gt;

&lt;p&gt;When adding the Command Line inspection into Continuous Integration we ideally want a report to be generated automatically and there are a number of options open to us.&lt;/p&gt;

&lt;p&gt;TeamCity offers out of the box support for Inspection Profiles in Continuous Integration.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.jetbrains.com/help/teamcity/inspections.html"&gt;https://www.jetbrains.com/help/teamcity/inspections.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Jenkins Warnings NG plugin supports the Command Line output from IntelliJ Inspections as one of the report formats.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/jenkinsci/warnings-ng-plugin"&gt;https://github.com/jenkinsci/warnings-ng-plugin&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/jenkinsci/warnings-ng-plugin/blob/master/SUPPORTED-FORMATS.md"&gt;https://github.com/jenkinsci/warnings-ng-plugin/blob/master/SUPPORTED-FORMATS.md&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Community projects like &lt;code&gt;idea CLI Inspector&lt;/code&gt; exist to support using Inspection Profiles in other CI tooling i.e.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/bentolor/idea-cli-inspector"&gt;https://github.com/bentolor/idea-cli-inspector&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The future of Inspection Profiles in a CI process looks even brighter with the introduction of the JetBrains Qodana project. The Qodana project is a headless version of IntelliJ with official Github Actions and Docker images.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/JetBrains/Qodana"&gt;https://github.com/JetBrains/Qodana&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Qodana is currently in beta, but the Sensei team is monitoring it so that it becomes an officially supported platform for running Sensei rules as part of Continuous Integration.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Intention Actions allow us to reinforce coding patterns and quickly fix them in the IDE when we make mistakes during coding.&lt;/p&gt;

&lt;p&gt;Inspection Profiles allow us to collect these into profiles which can run in batch as an Analyze and Inspect Code action. This can be useful if we encounter a pattern and want to double-check if we have missed that anywhere else in our code.&lt;/p&gt;

&lt;p&gt;Inspection Profiles can be run from the command line and even incorporated into Continuous Integration processes supporting a “trust, but verify” model and catch any accidental slippage.&lt;/p&gt;

&lt;p&gt;All of the above is built in IntelliJ functionality and JetBrains are improving their Continuous Integration process with the introduction of Qodana.&lt;/p&gt;

&lt;p&gt;Sensei recipes are loaded into IntelliJ to act as native Intention Actions and be collected into Inspection Profiles to support batch checking through Inspect Code and Continuous Integration support provided by the official JetBrains Command Line execution functionality.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;You can install Sensei from within IntelliJ using "Preferences \ Plugins" (Mac) or "Settings \ Plugins" (Windows)  then just search for "sensei secure code". Or install from the &lt;a href="https://plugins.jetbrains.com/plugin/14015-sensei-by-secure-code-warrior-"&gt;Jetbrains Marketplace Online&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.securecodewarrior.com/sensei?pk_source=contentsyndication&amp;amp;pk_medium=devto&amp;amp;pk_campaign=sensei_devrel&amp;amp;pk_content=generic"&gt;Learn more about Sensei&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>intellij</category>
    </item>
    <item>
      <title>Java Gotchas - Bitwise vs Boolean Operators</title>
      <dc:creator>Alan Richardson</dc:creator>
      <pubDate>Thu, 11 Feb 2021 15:09:24 +0000</pubDate>
      <link>https://dev.to/securecodewarrior/java-gotchas-bitwise-vs-boolean-operators-11km</link>
      <guid>https://dev.to/securecodewarrior/java-gotchas-bitwise-vs-boolean-operators-11km</guid>
      <description>&lt;h1&gt;
  
  
  Java Gotchas - Bitwise vs Boolean Operators
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;"Java Gotcha" - a common mistake pattern that is easy to accidentally implement.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A fairly simple Java Gotcha to accidentally fall into is: using a Bitwise operator instead of a Boolean Comparison operator.&lt;/p&gt;

&lt;p&gt;e.g. a simple mistype can result in writing "&amp;amp;" when you really meant to write "&amp;amp;&amp;amp;".&lt;/p&gt;

&lt;p&gt;A common heuristic we learn when reviewing code is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"&amp;amp;" or "|" when used in a conditional statement is probably not intended.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this section we will explore this heuristic a little and identify ways we can identify and fix this.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's the Problem? Bitwise operations work fine with Booleans
&lt;/h2&gt;

&lt;p&gt;Using Bitwise operators with Booleans is perfectly valid, so Java will not report a syntax error.&lt;/p&gt;

&lt;p&gt;If I construct a JUnit Test to explore a truth table for both Bitwise OR (&lt;code&gt;|&lt;/code&gt;) and Bitwise AND (&lt;code&gt;&amp;amp;&lt;/code&gt;) then we will see that the outputs from the Bitwise operator match the truth table. Given this, we might think that the use of Bitwise operators is not an issue.&lt;/p&gt;

&lt;h3&gt;
  
  
  AND Truth Table
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ybcI35wC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/i29h2ypqcxg92md4l0j4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ybcI35wC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/i29h2ypqcxg92md4l0j4.png" alt="a AND b truth table" width="179" height="183"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    @Test
    void bitwiseOperatorsAndTruthTable(){
        Assertions.assertEquals(true, true &amp;amp; true);
        Assertions.assertEquals(false, true &amp;amp; false);
        Assertions.assertEquals(false, false &amp;amp; true);
        Assertions.assertEquals(false, false &amp;amp; false);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  OR Truth Table
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cDbhH-_f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/gegox5p2bwdkxf1cmqvs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cDbhH-_f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/gegox5p2bwdkxf1cmqvs.png" alt="a OR b truth table" width="179" height="183"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    @Test
    void bitwiseOperatorsOrTruthTable(){
        Assertions.assertEquals(true, true | true);
        Assertions.assertEquals(true, true | false);
        Assertions.assertEquals(true, false | true);
        Assertions.assertEquals(false, false | false);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Truth table images were created using the web.standfor.edu &lt;a href="https://web.stanford.edu/class/cs103/tools/truth-table-tool/"&gt;truth table tool&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Issue: Short Circuit Operation
&lt;/h2&gt;

&lt;p&gt;The real issue is the difference in behaviour between Bitwise (&lt;code&gt;&amp;amp;&lt;/code&gt;, &lt;code&gt;|&lt;/code&gt;) and Boolean (&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;, &lt;code&gt;||&lt;/code&gt;) operators.&lt;/p&gt;

&lt;p&gt;A Boolean operator is a short circuit operator and only evaluates as much as it needs to.&lt;/p&gt;

&lt;p&gt;e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (args != null &amp;amp; args.length() &amp;gt; 23) {
    System.out.println(args);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, both boolean conditions will evaluate, because the Bitwise operator has been used:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;args != null&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;args.length() &amp;gt; 23&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This leaves my code open to a &lt;code&gt;NullPointerException&lt;/code&gt; if &lt;code&gt;args&lt;/code&gt; is &lt;code&gt;null&lt;/code&gt; because we will always perform the check for &lt;code&gt;args.length&lt;/code&gt;, even when &lt;code&gt;args&lt;/code&gt; is &lt;code&gt;null&lt;/code&gt; because both boolean conditions have to be evaluated.&lt;/p&gt;

&lt;h2&gt;
  
  
  Boolean Operators Short Circuit Evaluation
&lt;/h2&gt;

&lt;p&gt;When an &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; is used e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (args != null &amp;amp;&amp;amp; args.length() &amp;gt; 23) {
    System.out.println(args);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As soon as we know that &lt;code&gt;args != null&lt;/code&gt; evaluates to &lt;code&gt;false&lt;/code&gt; the condition expression evaluation stops.&lt;/p&gt;

&lt;p&gt;We don't need to evaluate the right-hand side.&lt;/p&gt;

&lt;p&gt;Whatever the outcome of the right-hand side condition, the final value of the Boolean expression will be false.&lt;/p&gt;

&lt;h2&gt;
  
  
  But this would never happen in production code
&lt;/h2&gt;

&lt;p&gt;This is a pretty easy mistake to make, and is not always picked up by Static Analysis tools.&lt;/p&gt;

&lt;p&gt;I used the following Google Dork to see if I could find any public examples of this pattern:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;filetype:java if "!=null &amp;amp; "&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This search brought back some code from Android in the &lt;code&gt;RootWindowContainer&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;isDocument = intent != null &amp;amp; intent.isDocument()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the type of code that might pass a code review because we often do use Bitwise operators in assignment statements to mask values. But in this instance, the outcome is the same as the &lt;code&gt;if&lt;/code&gt; statement example above. If &lt;code&gt;intent&lt;/code&gt; is ever null, then a &lt;code&gt;NullPointerException&lt;/code&gt; will be thrown.&lt;/p&gt;

&lt;p&gt;Very often we get away with this construct because we often code defensively and write redundant code. The check for &lt;code&gt;!= null&lt;/code&gt; may well be redundant in most use cases.&lt;/p&gt;

&lt;p&gt;This is an error made by programmers in production code.&lt;/p&gt;

&lt;p&gt;I don't know how current the results for the search are, but when I ran the search there were results back with code from: Google, Amazon, Apache... and me.&lt;/p&gt;

&lt;p&gt;A recent &lt;a href="https://github.com/eviltester/thingifier/pull/8/commits/5134461d7eb2e2208e421990e15b943ba3fd4b6d"&gt;pull request&lt;/a&gt; on one of my open source projects was to address exactly this error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(type!=null &amp;amp; type.trim().length()&amp;gt;0){
    acceptMediaTypeDefinitionsList.add(type.trim());
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to Find This
&lt;/h2&gt;

&lt;p&gt;When I checked my sample code in a few static analysers, none of them picked up this hidden self-destruct code.&lt;/p&gt;

&lt;p&gt;As a team at Secure Code Warrior, we created and reviewed a fairly simple Sensei recipe that could pick this up.&lt;/p&gt;

&lt;p&gt;Because Bitwise operators are perfectly valid, and often used in assignments we focussed on the use-case of &lt;code&gt;if&lt;/code&gt; statements, and the use of Bitwise &lt;code&gt;&amp;amp;&lt;/code&gt;, to find the problematic code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;search:
  expression:
    anyOf:
    - in:
        condition: {}
    value:
      caseSensitive: false
      matches: ".* &amp;amp; .*"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This uses a regular expression to match &lt;code&gt;" &amp;amp; "&lt;/code&gt; when it is used as a condition expression. e.g. in an &lt;code&gt;if&lt;/code&gt; statement.&lt;/p&gt;

&lt;p&gt;To fix it, we again relied on regular expressions. This time using the &lt;code&gt;sed&lt;/code&gt; function in the QuickFix to globally replace the &lt;code&gt;&amp;amp;&lt;/code&gt; in the expression with &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;availableFixes:
  - name: "Replace bitwise AND operator to logical AND operator"
    actions:
      - rewrite:
          to: "{{#sed}}s/&amp;amp;/&amp;amp;&amp;amp;/g,{{{ . }}}{{/sed}}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  End Notes
&lt;/h2&gt;

&lt;p&gt;This covers the most common misuse of Bitwise operator, i.e. when a Boolean operator was actually intended.&lt;/p&gt;

&lt;p&gt;There are other situations where this could crop up e.g. the assignment example, but when writing recipes we have to attempt to avoid false-positive identification, otherwise recipes will be ignored or turned off. We build recipes to match the most common occurrences. As Sensei evolves, we may well add additional specificity into the search functionality to cover more matching conditions.&lt;/p&gt;

&lt;p&gt;In its current form, this recipe would identify many of the live use-cases, and &lt;em&gt;most importantly&lt;/em&gt;, the one that was reported in my project.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;NOTE: A fair few secure code warriors contributed to this example and recipe review - Charlie Eriksen, Matthieu Calie, Robin Claerhaut, Brysen Ackx, Nathan Desmet, Downey Robersscheuten. Thanks for your help.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;em&gt;You can install Sensei from within IntelliJ using "Preferences \ Plugins" (Mac) or "Settings \ Plugins" (Windows)  then just search for "sensei secure code". Or install from the &lt;a href="https://plugins.jetbrains.com/plugin/14015-sensei-by-secure-code-warrior-"&gt;Jetbrains Marketplace Online&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The source code and recipes for this post can be found in the &lt;code&gt;sensei-blog-examples&lt;/code&gt; repository in the Secure Code Warrior GitHub account, in the &lt;code&gt;pojoexamples&lt;/code&gt; module.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/securecodewarrior/sensei-blog-examples"&gt;https://github.com/securecodewarrior/sensei-blog-examples&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.securecodewarrior.com/sensei?pk_source=contentsyndication&amp;amp;pk_medium=devto&amp;amp;pk_campaign=sensei_devrel&amp;amp;pk_content=generic"&gt;Learn more about Sensei&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>gotcha</category>
    </item>
    <item>
      <title>What is Static Analysis</title>
      <dc:creator>Alan Richardson</dc:creator>
      <pubDate>Mon, 01 Feb 2021 11:34:49 +0000</pubDate>
      <link>https://dev.to/securecodewarrior/what-is-static-analysis-1ip5</link>
      <guid>https://dev.to/securecodewarrior/what-is-static-analysis-1ip5</guid>
      <description>&lt;p&gt;&lt;em&gt;TLDR; An overview of Static Analysis and some IntelliJ IDE plugins which can help you improve your coding process.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Static Analysis?
&lt;/h2&gt;

&lt;p&gt;Static Analysis is the automated analysis of source code without executing the application.&lt;/p&gt;

&lt;p&gt;When the analysis is performed during program execution then it is known as Dynamic Analysis.&lt;/p&gt;

&lt;p&gt;Static Analysis is often used to detect:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Security vulnerabilities.&lt;/li&gt;
&lt;li&gt;Performance issues.&lt;/li&gt;
&lt;li&gt;Non-compliance with standards.&lt;/li&gt;
&lt;li&gt;Use of out of date programming constructs.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How does a Static Analysis tool work?
&lt;/h3&gt;

&lt;p&gt;The basic concept common to all Static Analysis tools is searching source code to identify specific coding patterns that have some sort of warning or information associated with them.&lt;/p&gt;

&lt;p&gt;This could be as simple as "JUnit 5 test classes do not need to be 'public'". Or something complex to identify like "Untrusted String input being used in an SQL execution statement".&lt;/p&gt;

&lt;p&gt;Static Analysis tools vary in how they implement this functionality.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;source code parsing technology to create an Abstract Syntax Tree (AST),&lt;/li&gt;
&lt;li&gt;text Regular Expression matching,&lt;/li&gt;
&lt;li&gt;a combination of the above.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Regular Expression matching on text is very flexible, easy to write rules to match, but can often lead to a lot of false positives and the matching rules are ignorant of the surrounding code context.&lt;/p&gt;

&lt;p&gt;AST matching treats the source code as program code, and not just files filled with text, this allows for more specific, contextual matching and can reduce the number of false positives reported against the code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Static Analysis in Continuous Integration
&lt;/h3&gt;

&lt;p&gt;Static Analysis is often performed during the  Continous Integration (CI) process to generate a report of compliance issues which can be reviewed to receive an objective view of the code-base over time.&lt;/p&gt;

&lt;p&gt;Some people use Static Analysis as an objective measure of their code quality by configuring the static analysis tool to only measure specific parts of the code, and only report on a subset of rules.&lt;/p&gt;

&lt;p&gt;The objectivity is provided by the rules used since these do not vary in their evaluation of code over time. Clearly, the combination of rules used and their configuration is a subjective decision and different teams choose to use different rules at different times.&lt;/p&gt;

&lt;p&gt;Having the Static Analysis performed in CI is useful but might delay the feedback to the programmer. Programmers don't receive feedback when coding, they receive feedback later when the code is run through the Static Analysis tool. Another side-effect of running the Static Analysis in CI is that the results are easier to ignore.&lt;/p&gt;

&lt;p&gt;To help make teams pay more attention to the results from Static Analysis it is usually possible to configure a threshold metric in the build process to fail the build if the metric is exceeded e.g. a number of rules triggered.&lt;/p&gt;

&lt;h3&gt;
  
  
  Static Analysis in the IDE
&lt;/h3&gt;

&lt;p&gt;To receive feedback faster, there are many IDE plugins that run the Static Analysis rules in the IDE on demand, or periodically as the code changes.&lt;/p&gt;

&lt;p&gt;The rule violations can then be seen in the IDE as the programmer is writing code, and to make the rules harder to ignore, the violations can often be configured to render as underlined code in the editor.&lt;/p&gt;

&lt;p&gt;I personally find this a useful way to improve my coding, particularly when working with a new library that is covered by the Static Analysis tool. Although it can be 'noisy' with false positives, or rules you are not interested in. But this is solved by taking the extra step to configure the Static Analysis tool to ignore certain rules.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fixing Code Based on Static Analysis Rules
&lt;/h3&gt;

&lt;p&gt;With most Static Analysis tools, the fixing of the rule is left to the programmer, so they have to understand the cause of the rule violation and how to fix it.&lt;/p&gt;

&lt;p&gt;Very few static analysis tools also include the ability to fix the violations because the fix is so often contextual to the team and the technology used and their agreed coding styles.&lt;/p&gt;

&lt;h3&gt;
  
  
  Default Rules
&lt;/h3&gt;

&lt;p&gt;False confidence in the quality of the rules may arise when the Static Analysis tools come with default rules, it is tempting to believe that they cover all the issues that a programmer might encounter, and all the circumstances that rule should apply. Sometimes the circumstances in which a rule should apply can be subtle and may not be easy to detect.&lt;/p&gt;

&lt;p&gt;The hope is that by using a Static Analysis tool, and researching the rules and violations in more detail, that programmers will develop the skill to detect and avoid the issue in the context of their specific domain.&lt;/p&gt;

&lt;p&gt;When the domain requires contextual rules, the Static Analysis tools may not have any rules that match your domain or library, and additionally, the tools can often be difficult to configure and expand.&lt;/p&gt;

&lt;h3&gt;
  
  
  Annoyances
&lt;/h3&gt;

&lt;p&gt;None of these 'annoyances' are insurmountable:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;false positives&lt;/li&gt;
&lt;li&gt;lack of fixes&lt;/li&gt;
&lt;li&gt;configuration to ignore rules&lt;/li&gt;
&lt;li&gt;adding context-specific rules&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But they are often used as excuses to avoid using the Static Analysis tools in the first place, which is a pity because the use of Static Analysis can be enormously useful, as a way to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;highlight better approaches to junior developers&lt;/li&gt;
&lt;li&gt;gain fast feedback on clear coding violations&lt;/li&gt;
&lt;li&gt;identify obscure issues that the programmer has not encountered before&lt;/li&gt;
&lt;li&gt;reinforce that the programmer has adopted a good coding approach (when no violations are reported)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  IDE Based Static Analysis Tools
&lt;/h3&gt;

&lt;p&gt;As an individual contributor to a project, I like to use Static Analysis tools that run from within the IDE so that I receive fast feedback on my code.&lt;/p&gt;

&lt;p&gt;This supplements any pull request review process, and CI integration that a project may have.&lt;/p&gt;

&lt;p&gt;I try to identify tools that will give me an edge, and improve my individual workflow.&lt;/p&gt;

&lt;p&gt;When tools run in the IDE, because they tend to share the same basic GUI and configuration approach, it can be tempting to view them interchangeably.&lt;/p&gt;

&lt;p&gt;The tools may have overlapping functionality or rule sets but to gain maximum advantage I install multiple tools to take advantage of their strengths.&lt;/p&gt;

&lt;p&gt;The Static Analysis IDE tools I actively use when coding are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the inbuilt IntelliJ Inspections - common coding patterns&lt;/li&gt;
&lt;li&gt;SpotBugs - common errors&lt;/li&gt;
&lt;li&gt;SonarLint - common usage patterns&lt;/li&gt;
&lt;li&gt;CheckStyle - common style patterns&lt;/li&gt;
&lt;li&gt;Sensei from Secure Code Warrior - custom rule creation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I use them all because they work well together to augment and supplement each other.&lt;/p&gt;

&lt;h4&gt;
  
  
  IntelliJ Inspections
&lt;/h4&gt;

&lt;p&gt;If you use IntelliJ then you are already using their Inspections.&lt;/p&gt;

&lt;p&gt;These are Static Analysis rules which are flagged in the IDE. Some of them also have QuickFix options to rewrite the code to address the issue.&lt;/p&gt;

&lt;p&gt;The rules are configurable on and off, and to choose the error level used to highlight it in the IDE.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Wxas7ofq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/09wmjtg26xydqonbuhds.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Wxas7ofq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/09wmjtg26xydqonbuhds.png" alt="Configure IntelliJ Inspections" width="800" height="588"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are a lot of good IntelliJ Inspections. I know that because I read through them while writing this. I use the IntelliJ Inspections as the defaults and haven't configured them, but to gain full value from the Inspections you should read through them, identify those relevant to your coding style, and configure the warning level so that you notice them in the code.&lt;/p&gt;

&lt;p&gt;The great thing about the IntelliJ Inspections is that they come free with the IDE and they help build the muscle memory of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;noticing warnings and errors in the source as you write code&lt;/li&gt;
&lt;li&gt;hovering the mouse over flagged code to learn the rule violations&lt;/li&gt;
&lt;li&gt;using &lt;code&gt;alt+enter&lt;/code&gt; to apply a QuickFix for the issue&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oKX2iFv0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/muqo38sbduh89kfmjkjk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oKX2iFv0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/muqo38sbduh89kfmjkjk.png" alt="Using IntelliJ Inspections" width="516" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  SpotBugs
&lt;/h4&gt;

&lt;p&gt;The &lt;a href="https://plugins.jetbrains.com/plugin/14014-spotbugs"&gt;SpotBugs&lt;/a&gt; IntelliJ plugin uses Static Analysis to try and alert you to bugs in your code.&lt;/p&gt;

&lt;p&gt;SpotBugs can be configured from the IntelliJ Preferences to scan your code, the actual rules used can be found in the Detector tab.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DQSwUD9p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/vn8s3yglusiruvjpfeyb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DQSwUD9p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/vn8s3yglusiruvjpfeyb.png" alt="Configure SpotBugs" width="455" height="343"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I tend to use SpotBugs after I've written and reviewed my code, then I'll run the 'Analyze Project Files Including Test Sources'.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uZZ-Dnbs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/ydc5xqo93r5tmjjin7s9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uZZ-Dnbs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/ydc5xqo93r5tmjjin7s9.png" alt="Running SpotBugs" width="634" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This does help me identify bugs, dead code, and obvious optimizations. It also forces me to research some of the flagged violations to help me decide what to do.&lt;/p&gt;

&lt;p&gt;SpotBugs will find issues but does not offer any QuickFix actions to attempt to resolve the issues.&lt;/p&gt;

&lt;p&gt;SpotBugs is easy to configure and I find it to be a useful objective second opinion to consult in my IDE.&lt;/p&gt;

&lt;h4&gt;
  
  
  SonarLint
&lt;/h4&gt;

&lt;p&gt;The &lt;a href="https://plugins.jetbrains.com/plugin/7973-sonarlint"&gt;SonarLint&lt;/a&gt; plugin.&lt;/p&gt;

&lt;p&gt;SonarLint can be configured from the IntelliJ Preferences to select which rules the code is validated against.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4H2A0CA7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/olahoszk1f6y8cilbi3d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4H2A0CA7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/olahoszk1f6y8cilbi3d.png" alt="Configuring Sonarl Lint" width="508" height="318"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By default, SonarLint runs in realtime and shows issues for the current code that you are editing.&lt;/p&gt;

&lt;p&gt;SonarLint does not offer quick fixes but the documentation associated with the violation reports is usually clear and well documented.&lt;/p&gt;

&lt;p&gt;I've found SonarLint to be useful in the past for alerting me to new Java features that I was aware of in the newer versions of Java.&lt;/p&gt;

&lt;h4&gt;
  
  
  CheckStyle
&lt;/h4&gt;

&lt;p&gt;The &lt;a href="https://plugins.jetbrains.com/plugin/1065-checkstyle-idea"&gt;CheckStyle&lt;/a&gt; plugin offers a mix of formatting and code-quality rules.&lt;/p&gt;

&lt;p&gt;The CheckStyle plugin comes bundled with 'Sun Checks' and 'Google Checks'.&lt;/p&gt;

&lt;p&gt;The definitions of these can be easily &lt;a href="https://checkstyle.sourceforge.io/style_configs.html"&gt;found online&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;CheckStyle adds the most value when a project has spent the time creating its own ruleset. Then the IDE plugin can be configured to use that ruleset and programmers can perform a scan, prior to committing the code to CI.&lt;/p&gt;

&lt;p&gt;CheckStyle is very often used as a build failing plugin for CI processes when the number of CheckStyle violations exceeds a threshold.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sensei
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://plugins.jetbrains.com/plugin/14015-sensei-by-secure-code-warrior-"&gt;Sensei&lt;/a&gt; uses Static Analysis based on an Abstract Syntax Tree (AST) for matching code and for creating QuickFixes, this allows for very specific identification of code with issues.&lt;/p&gt;

&lt;p&gt;The AST allows QuickFixes associated with a recipe to understand the surrounding code e.g. when adding a new class into the code, any import for that class will only be added to the source file once, and not for each replacement.&lt;/p&gt;

&lt;p&gt;Sensei was created to make it easy to build custom matching rules which may not exist, or which would be hard to configure, in other tools. &lt;/p&gt;

&lt;p&gt;Rather than amend a configuration file, all the configuration can be performed in the GUI. When creating new recipes the GUI makes it easy to see which code the recipe matches. And when defining the QuickFixes the before and after state of the code can be compared immediately. This makes it easier to create very contextual recipes i.e. unique to teams, or technology, and even individual programmers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D5Jlw1w5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/6u4md3ozr6cgpvjt8ps1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D5Jlw1w5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/6u4md3ozr6cgpvjt8ps1.png" alt="Sensei QuickFix" width="800" height="334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I use Sensei in combination with other Static Analysis tools e.g. most Static Analysis tools will find issues, but not fix them. A common use case for Sensei is to replicate the other tool's matching search in Sensei, and expand it with a Quick Fix. This has the benefit that the custom fix applied already meets the coding standards for your project.&lt;/p&gt;

&lt;p&gt;I periodically find myself creating Sensei recipes that already exist in the IntelliJ Intensions set because the Intension report doesn't quite match the context I've created or because the QuickFix provided by IntelliJ doesn't match the code pattern I want to use.&lt;/p&gt;

&lt;p&gt;I augment the existing tools, rather than attempt to fully replace them.&lt;/p&gt;

&lt;p&gt;Sensei can also be very useful when you identify a contextual variant of a common rule e.g. if you are using an SQL library not supported by the Static Analysis tool, but the common SQL rules in the Static Analysis engine still apply, then you can create library-specific variants of those rules using Sensei.&lt;/p&gt;

&lt;p&gt;Sensei does not come out of the box with a lot of generic recipes like the Static Analysis tools mentioned, its strength is in making it easy to create new recipes, complete with QuickFixes configured to match your specific coding style and use-cases.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;NOTE: we are working on a public repository of recipes to cover generic use-cases, and &lt;a href="https://github.com/SecureCodeWarrior/public-cookbooks"&gt;you can find it here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;I tend to pick tools that work together, are configurable, and easy to expand to meet my specific context. I've used Static Analysis tools in the IDE for years to help me identify issues, and learn more about the programming languages and libraries I use.&lt;/p&gt;

&lt;p&gt;I use all of the tools mentioned in combination:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;IntelliJ Intentions helps flag common code issues out the box, often with associated QuickFixes.&lt;/li&gt;
&lt;li&gt;SpotBugs finds simple bugs I might have missed and alerts me to performance issues.&lt;/li&gt;
&lt;li&gt;SonarLint identifies Java features I was unaware of and prompts me to additional ways of modelling my code.&lt;/li&gt;
&lt;li&gt;CheckStyle helps me conform to an agreed coding style that is also enforced during CI.&lt;/li&gt;
&lt;li&gt;Sensei helps me create QuickFixes to augment common scenarios found by Static Analysis tools and create specific project or technology recipes that can be hard to configure in another tool.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;You can install Sensei from within IntelliJ using "Preferences \ Plugins" (Mac) or "Settings \ Plugins" (Windows)  then just search for "sensei secure code". Or install from the &lt;a href="https://plugins.jetbrains.com/plugin/14015-sensei-by-secure-code-warrior-"&gt;Jetbrains Marketplace Online&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.securecodewarrior.com/sensei?pk_source=contentsyndication&amp;amp;pk_medium=devto&amp;amp;pk_campaign=sensei_devrel&amp;amp;pk_content=generic"&gt;Learn more about Sensei&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>staticanalysis</category>
      <category>intellij</category>
      <category>intellijidea</category>
    </item>
    <item>
      <title>How to catch and fix a Guice dependency injection issue using Sensei</title>
      <dc:creator>Alan Richardson</dc:creator>
      <pubDate>Mon, 25 Jan 2021 10:18:42 +0000</pubDate>
      <link>https://dev.to/securecodewarrior/how-to-catch-and-fix-a-guice-dependency-injection-issue-using-sensei-18k0</link>
      <guid>https://dev.to/securecodewarrior/how-to-catch-and-fix-a-guice-dependency-injection-issue-using-sensei-18k0</guid>
      <description>&lt;h2&gt;
  
  
  How to catch and fix a dependency injection issue using Sensei
&lt;/h2&gt;

&lt;p&gt;The Sensei project itself has its own set of recipes that have built up over time, this is an example of one of the scenarios the Sensei team built a recipe to cover. A misconfiguration of Guice, which led to a &lt;code&gt;NullPointerException&lt;/code&gt; being reported at runtime during our testing.&lt;/p&gt;

&lt;p&gt;This could be generalised to many Dependency Injection scenarios where code is syntactically correct, but because the wiring configuration was incorrect, an error slips through.&lt;/p&gt;

&lt;p&gt;This often happens when we are learning the technology, and we keep making the simple mistake of&lt;br&gt;
forgetting to wire things up. But this also happens to experienced professionals because, well... we all make mistakes, and we may not have Unit Tests to cover everything.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fAbdDvyb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/1bknnpa21d9lo3dhfc8o.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fAbdDvyb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/1bknnpa21d9lo3dhfc8o.gif" alt="Basic Guice Wiring Check" width="760" height="353"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  RunTime Exceptions from Incorrect Dependency Injection Wiring
&lt;/h3&gt;

&lt;p&gt;The code below fails at runtime with a &lt;code&gt;NullPointerException&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Injector injector = Guice.createInjector(
                 new SystemOutModule());
CountReporter reporter = injector.getInstance(
                  CountReporter.class);

String [] lines5 = {"1: line", "2: line",
              "3: line", "4: line", "5: line"};

reporter.reportThisMany(Arrays.asList(lines5));
Assertions.assertEquals(5, reporter.getCount());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code is syntactically correct but fails because we missed out a &lt;code&gt;requestStaticInjection&lt;/code&gt; in our &lt;code&gt;SystemOutModule&lt;/code&gt; configuration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class SystemOutModule extends AbstractModule {

    @Override
    protected void configure() {
        binder().bind(ILineReporter.class).to(
             SystemOutReporter.class);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we try to use the &lt;code&gt;reporter&lt;/code&gt;, created using the &lt;code&gt;Injector&lt;/code&gt;, it is not fully instantiated and we receive a &lt;code&gt;NullPointerException&lt;/code&gt; when we call &lt;code&gt;reportThisMany&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We may well have missed that in our code review, or we didn't have unit tests that triggered the dependency injection, and it slipped out into our build.&lt;/p&gt;

&lt;h3&gt;
  
  
  Warning Signs
&lt;/h3&gt;

&lt;p&gt;In this case, there is a warning sign, the &lt;code&gt;CountReporter&lt;/code&gt; has a static field annotated with &lt;code&gt;@Inject&lt;/code&gt; but... the &lt;code&gt;CountReporter&lt;/code&gt; class itself is &lt;code&gt;package private&lt;/code&gt;. In a complicated code base this could be a warning sign that it isn't used because the Module class configuring the bindings needs to be in the same package for this to work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class CountReporter {

    @Inject
    private static ILineReporter reporter;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another error that we made, which we might have picked up in a code review, is that we forgot to actually bind the fields in the &lt;code&gt;SystemOutModule&lt;/code&gt; &lt;code&gt;configure&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;binder().requestStaticInjection(
                   CountReporter.class);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Had we written the &lt;code&gt;requestStaticInjection&lt;/code&gt; code then the Syntax Error generated when trying to use the &lt;code&gt;CountReporter&lt;/code&gt; would have alerted us to the simple error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; 'reporters.CountReporter' is not public in 'reporters'.
  Cannot be accessed from outside package
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sadly. We forgot, and there were no syntactic warning signs in the code.&lt;/p&gt;

&lt;h3&gt;
  
  
  How could Sensei help?
&lt;/h3&gt;

&lt;p&gt;We probably wouldn't use Sensei to pick up the missing &lt;code&gt;requestStaticInjection&lt;/code&gt; since all&lt;br&gt;
our Guice configuration wiring would need to use that method, and we can't guarantee that&lt;br&gt;
all wiring is going to be as simple as this use-case.&lt;/p&gt;

&lt;p&gt;We could write a Sensei rule to look for some warning signs that our code is not up to scratch.&lt;/p&gt;

&lt;p&gt;In this case that would mean:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Find any classes with &lt;code&gt;@Inject&lt;/code&gt; annotated fields&lt;/li&gt;
&lt;li&gt;Where the classes are not public.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The above was the warning sign that they were unlikely to have been wired up.&lt;/p&gt;

&lt;p&gt;By creating a recipe, then we will have a warning sign early, during the coding, and reduce the reliance on our pull requests or resolving our tech debt to allow us to add Unit Tests.&lt;/p&gt;
&lt;h3&gt;
  
  
  How to create a recipe?
&lt;/h3&gt;

&lt;p&gt;The task I want to complete is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a recipe that matches fields annotated with &lt;code&gt;@Inject&lt;/code&gt; which are in &lt;code&gt;protected private&lt;/code&gt; classes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That should hopefully give us enough warning to identify any Modules using it and add the missing wiring code.&lt;/p&gt;

&lt;p&gt;In my &lt;code&gt;CountReporter&lt;/code&gt; class, I will use &lt;code&gt;Alt+Enter&lt;/code&gt; to &lt;code&gt;Create a new Recipe&lt;/code&gt; and I will &lt;code&gt;start from scratch&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I will name this and add a description:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Name: Guice: Injected Field Not Public
Description: If the Injected field is not public then the code might not be wired up
Level: Warning
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The search I write looks for a field with the &lt;code&gt;Inject&lt;/code&gt; annotation which has not been scoped as &lt;code&gt;public&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;search:
  field:
    with:
      annotation:
        type: "com.google.inject.Inject"
    in:
      class:
        without:
          modifier: "public"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ovSfsYYa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/pk1gwrvhpxgaid5aqd5x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ovSfsYYa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/pk1gwrvhpxgaid5aqd5x.png" alt="Guice Search Settings" width="800" height="401"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Fix
&lt;/h3&gt;

&lt;p&gt;The QuickFix in the recipe will amend the injected class, but that is not the only code I have to change.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;availableFixes:
- name: "Change class to public. Remember to also request injection on this class"
  actions:
  - changeModifiers:
      visibility: "public"
      target: "parentClass"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the recipe is triggered I still have a manual step to perform in my code, adding the line containing &lt;code&gt;requestStaticInjection&lt;/code&gt; to fully instantiate the object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class SystemOutModule extends AbstractModule {

    @Override
    protected void configure() {
        binder().bind(ILineReporter.class).to(SystemOutReporter.class);
        // instantiate via dependency injection
        binder().requestStaticInjection(
                      CountReporter.class);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I could potentially write another recipe to pick this up. I probably wouldn't do that unless forgetting to add the static injection became a semi-regular error that I made when coding.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;If we ever find ourselves making a mistake with a common root pattern, then Sensei can help codify the knowledge around detecting and fixing the issue, and then hopefully, it won't slip through code reviews and into production.&lt;/p&gt;

&lt;p&gt;Sometimes the recipes we write identify heuristic patterns i.e. matching them doesn't guarantee that there is a problem, but is likely that there is a problem.&lt;/p&gt;

&lt;p&gt;Also, the recipes and QuickFixes that we write, don't have to be fully comprehensive, they need to be good enough that they help us identify and fix problems without being overcomplicated. Because when they become overcomplicated they become harder to understand and harder to maintain.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;You can install Sensei from within IntelliJ using "Preferences \ Plugins" (Mac) or "Settings \ Plugins" (Windows) then just search for “sensei secure code”.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The source code and recipes for this post can be found in the &lt;code&gt;sensei-blog-examples&lt;/code&gt; repository in the Secure Code Warrior GitHub account, in the &lt;code&gt;guiceexamples&lt;/code&gt; module.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/securecodewarrior/sensei-blog-examples"&gt;https://github.com/securecodewarrior/sensei-blog-examples&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://www.securecodewarrior.com/sensei?pk_source=contentsyndication&amp;amp;pk_medium=devto&amp;amp;pk_campaign=sensei_devrel&amp;amp;pk_content=guicebacklink"&gt;Learn more about Sensei&lt;/a&gt;&lt;/p&gt;

</description>
      <category>guice</category>
      <category>java</category>
      <category>dependencyinjection</category>
    </item>
    <item>
      <title>Automatically Amending Method and Class Visibility for JUnit 5</title>
      <dc:creator>Alan Richardson</dc:creator>
      <pubDate>Thu, 14 Jan 2021 14:29:43 +0000</pubDate>
      <link>https://dev.to/securecodewarrior/automatically-amending-method-and-class-visibility-for-junit-5-31p8</link>
      <guid>https://dev.to/securecodewarrior/automatically-amending-method-and-class-visibility-for-junit-5-31p8</guid>
      <description>&lt;p&gt;One of the joys of programming is the constant learning required to keep up to date. One of the issues is that we build up familiarity and patterns of usage that can impact the adoption of new approaches. Sensei can help migration by identifying deprecated patterns and prompting us with the fix to use going forward.&lt;/p&gt;

&lt;p&gt;As an example, when I migrated from JUnit 4 to JUnit 5, I was used to writing all my test classes and methods as &lt;code&gt;public&lt;/code&gt;. But with JUnit 5 they can be &lt;code&gt;package private&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;e.g. instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Junit5VisibilityTest {

    @Test
    public void thisDoesNotNeedToBePublic(){
        Assertions.assertTrue(true);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I really want to write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Junit5VisibilityTest {

    @Test
    void thisDoesNotNeedToBePublic(){
        Assertions.assertTrue(true);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It took me a while to build the muscle memory to code to this, and I still slip up once in a while.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using Sensei
&lt;/h3&gt;

&lt;p&gt;With Sensei I can create recipes that find the &lt;code&gt;public&lt;/code&gt; methods and classes, and amend the declarations to be &lt;code&gt;package private&lt;/code&gt; automatically.&lt;/p&gt;

&lt;p&gt;To achieve this I created a recipe:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Name - JUnit: JUnit 5 test methods do not need to be public
Description - JUnit 5 test methods do not need public visibility
Level - Error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I classed it as &lt;code&gt;Error&lt;/code&gt; because I want to stamp out this coding practice and I want  higher visibility of the issue when I'm writing code in the IDE.&lt;/p&gt;

&lt;h3&gt;
  
  
  Amending the Class Declaration
&lt;/h3&gt;

&lt;p&gt;To find the classes, I search for any class which has a child annotation of &lt;code&gt;@Test&lt;/code&gt; from Junit 5 i.e. &lt;code&gt;org.junit.jupiter.api.Test&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And where the class has modifier &lt;code&gt;public&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;search:
  class:
    with:
      child:
        annotation:
          type: "org.junit.jupiter.api.Test"
    modifier: "public"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then the quick fix changes the modifier to remove the visibility so that it is the default, and the default is &lt;code&gt;package private&lt;/code&gt; which is what I'm looking for.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;availableFixes:
- name: "remove public visibility from JUnit 5  Test class"
  actions:
  - changeModifiers:
      visibility: ""
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Amending the Method Declarations
&lt;/h3&gt;

&lt;p&gt;The method declaration amendment recipe is much the same as the class recipe.&lt;/p&gt;

&lt;p&gt;First I search for &lt;code&gt;public&lt;/code&gt; methods annotated with &lt;code&gt;@Test&lt;/code&gt; from JUnit 5.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;search:
  method:
    annotation:
      type: "org.junit.jupiter.api.Test"
    modifier: "public"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then I change the modifier to be default visibility.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;availableFixes:
- name: "Remove @Test method public visibility"
  actions:
  - changeModifiers:
      visibility: ""
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Hint: Amending Multiple Methods
&lt;/h3&gt;

&lt;p&gt;Sensei has the ability to apply the QuickFix to all the violations in the current file.&lt;/p&gt;

&lt;p&gt;When I use &lt;code&gt;alt+enter&lt;/code&gt; to apply the QuickFix.&lt;/p&gt;

&lt;p&gt;If I expand the QuickFix name menu, I can see an option to:&lt;/p&gt;

&lt;p&gt;"Fix All: 'JUnit: JUnit 5 test methods do not need to be public' problems in the file"&lt;/p&gt;

&lt;p&gt;When I select that option then Sensei will amend all the occurrences of the problem, not just the one I select.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aaZf_CLK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/efhtc7ysghyr4cpa6nt3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aaZf_CLK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/efhtc7ysghyr4cpa6nt3.png" alt="Fix All visibility" width="800" height="213"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Amending the class
&lt;/h3&gt;

&lt;p&gt;In the same way that a method does not need to be public, neither does the class.&lt;/p&gt;

&lt;p&gt;I can create a recipe and a QuickFix to amend the class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Name - JUnit: Junit 5 Test classes do not need to be public
Description - Junit 5 Test classes do not need to be public
Level - Error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When I find a class that is &lt;code&gt;public&lt;/code&gt; and has a method with a &lt;code&gt;@Test&lt;/code&gt; annotation. Then I want to change the visibility.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;search:
  class:
    modifier: "public"
    anyOf:
    - child:
        method:
          annotation:
            type: "Test"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I can make the change to the class definition with the &lt;code&gt;changeModifiers&lt;/code&gt; action again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;availableFixes:
- name: "Remove @Test class public visibility"
  actions:
  - changeModifiers:
      visibility: ""
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;A static analysis tool initially alerted me to this recommended approach in JUnit. But the static analysis tool didn't help me build the muscle memory to change my code as I program.&lt;/p&gt;

&lt;p&gt;Use the 'Level' to alert you. When it is a problem I am trying to stamp out in my coding I initially make it 'Error' and then reduce this as I wean myself off the coding approach.&lt;/p&gt;

&lt;p&gt;Remember you can use Sensei to fix all the issues in the current file at the same time, by using the drop-down menu option when applying the QuickFix.&lt;/p&gt;

&lt;p&gt;By creating a Sensei recipe, I can see my old coding approach in real-time. And QuickFix it, to reinforce the approach if I occasionally slip up in my coding.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;You can install Sensei from within IntelliJ using "Preferences \ Plugins" (Mac) or "Settings \ Plugins" (Windows)  then just search for “sensei secure code”.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The source code and recipes for this can be found in the &lt;code&gt;sensei-blog-examples&lt;/code&gt; repository in the Secure Code Warrior GitHub account, in the &lt;code&gt;junitexamples&lt;/code&gt; module.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/securecodewarrior/sensei-blog-examples"&gt;github.com/securecodewarrior/sensei-blog-examples&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>junit5</category>
    </item>
    <item>
      <title>Automatically Adding a Private Constructor with Sensei</title>
      <dc:creator>Alan Richardson</dc:creator>
      <pubDate>Wed, 13 Jan 2021 16:20:41 +0000</pubDate>
      <link>https://dev.to/securecodewarrior/automatically-adding-a-private-constructor-with-sensei-47fl</link>
      <guid>https://dev.to/securecodewarrior/automatically-adding-a-private-constructor-with-sensei-47fl</guid>
      <description>&lt;p&gt;&lt;em&gt;TLDR; Sensei can identify a Utility class coding pattern, and automatically generate a private constructor to make it impossible to instantiate the class.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In a utility class, when the fields and methods are static, there is no obvious reason why I would ever instantiate it.&lt;/p&gt;

&lt;p&gt;e.g. &lt;code&gt;UtilityClass utility = new UtilityClass();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The code below is a simple implementation of a Utility class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class UtilityClass {

    public static final Boolean ULTIMATE_TRUTH = true;

    public static boolean getTrue(){
        return ULTIMATE_TRUTH;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Now that I know I can fix the problem. I’ll refine the search conditions to show the recipe when it is most appropriate.&lt;/p&gt;

&lt;h3&gt;
  
  
  Searching for the Class
&lt;/h3&gt;

&lt;p&gt;I create a new recipe on the Utility class called &lt;code&gt;Static Classes: create private constructor&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nXpaWhFF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/zg1pcxepws1bacl9gwun.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nXpaWhFF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/zg1pcxepws1bacl9gwun.png" alt="search for class" width="447" height="126"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Name: Static Classes: create private constructor
Description: create a private constructor for static classes
Level: Error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And initially, I'll search for a class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;search:
  class: {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will match any class, which is good enough to let me get started writing a Quick Fix, and 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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9aRdUHF6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/3ryg8wf07ouwgp19c79x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9aRdUHF6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/3ryg8wf07ouwgp19c79x.png" alt="search settings" width="685" height="246"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Quick Fix
&lt;/h3&gt;

&lt;p&gt;For the Quick Fix, I will want to generate a private constructor.&lt;/p&gt;

&lt;p&gt;In the example class this would look like:&lt;/p&gt;

&lt;p&gt;~~~~&lt;del&gt;&lt;/del&gt;&lt;br&gt;
    private UtilityClass(){}&lt;br&gt;
~~~~&lt;del&gt;&lt;/del&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;availableFixes:
- name: "add private constructor"
  actions:
  - addMethod:
      method: "private {{{ name }}}(){}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the GUI Editor, I use the &lt;code&gt;Show Variables&lt;/code&gt; to create the Mustache template and then edit the field to add the &lt;code&gt;private&lt;/code&gt; modifier, brackets, and braces to make it syntactically correct.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SjLZm4Im--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/bjstuthvlyuwe93p2cex.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SjLZm4Im--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/bjstuthvlyuwe93p2cex.png" alt="quick fix" width="800" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And this would now allow me to add a private constructor to any class.&lt;/p&gt;

&lt;p&gt;The QuickFix preview helps me because I can see the generated code as I write the Mustache template.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5FXnAYDy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/orv21ibd1wc3p8svlg4k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5FXnAYDy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/orv21ibd1wc3p8svlg4k.png" alt="fixed" width="800" height="256"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Search for Missing Constructors
&lt;/h3&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;search:
  class:
    without:
      child:
        method:
          constructor: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The YAML is slightly different from the GUI.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--19k67DTt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/v1/images/private-constructors-yaml.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--19k67DTt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/v1/images/private-constructors-yaml.png" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This recipe will now only reveal itself for any class without a constructor.&lt;/p&gt;

&lt;h3&gt;
  
  
  Narrow Search for Likely Culprits
&lt;/h3&gt;

&lt;p&gt;So I might want to go further and look for the presence of static methods or fields.&lt;/p&gt;

&lt;p&gt;I look for any class without a constructor and which has either all public static fields or all public static methods.&lt;/p&gt;

&lt;p&gt;~~~~&lt;del&gt;&lt;/del&gt;&lt;br&gt;
search:&lt;br&gt;
  class:&lt;br&gt;
    with:&lt;br&gt;
      anyOf:&lt;br&gt;
      - child:&lt;br&gt;
          method:&lt;br&gt;
            allOf:&lt;br&gt;
            - modifier: "public"&lt;br&gt;
            - modifier: "static"&lt;br&gt;
      - child:&lt;br&gt;
          field:&lt;br&gt;
            allOf:&lt;br&gt;
            - modifier: "static"&lt;br&gt;
            - modifier: "public"&lt;br&gt;
    without:&lt;br&gt;
      child:&lt;br&gt;
        method:&lt;br&gt;
          constructor: true&lt;br&gt;
~~~~&lt;del&gt;&lt;/del&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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' &lt;code&gt;public static&lt;/code&gt; methods, rather than 'all'.&lt;/p&gt;

&lt;p&gt;~~~~&lt;del&gt;&lt;/del&gt;&lt;br&gt;
        - child:&lt;br&gt;
            field:&lt;br&gt;
              anyOf:&lt;br&gt;
                - modifier: "static"&lt;br&gt;
                - modifier: "public"&lt;br&gt;
~~~~&lt;del&gt;&lt;/del&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Hints
&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Sometimes when working on recipes I'm not sure how to perform the search, so I work on that first.&lt;/p&gt;

&lt;p&gt;I find recipes easier to create when I build them incrementally, switching between refactoring of the QuickFix and the search.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code Links
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;You can install Sensei from within IntelliJ using "Preferences \ Plugins" (Mac) or "Settings \ Plugins" (Windows)  then just search for “sensei secure code”.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The source code and recipes for this can be found in the &lt;code&gt;sensei-blog-examples&lt;/code&gt; repository in the Secure Code Warror github account, in the &lt;code&gt;pojoexamples&lt;/code&gt; module.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/securecodewarrior/sensei-blog-examples"&gt;https://github.com/securecodewarrior/sensei-blog-examples&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Improving A Personal Programming Process Using Sensei</title>
      <dc:creator>Alan Richardson</dc:creator>
      <pubDate>Thu, 10 Dec 2020 15:36:05 +0000</pubDate>
      <link>https://dev.to/securecodewarrior/improving-a-personal-programming-process-using-sensei-hok</link>
      <guid>https://dev.to/securecodewarrior/improving-a-personal-programming-process-using-sensei-hok</guid>
      <description>&lt;h2&gt;
  
  
  Adding Annotations and Method Renaming
&lt;/h2&gt;

&lt;p&gt;For this post, I've recreated a 'bad' coding approach that I used when I was learning JUnit, and will demonstrate how to convert the ‘bad’ pattern to an agreed, and "better", coding pattern using Sensei.&lt;/p&gt;

&lt;p&gt;When I was learning JUnit, I could only keep so much in my head at any one time. I constantly forgot how to skip tests when they were not working.&lt;/p&gt;

&lt;p&gt;If we are working in a Team then we can use code reviews on pull requests to help enforce coding styles. And we can shorten the feedback cycle when pair programming with a more experienced programmer.&lt;/p&gt;

&lt;p&gt;We can also augment our process with tooling and have the tools prompt us to do the right thing. Thoughtworks describe this as "tools over rules," in their &lt;a href="https://www.thoughtworks.com/radar/tools/sensei"&gt;Technology Radar listing for Sensei&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"make it easy to do the right thing over applying checklist-like governance rules and procedures"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Disabling a JUnit Test
&lt;/h3&gt;

&lt;p&gt;Ideally, I would, as we all know, use the &lt;code&gt;@Disabled&lt;/code&gt; annotation and write:&lt;/p&gt;

&lt;p&gt;~~~~&lt;del&gt;&lt;/del&gt;&lt;br&gt;
    @Disabled&lt;br&gt;
    &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;&lt;br&gt;
    void canWeAddTwoNumbers(){&lt;br&gt;
        Assertions.fail("this test was skipped and should not run");&lt;br&gt;
    }&lt;br&gt;
~~~~&lt;del&gt;&lt;/del&gt;&lt;/p&gt;

&lt;p&gt;But, when learning, I had to train myself to use &lt;code&gt;@Disabled&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When I forgot how to disable a Test method I would remove the &lt;code&gt;@Test&lt;/code&gt; annotation and rename the test: &lt;/p&gt;

&lt;p&gt;~~~~&lt;del&gt;&lt;/del&gt;&lt;br&gt;
class SkipThisTest {&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void SKIPTHIScanWeAddTwoNumbers(){
    Assertions.fail("this test was skipped and should not run");
}   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;}&lt;br&gt;
~~~~&lt;del&gt;&lt;/del&gt;&lt;/p&gt;

&lt;p&gt;It wasn't good, but it got the job done. I didn't have something like Sensei to help me remember and so I fell into using poor coding patterns.&lt;/p&gt;

&lt;p&gt;The tasks I've taken on board for this readme are to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a rule which finds methods that have been 'skipped' or 'disabled' by renaming the method.&lt;/li&gt;
&lt;li&gt;Create a QuickFix to rename the method and add both an &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt; and @Disabled annotation.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Recipe Settings
&lt;/h3&gt;

&lt;p&gt;The first step I take with Sensei is to "add new recipe" and search for the coding pattern I want the recipe to act on.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Name: JUnit: Make @Disabled @Test from SKIPTHIS
Short Description: Stop naming methods SKIPTHIS, use @Disabled @Test instead
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And my search is very simple. I use a basic regex to match  the method name.&lt;/p&gt;

&lt;p&gt;~~~~&lt;del&gt;&lt;/del&gt;&lt;br&gt;
search:&lt;br&gt;
  method:&lt;br&gt;
    name:&lt;br&gt;
      matches: "SKIPTHIS.*"&lt;br&gt;
~~~~&lt;del&gt;&lt;/del&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--G4MHh3UX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/s1yxf8knxcsfx41vntl6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--G4MHh3UX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/s1yxf8knxcsfx41vntl6.png" alt="Search for method name" width="800" height="217"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  QuickFix Settings
&lt;/h3&gt;

&lt;p&gt;The QuickFix is a little more complicated because it will rewrite the code, and I'll use a few steps to achieve my final code.&lt;/p&gt;

&lt;p&gt;I want to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;add an &lt;code&gt;@Test&lt;/code&gt; annotation to the method&lt;/li&gt;
&lt;li&gt;add an &lt;code&gt;@Disabled&lt;/code&gt; annotation to the method&lt;/li&gt;
&lt;li&gt;amend the method name&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Adding the annotations is simple enough using the &lt;code&gt;addAnnotation&lt;/code&gt; fix. If I use a fully qualified name for the annotation then Sensei will automatically add the imports for me.&lt;/p&gt;

&lt;p&gt;~~~~&lt;del&gt;&lt;/del&gt;&lt;br&gt;
availableFixes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;name: "Add @Disabled and &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt; Annotation"
actions:

&lt;ul&gt;
&lt;li&gt;addAnnotation:
annotation: "@org.junit.jupiter.api.Test"&lt;/li&gt;
&lt;li&gt;addAnnotation:
annotation: "@org.junit.jupiter.api.Disabled"
~~~~&lt;del&gt;&lt;/del&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The actual renaming seems a little more complicated but I'm just using a regex replacement, and the generic way to do this with Sensei is to use &lt;code&gt;sed&lt;/code&gt; in a rewrite action.&lt;/p&gt;

&lt;p&gt;Because the rewrite actions a Mustache templates, Sensei has so functional extensions in the template mechanism. A function is represented with &lt;code&gt;{{#...}}&lt;/code&gt; so for &lt;code&gt;sed&lt;/code&gt; the function is &lt;code&gt;{{#sed}}&lt;/code&gt;. The function takes two arguments which are comma-separated.&lt;/p&gt;

&lt;p&gt;The first argument is the &lt;code&gt;sed&lt;/code&gt; statement:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;s/(.*) SKIPTHIS(.*)/$1 $2/&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The second argument is the String to apply the &lt;code&gt;sed&lt;/code&gt; statement to, which in this case is the method itself, and this is represented in the Mustache variables as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;{{{.}}}&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  - rewrite:
       to: "{{#sed}}s/(.*) SKIPTHIS(.*)/$1 $2/,{{{.}}}{{/sed}}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5vOlpxJU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/70uzr5cr1ijzexgwpzqi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5vOlpxJU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/70uzr5cr1ijzexgwpzqi.png" alt="Annotations QuickFix" width="800" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;sed&lt;/code&gt; implementation requires that when the arguments themselves contain commas, that they are wrapped with &lt;code&gt;{{#encodeString}}&lt;/code&gt; and &lt;code&gt;{{/encodeString}}&lt;/code&gt;&lt;br&gt;
    - e.g. &lt;code&gt;{{#encodeString}}{{{.}}}{{/encodeString}}&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Reverse Recipe
&lt;/h3&gt;

&lt;p&gt;Since this is an example, and we might want to use this in demos, I wanted to explore how to reverse out the above change using a Sensei recipe.&lt;/p&gt;

&lt;p&gt;Thinking it through I want to find a method annotated with &lt;code&gt;@Disabled&lt;/code&gt;&lt;br&gt;
but only in the class &lt;code&gt;SkipThisTest&lt;/code&gt; where I do the demo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Name: JUnit: demo in SkipThisTest remove @Disabled and revert to SKIPTHIS
Short Description: remove @Disabled and revert to SKIPTHIS for demo purposes in the project
Level: warning
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Recipe Settings Search is very simple, matching the annotation in a specific class.&lt;/p&gt;

&lt;p&gt;~~~~&lt;del&gt;&lt;/del&gt;&lt;br&gt;
search:&lt;br&gt;
  method:&lt;br&gt;
    annotation:&lt;br&gt;
      type: "Disabled"&lt;br&gt;
    in:&lt;br&gt;
      class:&lt;br&gt;
        name: "SkipThisTest"&lt;br&gt;
~~~~&lt;del&gt;&lt;/del&gt;&lt;/p&gt;

&lt;p&gt;To avoid making the code look like it is an error I defined the general setting on the recipe to be a Warning. Warnings are shown with highlights in the code and it doesn't make the code look like it has a major problem.&lt;/p&gt;

&lt;p&gt;For the Quick fix, since we have matched the method, I use the rewrite action and populate the template using the variables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;availableFixes:
- name: "Remove Disabled and rename to SKIPTHIS..."
  actions:
  - rewrite:
      to: "{{{ returnTypeElement }}} SKIPTHIS{{{ nameIdentifier }}}{{{ parameterList\
        \ }}}{{{ body }}}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I basically add every variable except the modifier (since I want to get rid of the annotations) and add the &lt;code&gt;SKIPTHIS&lt;/code&gt; text into the template.&lt;/p&gt;

&lt;p&gt;This fix has the weakness that by removing the modifiers, I remove any other annotations as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  Add another Action
&lt;/h3&gt;

&lt;p&gt;I can add another named fix, to give me a choice when the &lt;code&gt;alt+enter&lt;/code&gt; is used to display the QuickFix.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;availableFixes:
- name: "Remove Disabled and rename to SKIPTHIS..."
  actions:
  - rewrite:
      to: "{{{ returnTypeElement }}} SKIPTHIS{{{ nameIdentifier }}}{{{ parameterList\
        \ }}}{{{ body }}}"
      target: "self"
- name: "Remove Disabled, keep other annotations, and rename to SKIPTHIS..."
  actions:
  - rewrite:
      to: "{{#sed}}s/(@Disabled\n.*@Test)//,{{{ modifierList }}}{{/sed}}\n\
        {{{ returnTypeElement }}} SKIPTHIS{{{ nameIdentifier }}}{{{ parameterList\
        \ }}}{{{ body }}}"
      target: "self"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, I added an additional line in the new Quick Fix.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{{#sed}}s/(@Disabled\n.*@Test)//,{{{ modifierList }}}{{/sed}}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This takes the modifier list, encodes it as a string, then uses &lt;code&gt;sed&lt;/code&gt; to remove the line with &lt;code&gt;@Disabled&lt;/code&gt; from the string, but leaves all other lines in the modifier, i.e. it leaves all other annotations alone.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;NOTE: Remember to add the "," in the &lt;code&gt;sed&lt;/code&gt;, otherwise you will see a comment added to your preview because this is how Sensei alerts you to syntax errors in the &lt;code&gt;sed&lt;/code&gt; command.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Nested &lt;code&gt;sed&lt;/code&gt; calls
&lt;/h3&gt;

&lt;p&gt;I was lucky that I could match both the &lt;code&gt;@Disabled&lt;/code&gt; and &lt;code&gt;@Test&lt;/code&gt; in a single search and replace.&lt;/p&gt;

&lt;p&gt;In the event that the code is more complicated and I wanted to have a sequence of sed commands then I can do that by nesting them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{{#sed}}s/@Test//,{{#sed}}s/@Disabled\n//,{{{ modifierList }}}{{/sed}}{{/sed}}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, I apply the &lt;code&gt;@Test&lt;/code&gt; replacement to the results of applying the &lt;code&gt;@Disabled&lt;/code&gt; replacement on the &lt;code&gt;{{{ modifierList }}}&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;sed&lt;/code&gt; is a very flexible way to achieve code rewriting and it is possible to nest the &lt;code&gt;sed&lt;/code&gt; function calls for complicated rewrite conditions.&lt;/p&gt;

&lt;p&gt;Recipes like this often end up being temporary because we are using them to improve our programming process, and once we have built up the muscle memory and no longer use the poor programming pattern we can remove or disable them in the Cookbook.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;NOTE: Rather than add two available fixes, I could clone the recipe, but if I do, then I have to remember to uncheck the "Add disable entry for cloned recipe" checkbox, otherwise only the cloned recipe will be available for use.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;You can install Sensei from within IntelliJ using "Preferences \ Plugins" (Mac) or "Settings \ Plugins" (Windows) then just search for “sensei secure code”.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;All the code for this blog post can be found on GitHub in the &lt;code&gt;junitexamples&lt;/code&gt; module of our blog examples repository&lt;/em&gt;&lt;br&gt;
 &lt;a href="https://github.com/SecureCodeWarrior/sensei-blog-examples"&gt;https://github.com/SecureCodeWarrior/sensei-blog-examples&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>junit</category>
      <category>junit5</category>
    </item>
    <item>
      <title>Migrating from System.out.println to a Logger in Java</title>
      <dc:creator>Alan Richardson</dc:creator>
      <pubDate>Mon, 30 Nov 2020 13:32:53 +0000</pubDate>
      <link>https://dev.to/securecodewarrior/migrating-266b</link>
      <guid>https://dev.to/securecodewarrior/migrating-266b</guid>
      <description>&lt;p&gt;When writing the code below I made a bunch of mistakes:&lt;/p&gt;

&lt;p&gt;~~~~&lt;del&gt;&lt;/del&gt;&lt;br&gt;
    private String getCountdownString() {&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    String output = "";
    String prefix="";

    for(int countdown = 10; countdown &amp;gt; 0; countdown-- ){

        output = output + prefix + countdown;

        System.out.println(output);

        prefix=", ";
    }

    System.out.println(output);

    return output;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;~~~~&lt;del&gt;&lt;/del&gt;&lt;/p&gt;

&lt;p&gt;Initially, I had &lt;code&gt;countdown++&lt;/code&gt; and the loop didn't finish.&lt;/p&gt;

&lt;p&gt;And I used &lt;code&gt;countdown &amp;gt; 1&lt;/code&gt; so didn't get the output I wanted.&lt;/p&gt;

&lt;p&gt;In the end, I littered my code with &lt;code&gt;System.out.println&lt;/code&gt; to help me debug.&lt;/p&gt;

&lt;p&gt;And I realised, I really need to learn how to use a logger.&lt;/p&gt;
&lt;h2&gt;
  
  
  Research
&lt;/h2&gt;

&lt;p&gt;Fortunately, I read through the &lt;a href="https://sensei-docs-beta.securecodewarrior.com/index.html?pk_source=contentsyndication&amp;amp;pk_medium=devto&amp;amp;pk_campaign=sensei_devrel"&gt;Sensei documentation&lt;/a&gt; and decided to use the "Getting Started" guide to help me create a recipe to convert from &lt;code&gt;System.out.println&lt;/code&gt; and encourage me to use a logger:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;java.util.logging.Logger&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Video - Using Sensei to convert &lt;code&gt;System.out.println&lt;/code&gt; to a Logger
&lt;/h2&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/qPlNm0yaSto"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a Recipe
&lt;/h2&gt;

&lt;p&gt;The first thing I do is click on the &lt;code&gt;println&lt;/code&gt; then &lt;code&gt;alt+enter&lt;/code&gt; to create a new recipe.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--K3n27PMz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/i7dc569asjamrntebpmk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--K3n27PMz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/i7dc569asjamrntebpmk.png" alt="Create a new recipe popup" width="351" height="102"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I create it with the following details:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Name: Logger: use logger instead of println
Description: use logger - stop using System.out.println
Level: Error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And I'll start by matching &lt;code&gt;methodcall&lt;/code&gt; with the name &lt;code&gt;println&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;~~~~&lt;del&gt;&lt;/del&gt;&lt;br&gt;
search:&lt;br&gt;
  methodcall:&lt;br&gt;
    name: "println"&lt;br&gt;
~~~~&lt;del&gt;&lt;/del&gt;&lt;/p&gt;

&lt;p&gt;And the preview shows me all the matches in my code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9TdGqAAK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/qatdn9dlkwvpbglf3unf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9TdGqAAK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/qatdn9dlkwvpbglf3unf.png" alt="Search for println" width="800" height="340"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I can see that all the matches in my code are for &lt;code&gt;System.out.println&lt;/code&gt; but I don't trust that long term this will be the only match. I want to match a more qualified statement I want to change.&lt;/p&gt;

&lt;p&gt;I expand the matcher to search for a &lt;code&gt;methodcall&lt;/code&gt; on a &lt;code&gt;field&lt;/code&gt; named &lt;code&gt;out&lt;/code&gt; in the class &lt;code&gt;System&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;~~~~&lt;del&gt;&lt;/del&gt;&lt;br&gt;
search:&lt;br&gt;
  methodcall:&lt;br&gt;
    name: "println"&lt;br&gt;
    "on":&lt;br&gt;
      field:&lt;br&gt;
        in:&lt;br&gt;
          class:&lt;br&gt;
            name: "System"&lt;br&gt;
        name: "out"&lt;br&gt;
~~~~&lt;del&gt;&lt;/del&gt;&lt;/p&gt;

&lt;p&gt;I could, if I wanted, fully qualify the &lt;code&gt;System&lt;/code&gt; name to &lt;code&gt;java.lang.System&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rebmPkcq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/ubfzeev45vaa8rcusuu5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rebmPkcq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/ubfzeev45vaa8rcusuu5.png" alt="Fully Qualified Search" width="800" height="323"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Amending the Code to log
&lt;/h2&gt;

&lt;p&gt;Next, I want to create the QuickFix.&lt;/p&gt;

&lt;p&gt;First I want to amend the line of code that logs the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;availableFixes:
- name: "use Logger"
  actions:
  - rewrite:
      to: "logger.log(Level.INFO, {{{ arguments.0 }}})"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I don't have to remember the mustache format. I used the &lt;code&gt;Show Variables&lt;/code&gt; in the GUI to show me the argument and double-clicked on it. Then the GUI filled in the appropriate matching mustache template.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Q6AYOqJW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/si7e45wc2gth0kgy67xi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q6AYOqJW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/si7e45wc2gth0kgy67xi.png" alt="Mustache variables in the GUI" width="800" height="399"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When I try it out, I can see that I still have to &lt;code&gt;alt+enter&lt;/code&gt; to import the &lt;code&gt;Level&lt;/code&gt; enum. But if I amend my QuickFix to have a fully qualified item then Sensei will add the import for me e.g.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It will replace &lt;code&gt;System.out.println(output);&lt;/code&gt; with&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;~~~~&lt;del&gt;&lt;/del&gt;&lt;br&gt;
logger.log(Level.INFO, output);&lt;br&gt;
~~~~&lt;del&gt;&lt;/del&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;And add an import for the enum:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;~~~~&lt;del&gt;&lt;/del&gt;&lt;br&gt;
import java.util.logging.Level;&lt;br&gt;
~~~~&lt;del&gt;&lt;/del&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If I rewrite to:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;logger.log(java.util.logging.Level.INFO, {{{ arguments.0 }}})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;And this will work, but I will still have to remember the syntax to instantiate the logger in the first place.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1bcH7-pL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/uzy9h5pl075rm8zvvsf9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1bcH7-pL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/uzy9h5pl075rm8zvvsf9.png" alt="Code rewrite rule" width="769" height="299"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Amending the code to add the logger field
&lt;/h2&gt;

&lt;p&gt;I can amend my QuickFix to create the &lt;code&gt;logger&lt;/code&gt; field for me as well.&lt;/p&gt;

&lt;p&gt;I will code the &lt;code&gt;logger&lt;/code&gt; first, and then add that to my recipe so I never have to code it again.&lt;/p&gt;

&lt;p&gt;~~~~&lt;del&gt;&lt;/del&gt;&lt;br&gt;
Logger logger2 = Logger.getLogger(SysOutTest.class.getName());&lt;br&gt;
~~~~&lt;del&gt;&lt;/del&gt;&lt;/p&gt;

&lt;p&gt;I tend to write the code first because then I can use IntelliJ code completion and syntax checking to make sure I get it right. As a side-effect, it will then be in the code preview when I edit the recipe to add the QuickFix lines that will create that code.&lt;/p&gt;

&lt;p&gt;And when I write the code, I want to use a different field name (here I'm using &lt;code&gt;logger2&lt;/code&gt;) because Sensei is clever enough not to add a duplicate field, so I have to fool it by using a different name.&lt;/p&gt;

&lt;p&gt;So I'll amend the recipe to create this code by adding a field called logger.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;availableFixes:
- name: "use Logger"
  actions:
  - rewrite:
      to: "logger.log(java.util.logging.Level.INFO, {{{ arguments.0 }}})"
  - addField:
      field: "java.util.logging.Logger logger = Logger.getLogger({{{ containingClass.name\
        \ }}}.class.getName())"
      target: "parentClass"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that I changed &lt;code&gt;SysOutTest&lt;/code&gt; to be a mustache variable so that it picks up the name of any class I use this recipe in. And again, I didn't remember the mustache syntax, I used the GUI &lt;code&gt;show variables&lt;/code&gt; to find the replacement I needed.&lt;/p&gt;

&lt;p&gt;And by fully qualifying the &lt;code&gt;Logger&lt;/code&gt; to &lt;code&gt;java.util.logging.Logger&lt;/code&gt;, Sensei will add the import and write the line of code that I want i.e.&lt;/p&gt;

&lt;p&gt;~~~~&lt;del&gt;&lt;/del&gt;&lt;br&gt;
Logger logger = Logger.getLogger(SysOutTest.class.getName());&lt;br&gt;
~~~~&lt;del&gt;&lt;/del&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0nnN7BVB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/k3rmvecd3a72by8p52rk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0nnN7BVB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/k3rmvecd3a72by8p52rk.png" alt="Seeing The Results of a QuickFix" width="800" height="603"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One useful thing about this recipe is that because it will only add the logger field once, I can use this on any existing code where I have used &lt;code&gt;System.out.println&lt;/code&gt; and use Sensei to change all the occurrences in my code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r9zNkjZG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/opo4655qvsqpd7l3gtb6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r9zNkjZG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/opo4655qvsqpd7l3gtb6.png" alt="Replace All println for Logger" width="800" height="198"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Next Steps
&lt;/h2&gt;

&lt;p&gt;Once I get used to this, I'll eventually train myself away from using &lt;code&gt;System.out.println&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And I can use Sensei to help me proactively write code by creating a second recipe which helps me create a logger.&lt;/p&gt;

&lt;p&gt;e.g. I can match on a class, where there is no field called logger, and add one.&lt;/p&gt;

&lt;p&gt;I created a recipe of level &lt;code&gt;Information&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Name: Logger: add logger
Description: Add logger to class
Level: Information
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To match on a class without a logger field:&lt;/p&gt;

&lt;p&gt;~~~~&lt;del&gt;&lt;/del&gt;&lt;br&gt;
search:&lt;br&gt;
  class:&lt;br&gt;
    without:&lt;br&gt;
      child:&lt;br&gt;
        field:&lt;br&gt;
          name: "logger"&lt;br&gt;
~~~~&lt;del&gt;&lt;/del&gt;&lt;/p&gt;

&lt;p&gt;And then I will reuse part of the QuickFix we saw earlier&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;availableFixes:
  - name: "Add Logger"
    actions:
      - addField:
          field: "java.util.logging.Logger logger = Logger.getLogger({{{ containingClass.name\
        \ }}}.class.getName())"
          target: "self"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note the difference in &lt;code&gt;target&lt;/code&gt; here compared to the first QuickFix. This uses &lt;code&gt;self&lt;/code&gt; because our Search matched the class. The first QuickFix uses &lt;code&gt;parentClass&lt;/code&gt; because we matched code within the class itself.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;This represents one of the key flows associated with using Sensei to help improve your personal programming skillset:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create a recipe to help with your immediate 'best practice'&lt;/li&gt;
&lt;li&gt;once you know how to use that best practice... create a recipe to make your workflow faster&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;You can install Sensei from within IntelliJ using "Preferences \ Plugins" (Mac) or "Settings \ Plugins" (Windows)  then just search for “sensei secure code”. &lt;a href="https://plugins.jetbrains.com/plugin/14015-sensei-by-secure-code-warrior-/getstarted"&gt;More Install Instructions&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The source code and recipes for this can be found in the &lt;code&gt;sensei-blog-examples&lt;/code&gt; repository in the Secure Code Warrior GitHub account, in the &lt;code&gt;pojoexamples&lt;/code&gt; module &lt;code&gt;test&lt;/code&gt; folder.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/securecodewarrior/sensei-blog-examples"&gt;github.com/securecodewarrior/sensei-blog-examples&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>staticanalysis</category>
      <category>logger</category>
    </item>
    <item>
      <title>Team Support for Sharing Programming Knowledge with Sensei</title>
      <dc:creator>Alan Richardson</dc:creator>
      <pubDate>Fri, 27 Nov 2020 13:53:28 +0000</pubDate>
      <link>https://dev.to/securecodewarrior/team-support-for-sharing-programming-knowledge-with-sensei-nb7</link>
      <guid>https://dev.to/securecodewarrior/team-support-for-sharing-programming-knowledge-with-sensei-nb7</guid>
      <description>&lt;p&gt;&lt;em&gt;TLDR; When one person creates a recipe to improve their code quality or productivity, everyone on the team can benefit when the cookbooks are shared. Sensei supports multiple mechanisms for sharing cookbooks e.g. shared folders, Github, and via HTTP downloads.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;The Secure Code Warrior Sensei plugin allows anyone to create a recipe to match poor quality coding patterns and fix them to be better quality. One thing we knew we had to solve, was how to share those recipes across a team so that the knowledge encoded in the recipe was transferred.&lt;/p&gt;

&lt;p&gt;Sensei provides a number of mechanisms for sharing cookbooks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Store Cookbooks in the Project Under Version Control&lt;/li&gt;
&lt;li&gt;Storing Cookbooks in a shared folder&lt;/li&gt;
&lt;li&gt;Store Cookbooks in Github&lt;/li&gt;
&lt;li&gt;Zipped files over HTTP(s)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By sharing the cookbooks, Sensei helps teams collaborate on knowledge sharing. The collaboration helps improve communication and embed the agreed code quality approaches.&lt;/p&gt;

&lt;p&gt;For example, sharing a cookbook allows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;team members to share useful recipes with each other.&lt;/li&gt;
&lt;li&gt;team leads to codify agreed coding practices for junior staff. 

&lt;ul&gt;
&lt;li&gt;To identify common violations with a quick fix for the agreed version.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;increased Inter-team co-operation.

&lt;ul&gt;
&lt;li&gt;An AppSec team might create recipes to highlight a problem in the code, and the development team could write the quick fix.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The next few sections explain how to implement each of the sharing mechanisms.&lt;/p&gt;

&lt;h3&gt;
  
  
  Store Cookbooks in the Project Under Version Control
&lt;/h3&gt;

&lt;p&gt;The project &lt;code&gt;.sensei&lt;/code&gt; folder is the default option when creating a cookbook file.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;`project://.sensei’&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All cookbooks and recipes would be stored in a &lt;code&gt;.sensei&lt;/code&gt; folder in your project. &lt;/p&gt;

&lt;p&gt;The easiest approach to sharing is to add the project &lt;code&gt;.sensei&lt;/code&gt; folder to version control.&lt;/p&gt;

&lt;p&gt;Then the &lt;code&gt;.sensei&lt;/code&gt; folder can be managed like any other shared code artifact associated with the project. The cookbooks are stored as YAML configuration, making them easy to merge during any commit and review process.&lt;/p&gt;

&lt;p&gt;This is the approach taken for the public &lt;code&gt;sensei-blog-examples&lt;/code&gt; project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/SecureCodeWarrior/sensei-blog-examples"&gt;https://github.com/SecureCodeWarrior/sensei-blog-examples&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.sensei&lt;/code&gt; folder contains the cookbook with all the recipes, and they are available to anyone that clones the repository.&lt;/p&gt;

&lt;h3&gt;
  
  
  Store Cookbooks in Any Folder
&lt;/h3&gt;

&lt;p&gt;Teams can also use cookbooks stored in central locations.&lt;/p&gt;

&lt;p&gt;Saving the cookbook to any folder with shared write access permissions will allow the whole team to update the recipes, and import them into any project that they happen to be working on.&lt;/p&gt;

&lt;p&gt;The location would be set to the directory path.&lt;/p&gt;

&lt;h3&gt;
  
  
  Store Recipes in Github
&lt;/h3&gt;

&lt;p&gt;Sensei can also access recipes that are stored in a Github repo. Both private and public repositories are supported.&lt;/p&gt;

&lt;h4&gt;
  
  
  Github over SSH
&lt;/h4&gt;

&lt;p&gt;SSH Repository access is configured using the following syntax for the &lt;code&gt;Location&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git@github.com:SecureCodeWarrior/acookbook.git&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For this to work, the repository would contain the contents of a cookbook folder.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;e.g. our Basic Protection Cookbook takes this approach and could be added as a "Locally configured Cookbook" using the location &lt;code&gt;git@github.com:SecureCodeWarrior/cookbook-basic-protection-set.git&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It is also possible to configure the branch and the subfolder for the cookbook e.g. in the &lt;code&gt;master&lt;/code&gt; branch in the &lt;code&gt;cookbook&lt;/code&gt; subfolder&lt;/p&gt;

&lt;p&gt;e.g.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;git@github.com:SecureCodeWarrior/sensei-blog-examples.git|master|.sensei&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An SSH key needs to be configured for private repositories.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/settings/keys"&gt;https://github.com/settings/keys&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And the key should not have a passphrase.&lt;/p&gt;

&lt;h4&gt;
  
  
  Github over HTTPS
&lt;/h4&gt;

&lt;p&gt;It is also possible to access public repositories over HTTPS, and the same &lt;code&gt;repo.git|branch|folder&lt;/code&gt; syntax is used e.g.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;https://github.com/SecureCodeWarrior/sensei-blog-examples.git|master|.sensei&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;or for the Basic Protection Cookbook&lt;/p&gt;

&lt;p&gt;&lt;code&gt;https://github.com/SecureCodeWarrior/cookbook-basic-protection-set.git&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Zipped over HTTP(s)
&lt;/h4&gt;

&lt;p&gt;Sensei can also access cookbooks which are zipped, over HTTP or HTTPS.&lt;/p&gt;

&lt;p&gt;e.g.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;http://localhost:8000/rules.sensei.zip&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The zip cookbook file should contain the contents of a cookbook folder e.g the &lt;code&gt;rules.sensei&lt;/code&gt; file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sharing Summary
&lt;/h3&gt;

&lt;p&gt;Sensei supports using multiple cookbooks so that an individual programmer can have recipes that support their own learning and productivity.&lt;/p&gt;

&lt;p&gt;More importantly, we know that teams work most effectively when knowledge is shared. Having shared team repositories, e.g. for a specific project, or a specific library, or for a shared set of migration patterns, can help boost team productivity and codify the team’s experience.&lt;/p&gt;

&lt;p&gt;When a cookbook is shared, multiple teams can use the same cookbook which can also improve inter-team collaboration from different disciplines e.g. AppSec to development.&lt;/p&gt;

&lt;p&gt;With four core sharing mechanisms available, Sensei hopefully has at least one approach you can use to increase collaboration on knowledge sharing.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;You can install Sensei from within IntelliJ using "Preferences \ Plugins" (Mac) or "Settings \ Plugins" (Windows)  then just search for “sensei secure code” (or find it on the &lt;a href="https://plugins.jetbrains.com/plugin/14015-sensei-by-secure-code-warrior-"&gt;JetBrains marketplace here&lt;/a&gt;)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;All the Sensei blog posts code and recipes are on Github in:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/SecureCodeWarrior/sensei-blog-examples"&gt;github.com/SecureCodeWarrior/sensei-blog-examples&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>java</category>
      <category>jetbrains</category>
      <category>development</category>
    </item>
    <item>
      <title>Using Documentation Links with Sensei</title>
      <dc:creator>Alan Richardson</dc:creator>
      <pubDate>Wed, 11 Nov 2020 13:57:30 +0000</pubDate>
      <link>https://dev.to/securecodewarrior/using-documentation-links-with-sensei-2jbc</link>
      <guid>https://dev.to/securecodewarrior/using-documentation-links-with-sensei-2jbc</guid>
      <description>&lt;p&gt;One of the difficulties with learning a new library, or sharing agreed practices across our team is documenting and creating examples.&lt;/p&gt;

&lt;p&gt;Very often we create small example projects, but we don't have them open when working with actual code.&lt;/p&gt;

&lt;p&gt;I've often thought it would be great to have the ability to link to our examples, or online examples and be able to goto a URL for more explanation when needed.&lt;/p&gt;

&lt;p&gt;With Java, we have JavaDoc comments, which can have a &lt;code&gt;see&lt;/code&gt; annotation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * @see &amp;lt;a href="https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations"&amp;gt;Junit 5 Annotation docs&amp;lt;/a&amp;gt;
 */
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaDoc like this in 3rd party libraries is a great help because we can use the &lt;code&gt;Quick Documentation&lt;/code&gt; functionality in IntelliJ to have access to more detailed examples. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--171d27dQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/v6dpnx9vs09zmilwx5l3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--171d27dQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/v6dpnx9vs09zmilwx5l3.png" alt="Documentation Link" width="545" height="232"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But, we all know that comments don't get updated as often as code, and web presence maintenance is often disconnected from library maintenance and sometimes performed by a different team entirely.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Sensei Helps
&lt;/h2&gt;

&lt;p&gt;Sensei provides the ability to match on library annotations and methods to provide links to long-form documentation on a wiki or third party tutorial site.&lt;/p&gt;

&lt;p&gt;As an example, I'm using the &lt;code&gt;@Test&lt;/code&gt; annotation from JUnit.&lt;/p&gt;

&lt;p&gt;The JavaDoc is very detailed, and the &lt;code&gt;Quick Documentation&lt;/code&gt; view explains how to use the annotation.&lt;/p&gt;

&lt;p&gt;But the official documentation on the web site is often easier to read and has more examples.&lt;/p&gt;

&lt;p&gt;When a team starts learning a library, having a set of recommended tutorials, can be very useful.&lt;/p&gt;

&lt;p&gt;Sensei has a &lt;code&gt;goto&lt;/code&gt; action that can open a URL, allowing us to link to external sites and examples for documentation that we, as a team, find useful.      &lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing the Goto URL
&lt;/h2&gt;

&lt;p&gt;To implement this I would create a search that matches the &lt;code&gt;@Test&lt;/code&gt; annotation from Junit.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;search:
  annotation:
    owner:
      method: {}
    type: "org.junit.jupiter.api.Test"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then I would add &lt;code&gt;goto&lt;/code&gt; actions for each of the URLs I find useful.&lt;/p&gt;

&lt;p&gt;e.g.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations"&gt;https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://junit.org/junit5/docs/current/user-guide/#writing-tests-classes-and-methods"&gt;https://junit.org/junit5/docs/current/user-guide/#writing-tests-classes-and-methods&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The example below would create a single Action &lt;code&gt;JUnit Annotations (learn)&lt;/code&gt; which would open both URLs in a browser at the same time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;availableFixes:
- name: "Learn about JUnit Annotations"
  actions:
  - goto:
      type: "URL"
      value: "https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations"
  - goto:
      type: "URL"
      value: "https://junit.org/junit5/docs/current/user-guide/#writing-tests-classes-and-methods"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And when I activate it in IntelliJ with &lt;code&gt;Alt+Enter&lt;/code&gt; I see the context menu which I can select to jump to the documentation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cMEKfLQ7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/3ln59nyaobutg3vfaruh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cMEKfLQ7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/3ln59nyaobutg3vfaruh.png" alt="documentation-links-alt-enter-link" width="279" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Multiple Actions
&lt;/h2&gt;

&lt;p&gt;I might choose to have multiple Actions so that each URL or tutorial has its own option in the &lt;code&gt;alt+enter&lt;/code&gt; Quick Fix pop up menu.&lt;/p&gt;

&lt;p&gt;For example, for the &lt;code&gt;@Parameterized&lt;/code&gt; annotation, I might want to link to the official documentation and a set of online example code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests"&gt;https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/eviltester/junitexamples/blob/master/src/test/java/parameterized/junit5/InitialExampleTest.java"&gt;https://github.com/eviltester/junitexamples/blob/master/src/test/java/parameterized/junit5/InitialExampleTest.java&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I would simply create a recipe that searches for the annotation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;search:
  annotation:
    owner:
      method: {}
    type: "org.junit.jupiter.params.ParameterizedTest"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And links off to the sites I identified as being useful:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;availableFixes:
- name: "JUnit Annotations (learn)"
  actions:
  - goto:
      type: "URL"
      value: "https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations"
- name: "What is a JUnit Test? (learn)"
  actions:
  - goto:
      type: "URL"
      value: "https://junit.org/junit5/docs/current/user-guide/#writing-tests-classes-and-methods"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both links would then appear in the pop-up dialog.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--E7-HZ6Dt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/zy97awz5guyu5kwv3gyd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--E7-HZ6Dt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/zy97awz5guyu5kwv3gyd.png" alt="documentation-links-show-link" width="417" height="193"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Who would benefit?
&lt;/h2&gt;

&lt;p&gt;I would have found this useful when using and learning libraries, especially when leading teams and helping them adopt a new library.&lt;/p&gt;

&lt;p&gt;This could also benefit teams creating libraries, by creating a standard set of documentation recipes to help guide people through the adoption of the library or new features in the library.&lt;/p&gt;

&lt;p&gt;This would be especially useful if the code maintenance and documentation maintenance are performed by different teams.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;You can install Sensei from within IntelliJ using &lt;code&gt;preferences &amp;gt; plugins&lt;/code&gt; (just search for “sensei secure code”).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;All the code for this blog post is on Github in the &lt;code&gt;junitexamples&lt;/code&gt; module in &lt;a href="https://github.com/SecureCodeWarrior/sensei-blog-examples"&gt;github.com/SecureCodeWarrior/sensei-blog-examples&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>intellij</category>
      <category>plugin</category>
    </item>
    <item>
      <title>What is Sensei?</title>
      <dc:creator>Alan Richardson</dc:creator>
      <pubDate>Wed, 11 Nov 2020 13:46:47 +0000</pubDate>
      <link>https://dev.to/securecodewarrior/what-is-sensei-4012</link>
      <guid>https://dev.to/securecodewarrior/what-is-sensei-4012</guid>
      <description>&lt;h1&gt;
  
  
  What is Sensei?
&lt;/h1&gt;

&lt;p&gt;The Sensei plugin provides an easy way to find specific code patterns in your source code, and then apply rewrite rules to amend the matching code. All within the Intellij IDE, and in real-time.&lt;/p&gt;

&lt;p&gt;For example, you could create a rule that matches on JUnit &lt;code&gt;@Disabled&lt;/code&gt; annotations which do not have a reason, Sensei would then tell you about the issue by highlighting the code in the IDE.&lt;/p&gt;

&lt;p&gt;Additionally, when you &lt;code&gt;alt+enter&lt;/code&gt;, you can have the option to &lt;code&gt;Add a todo comment parameter&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When selected, this would amend the code to add a boilerplate reason, which you can then amend, and if you don’t, it will show up in your TODO panel.&lt;/p&gt;

&lt;p&gt;e.g. &lt;code&gt;@Disabled&lt;/code&gt; would become &lt;code&gt;@Disabled("TODO: add a description here")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Sensei combines the functionality of a Static Analysis code scanner with a code rewriting engine.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9rAZqlel--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/2u862djazb0t5lvcnnb7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9rAZqlel--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/2u862djazb0t5lvcnnb7.gif" alt="Alt Text" width="758" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Demo Video
&lt;/h2&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/HPj1Lo97aTQ"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  IntelliJ Intention Actions
&lt;/h2&gt;

&lt;p&gt;Based on the above description, the obvious alternative (if you were not using Sensei) is to use the IntelliJ Intention Actions functionality.&lt;/p&gt;

&lt;p&gt;Sensei differs from IntelliJ Intention Actions because the aim with Sensei is to provide a way to create matchers and rewriters which are project specific, or even local to an individual developer.&lt;/p&gt;

&lt;p&gt;We have tried to put together a GUI that makes both the matching and rewrite rules easy to write and experiment with.&lt;/p&gt;

&lt;h2&gt;
  
  
  How can it help me personally improve?
&lt;/h2&gt;

&lt;p&gt;When I'm learning a new library, it takes me time to build up muscle memory around the methods and formatting. So I might choose to create personal recipes which&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  link off to the official documentation or tutorial pages&lt;/li&gt;
&lt;li&gt;  have boilerplate templates which are most effective&lt;/li&gt;
&lt;li&gt;  fix poor coding practices&lt;/li&gt;
&lt;li&gt;  add boilerplate code to help use a library&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I can use Sensei to build temporary recipes which prompt me for the current practices I've chosen to use and help me build up effective habits. And the recipes are temporary because I can remove them when I outgrow them.&lt;/p&gt;

&lt;h3&gt;
  
  
  How can it help my team improve?
&lt;/h3&gt;

&lt;p&gt;In the same way we can help teams build up muscle memory around agreed coding standards.&lt;/p&gt;

&lt;p&gt;Creating cookbooks of recipes that we apply when we find the same comments in pull request reviews. Since the cookbooks are stored in version control with the project, they are available to everyone on the project. And we can switch them off when we no longer need prompting.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sensei helps provide feedback early
&lt;/h3&gt;

&lt;p&gt;What we've tried to build with Sensei is a way of pulling the feedback that helps us improve, and reminders of corrective action, as early into the coding process as we can.&lt;/p&gt;

&lt;p&gt;Rather than wait for&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  the results of a static analyser&lt;/li&gt;
&lt;li&gt;  the comments from a code review&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can instead see the feedback, for custom standards we want to enforce, as we code.&lt;/p&gt;

&lt;p&gt;And we have either reminders, or actual rewrite rules, to help us write code that complies with the standards.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sensei is flexible
&lt;/h2&gt;

&lt;p&gt;In that way, Sensei is a bit of a mix, since it’s:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  part Static Analyser
&lt;/li&gt;
&lt;li&gt;  part coding tutor&lt;/li&gt;
&lt;li&gt;  part rewrite engine&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sensei is flexible enough to make the job of saying "What is Sensei?" that little bit harder.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sensei fills a gap in the programming workflow
&lt;/h3&gt;

&lt;p&gt;We've tried to make Sensei the missing piece of the programmer workflow that helps you improve specific elements in your coding style, or library use, that you and your team are currently working with.&lt;/p&gt;

&lt;p&gt;This flexibility means that it takes a little more time to get to grips with Sensei than a static analysis tool or the built-in IntelliJ Intensions. Still, by spending the time to experiment, you will gain a new way to speed up your learning in your personal development process.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to experiment?
&lt;/h2&gt;

&lt;p&gt;Once you have downloaded and installed Sensei from the &lt;a href="https://plugins.jetbrains.com/plugin/14015"&gt;Intellij Marketplace&lt;/a&gt;, we recommend installing via the plugins manager in IntelliJ preferences (just search for 'Sensei secure code' and you'll find it).&lt;/p&gt;

&lt;p&gt;We have also created a getting started guide on the &lt;a href="https://plugins.jetbrains.com/plugin/14015-sensei-by-secure-code-warrior-/getstarted"&gt;Intellij Marketplace - get started&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The easiest way to make Sensei work for you is to look at your coding process and consider:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  What documentation do you keep looking up?

&lt;ul&gt;
&lt;li&gt;  You could add some Sensei recipes that link back to that documentation.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;  What simple mistakes do you keep making?

&lt;ul&gt;
&lt;li&gt;  You could temporarily codify that poor coding pattern as a matcher, and write a Quick Fix rewrite that amends the code to be what you really want to write.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;  What boilerplate code do you write to use a library?

&lt;ul&gt;
&lt;li&gt;  You could create a Quick Fix rule to write the code for you.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since Sensei is designed to work alongside whatever static analysis tool you're using, if you find that the same violations are being reported from static analysis, then you could replicate the condition in a Sensei recipe. You can then add a Quick Fix to help train you, not just to identify the mistake but also to move quickly to writing the correct code.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;To learn more about Sensei - check out our &lt;a href="https://github.com/SecureCodeWarrior/sensei-blog-examples"&gt;Examples Project on Github&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It contains code examples, recipes and explanations.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/SecureCodeWarrior/sensei-blog-examples"&gt;github.com/SecureCodeWarrior/sensei-blog-examples&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>java</category>
      <category>intellij</category>
      <category>plugin</category>
    </item>
  </channel>
</rss>
