Skip to content

Commit ed8bb74

Browse files
carryxyhcvictory
authored andcommitted
Review code of TypeDefinitionBuilder (#3064)
* Review code of TypeDefinitionBuilder 1. use init method to init builds' list * use single list for all builders. Seems like the builder is thread-safe, we can keep them static and final. * clean code.
1 parent 26f010d commit ed8bb74

File tree

5 files changed

+17
-43
lines changed

5 files changed

+17
-43
lines changed

dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/TypeDefinitionBuilder.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.lang.reflect.Type;
2828
import java.util.ArrayList;
29+
import java.util.Arrays;
2930
import java.util.HashMap;
3031
import java.util.List;
3132
import java.util.Map;
@@ -35,20 +36,16 @@
3536
*/
3637
public class TypeDefinitionBuilder {
3738

38-
private static final ThreadLocal<ArrayList<TypeBuilder>> builders;
39+
private static final TypeBuilder ARRAY_BUILDER = new ArrayTypeBuilder();
40+
private static final TypeBuilder COLLECTION_BUILDER = new CollectionTypeBuilder();
41+
private static final TypeBuilder MAP_BUILDER = new MapTypeBuilder();
42+
private static final TypeBuilder ENUM_BUILDER = new EnumTypeBuilder();
3943

40-
static {
41-
builders = new ThreadLocal<ArrayList<TypeBuilder>>();
42-
builders.set(new ArrayList<TypeBuilder>());
43-
builders.get().add(new ArrayTypeBuilder());
44-
builders.get().add(new CollectionTypeBuilder());
45-
builders.get().add(new MapTypeBuilder());
46-
builders.get().add(new EnumTypeBuilder());
47-
}
44+
private static final List<TypeBuilder> BUILDERS = Arrays.asList(ARRAY_BUILDER, COLLECTION_BUILDER, MAP_BUILDER, ENUM_BUILDER);
4845

4946
public static TypeDefinition build(Type type, Class<?> clazz, Map<Class<?>, TypeDefinition> typeCache) {
5047
TypeBuilder builder = getGenericTypeBuilder(type, clazz);
51-
TypeDefinition td = null;
48+
TypeDefinition td;
5249
if (builder != null) {
5350
td = builder.build(type, clazz, typeCache);
5451
} else {
@@ -58,22 +55,22 @@ public static TypeDefinition build(Type type, Class<?> clazz, Map<Class<?>, Type
5855
}
5956

6057
private static TypeBuilder getGenericTypeBuilder(Type type, Class<?> clazz) {
61-
for (TypeBuilder builder : builders.get()) {
58+
for (TypeBuilder builder : BUILDERS) {
6259
if (builder.accept(type, clazz)) {
6360
return builder;
6461
}
6562
}
6663
return null;
6764
}
6865

69-
private Map<Class<?>, TypeDefinition> typeCache = new HashMap<Class<?>, TypeDefinition>();
66+
private Map<Class<?>, TypeDefinition> typeCache = new HashMap<>();
7067

7168
public TypeDefinition build(Type type, Class<?> clazz) {
7269
return build(type, clazz, typeCache);
7370
}
7471

7572
public List<TypeDefinition> getTypeDefinitions() {
76-
return new ArrayList<TypeDefinition>(typeCache.values());
73+
return new ArrayList<>(typeCache.values());
7774
}
7875

7976
}

dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/ArrayTypeBuilder.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,7 @@ public boolean accept(Type type, Class<?> clazz) {
3232
if (clazz == null) {
3333
return false;
3434
}
35-
36-
if (clazz.isArray()) {
37-
return true;
38-
}
39-
40-
return false;
35+
return clazz.isArray();
4136
}
4237

4338
@Override
@@ -47,8 +42,7 @@ public TypeDefinition build(Type type, Class<?> clazz, Map<Class<?>, TypeDefinit
4742
TypeDefinitionBuilder.build(componentType, componentType, typeCache);
4843

4944
final String canonicalName = clazz.getCanonicalName();
50-
TypeDefinition td = new TypeDefinition(canonicalName);
51-
return td;
45+
return new TypeDefinition(canonicalName);
5246
}
5347

5448
}

dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/CollectionTypeBuilder.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,14 @@
2828
/**
2929
* 2015/1/27.
3030
*/
31-
public class
32-
CollectionTypeBuilder implements TypeBuilder {
31+
public class CollectionTypeBuilder implements TypeBuilder {
3332

3433
@Override
3534
public boolean accept(Type type, Class<?> clazz) {
3635
if (clazz == null) {
3736
return false;
3837
}
39-
40-
if (Collection.class.isAssignableFrom(clazz)) {
41-
return true;
42-
}
43-
44-
return false;
38+
return Collection.class.isAssignableFrom(clazz);
4539
}
4640

4741
@Override
@@ -72,8 +66,7 @@ public TypeDefinition build(Type type, Class<?> clazz, Map<Class<?>, TypeDefinit
7266
}
7367
}
7468

75-
TypeDefinition td = new TypeDefinition(type.toString());
76-
return td;
69+
return new TypeDefinition(type.toString());
7770
}
7871

7972
}

dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/EnumTypeBuilder.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,7 @@ public boolean accept(Type type, Class<?> clazz) {
3232
if (clazz == null) {
3333
return false;
3434
}
35-
36-
if (clazz.isEnum()) {
37-
return true;
38-
}
39-
40-
return false;
35+
return clazz.isEnum();
4136
}
4237

4338
@Override

dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/MapTypeBuilder.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,7 @@ public boolean accept(Type type, Class<?> clazz) {
3434
if (clazz == null) {
3535
return false;
3636
}
37-
38-
if (Map.class.isAssignableFrom(clazz)) {
39-
return true;
40-
}
41-
42-
return false;
37+
return Map.class.isAssignableFrom(clazz);
4338
}
4439

4540
@Override

0 commit comments

Comments
 (0)