DevBooststLogotudLogorewerseLogo modelplexLogo
Bridging the gap between models and source code.

Generic Source Code Analysis based on OCL

Return to Main JaMoPP Applications Page



Source code analysis is a common approach to find irregularities and bugs in programs or to enforce coding conventions. As a consequence various tools exist to support developers in this task (e.g., Findbugs for the Java language). Based on pattern matching or abstract interpretation these tools can find faults in programs without actually executing them.

Using model-based representations of programs, such analysis can be performed uniformly for arbitrary languages. For example, our analysis tool RestrictED uses declarative expressions written in OCL to specify undesired code patterns. Earlier experiments showed that static analysis can be performed on small toy languages. With the advent of JaMoPP this approach can be applied to the Java language in its full extent.

Returning to our running example, we might now want to make manual additions to the code generated in the last section. Such additions should not violate the guidelines that were respected by the code generation. We will use RestrictED to present two simple OCL invariants to check common coding conventions.

The first OCL expression is shown below. This invariant states that fields must not use the modifier public by collecting the set of modifiers that have the type Public and checking that this set is empty.

 context members::Field inv: 
   self->modifiers->select(m|m.oclIsKindOf(modifiers::Public))->size() = 0

A second invariant (shown below) states that whenever a classifier contains a method equals() an implementation of the hashCode() method must be present too. This is important because the implementation of both methods must match to ensure correct comparison in collections.

 context members::Method inv: 
   if ( = 'equals') then 
     (self.container->members->exists(m | = 'hashCode')) 

Figure 1 shows a screenshot of the RestrictED tool applied to one of the classes generated using an ATL transformation. The two basic examples indicate how arbitrary OCL expressions can be used to analyse Java source code. In contrast to most other analysis tools for Java, users can add custom restrictions, do so declaratively and use the same language for all software artefacts. Furthermore, JaMoPP enables using model transformations to declaratively repair code according to given rules.

Figure 1. Screenshot of the RestrictED tool