November 20, 2008, 02:09:56 pm *
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Embedded queries  (Read 1227 times)
sku
Newbie
*
Posts: 1


« on: April 11, 2007, 11:18:44 am »

Hi there!

I was wondering if it would be feasible to add a feature to allow users to embed queries directly in source code, for example in specially formatted comments.

The idea behind this would be to have something like compile time assertions (similar to AspectJ’s ‘declare error’), but with the full power and elegance of .QL queries.  Smiley

These embedded queries could be evaluated automatically (at compile time), and queries that ‘fail’ (e.g. produce any results) could be shown as errors.

Here’s an example of what it could look like:

Code:
public class C {
    /*.QL:ASSERT(“Method C.foo should not be called from within C”)
    {
        from MethodCall c
        where
            c.getCaller().getDeclaringType()=thisType
            and
            c.getCallee()=thisMethod
            select call.getCaller()
    }
    */
    public void foo() {…}
    …
}

This would (roughly) be equivalent to the following AspectJ error declaration:
Code:
declare error: call(* C.foo(..)) && within(C) : “error…”;

Note that thisType and thisMethod would be context dependent, that is, they would depend on the location of the query.

The possible advantages of having queries directly in source code are that they are easy to create and evaluate, and more importantly, they can be placed close to the entity that they refer to, making it practical to write specialized queries that refer to particular bits of code.

Anyway, this is just an idea…(and admittedly, in this example, the AspectJ version is a bit shorter…  Wink)

-sk
Logged
Oege de Moor
Newbie
*
Posts: 30


« Reply #1 on: April 12, 2007, 12:11:14 pm »

Indeed that would be a good feature to have: one of the strengths of SemmleCode is that it becomes very easy to annotate libraries with coding conventions that are specific to that library.

For  example, it would be natural if requirements for a framework such as j2ee would be expressed as .QL queries in special comments. The sort of coding conventions I am thinking of are

http://checkstyle.sourceforge.net/config_j2ee.html

These are readily expressible in .QL (and rather more awkward, if expressible at all, in AspectJ  Wink .

It shouldn't be too hard to implement, so we'll add it to our to-do list.



Logged
Pages: [1]
  Print  
 
Jump to: