概述
package org.javaparser.examples.chapter2;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.visitor.VoidVisitor;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import com.github.javaparser.printer.PrettyPrinter;
import com.github.javaparser.printer.PrettyPrinterConfiguration;
import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class VoidVisitorComplete {
static CompilationUnit cu;
public static void main(String[] args) throws Exception {
Collection files = FileUtils.listFiles(new File("C:\Users\mi\code\test-service\src\main"), new String[]{"java"}, true);
for(File f: files) {
// if(f.getName().equals("JsonSedesConfig.java")) {
cu = StaticJavaParser.parse(new FileInputStream(f));
VoidVisitor methodNameVisitor = new MethodNamePrinter();
methodNameVisitor.visit(cu, f);
// }
}
}
private static class MethodNamePrinter extends VoidVisitorAdapter {
@Override
public void visit(ClassOrInterfaceDeclaration d, File file) {
super.visit(d, file);
if(!d.isInterface()) {
NodeList nodeList = d.getImplementedTypes();
boolean hasSerializable = false;
for(ClassOrInterfaceType classOrInterfaceType: nodeList) {
// System.out.println("classOrInterfaceType:" + classOrInterfaceType);
if(classOrInterfaceType.getName().getIdentifier().equals("Serializable")) {
hasSerializable = true;
}
}
if(!hasSerializable) {
System.out.println("!hasSerializable:" + d.getName());
if(cu.getClassByName(d.getName().getIdentifier()).isPresent()) {
cu.getClassByName(d.getName().getIdentifier()).get().addImplementedType(Serializable.class);
PrettyPrinterConfiguration conf = new PrettyPrinterConfiguration();
// conf.setIndentSize(1);
// conf.setIndentType(PrettyPrinterConfiguration.IndentType.SPACES);
// conf.setPrintComments(false);
PrettyPrinter prettyPrinter = new PrettyPrinter(conf);
String content = prettyPrinter.print(cu);
System.out.println("content:" + content);
try {
FileUtils.writeStringToFile(file, content, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
}
最后
以上就是炙热曲奇为你收集整理的java parser_java parser的全部内容,希望文章能够帮你解决java parser_java parser所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复