This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch 3.2
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.2 by this push:
     new b7cd1f933f Fix NPE in DefaultTypeBuilder (#13732)
b7cd1f933f is described below

commit b7cd1f933f0df9e2d94734760200938e9097e72f
Author: Albumen Kevin <[email protected]>
AuthorDate: Sun Feb 4 09:59:20 2024 +0800

    Fix NPE in DefaultTypeBuilder (#13732)
    
    * Fix NPE in DefaultTypeBuilder
    
    * Fix style
---
 .../definition/builder/DefaultTypeBuilder.java     | 11 ++++--
 .../definition/DefaultTypeBuilderTest.java         | 44 ++++++++++++++++++++++
 2 files changed, 51 insertions(+), 4 deletions(-)

diff --git 
a/dubbo-common/src/main/java/org/apache/dubbo/metadata/definition/builder/DefaultTypeBuilder.java
 
b/dubbo-common/src/main/java/org/apache/dubbo/metadata/definition/builder/DefaultTypeBuilder.java
index 2da24303cb..adf70e16cf 100755
--- 
a/dubbo-common/src/main/java/org/apache/dubbo/metadata/definition/builder/DefaultTypeBuilder.java
+++ 
b/dubbo-common/src/main/java/org/apache/dubbo/metadata/definition/builder/DefaultTypeBuilder.java
@@ -32,15 +32,18 @@ import java.util.Map;
 public final class DefaultTypeBuilder {
 
     public static TypeDefinition build(Class<?> clazz, Map<String, 
TypeDefinition> typeCache) {
-        final String canonicalName = clazz.getCanonicalName();
+        String className = clazz.getCanonicalName();
+        if (className == null) {
+            className = clazz.getName();
+        }
 
         // Try to get a cached definition
-        TypeDefinition td = typeCache.get(canonicalName);
+        TypeDefinition td = typeCache.get(className);
         if (td != null) {
             return td;
         }
-        td = new TypeDefinition(canonicalName);
-        typeCache.put(canonicalName, td);
+        td = new TypeDefinition(className);
+        typeCache.put(className, td);
 
         // Primitive type
         if (!JaketConfigurationUtils.needAnalyzing(clazz)) {
diff --git 
a/dubbo-common/src/test/java/org/apache/dubbo/metadata/definition/DefaultTypeBuilderTest.java
 
b/dubbo-common/src/test/java/org/apache/dubbo/metadata/definition/DefaultTypeBuilderTest.java
new file mode 100644
index 0000000000..74a691227c
--- /dev/null
+++ 
b/dubbo-common/src/test/java/org/apache/dubbo/metadata/definition/DefaultTypeBuilderTest.java
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 is 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 org.apache.dubbo.metadata.definition;
+
+import org.apache.dubbo.metadata.definition.builder.DefaultTypeBuilder;
+import org.apache.dubbo.rpc.model.FrameworkModel;
+
+import java.util.HashMap;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class DefaultTypeBuilderTest {
+    @Test
+    void testInnerClass() {
+        TypeDefinitionBuilder.initBuilders(FrameworkModel.defaultModel());
+
+        Assertions.assertEquals(
+                String.class.getName(),
+                DefaultTypeBuilder.build(String.class, new 
HashMap<>()).getType());
+
+        DefaultTypeBuilderTest innerObject = new DefaultTypeBuilderTest() {};
+        Assertions.assertEquals(
+                DefaultTypeBuilderTest.class.getName() + "$1",
+                DefaultTypeBuilder.build(innerObject.getClass(), new 
HashMap<>())
+                        .getType());
+
+        TypeDefinitionBuilder.BUILDERS = null;
+    }
+}

Reply via email to