Hello,
I am a newbie and may be missing something.
I am having trouble expressing the concept "invokes method call through a field" in .QL.
Task #1: Locate all calls to System.out.println()
The following .QL query just happens to work because hardly anyone uses a System.out
field read for any purpose other than invoking the println() method
// readers of System.out.println()
from FieldRead out, Class system, Method m
where
system.hasQualifiedName("java.lang", "System")
and out.getField().hasName("out")
and out.getField().getDeclaringType() == system
and out.getSite() == m
and m.fromSource()
select
m.getDeclaringType().getPackage().getName(),
m.getDeclaringType().getCompilationUnit().getName(),
m.getName(),
out
But to make this query industrial strength, we need to add the fact that field System.out
is of type java.io.PrintStream and println() is invoked through System.out.
// invokers of System.out
from FieldRead out, Class system, Class printStream, Method println, MethodCall cc, Method m
where
system.hasQualifiedName("java.lang", "System")
and out.getField().hasName("out")
and out.getField().getDeclaringType() == system
and printStream.hasQualifiedName("java.io", "PrintStream")
and println.hasName("println")
and println.getDeclaringType() == printStream
and out.getField().getType() == printStream
// ===========================================
// here is where I have trouble expressing that FieldRead out was
// for the purpose of invoking println()
// ===========================================
and cc.getCallee() == println
and cc.getCaller() == m
and out.getSite() == m
and m.fromSource()
select
m.getDeclaringType().getPackage().getName(),
m.getDeclaringType().getCompilationUnit().getName(),
m.getName(),
println
Task #2: Locate places in code where an instance variable is used to invoke a static method.
This is considered bad form in some circles.
If task #1 is possible, then we could do task #2.
Any help would be appreciated.
Thanks,
Rod.