Search in sources :

Example 1 with PlainSQL

use of org.jooq.PlainSQL in project jOOQ by jOOQ.

the class PlainSQLChecker method createSourceVisitor.

@Override
protected SourceVisitor<Void, Void> createSourceVisitor() {
    return new SourceVisitor<Void, Void>(getChecker()) {

        @Override
        public Void visitMethodInvocation(MethodInvocationTree node, Void p) {
            try {
                ExecutableElement elementFromUse = elementFromUse(node);
                PlainSQL plainSQL = elementFromUse.getAnnotation(PlainSQL.class);
                // all jOOQ API method calls will type check.
                if (plainSQL != null) {
                    Element enclosing = elementFromDeclaration(enclosingMethod(getPath(root, node)));
                    boolean allowed = false;
                    moveUpEnclosingLoop: while (enclosing != null) {
                        if (enclosing.getAnnotation(Allow.PlainSQL.class) != null) {
                            allowed = true;
                            break moveUpEnclosingLoop;
                        }
                        enclosing = enclosing.getEnclosingElement();
                    }
                    if (!allowed)
                        error(node, "Plain SQL usage not allowed at current scope. Use @Allow.PlainSQL.");
                }
            } catch (final Exception e) {
                print(new Printer() {

                    @Override
                    public void print(PrintWriter t) {
                        e.printStackTrace(t);
                    }
                });
            }
            return super.visitMethodInvocation(node, p);
        }
    };
}
Also used : MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) SourceVisitor(org.checkerframework.framework.source.SourceVisitor) ExecutableElement(javax.lang.model.element.ExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) PlainSQL(org.jooq.PlainSQL) Allow(org.jooq.Allow) PrintWriter(java.io.PrintWriter)

Aggregations

MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)1 PrintWriter (java.io.PrintWriter)1 Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 SourceVisitor (org.checkerframework.framework.source.SourceVisitor)1 Allow (org.jooq.Allow)1 PlainSQL (org.jooq.PlainSQL)1