Skip to content
This repository was archived by the owner on Dec 4, 2025. It is now read-only.

Commit e1f09e3

Browse files
authored
Merge pull request #790 from muyuanjin/#789
fix #789 NPE in JsonBuildHelper
2 parents 4c06abb + 3937691 commit e1f09e3

2 files changed

Lines changed: 64 additions & 3 deletions

File tree

src/main/java/com/ly/doc/utils/JavaClassUtil.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -835,13 +835,15 @@ private static String getFieldGenericType(JavaField javaField, ClassLoader class
835835

836836
private static String getReturnGenericType(JavaMethod javaMethod, ClassLoader classLoader) {
837837
String methodName = javaMethod.getName();
838-
String canonicalClassName = javaMethod.getDeclaringClass().getCanonicalName();
838+
// `BinaryName` is the correct name for inner classes required by `ClassLoader.loadClass`
839+
// and `Class.forName`, as inner class paths use `$` instead of `.`.
840+
String binaryName = javaMethod.getDeclaringClass().getBinaryName();
839841
try {
840842
Class<?> c;
841843
if (Objects.nonNull(classLoader)) {
842-
c = classLoader.loadClass(canonicalClassName);
844+
c = classLoader.loadClass(binaryName);
843845
} else {
844-
c = Class.forName(canonicalClassName);
846+
c = Class.forName(binaryName);
845847
}
846848

847849
Method m = c.getDeclaredMethod(methodName);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.ly.doc.helper;
2+
3+
import com.ly.doc.builder.ProjectDocConfigBuilder;
4+
import com.ly.doc.model.ApiConfig;
5+
import com.thoughtworks.qdox.JavaProjectBuilder;
6+
import com.thoughtworks.qdox.model.JavaClass;
7+
import org.junit.jupiter.api.Assertions;
8+
import org.junit.jupiter.api.Test;
9+
10+
import java.util.HashMap;
11+
import java.util.HashSet;
12+
13+
/**
14+
* @author muyuanjin
15+
* @date 2024/5/16 下午7:15
16+
*/
17+
class JsonBuildHelperTest {
18+
/**
19+
* com.ly.doc.helper.JsonBuildHelper#buildJson
20+
* issue:NPE in JsonBuildHelper #789
21+
*/
22+
@Test
23+
void testBuildJsonWithNPE() {
24+
ApiConfig apiConfig = new ApiConfig();
25+
JavaProjectBuilder projectBuilder = JavaProjectBuilderHelper.create();
26+
Assertions.assertNotNull(projectBuilder.getClassByName(getClass().getName()));
27+
JavaClass taskType = projectBuilder.getClassByName(Task.class.getName());
28+
Assertions.assertNotNull(taskType);
29+
Assertions.assertFalse(taskType.getMethods().isEmpty());
30+
ProjectDocConfigBuilder builder = new ProjectDocConfigBuilder(apiConfig, projectBuilder);
31+
Assertions.assertNull(builder.getClassByName(getClass().getName()));
32+
Assertions.assertNotNull(builder.getClassByName(Task.class.getName()));
33+
Assertions.assertFalse(builder.getClassByName(Task.class.getName()).getMethods().isEmpty());
34+
String json = JsonBuildHelper.buildJson(Task.class.getName(),
35+
Task.class.getCanonicalName(),
36+
false, 0, new HashMap<>(16), new HashSet<>(), builder
37+
);
38+
System.out.println(json);
39+
}
40+
41+
interface Serialize<T extends Serialize<T>> {
42+
@SuppressWarnings("unchecked")
43+
default Class<T> getOriginalClass() {
44+
return (Class<T>) this.getClass();
45+
}
46+
}
47+
48+
public static class Task implements Serialize<Task> {
49+
private String taskType;
50+
51+
public String getTaskType() {
52+
return taskType;
53+
}
54+
55+
public void setTaskType(String taskType) {
56+
this.taskType = taskType;
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)