November 20, 2008, 08:48:48 am *
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: queries for idiomatic (atomic) crosscutting concerns  (Read 1551 times)
marius
Newbie
*
Posts: 2


« on: April 30, 2007, 07:23:47 pm »

Hi,

This is a (fairly long) summary of a set of queries that I have implemented in a tool called SoQueT. The tool is aimed at using queries to consistently document common idiomatic implementations of crosscutting concerns.
We describe an idiom as a crosscutting concern *sort*. A sort query would typically be simple, as each of the sorts corresponds to an atomic concern.
In the future, I would like to connect SoQueT to Semmle, and have these (and other queries) run by the Semmle query engine. For now, I would like to see how these queries would look like in Semmle.

Next, is a brief description of some of the sorts/queries - these are part of the library of queries of the aforementioned tool :

* Consistent behavior: The idiom is basically method invocations, and typical concerns
of this sorts would include implementations of a logging function, a listeners' notification
mechanism, or programmatic transaction management (the calls to demarcate a transaction).
 Query: invocations to a given method "m" in some (user-defined) context, e.g., a type hierarchy,
a package, etc.


I have some problems getting the corect results with the next query on the jhotdraw 60b1 code
(perhaps it also has to deal with polymorphism issues?):

from Method m, Method n
where m.getName().matches("execute")
   and
   n.calls(m)
   and
   m.getDeclaringType().getName().matches("AbstractCommand")
//   and ... condition to limit the callers to those in the Command hierarchy
select n

(or: m.getDeclaringType().hasQualifiedName("org.jhotdraw.standard", "AbstractCommand"));

where:
  - Command is an interface declaring the "execute" method
  - AbstractCommand is the default implementation of the interface (abstract class, but
  implements "execute")
  - Concrete Command classes extend AbstractCommand and have their execute method calling
  the super implementation; I am trying to have these callers reported


* Role superimposition: This sort is about implementation of secondary roles, like the
ones present in design paterns, eg, Subject, Listener, Visitable, etc.
 Query: report the implement relations for a certain type in a particular context. The
context idea is similar to Consistent behavior.


* Exception propagation deals with the exeption re-throwing mechanism along a call
chain. The query starts from the method throwing the exception of interest
and reports recursively those callers that re-throw the exception.

This query:

 from Method m, Method n, Exception exception
 where
    m.getName().matches("readStorable")
    and m.getDeclaringType().getName().matches("StorableInput")
    and n.calls(m)
    and exception.hasName("IOException")
    and m.getAnException() == exception
 select n, m

returns also callers that catch the exception instead of re-throwing it. Is this the
meaning of "getAnException"?


Some sort queries are a bit more fancy ...

* Redirection layer, for example, looks for redirection calls between methods of two given types.
A typical example is in the Decorator pattern, where the methods of the Decorator consistently
forward their calls to pair methods in the decorated type. The query receives the redirector type
as an input parameter, and reports those methods that (consistently) forward their calls to pair
methods in the receiver type.

* Suport classes - the query searches for classes of a type that are nested within classes of a
different type; that is, two type hierarchies that interact through nested clases. This is
a more complex case of role superimposition.


And a bit harder one ...

* Expose context/Context passing is querying for those methods in a call chain that pass
a given parameter along the chain.


I have played a bit with Semmle, but I am not sure yet about some of these queries,
particularly the last one, (or the query for redirection). I guess these would take me
some more time.

I would like to have your opinions on the Semmle support for any of these queries.

Thanks,
Marius
Logged
Oege de Moor
Newbie
*
Posts: 30


« Reply #1 on: May 29, 2007, 08:35:07 am »

There are quite a lot of questions here, so we sat down at ICSE to try out some of these.

To look for redirecting calls, we want to find methods m and n such that m calls n, m does not call other methods declared in the same class as n, and no other method in m's class call n . In .QL, that reads:

Code:
predicate redirect(Method m, Method n) {
   // m makes call to n
   m.calls(n)
   and
   // m makes no other calls to class of n
   not exists(Method k | k.getDeclaringType() = n.getDeclaringType()
                         and m.calls(k) and k!=n)
   and
   not exists(Method l | l.getDeclaringType() = m.getDeclaringType()
                         and l.calls(n) and l!=m)
}

Using this predicate, we can look for occurrences of the wrapper pattern by finding a class wrapper and another type wrappee such that the wrapper class redirects more than 4 methods to the wrappee:

Code:
from Class wrapper, RefType wrappee
where wrapper.getPackage().getName().matches("org.jhotdraw%")
      and
      wrappee.getPackage().getName().matches("org.jhotdraw%")
      and
      wrapper != wrappee
      and
      count(Method m, Method n |
             m.getDeclaringType() = wrapper and
             n.getDeclaringType() = wrappee and
             redirect(m,n)) > 4
select wrapper, wrappee

Thanks, Marius, for the interesting examples!
Logged
Pages: [1]
  Print  
 
Jump to: