Questions tagged [javaparser]

A Java Parser with AST (abstract syntax tree) generation and visitor support.

Javaparser is a Java 1.0..9 Parser with AST (abstract syntax tree) generation and visitor support. The AST records the source code structure, javadoc and comments. It is also possible to change the AST nodes or create new ones to modify the source code.

Main features

  • lightweight
  • good performance
  • easy to use
  • lexical preservation
  • AST can be modified
  • AST can be created from scratch
  • comments support
192 questions
8
votes
3 answers

What is the correct way to parse variables using JavaParser?

Using JavaParser I can get a list of my methods and fields using: //List of all methods System.out.println("Methods: " + this.toString()); List types = n.getTypes(); for (TypeDeclaration type : types) { List
ialexander
  • 898
  • 10
  • 28
7
votes
1 answer

How to check if a Java source file is valid (has no errors)?

In my code I open my file.java and parse him with JavaParser. FileInputStream in = new FileInputStream(".../file.java"); CompilationUnit cu; try { // parse the file cu = JavaParser.parse(in); }…
user1951618
  • 131
  • 12
6
votes
2 answers

Testing of a new Java parser

Purely as a self-learning exercise, I'm trying to write a Java parser in Perl using the Parse::RecDescent module. I may later re-implement the parser using other other tools like Antlr, bison, etc. But how would I ensure that my parser is indeed…
Harry
  • 3,684
  • 6
  • 39
  • 48
5
votes
0 answers

How to add a new statement on the same line using JavaParser

I know that JavaParser operates on the AST, yet I was curious if it was possible to add new statements on the same line as another one. Let me give an example on what I would like to achieve. Example input X.java (do not mind the "logic"): public…
5
votes
2 answers

How to get class level variable names using javaparser?

I was able to get class level variable's declarations using the following code. But I only need the variable name. This is the output I get for following code - [private boolean flag = true;] import com.github.javaparser.JavaParser; import…
5
votes
2 answers

How to get final package name?

I have the code private static class MyVisitor extends VoidVisitorAdapter { @Override public void visit(MethodCallExpr exp, Object arg) { System.out.println("Scope: " + exp.getScope()); System.out.println("Method: "…
ivan
  • 287
  • 3
  • 14
5
votes
1 answer

JavaParser visit method arg parameter clarification

Could someone give me clarification on the usage of the second argument arg of the visit method as shown in the following code from the JavaParser documentation example page ? I can't find any information on the internet. public class MethodPrinter…
Heisenberg
  • 53
  • 3
4
votes
1 answer

Understanding JavaParser compared to JavaCC and Eclipse JDT

I'm currently beginning an automated software analysis project of which I am the research phase. I'm quite new to parsing and struggling to find info on resources regarding comparisons between the main java parsing options. I understand JavaParser…
neversnow1
  • 87
  • 5
4
votes
0 answers

Track line number changes in JavaParser

I currently try to use JavaParser to slightly modify existing source code. I now would like to able to track line number changes introduced by theses modifications. As an example, assume that we have a ModifierVisitor that adds one line to the body…
Markus Weninger
  • 11,931
  • 7
  • 64
  • 137
4
votes
2 answers

How to parse inner class from java source code

I use the https://github.com/javaparser/javaparser to parse the java source code I tried a lot of methods to parse inner class ; like this : class A { int x; public void method2() {...} class B { int number; …
Mars
  • 103
  • 1
  • 8
4
votes
1 answer

JavaParser: How to add new language elements

I want to create a Java transpiler that will read nearly-Java code (call it JavaHash) and emit "pure" Java code on the other end. In particular, I want to add a new token that is the hashtag "#" in front of a hashmap member so that I might access it…
user244277
4
votes
7 answers

how get the fully qualified name of the java class

I have a class like below. public class Login { private Keyword browser; private String page; } Keyword is a class in different package. I want to get the fully qualified name of the class Keyword while parsing the Login class using…
user3181223
3
votes
1 answer

Is it possible to statically distinguish between fully qualified names and nested class types?

I am using JavaParser (open source) to parse the following code. package testfiles.simple.tricky.before; import testfiles.simple.before.InnerClassSample; public class InnerClassReference { public void ref(InnerClassSample.MyInnerClass…
3
votes
1 answer

Find Class name of Super Keyword in JavaParser

I am developing a Java application based on JavaParser. I do not know how to get class name of Super keyword used in a method body. As an example, I need to know Super keyword in the below code is referred to class A. class A { public void…
Iman
  • 91
  • 7
3
votes
1 answer

A faster way to build model in inria Spoon

I am using inria Spoon to parse Java projects, and then extract information about classes, Interfaces, fields, and methods and all their references. I am using the below code to build model of an input project. SpoonAPI spoonAPI = new…
Iman
  • 91
  • 7
1
2 3
12 13