Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a class parameter to DictionaryValuesResolver. #428

Merged
merged 2 commits into from
Feb 13, 2023

Conversation

ZhReimu
Copy link
Contributor

@ZhReimu ZhReimu commented Feb 13, 2023

Summary (Description of this PR)

Add a method com.power.doc.extension.dict.DictionaryValuesResolver#resolve(Class)
Use the javax.annotation.Nonnull annotation for several parameters and return values in DictionaryValuesResolver

Basic Example (PR Use Case)

public class BaseExceptionValuesResolver implements DictionaryValuesResolver {

    @NotNull
    @Override
    @SuppressWarnings("unchecked")
    public <T extends EnumDictionary> Collection<T> resolve(@NotNull Class<?> clazz) {
        return (Collection<T>) Arrays.stream(clazz.getDeclaredFields())
                .map(it -> fieldMapper(it, clazz))
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }

    private EnumDictionary fieldMapper(Field field, Class<?> codeClass) {
        try {
            if (IAssert.class.isAssignableFrom(field.getType())) {
                IAssert anAssert = (IAssert) field.get(codeClass);
                return EnumDictionary.builder()
                        .setName(field.getName())
                        .setDesc(anAssert.getMsg())
                        .setValue(String.valueOf(anAssert.getCode()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}

Motivation (Purpose of this PR)

Since we have already set valuesResolverClass, there is still a need to set enumClassName. Why not add the class obtained from enumClassName as a parameter in the DictionaryValuesResolver interface? Otherwise, being tied to a configuration feels uncomfortable.

Appendix: Not setting enumClassName will lead to NPE

java.lang.NullPointerException: Cannot invoke "java.lang.Class.isInterface()" because "enumClass" is null
    at com.power.doc.builder.ProjectDocConfigBuilder.getEnumImplementsByInterface (ProjectDocConfigBuilder.java:146)
    at com.power.doc.builder.ProjectDocConfigBuilder.initDict (ProjectDocConfigBuilder.java:141)
    at com.power.doc.builder.ProjectDocConfigBuilder.<init> (ProjectDocConfigBuilder.java:117)
    at com.power.doc.builder.ApiDocBuilder.buildApiDoc (ApiDocBuilder.java:72)
    at com.smartdoc.mojo.MarkDownMojo.executeMojo (MarkDownMojo.java:44)
    at com.smartdoc.mojo.BaseDocsGeneratorMojo.execute (BaseDocsGeneratorMojo.java:158)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:370)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:351)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:171)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:163)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.builder.multithreaded.MultiThreadedBuilder$1.call (MultiThreadedBuilder.java:210)
    at org.apache.maven.lifecycle.internal.builder.multithreaded.MultiThreadedBuilder$1.call (MultiThreadedBuilder.java:195)
    at java.util.concurrent.FutureTask.run (FutureTask.java:264)
    at java.util.concurrent.Executors$RunnableAdapter.call (Executors.java:539)
    at java.util.concurrent.FutureTask.run (FutureTask.java:264)
    at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1136)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:635)
    at java.lang.Thread.run (Thread.java:833)

Signed-off-by: zhReimu <1725338233@qq.com>
Signed-off-by: zhReimu <1725338233@qq.com>
@shalousun
Copy link
Collaborator

good

@shalousun shalousun merged commit 327f5c6 into TongchengOpenSource:master Feb 13, 2023
@shalousun shalousun added this to the 2.6.5 milestone Feb 26, 2023
@shalousun shalousun changed the title DictionaryValuesResolver 增加 class 参数 Add a class parameter to DictionaryValuesResolver. Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants