Add JdbcMapper.WarnOnUnusedParams annotation

This commit is contained in:
Travis Burtrum 2017-08-07 15:26:52 -04:00
parent 6f23696952
commit d2353bd74b
4 changed files with 17 additions and 1 deletions

View File

@ -50,6 +50,13 @@ public interface JdbcMapper extends Closeable {
String arrayStringTypeName() default "";
}
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.METHOD})
/**
* Instead of a compile-time error this will only warn on unused params, mainly for debugging or presenting an unfinished API
*/
public @interface WarnOnUnusedParams {}
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.METHOD})
public @interface SQL {

View File

@ -205,6 +205,7 @@ public class JdbcMapperProcessor extends AbstractProcessor {
lookupCloseMethod = false;
continue; // skip close method
}
final JdbcMapper.WarnOnUnusedParams warnOnUnusedParams = eeMethod.getAnnotation(JdbcMapper.WarnOnUnusedParams.class);
final JdbcMapper.SQL sql = eeMethod.getAnnotation(JdbcMapper.SQL.class);
if (sql == null || sql.value().isEmpty()) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@JdbcMapper.SQL with non-empty query is required on abstract or interface methods", methodElement);
@ -318,7 +319,7 @@ public class JdbcMapperProcessor extends AbstractProcessor {
} else if(isPrimitiveInteger(unusedType.getKind()) && maxRows == null && this.allowedMaxRowParamNames.contains(unusedParam.getKey())) {
maxRows = CompileTimeResultSetMapper.MaxRows.getMaxRows(unusedParam.getKey(), unusedType.toString());
} else
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, String.format("@JdbcMapper.SQL method has unused parameter '%s'", unusedParam.getKey()), unusedParam.getValue());
processingEnv.getMessager().printMessage(warnOnUnusedParams != null ? Diagnostic.Kind.MANDATORY_WARNING : Diagnostic.Kind.ERROR, String.format("@JdbcMapper.SQL method has unused parameter '%s'", unusedParam.getKey()), unusedParam.getValue());
}
}

View File

@ -51,6 +51,10 @@ public interface PersonDAO extends JdbcMapper {
@JdbcMapper.SQL("SELECT person_no FROM person WHERE last_name = {lastName}")
long getPersonNo(String lastName) throws SQLException;
@JdbcMapper.WarnOnUnusedParams
@JdbcMapper.SQL("SELECT person_no FROM person WHERE last_name = {lastName}")
long getPersonNoUnusedParam(String lastName, String unused) throws SQLException;
@JdbcMapper.SQL("SELECT first_name, last_name FROM person WHERE last_name = {lastName}")
ResultSet getPeopleResultSet(String lastName) throws SQLException;

View File

@ -51,6 +51,10 @@ public interface PrestoPersonDAO extends PersonDAO {
@JdbcMapper.SQL("SELECT person_no FROM person WHERE last_name = {lastName}")
long getPersonNo(String lastName) throws SQLException;
@JdbcMapper.WarnOnUnusedParams
@JdbcMapper.SQL("SELECT person_no FROM person WHERE last_name = {lastName}")
long getPersonNoUnusedParam(String lastName, String unused) throws SQLException;
@JdbcMapper.SQL("SELECT first_name, last_name FROM person WHERE last_name = {lastName}")
ResultSet getPeopleResultSet(String lastName) throws SQLException;