Skip to content

Commit

Permalink
优化分表分库结构
Browse files Browse the repository at this point in the history
  • Loading branch information
ash committed Mar 22, 2016
1 parent 2042df7 commit 88e53b2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
38 changes: 21 additions & 17 deletions src/main/java/org/jfaster/mango/operator/OperatorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
import org.jfaster.mango.util.SQLType;
import org.jfaster.mango.util.Strings;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Set;

/**
* Operator工厂
Expand Down Expand Up @@ -168,14 +168,16 @@ TableGenerator getTableGenerator(MethodDescriptor md, ASTRootNode rootNode, Name
TypeToken<?> tablePartitionToken = null;
if (tpc != null && !tpc.equals(IgnoreTablePartition.class)) {
tablePartition = Reflection.instantiateClass(tpc);
Type[] types = tpc.getGenericInterfaces();
Type genType = types.length > 0 ? types[0] : tpc.getGenericSuperclass();
if (genType instanceof ParameterizedType) {
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
tablePartitionToken = TypeToken.of(params[0]);
} else if (genType instanceof Class) {
tablePartitionToken = TypeToken.of(Object.class);
} else {
Set<TypeToken<?>> fathers = TypeToken.of(tpc).getTypes();
for (TypeToken<?> father : fathers) {
if (TablePartition.class.equals(father.getRawType())) {
tablePartitionToken = father.resolveType(TablePartition.class.getTypeParameters()[0]);
if (Object.class.equals(tablePartitionToken.getRawType())) { // 处理范型T
tablePartitionToken = TypeToken.of(Object.class);
}
}
}
if (tablePartitionToken == null) {
throw new IllegalStateException();
}
}
Expand Down Expand Up @@ -260,14 +262,16 @@ DataSourceGenerator getDataSourceGenerator(DataSourceFactory dataSourceFactory,
TypeToken<?> dataSourceRouterToken = null;
if (dsrc != null && !dsrc.equals(IgnoreDataSourceRouter.class)) {
dataSourceRouter = Reflection.instantiateClass(dsrc);
Type[] types = dsrc.getGenericInterfaces();
Type genType = types.length > 0 ? types[0] : dsrc.getGenericSuperclass();
if (genType instanceof ParameterizedType) {
Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
dataSourceRouterToken = TypeToken.of(params[0]);
} else if (genType instanceof Class) {
dataSourceRouterToken = TypeToken.of(Object.class);
} else {
Set<TypeToken<?>> fathers = TypeToken.of(dsrc).getTypes();
for (TypeToken<?> father : fathers) {
if (DataSourceRouter.class.equals(father.getRawType())) {
dataSourceRouterToken = father.resolveType(DataSourceRouter.class.getTypeParameters()[0]);
if (Object.class.equals(dataSourceRouterToken.getRawType())) { // 处理范型T
dataSourceRouterToken = TypeToken.of(Object.class);
}
}
}
if (dataSourceRouterToken == null) {
throw new IllegalStateException();
}
}
Expand Down
22 changes: 10 additions & 12 deletions src/main/java/org/jfaster/mango/reflect/TypeToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,40 +127,39 @@ public String toString() {
return Types.toString(runtimeType);
}

public Set<TypeToken<? super T>> getTypes() {
Set<TypeToken<? super T>> tokens = new HashSet<TypeToken<? super T>>();
public Set<TypeToken<?>> getTypes() {
Set<TypeToken<?>> tokens = new HashSet<TypeToken<?>>();
tokens.add(this);
TypeToken<? super T> superclass = getGenericSuperclass();
TypeToken<?> superclass = getGenericSuperclass();
if (superclass != null) {
tokens.add(superclass);
tokens.addAll(superclass.getTypes());
}
List<TypeToken<? super T>> interfaces = getGenericInterfaces();
for (TypeToken<? super T> anInterface : interfaces) {
List<TypeToken<?>> interfaces = getGenericInterfaces();
for (TypeToken<?> anInterface : interfaces) {
tokens.add(anInterface);
tokens.addAll(anInterface.getTypes());
}
return tokens;
}

final List<TypeToken<? super T>> getGenericInterfaces() {
final List<TypeToken<?>> getGenericInterfaces() {
if (runtimeType instanceof TypeVariable) {
throw new IllegalStateException();
}
if (runtimeType instanceof WildcardType) {
throw new IllegalStateException();
}
List<TypeToken<? super T>> tokens = new ArrayList<TypeToken<? super T>>();
List<TypeToken<?>> tokens = new ArrayList<TypeToken<?>>();
for (Type interfaceType : getRawType().getGenericInterfaces()) {
@SuppressWarnings("unchecked") // interface of T
TypeToken<? super T> resolvedInterface = (TypeToken<? super T>) resolveSupertype(interfaceType);
TypeToken<?> resolvedInterface = resolveSupertype(interfaceType);
tokens.add(resolvedInterface);
}
return tokens;
}

@Nullable
final TypeToken<? super T> getGenericSuperclass() {
final TypeToken<?> getGenericSuperclass() {
if (runtimeType instanceof TypeVariable) {
throw new IllegalStateException();
}
Expand All @@ -171,8 +170,7 @@ final TypeToken<? super T> getGenericSuperclass() {
if (superclass == null) {
return null;
}
@SuppressWarnings("unchecked") // super class of T
TypeToken<? super T> superToken = (TypeToken<? super T>) resolveSupertype(superclass);
TypeToken<?> superToken = resolveSupertype(superclass);
return superToken;
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/jfaster/mango/reflect/TypeTokenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void testResolveType() throws Exception {
public void testGetTypes() throws Exception {
TypeToken<HashMap<String,Integer>> t = new TypeToken<HashMap<String, Integer>>() {
};
Set<TypeToken<? super HashMap<String, Integer>>> types = t.getTypes();
Set<TypeToken<?>> types = t.getTypes();
assertThat(types.size(), equalTo(6));
types.contains(new TypeToken<Map<String, Integer>>() {
});
Expand Down

0 comments on commit 88e53b2

Please sign in to comment.