diff --git a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/TypeDefinitionBuilder.java b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/TypeDefinitionBuilder.java index 797d9f8a0e5..53e8c88b8f3 100755 --- a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/TypeDefinitionBuilder.java +++ b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/TypeDefinitionBuilder.java @@ -26,6 +26,7 @@ import java.lang.reflect.Type; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -35,20 +36,16 @@ */ public class TypeDefinitionBuilder { - private static final ThreadLocal> builders; + private static final TypeBuilder ARRAY_BUILDER = new ArrayTypeBuilder(); + private static final TypeBuilder COLLECTION_BUILDER = new CollectionTypeBuilder(); + private static final TypeBuilder MAP_BUILDER = new MapTypeBuilder(); + private static final TypeBuilder ENUM_BUILDER = new EnumTypeBuilder(); - static { - builders = new ThreadLocal>(); - builders.set(new ArrayList()); - builders.get().add(new ArrayTypeBuilder()); - builders.get().add(new CollectionTypeBuilder()); - builders.get().add(new MapTypeBuilder()); - builders.get().add(new EnumTypeBuilder()); - } + private static final List BUILDERS = Arrays.asList(ARRAY_BUILDER, COLLECTION_BUILDER, MAP_BUILDER, ENUM_BUILDER); public static TypeDefinition build(Type type, Class clazz, Map, TypeDefinition> typeCache) { TypeBuilder builder = getGenericTypeBuilder(type, clazz); - TypeDefinition td = null; + TypeDefinition td; if (builder != null) { td = builder.build(type, clazz, typeCache); } else { @@ -58,7 +55,7 @@ public static TypeDefinition build(Type type, Class clazz, Map, Type } private static TypeBuilder getGenericTypeBuilder(Type type, Class clazz) { - for (TypeBuilder builder : builders.get()) { + for (TypeBuilder builder : BUILDERS) { if (builder.accept(type, clazz)) { return builder; } @@ -66,14 +63,14 @@ private static TypeBuilder getGenericTypeBuilder(Type type, Class clazz) { return null; } - private Map, TypeDefinition> typeCache = new HashMap, TypeDefinition>(); + private Map, TypeDefinition> typeCache = new HashMap<>(); public TypeDefinition build(Type type, Class clazz) { return build(type, clazz, typeCache); } public List getTypeDefinitions() { - return new ArrayList(typeCache.values()); + return new ArrayList<>(typeCache.values()); } } diff --git a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/ArrayTypeBuilder.java b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/ArrayTypeBuilder.java index ce53c708682..9ad61cefb26 100755 --- a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/ArrayTypeBuilder.java +++ b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/ArrayTypeBuilder.java @@ -32,12 +32,7 @@ public boolean accept(Type type, Class clazz) { if (clazz == null) { return false; } - - if (clazz.isArray()) { - return true; - } - - return false; + return clazz.isArray(); } @Override @@ -47,8 +42,7 @@ public TypeDefinition build(Type type, Class clazz, Map, TypeDefinit TypeDefinitionBuilder.build(componentType, componentType, typeCache); final String canonicalName = clazz.getCanonicalName(); - TypeDefinition td = new TypeDefinition(canonicalName); - return td; + return new TypeDefinition(canonicalName); } } diff --git a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/CollectionTypeBuilder.java b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/CollectionTypeBuilder.java index 14c3b4ba68c..24b1c868a1b 100755 --- a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/CollectionTypeBuilder.java +++ b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/CollectionTypeBuilder.java @@ -28,20 +28,14 @@ /** * 2015/1/27. */ -public class -CollectionTypeBuilder implements TypeBuilder { +public class CollectionTypeBuilder implements TypeBuilder { @Override public boolean accept(Type type, Class clazz) { if (clazz == null) { return false; } - - if (Collection.class.isAssignableFrom(clazz)) { - return true; - } - - return false; + return Collection.class.isAssignableFrom(clazz); } @Override @@ -72,8 +66,7 @@ public TypeDefinition build(Type type, Class clazz, Map, TypeDefinit } } - TypeDefinition td = new TypeDefinition(type.toString()); - return td; + return new TypeDefinition(type.toString()); } } diff --git a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/EnumTypeBuilder.java b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/EnumTypeBuilder.java index cca8c9ca81d..d0dea96302c 100755 --- a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/EnumTypeBuilder.java +++ b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/EnumTypeBuilder.java @@ -32,12 +32,7 @@ public boolean accept(Type type, Class clazz) { if (clazz == null) { return false; } - - if (clazz.isEnum()) { - return true; - } - - return false; + return clazz.isEnum(); } @Override diff --git a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/MapTypeBuilder.java b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/MapTypeBuilder.java index 4c7cf93c866..bf2b22209cd 100755 --- a/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/MapTypeBuilder.java +++ b/dubbo-metadata-report/dubbo-metadata-definition/src/main/java/org/apache/dubbo/metadata/definition/builder/MapTypeBuilder.java @@ -34,12 +34,7 @@ public boolean accept(Type type, Class clazz) { if (clazz == null) { return false; } - - if (Map.class.isAssignableFrom(clazz)) { - return true; - } - - return false; + return Map.class.isAssignableFrom(clazz); } @Override