/* * Copyright 2009-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License i distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.paoding.rose.jade.statement; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.regex.Pattern; import net.paoding.rose.jade.annotation.SQL; import net.paoding.rose.jade.annotation.SQLParam; import net.paoding.rose.jade.annotation.SQLType; import net.paoding.rose.jade.annotation.ShardBy; /** * {@link StatementMetaData} 封装、缓存了一个DAO方法的相关信息 *
* * * @author 王志亮 [qieqie.wang@gmail.com] */ @SuppressWarnings({ "rawtypes" }) public class StatementMetaData { /** * 所属的DAO类的classMetaData */ private final DAOMetaData daoMetaData; /** * 所在的DAO方法 */ private final Method method; /** * DAO方法上的原始SQL语句,如果没有执行SQL语句,则根据方法签名生成相应的串辅助debug */ private final String sql; /** * SQL类型(查询类型或者更新类型):默认由方法名和SQL语句判断,除非强制指定。 * @see SQLType */ private final SQLType sqlType; /** * DAO方法的“最低返回类型”。 *
* 大部分情况returnType和method.getReturnType是相同的,但对于一些声明为泛型的返回类型, * Jade会尽量提取出实际的类型作为returnType *
* 比如: * *
* //@DAO、@SQL注解从略 * public interface BaseDAO[E] { * * public E getById(Long id); * * } * public interface UserDAO extends BaseDAO[User] { * * } ** * 此时,UserDAO#getById方法的returnType是User,而非Object; */ private final Class returnType; /** * 方法返回参数的范型类型(不支持多级)-从method中获取并缓存 */ private final Class[] genericReturnTypes; /** * {@link SQLParam} 注解数组-从method中获取并缓存 *
* 此数组的长度为方法的参数个数,如果对应位置的方法参数没有注解 {@link SQLParam},该位置的元素值为null
*/
private final SQLParam[] sqlParams;
/**
* @{@link ShardBy}
标注在哪个参数上?(从0开始,负数代表无)-从method中获取并缓存
*/
private final int shardByIndex;
private final ShardBy shardBy;
private final int parameterCount;
// --------------------------------------------
public StatementMetaData(DAOMetaData daoMetaData, Method method) {
this.daoMetaData = daoMetaData;
this.method = method;
SQL sqlAnnotation = method.getAnnotation(SQL.class);
if (sqlAnnotation == null) {
sqlAnnotation = new SQL() {
@Override
public Class extends Annotation> annotationType() {
return SQL.class;
}
@Override
public String value() {
String toString = StatementMetaData.this.method.toString();
int paramStart = toString.indexOf("(");
int methodNameStart = toString.lastIndexOf('.', paramStart) + 1;
return toString.substring(methodNameStart) + "@" + StatementMetaData.this.method.getDeclaringClass().getName();
}
@Override
public SQLType type() {
return SQLType.AUTO_DETECT;
}
};
}
this.sql = sqlAnnotation.value();
this.sqlType = resolveSQLType(sqlAnnotation);
this.returnType = GenericUtils.getReturnType(this);
this.genericReturnTypes = GenericUtils.getActualClass(method.getGenericReturnType(), daoMetaData);
Annotation[][] annotations = method.getParameterAnnotations();
this.parameterCount = annotations.length;
this.sqlParams = new SQLParam[annotations.length];
int shardByIndex = -1;
ShardBy shardBy = null;
for (int index = 0; index < annotations.length; index++) {
for (Annotation annotation : annotations[index]) {
if (annotation instanceof ShardBy) {
if (shardByIndex >= 0) {
throw new IllegalArgumentException("duplicated @" + ShardBy.class.getName());
}
shardByIndex = index;
shardBy = (ShardBy) annotation;
} else if (annotation instanceof SQLParam) {
this.sqlParams[index] = (SQLParam) annotation;
}
}
}
this.shardByIndex = shardByIndex;
this.shardBy = shardBy;
}
public DAOMetaData getDAOMetaData() {
return daoMetaData;
}
public Method getMethod() {
return method;
}
public Class> getReturnType() {
return returnType;
}
public String getSQL() {
return sql;
}
public int getParameterCount() {
return parameterCount;
}
public SQLParam getSQLParamAt(int argIndex) {
return sqlParams[argIndex];
}
public int getShardByIndex() {
return shardByIndex;
}
public ShardBy getShardBy() {
return shardBy;
}
public Class>[] getGenericReturnTypes() {
return genericReturnTypes;
}
public