使用JavaParser我可以使用以下方法获取我的方法和字段列表:
//List of all methods
System.out.println("Methods: " + this.toString());
List<TypeDeclaration> types = n.getTypes();
for (TypeDeclaration type : types)
{
List<BodyDeclaration> members = type.getMembers();
for (BodyDeclaration member : members)
{
if (member instanceof MethodDeclaration)
{
MethodDeclaration method = (MethodDeclaration) member;
System.out.println("Methods: " + method.getName());
}
}
}
//List all field variables.
System.out.println("Field Variables: " + this.toString());
List<TypeDeclaration> f_vars = n.getTypes();
for (TypeDeclaration type : f_vars)
{
List<BodyDeclaration> members = type.getMembers();
for (BodyDeclaration member : members)
{
if (member instanceof FieldDeclaration)
{ …Run Code Online (Sandbox Code Playgroud) 在我的代码我打开我的file.java和解析他JavaParser类.
FileInputStream in = new FileInputStream(".../file.java");
CompilationUnit cu;
try {
// parse the file
cu = JavaParser.parse(in);
} finally {
in.close();
}
........
Run Code Online (Sandbox Code Playgroud)
file.java:
public class File{
public void ownMethod(){...}
public static void main(String[] args){
ownMethod(5); //Note the extra parameter
}
}
Run Code Online (Sandbox Code Playgroud)
在file.java中有一个错误:该main方法ownMethod使用一个参数调用该方法,但ownMethod需要0个参数.JavaParser未检测到此错误,并使用此错误解析file.java.我怎么知道(在我的代码中)file.java是否没有错误?没有使用java编译器它是否可行?
纯粹作为一种自学习练习,我正在尝试使用该Parse::RecDescent模块在Perl中编写Java解析器.我稍后可能会使用Antlr,Bison等其他工具重新实现解析器.
但是,根据Java语言规范,我如何确保我的解析器确实生成了正确的解析?意义,正确处理悬空else,操作员关联和优先等.
一种方法是通过让两个解析器为大量测试Java程序生成AST,然后比较两组AST来将我的解析器与已知的无错解析器进行比较.
如果这确实是唯一的方法,我在哪里可以找到一整套测试Java程序,完全覆盖整个Java语言规范?
我查看了JavaParser,但它似乎没有详尽的测试数据集.
当然,另一种方法是自己手工编写成千上万的测试Java程序,这对我来说是非常不切实际的,不仅在时间方面,而且在确保其详尽无遗!
arg有人可以向我澄清该方法的第二个参数的用法,如JavaParser 文档示例页面visit中的以下代码所示吗?
我在互联网上找不到任何信息。
public class MethodPrinter {
public static void main(String[] args) throws Exception {
// creates an input stream for the file to be parsed
FileInputStream in = new FileInputStream("test.java");
CompilationUnit cu;
try {
// parse the file
cu = JavaParser.parse(in);
} finally {
in.close();
}
// visit and print the methods names
new MethodVisitor().visit(cu, null);
}
/**
* Simple visitor implementation for visiting MethodDeclaration nodes.
*/
private static class MethodVisitor extends VoidVisitorAdapter {
@Override …Run Code Online (Sandbox Code Playgroud) 我有代码
private static class MyVisitor extends VoidVisitorAdapter<Object> {
@Override
public void visit(MethodCallExpr exp, Object arg) {
System.out.println("Scope: " + exp.getScope());
System.out.println("Method: " + exp.getName());
if(exp.getArgs() != null)
for(Expression e : exp.getArgs()) {
System.out.println("\tArgument: " + e.toString());
}
System.out.println();
}
}
Run Code Online (Sandbox Code Playgroud)
和
CompilationUnit cu = JavaParser.parse(new File("Test.java"));
for(TypeDeclaration type : cu.getTypes()) {
for(BodyDeclaration dec : type.getMembers()) {
if(dec instanceof MethodDeclaration) {
MethodDeclaration mdec = (MethodDeclaration) dec;
BlockStmt block = mdec.getBody();
for(Statement stmt : block.getStmts()) {
MyVisitor visitor = new MyVisitor();
s.accept(visitor, …Run Code Online (Sandbox Code Playgroud) 我能够使用以下代码获取类级别变量的声明。但我只需要变量名。这是我从以下代码得到的输出 - [private boolean flag = true;]
import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import java.io.FileInputStream;
public class CuPrinter{
public static void main(String[] args) throws Exception {
// creates an input stream for the file to be parsed
FileInputStream in = new FileInputStream("C:\\Users\\arosh\\IdeaProjects\\Bot_Twitter\\src\\MyBot.java");
CompilationUnit cu;
try {
// parse the file
cu = JavaParser.parse(in);
} finally {
in.close();
}
cu.accept(new ClassVisitor(), null);
}
private static class ClassVisitor extends VoidVisitorAdapter<Void> {
@Override
public void visit(ClassOrInterfaceDeclaration n, Void arg) {
/* …Run Code Online (Sandbox Code Playgroud) 我知道JavaParser在 AST 上运行,但我很好奇是否可以在与另一条语句相同的行上添加新语句。让我举一个例子来说明我想要实现的目标。
输入示例X.java(不要介意“逻辑”):
public class X {
public static void main(String[] args) {
int a = 1;
while(a < 100) {
a *= 2;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想要实现的是在System.out.println("End of loop reached");每个循环体的末尾添加语句 - 但是,如果可能的话,我想在循环体中的最后一个语句旁边添加这个新语句。因此,结果应该如下所示:
public class X {
public static void main(String[] args) {
int a = 1;
while(a < 100) {
a *= 2; System.out.println("End of loop reached");
}
}
}
Run Code Online (Sandbox Code Playgroud)
目前我使用以下访问者(用 Kotlin 编写),只是为了了解 JavaParser。然而,这会导致为打印语句添加新行。关于如何指示 JavaParser 在同一行添加新节点有什么想法吗?
class WhileVisitor : VoidVisitorAdapter<Void>() …Run Code Online (Sandbox Code Playgroud) java abstract-syntax-tree visitor-pattern javaparser code-transformation
我想创建一个Java转换器,它将读取几乎Java代码(称为JavaHash)并在另一端发出"纯"Java代码.特别是,我想在hashmap成员前面添加一个新标记,它是一个#"标签,以便我可以像JavaScript哈希对象一样访问它:
Map<String, String> foo = new HashMap<String, String>();
...
foo.put("name", "Roger");
...
String name = #foo.name;
Run Code Online (Sandbox Code Playgroud)
我无法让JavaParser做任何事情,只是在"#"主题标签上抛出错误.
有没有办法在解析之前捕获令牌?
我使用https://github.com/javaparser/javaparser来解析java源代码
我尝试了很多方法来解析内部类;像这样 :
class A {
int x;
public void method2() {...}
class B {
int number;
public void methods() {...}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试解析 B 类及其变量和方法,但失败了。
有没有例子说明如何获得B级?
我可以解析 A 类方法名称和内容或变量内容,如下所示:
CompilationUnit cu = JavaParser.parse(in);
ClassVisitor classVisitor = new ClassVisitor();
classVisitor.visit(cu, null);
class ClassVisitor extends VoidVisitorAdapter<Void> {
@Override
public void visit(ClassOrInterfaceDeclaration n, Void arg) {
System.out.println(n.getFields());
// get class methods
for(MethodDeclaration method : n.getMethods()) {
System.out.println("Name :" + method.getName());
System.out.println("Body :" + method.getBody().get());
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我尝试解析B类变量和方法,失败了!
尝试 …
我正在使用Javaparser来解析 Java 源代码。
有没有办法实现一个可以访问抽象Node类的Visitor?
我想访问每个节点并打印其行号,但我不想为每个节点类型(AssignExpr、BinaryExpr、IfStmt 等)实现访问()方法,因为类型太多。