当前位置: 首页 > 工具软件 > HTTP Parser > 使用案例 >

java parser_java parser

欧阳向文
2023-12-01

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();

}

}

}

}

}

}

}

 类似资料:

相关阅读

相关文章

相关问答