java源码解析JavaParser
package com.bootdo.jparser;
import java.io.File;
import java.io.FileNotFoundException;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
public class Jparser {
public static void main(String[] args) {
try {
//将⼀个java代码的⽂本解析为⼀棵CompilationUnit类型的树
CompilationUnit unit = JavaParser.parse(new File("D:\\Atools\\sts-
bundle\\stsWorkspace\\bootdo2\\src\\main\\java\\com\\bootdo\\jparser\\DetectedController.java")); classUnit vc = new classUnit();
//在指定的树上进⾏搜索,然后根据遇到的节点的类型调⽤具体的回调
//vc.visit(unit, null);
unit.accept(vc,null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
package com.bootdo.jparser;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.javaparser.ast.ImportDeclaration;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.PackageDeclaration;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.astments.JavadocComment;
import com.github.pr.AnnotationExpr;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
public class classUnit extends VoidVisitorAdapter<Object> {
Logger log = Logger(classUnit.class);
//alt+shift m 成员⽅法
/**
* 包名
*/
@Override
public void visit(PackageDeclaration n, Object arg) {
System.out.println("package---------"+n.getName());
super.visit(n, arg);
super.visit(n, arg);
}
/**
* ⽀持包
*/
@Override
public void visit(ImportDeclaration n, Object arg) {
log.info("ImportDeclaration-------------"+n.toString());
super.visit(n, arg);
}
/
**
* 类
*/
@Override
public void visit(ClassOrInterfaceDeclaration n, Object arg) {
log.info("arg-----------"+arg);//null
log.info("n.toString()--------"+n.toString());//整个类的内容,不包括package和import log.info("n.getNameAsString()---------"+n.getNameAsString());//类名
//获取类的注解
NodeList<AnnotationExpr> annotations = n.getAnnotations();
//判断是否有注解
if(annotations.size()>0) {
for (AnnotationExpr annotation : annotations) {
log.String());
}
}
//获取类的注释
Optional<JavadocComment> javadocComment = n.getJavadocComment();
log.String());
super.visit(n, arg);
}
/**
* 成员⽅法
*/
@Override
public void visit(MethodDeclaration n, Object arg) {
log.info("***********************************************************");
log.info("arg-----------"+arg);//null
log.info("n.toString()--------"+n.toString());//整个⽅法的内容
log.info("n.getNameAsString()---------"+n.getNameAsString());//⽅法名
log.info("n.getParameters()---------"+n.getParameters());//所有参数
NodeList<Parameter> parameters = n.getParameters();
for (Parameter parameter : parameters) {
log.String());
}
//获取⽅法的注解
NodeList<AnnotationExpr> annotations = n.getAnnotations();
//判断是否有注解
if(annotations.size()>0) {
for (AnnotationExpr annotation : annotations) {
log.String());
}
}
}
//获取⽅法的注释
Optional<JavadocComment> javadocComment = n.getJavadocComment(); log.String());
//获取⽅法⾥⾯的⽅法
/*CompilationUnit cu = JavaParser.String());
cu.accept(new methodUnit(), null);*/
super.visit(n, arg);
}
/**
* 变量
*/
@Override
public void visit(VariableDeclarator n, Object arg) {
log.info("VariableDeclarator--------"+n.toString());
log.info("VariableDeclarator--------");
//bContentService 成员变量
//bContentDO = (cid) ⽅法内部的变量
super.visit(n, arg);
}
/**
* 成员变量的注解
*/
@Override
public void visit(FieldDeclaration n, Object arg) {
log.info("FieldDeclaration>>>"+n.toString());//@Autowired System.out.println("所在位置的⾏号,int类型"+n.getRange().get().begin.line);
super.visit(n, arg);
}
}
扫描的java⽂件
package com.bootdo.jparser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bootdo.blog.domain.ContentDO;
import com.bootdo.blog.service.ContentService;
/**
* ⽂章内容
*
* @author chglee
* @email 1992lcg@163
* @date 2017-09-09 10:03:34
*/
@Controller
@RequestMapping("/blog/bContent")
public class DetectedController {
Logger log = Class());
java的tostring方法@Autowired
ContentService bContentService;
/**
* @return 需要配置thymeleaf模板引擎
*/
@GetMapping()
String bContent() {
a();
return "blog/bContent/bContent";
}
/**
* @param cid
* @return 测试能否获得对象
*/
@GetMapping("/edit/{cid}")
public ContentDO edit(@PathVariable("cid") Long cid) { ContentDO bContentDO = (cid); return bContentDO;
}
public void a() {
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
Java笔试题大全(附带答案)
下一篇 »
发表评论