Search in sources :

Example 1 with SourceVisitor

use of org.checkerframework.framework.source.SourceVisitor in project jOOQ by jOOQ.

the class SQLDialectChecker 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);
                Support support = elementFromUse.getAnnotation(Support.class);
                // all jOOQ API method calls will type check.
                if (support != null && support.value().length > 0) {
                    Element enclosing = elementFromDeclaration(enclosingMethod(getPath(root, node)));
                    EnumSet<SQLDialect> supported = EnumSet.copyOf(asList(support.value()));
                    EnumSet<SQLDialect> allowed = EnumSet.noneOf(SQLDialect.class);
                    EnumSet<SQLDialect> required = EnumSet.noneOf(SQLDialect.class);
                    boolean evaluateRequire = true;
                    while (enclosing != null) {
                        Allow allow = enclosing.getAnnotation(Allow.class);
                        if (allow != null)
                            allowed.addAll(asList(allow.value()));
                        if (evaluateRequire) {
                            Require require = enclosing.getAnnotation(Require.class);
                            if (require != null) {
                                evaluateRequire = false;
                                required.clear();
                                required.addAll(asList(require.value()));
                            }
                        }
                        enclosing = enclosing.getEnclosingElement();
                    }
                    if (allowed.isEmpty())
                        error(node, "No jOOQ API usage is allowed at current scope. Use @Allow.");
                    if (required.isEmpty())
                        error(node, "No jOOQ API usage is allowed at current scope due to conflicting @Require specification.");
                    boolean allowedFail = true;
                    allowedLoop: for (SQLDialect a : allowed) {
                        for (SQLDialect s : supported) {
                            if (a.supports(s)) {
                                allowedFail = false;
                                break allowedLoop;
                            }
                        }
                    }
                    if (allowedFail)
                        error(node, "The allowed dialects in scope " + allowed + " do not include any of the supported dialects: " + supported);
                    boolean requiredFail = false;
                    requiredLoop: for (SQLDialect r : required) {
                        for (SQLDialect s : supported) if (r.supports(s))
                            continue requiredLoop;
                        requiredFail = true;
                        break requiredLoop;
                    }
                    if (requiredFail)
                        error(node, "Not all of the required dialects " + required + " from the current scope are supported " + supported);
                }
            } catch (final Exception e) {
                print(new Printer() {

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

Example 2 with SourceVisitor

use of org.checkerframework.framework.source.SourceVisitor 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)

Example 3 with SourceVisitor

use of org.checkerframework.framework.source.SourceVisitor in project checker-framework by typetools.

the class SignaturePrinter method init.

// /////// Initialization /////////////
/**
 * Initialization.
 *
 * @param env the ProcessingEnvironment
 * @param checkerName the name of the checker
 */
private void init(ProcessingEnvironment env, @Nullable @BinaryName String checkerName) {
    if (checkerName != null) {
        try {
            Class<?> checkerClass = Class.forName(checkerName);
            Constructor<?> cons = checkerClass.getConstructor();
            checker = (SourceChecker) cons.newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } else {
        checker = new SourceChecker() {

            @Override
            protected SourceVisitor<?, ?> createSourceVisitor() {
                return null;
            }

            @Override
            public AnnotationProvider getAnnotationProvider() {
                throw new UnsupportedOperationException("getAnnotationProvider is not implemented for this class.");
            }
        };
    }
    checker.init(env);
}
Also used : SourceVisitor(org.checkerframework.framework.source.SourceVisitor) AnnotationProvider(org.checkerframework.javacutil.AnnotationProvider) SourceChecker(org.checkerframework.framework.source.SourceChecker)

Aggregations

SourceVisitor (org.checkerframework.framework.source.SourceVisitor)3 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)2 PrintWriter (java.io.PrintWriter)2 Element (javax.lang.model.element.Element)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 Allow (org.jooq.Allow)2 SourceChecker (org.checkerframework.framework.source.SourceChecker)1 AnnotationProvider (org.checkerframework.javacutil.AnnotationProvider)1 PlainSQL (org.jooq.PlainSQL)1 Require (org.jooq.Require)1 SQLDialect (org.jooq.SQLDialect)1 Support (org.jooq.Support)1