를 실행하지 않습니다 나는이 두 줄 방법 setForStatement
를 실행하려고 :forEach (Main :: myMethod); 방법을
cu.getNodesByType(ForStmt.class) .forEach(Main::setForStatement);
나는 부분이 작동되는 확인 코드의 다른 부분에서 안녕하세요 문자열을 넣어.
import javax.tools.*;
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
System.out.println("hello");
final String fileName = "Class.java";
final String alteredFileName = "src\\ClassAltered.java";
CompilationUnit cu;
try(FileInputStream in = new FileInputStream(fileName)){
cu = JavaParser.parse(in);
}
cu.getNodesByType(ForStmt.class)
.forEach(Main::setForStatement);
cu.getClassByName("Class").get().setName("ClassAltered");
try(FileWriter output = new FileWriter(new File(alteredFileName), false)) {
output.write(cu.toString());
}
System.out.println("hello2");
File[] files = {new File(alteredFileName)};
String[] options = { "-d", "out//production//Synthesis" };
System.out.println("hello3");
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
Iterable<? extends JavaFileObject> compilationUnits =
fileManager.getJavaFileObjectsFromFiles(Arrays.asList(files));
compiler.getTask(
null,
fileManager,
diagnostics,
Arrays.asList(options),
null,
compilationUnits).call();
System.out.println("hello4");
diagnostics.getDiagnostics().forEach(d -> System.out.println(d.getMessage(null)));
}
}
private static void setForStatement(ForStmt forStmt) {
System.out.println("hello5");
MethodCallExpr initializer = (MethodCallExpr) forStmt.getInitialization().get(0);
SimpleName name = initializer.getName();
if(!name.toString().equals("FOR")) {
if (initializer.getArguments().size() == 5
&& initializer.getArgument(0) instanceof NameExpr
&& initializer.getArgument(1) instanceof NameExpr
&& initializer.getArgument(2) instanceof IntegerLiteralExpr
&& initializer.getArgument(3) instanceof CharLiteralExpr
&& initializer.getArgument(4) instanceof CharLiteralExpr) {
NameExpr a1 = (NameExpr) initializer.getArgument(0);
NameExpr a2 = (NameExpr) initializer.getArgument(1);
IntegerLiteralExpr a3 = (IntegerLiteralExpr) initializer.getArgument(2);
CharLiteralExpr a4 = (CharLiteralExpr) initializer.getArgument(3);
CharLiteralExpr a5 = (CharLiteralExpr) initializer.getArgument(4);
}
}
}
}
아마도 [mcve]를 게시하고 싶습니다. – khelwood