You can use the check for the flag instance and show you a warning / error. This can be exceptional for the end.
To add a custom inspection, follow https://www.jetbrains.com/idea/help/creating-custom-inspections.html or do the following:
- Open the settings (CTRL + ALT + S) and search for checks.
- In the tree, find "General" and under it "Inspection of structural search".
- In the description you will see a panel of cruelty / parameters. Next to the options, click + and add a search template.
To customize the check to your question:
- Add a variable to the search template field followed by a method call, for example: $ Instance $ .get ()
- Click "Edit Variables" and select "$ Instance" from the list.
- In Expression Constraints, add an optional expression type and apply the constraint in the type hierarchy.
Save the changes and check. (CTRL + Shift + Alt + I, type "Check structural search"). This should show you all instances of Optional.get ().
Edit: in your question, you are asking if it is possible to get a compilation error. I believe that this will strictly depend on how you build your code. If you add a check and set the error level to Error, it should fail in Make / Compile, but not in mvn install.
source share