use of org.jooq.Require 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);
}
};
}
use of org.jooq.Require in project jOOQ by jOOQ.
the class Tools method checkSQLDialect.
static final <T> T checkSQLDialect(MethodInvocationTree node, Supplier<Element> enclosingSupplier, Function<? super String, ? extends T> error, Function<? super Printer, ? extends T> print) {
try {
ExecutableElement elementFromUse = elementFromUse(node);
Support support = elementFromUse.getAnnotation(Support.class);
// In the absence of a @Support annotation, all jOOQ API method calls will type check.
if (support != null) {
Element enclosing = enclosingSupplier.get();
// [#7929] "Empty" @Support annotations expand to all SQLDialects
EnumSet<SQLDialect> supported = EnumSet.copyOf(support.value().length > 0 ? asList(support.value()) : asList(SQLDialect.values()));
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())
return error.apply("No jOOQ API usage is allowed at current scope. Use @Allow.");
boolean allowedFail = true;
allowedLoop: for (SQLDialect a : allowed) {
for (SQLDialect s : supported) {
if (a.supports(s)) {
allowedFail = false;
break allowedLoop;
}
}
}
if (allowedFail)
return error.apply("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)
return error.apply("Not all of the required dialects " + required + " from the current scope are supported " + supported);
}
} catch (final Exception e) {
return print.apply(new Printer() {
@Override
public void print(PrintWriter t) {
e.printStackTrace(t);
}
});
}
return null;
}
Aggregations