SlideShare a Scribd company logo
A basic introduction to Writing Custom Detectors for FindBugs
FindBugs Static analysis tool for Java
Detects suspicious patterns in code ->  See bug examples Detectors for ~370 bug types ->  See  bug list Use filters to select specific sets of detectors  What's FindBugs?
Run as... IDE plugin
Ant task / maven plugin – part of CI build
Standalone app (CLI and GUI) Operates on Java ByteCode, so you can analyse: Your compiled Java code
Dependent libraries (binaries)
Other JVM languages, compiled to ByteCode? FindBugs What's FindBugs?
Enforce a project-specific constraint, e.g.: Ensure all logging is guarded
Flag common API misuses
Environment/platform-specific constraints
Enforce naming conventions You've identified a new, general bug pattern: A common misuse of a JCL API
A sequence of operations that is doomed to fail Custom Detectors Why create a custom detector?
A plugin is a jar file containing at least 3 files: findbugs.xml
messages.xml
A detector class FindBugs loads plugin jars from its “plugin” directory. One plugin jar can contain multiple detector classes.
One detector class can report multiple bug types. Custom Detectors FindBugs Plugins
Custom Detectors The XML files ->  See examples

More Related Content

PPT
Rational Robot (http://www.geektester.blogspot.com)
ODP
Introduction to Binary Exploitation
PDF
Exception handling & logging in Java - Best Practices (Updated)
PDF
JavaCro'14 - Test Automation using RobotFramework Libraries – Stojan Peshov
PDF
Exception handling and logging best practices
PDF
Introduction to Robot Framework (external)
PDF
Network Protocol Testing Using Robot Framework
PPT
Defensive programming
Rational Robot (http://www.geektester.blogspot.com)
Introduction to Binary Exploitation
Exception handling & logging in Java - Best Practices (Updated)
JavaCro'14 - Test Automation using RobotFramework Libraries – Stojan Peshov
Exception handling and logging best practices
Introduction to Robot Framework (external)
Network Protocol Testing Using Robot Framework
Defensive programming

What's hot (20)

PDF
Defencive programming
PPTX
Exception handling
PPTX
Java ce241
PPT
Java findamentals1
PPT
Java findamentals1
PPT
Java findamentals1
PDF
Introduction to Robot Framework – Exove
PPT
Stopping the Rot - Putting Legacy C++ Under Test
PDF
Smart Migration to JDK 8
PPTX
Role of .NET in Exception Handling
PPT
Software Testing - Tool support for testing (CAST) - Mazenet Solution
PDF
Java 8 features
PDF
Typescript for the programmers who like javascript
PPT
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
PPTX
Let's start with Java- Basic Concepts
PPT
Advanced Rational Robot A Tribute (http://www.geektester.blogspot.com)
PPS
Java Exception handling
PPTX
Java 8 Feature Preview
PDF
Java se 8 language enhancements & features
Defencive programming
Exception handling
Java ce241
Java findamentals1
Java findamentals1
Java findamentals1
Introduction to Robot Framework – Exove
Stopping the Rot - Putting Legacy C++ Under Test
Smart Migration to JDK 8
Role of .NET in Exception Handling
Software Testing - Tool support for testing (CAST) - Mazenet Solution
Java 8 features
Typescript for the programmers who like javascript
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
Let's start with Java- Basic Concepts
Advanced Rational Robot A Tribute (http://www.geektester.blogspot.com)
Java Exception handling
Java 8 Feature Preview
Java se 8 language enhancements & features
Ad

Similar to Custom Detectors for FindBugs (London Java Community Unconference 2) (20)

PDF
Code quality par Simone Civetta
PDF
Gallio Crafting A Toolchain
PDF
(1) c sharp introduction_basics_dot_net
PPT
C#/.NET Little Wonders
PPT
What is Java Technology (An introduction with comparision of .net coding)
PPT
Java Basics
PDF
Compiler construction lecture 01 .pptx.pdf
PPTX
Whats New in Visual Studio 2012 for C++ Developers
PPTX
Back-2-Basics: .NET Coding Standards For The Real World (2011)
PPT
.NET Debugging Tips and Techniques
PPT
.Net Debugging Techniques
PPTX
OpenDaylight Developer Experience 2.0
PPTX
A Comparison of .NET Framework vs. Java Virtual Machine
PDF
Using Stratego/XT for generation of software connectors.
PPTX
Using the android ndk - DroidCon Paris 2014
PPT
Code Documentation. That ugly thing...
PPTX
Framework engineering JCO 2011
PPT
C# features
PPT
basic_java.ppt
PPTX
PVS-Studio and static code analysis technique
Code quality par Simone Civetta
Gallio Crafting A Toolchain
(1) c sharp introduction_basics_dot_net
C#/.NET Little Wonders
What is Java Technology (An introduction with comparision of .net coding)
Java Basics
Compiler construction lecture 01 .pptx.pdf
Whats New in Visual Studio 2012 for C++ Developers
Back-2-Basics: .NET Coding Standards For The Real World (2011)
.NET Debugging Tips and Techniques
.Net Debugging Techniques
OpenDaylight Developer Experience 2.0
A Comparison of .NET Framework vs. Java Virtual Machine
Using Stratego/XT for generation of software connectors.
Using the android ndk - DroidCon Paris 2014
Code Documentation. That ugly thing...
Framework engineering JCO 2011
C# features
basic_java.ppt
PVS-Studio and static code analysis technique
Ad

More from Robin Fernandes (6)

PDF
AtlasCamp 2016: Art of PaaS - Lessons learned running a platform for hundreds...
PDF
AtlasCamp 2014: Building a Production Ready Connect Add-On
ODP
Summit2011 satellites-robinf-20110605
ODP
Php On Java (London Java Community Unconference)
ODP
PHP on Java (BarCamp London 7)
ODP
An Introduction to SPL, the Standard PHP Library
AtlasCamp 2016: Art of PaaS - Lessons learned running a platform for hundreds...
AtlasCamp 2014: Building a Production Ready Connect Add-On
Summit2011 satellites-robinf-20110605
Php On Java (London Java Community Unconference)
PHP on Java (BarCamp London 7)
An Introduction to SPL, the Standard PHP Library

Recently uploaded (20)

PDF
Machine learning based COVID-19 study performance prediction
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Approach and Philosophy of On baking technology
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
cuic standard and advanced reporting.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Electronic commerce courselecture one. Pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Cloud computing and distributed systems.
Machine learning based COVID-19 study performance prediction
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Reach Out and Touch Someone: Haptics and Empathic Computing
Approach and Philosophy of On baking technology
Spectroscopy.pptx food analysis technology
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Per capita expenditure prediction using model stacking based on satellite ima...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
The AUB Centre for AI in Media Proposal.docx
cuic standard and advanced reporting.pdf
Unlocking AI with Model Context Protocol (MCP)
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
NewMind AI Weekly Chronicles - August'25 Week I
Building Integrated photovoltaic BIPV_UPV.pdf
Encapsulation theory and applications.pdf
Electronic commerce courselecture one. Pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Cloud computing and distributed systems.

Custom Detectors for FindBugs (London Java Community Unconference 2)

  • 1. A basic introduction to Writing Custom Detectors for FindBugs
  • 2. FindBugs Static analysis tool for Java
  • 3. Detects suspicious patterns in code -> See bug examples Detectors for ~370 bug types -> See bug list Use filters to select specific sets of detectors What's FindBugs?
  • 4. Run as... IDE plugin
  • 5. Ant task / maven plugin – part of CI build
  • 6. Standalone app (CLI and GUI) Operates on Java ByteCode, so you can analyse: Your compiled Java code
  • 8. Other JVM languages, compiled to ByteCode? FindBugs What's FindBugs?
  • 9. Enforce a project-specific constraint, e.g.: Ensure all logging is guarded
  • 10. Flag common API misuses
  • 12. Enforce naming conventions You've identified a new, general bug pattern: A common misuse of a JCL API
  • 13. A sequence of operations that is doomed to fail Custom Detectors Why create a custom detector?
  • 14. A plugin is a jar file containing at least 3 files: findbugs.xml
  • 16. A detector class FindBugs loads plugin jars from its “plugin” directory. One plugin jar can contain multiple detector classes.
  • 17. One detector class can report multiple bug types. Custom Detectors FindBugs Plugins
  • 18. Custom Detectors The XML files -> See examples
  • 19. The Detector Class The Most Simple Detector... public class MyDetector implements Detector { private BugReporter reporter ; /** Instantiated when analysis starts. */ public MyDetector(BugReporter reporter) { this . reporter = reporter; } /** Invoked for every class to analyse */ @Override public void visitClassContext(ClassContext classContext) { } /** Invoked after all classes have been analysed by all detectors. */ @Override public void report() { } } -> Examples
  • 20. The Detector Class Visitors & Detectors Visitor visit(class) visit(const) visit(field) visit(method) … state -> Examples
  • 21. A character set used on IBM mainframes.
  • 23. It is not ASCII-compatible.
  • 24. … but it becomes relevant when your code runs in an IBM mainframe! The EBCDIC Issue What's EBCDIC? “ EBCDIC is not relevant to your life.” -Joel Spolsky
  • 25. Programs often convert between bytes and character data Writing/reading text to/from the file system
  • 26. Sending/receiving text over the network Such conversions always use a character set, e.g.: The EBCDIC Issue é [0xC3, 0xA9] [0xE9] Text Byte value Charset Character Sets
  • 27. In Java, if no charset is specified, a default is used.
  • 28. The default is platform-specific. The EBCDIC Issue hello [0x68,0x65,0x6C,0x6C,0x6F] Default Charset in Java String s1 = new String(myByteArray, Charset.forName( "UTF-8" )); // Uses UTF-8 String s2 = new String( myByteArray ); // Uses default charset On most platforms, this default is “ASCII-compatible”: These characters have the same byte value in all ASCII-compatible Character sets.
  • 29. EBCDIC is not ASCII-compatible.
  • 30. Imagine you're sending bytes over the network and the client is expecting ISO8859-1 text:
  • 31. On ASCII-compatible platforms , the code above sends the correct ISO8859-1 bytes for HELLO .
  • 32. On z/OS , it sends data that ISO88591-decodes to: ÈÅÓÓÖ
  • 33. The code should look something like: The EBCDIC Issue If you �Unicode, you’ll �����EBCDIC connection.getOutputStream().write( "HELLO" .getBytes()); connection.getOutputStream().write( "HELLO" .getBytes( " ISO8859-1 " ));
  • 34. The file.encoding system property can be used to change the default.
  • 35. Not a suitable solution if different libraries make different assumptions about the default.
  • 36. Can be useful for testing that your code works OK in an EBCDIC environment, e.g.: The EBCDIC Issue -Dfile.encoding java -Dfile.encoding=IBM-1047 -Dconsole.encoding=ISO8859-1 ...
  • 37. The EBCDIC Issue Affected Java Class Library Methods java.lang.String.getBytes() java.lang.String(byte[] bytes) java.io.ByteArrayOutputStream.toString() java.io.FileReader(String filename) java.io.FileReader(File file) java.io.FileReader(FileDescriptor fileDescriptor) java.io.FileWriter(String filename) java.io.FileWriter(File file) java.io.FileWriter(FileDescriptor fileDescriptor) java.io.InputStreamReader(InputStream input) java.io.OutputStreamWriter(OutputStream output) java.io.PrintStream(File file) java.io.PrintStream(OutputStream output) java.io.PrintStream(String string) java.io.PrintWriter(File file) java.io.PrintWriter(OutputStream output) java.io.PrintWriter(String string) java.util.Scanner(InputStream input) java.util.Formatter(String filename) java.util.Formatter(File file) java.util.Formatter(OutputStream output)
  • 38. -> See code. Default Encoding Detector Implementation of the default encoding detector
  • 39. BugAccumulator: helps avoid reporting same bug many times
  • 40. Class metadata and identifiers : XClass, ClassDescriptor, JavaClass (BCEL)
  • 42. ... AnnotationDatabase: helps to simplify marking interesting classes, methods, fields... and identify their usage.
  • 43. StatelessDetector: Marker interface – detector is cloned on each class so any state that is not cloned can be GC'd
  • 44. DataflowAnalysis: Provides access to a control flow graph More FindBugs Classes
  • 45. Examine the built-in detectors Find one that detects a pattern similar to yours. Testing your detector Run Findbugs on test data, compare report against baseline
  • 46. Or see this blog post for a lighter approach. -> Example using annotations to mark expected bugs Don't be put off by ByteCode Try the ByteCode Outline plugin for Eclipse Misc. Tips
  • 47. This presentation: http://bit.ly/fb_slides Today's code: http://bit.ly/fb_demo2011 Encoding detector: http://bit.ly/fb_enc References / further reading / tools: developerWorks article about custom detectors: http://bit.ly/fb_dw
  • 48. Presentation on custom detectors by FindBugs authors: http://bit.ly/fb_4r
  • 49. Daniel Schneller's blog posts about custom detectors: http://bit.ly/fb_dsblog
  • 50. Josh Cummings' blog post about testing detectors: http://bit.ly/fb_jcblog
  • 51. ByteCode Outline plugin for Eclipse: http://asm.ow2.org/eclipse/index.html