云购物网站建设,成都logo设计公司,wordpress rewrite规则,wordpress代码分割之前分享过一篇使用 AI 可视化 Java 项目的文章#xff0c;同步在 AI 破局星球、知乎、掘金等地方都分享了。
原文在这里AI 编程#xff1a;可视化 Java 项目
有很多人感兴趣#xff0c;我打算写一个系列文章拆解这个项目#xff0c;大家多多点赞支持~
今天分享的是第一…之前分享过一篇使用 AI 可视化 Java 项目的文章同步在 AI 破局星球、知乎、掘金等地方都分享了。
原文在这里AI 编程可视化 Java 项目
有很多人感兴趣我打算写一个系列文章拆解这个项目大家多多点赞支持~
今天分享的是第一篇如何使用 Spoon JavaParser 工具解析一个本地的 Java 项目。
解析这一步骤是整个项目的基础是为了获得整个 Java 项目的元数据。
这个元数据包含什么呢1整个项目的所有的类信息2整个项目的所有方法信息
方法信息
序号字段名称字段描述1method_id方法唯一id标识2project_name3method_name方法名4class_id方法所属类名5param_type参数类型6response_type返回类型7begin_line方法内容开始行8end_line方法内容结束行9branch分支10method_desc方法描述11chat_descGPT 描述12invoke_count被调用数量13mermaid_flow_graph流程图数据14flow_graph_ignored是否忽略流程图15annotation_info注解信息16annotation_type注解类型17access_modifier修饰符
类信息
序号字段名称字段描述1class_id类唯一标识2class_name类名3project_name项目唯一标识4package_name包名5branch分支6class_type类的类型7chat_descGPT 类描述8class_desc类注释9annotation_info类注解10method_annotation_info方法注解信息11annotation_type注解类型
怎么拿到整个项目的类信息和方法信息呢
首先我们需要一个类解析器、一个方法解析器。使用 Java 的反射我们就能拿到具体类和方法的详细信息。
类解析器代码
public void execute(ListCtType? elements) {classStructs Lists.newArrayList();for (CtType? type : elements) {try {// 匿名内部类和泛型会跳过解析if (type.isAnonymous()) {continue;}if (Objects.isNull(type.getPackage())) {continue;}// 获取类的简单类名String simpleClassName type.getSimpleName();// GPT 接口获取解释String chatDesc ;// 获取类所属包String packageName type.getPackage().getQualifiedName();// 获取类注释信息String classComment type.getDocComment();// 判断接口还是类ClassType classType getClassType(type);// 获取类注解信息ListAnnotationInfo annotationInfos Lists.newArrayList();ListCtAnnotation? annotations type.getAnnotations();for (CtAnnotation? annotation : annotations) {AnnotationInfo annotationInfo new AnnotationInfo();String annotationName annotation.getAnnotationType().getSimpleName();annotationInfo.setAnnotationName(annotationName);MapString, CtExpression annotationValues annotation.getValues();for (Map.EntryString, CtExpression entry : annotationValues.entrySet()) {String attributeName entry.getKey();Object attributeValue entry.getValue();annotationInfo.addAttributeName(attributeName, attributeValue.toString());}annotationInfos.add(annotationInfo);}// 构造类元信息ClassStruct classStruct buildClassStruct(simpleClassName, packageName, classType,classComment, annotationInfos, chatDesc);classStructs.add(classStruct);} catch (Exception e) {log.error(class parse error, className {}, errMsg , type.getSimpleName(), e);}}// 类元信息入库}方法解析器
public void execute(ListCtType? elements) {methodStructs Lists.newArrayList();for (CtType? element : elements) {if (element.isAnonymous()) {continue;}if (Objects.isNull(element.getPackage())) {continue;}// 获取包名String packageName element.getPackage().getQualifiedName();// 获取类名String className element.getSimpleName();// 获取方法列表SetCtMethod? methods element.getMethods();for (CtMethod? method : methods) {try {// 获取简单方法名String methodName method.getSimpleName();// 获取全限定参数String signatureParameters method.getSignature();int firstIndex method.getSignature().indexOf(();int lastIndex method.getSignature().indexOf());String parameters signatureParameters.substring(firstIndex 1, lastIndex);ListString methodParameters Splitter.on(,).omitEmptyStrings().splitToList(parameters);// 获取响应体类型String responseType method.getType().getQualifiedName();// 获取方法开始的行int startLine method.getPosition().getLine();// 获取方法结束的行int endLine method.getPosition().getEndLine();// 获取方法注释String methodComment method.getDocComment();// 获取方法包含的注解信息ListAnnotationInfo annotationInfos Lists.newArrayList();ListCtAnnotation? annotations method.getAnnotations();for (CtAnnotation? annotation : annotations) {AnnotationInfo annotationInfo new AnnotationInfo();String annotationName annotation.getAnnotationType().getSimpleName();annotationInfo.setAnnotationName(annotationName);MapString, CtExpression annotationValues annotation.getValues();for (Map.EntryString, CtExpression entry : annotationValues.entrySet()) {String attributeName entry.getKey();Object attributeValue entry.getValue();annotationInfo.addAttributeName(attributeName, attributeValue.toString());}annotationInfos.add(annotationInfo);}// 获取方法的访问修饰符String accessModifier ;if (Objects.isNull(method.getVisibility())) {accessModifier default;} else {accessModifier method.getVisibility().toString();}String methodId generateIdentityUtil.generateMethodId(MethodSignature.builder().packagePath(packageName).className(className).methodName(methodName).parameterTypeString(methodParameters).build(), endLine - startLine 1);MethodStruct old null;// 基于规则判断一波是否需要询问chatint lineCount endLine - startLine;MethodStruct methodStruct MethodStruct.builder().appCode(GlobalVariableUtil.getAppCodeName()).methodId(methodId).methodName(methodName).classId(generateIdentityUtil.generateClassId(className, packageName, GlobalVariableUtil.getAppCodeName())).paramTypes(methodParameters).responseType(responseType).beginLine(startLine).endLine(endLine).branch(GlobalVariableUtil.getBranch()).methodDesc(methodComment).annotationInfos(annotationInfos).accessModifier(accessModifier).invokeCount(old null ? 0 : old.getInvokeCount()).mermaidFlowGraph(old null ? : old.getMermaidFlowGraph()).build();if (old null) {methodStructs.add(methodStruct);}} catch (Exception e) {log.error(method parse error, className {}, methodName {}, errMsg ,className, method.getSimpleName(), e);}}}
// 方法元信息入库}通过这种方式我们就能拿到整个 Java 项目的方法信息。
需要注意的是我们这个时候还没有使用 AI 技术所以这个元信息中部分字段是空的。
我们看到类解析器和方法解析器方法的入参都是 ListCtType? elements。
那么这个信息从哪里来的呢
我这里使用的是 Spoon 工具。
Spoon 是什么
Spoon 框架常被用于解析和处理 Java 源代码。Spoon 是一个强大的源码分析与转换工具它通过构建抽象语法树Abstract Syntax Tree, AST来表示 Java 源代码并提供了一套丰富的 API 供开发者操作 AST。
Spoon 能够完整且准确地捕获源代码的所有细节所以它非常适合于进行复杂的静态代码分析、重构、自动插入代码逻辑等工作。
Spoon 不会用没关系AI 可以帮你写代码。 我们可以看到GPT 直接帮我们生成完整代码我们只需要在对应的地方替换成我们的类解析器和方法解析器即可。
提示词如下 你是一个Java技术专家。 我需要解析本地的一个 Java 项目获得这个项目中的类信息和方法信息。我会给你提供这个 Java 项目的绝对路径。 请你使用 Spoon 生成解析代码 写到这里我要告诉你的是其实类解析器和方法解析的代码也可以交给 AI 来完成哟~ 你可以试试看如果有问题随时找阿七给你解答。
到这里我们今天的内容就结束啦。
下一篇我们分享使用 AI 生成方法的流程图请期待~