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
3
votes
1 answer

How to generate field and initialize value

How to initialize a field that is generated. Or in the code example below, where can the AssignExpr object be added for the code to work? private void addConfigField(ClassOrInterfaceDeclaration clazz) { var className = "BlaConfig"; var…
Yoshua Nahar
  • 1,304
  • 11
  • 28
3
votes
1 answer

Obtaining a list of all used class names from a Java source code

I am looking for a Java library (source code parser) that would help me extract unqualified names of all class names being used in the source code. For example for the given code example: public class Example { private ClassName1; …
Klemen
  • 169
  • 1
  • 2
  • 16
3
votes
1 answer

JavaParser doesn't remove comments before package declaration

I am writing a Java application that removes comments from Java files. I wrote this code: static void removeComments(Node node) { for (Comment child : node.getAllContainedComments()) { child.remove(); } …
undisp
  • 711
  • 3
  • 11
  • 34
3
votes
1 answer

How to add class level variable declarations using javaparser?

class A{ int x = 10; } This is A.java I want to get NewA.java class NewA{ int x = 10; Sting text = "B"; } I want to add a variable using javaparser.
HooMin.Lee
  • 115
  • 1
  • 8
3
votes
1 answer

.java Report Generation, Method Caller & Callee

Hello to the most helpful community on the internet! I'm working on a project that can read a .java file and spit out data. One of my programs to extract data is not working exactly how I intend it work (very cliche). It parses through all the .java…
kalame04
  • 75
  • 7
3
votes
1 answer

Extract methods calls from java code

I want to extract all method calls from java code. I have written following two regular expression but they are not able to extract all the method calls. Reg1 : Pattern.compile("([a-zA-Z][0-9_a-zA-Z]*\\([a-zA-Z0-9_\\s,\\[\\]\\(\\)\\.]+\\))"); Reg2…
Sangeeta
  • 589
  • 1
  • 7
  • 26
3
votes
1 answer

Parse Attributes from Java Files using Java Parser

I have 3 classes below. public class A { B b; } public class B { C c; public C getC() { A a; return c; } } public class C { } I want to parse the classes using java parser having following info: Class…
Md. Masudur Rahman
  • 1,028
  • 12
  • 30
3
votes
1 answer

Javaparser: Visit all node types with one method

I am using Javaparser to parse Java source code. Is there a way to implement a Visitor that can visit the abstract Node class? I want to visit every Node and print its line number, but I don't want to implement a visit() method for every Node type…
jem
  • 130
  • 8
3
votes
1 answer

JavaParser add Generic Type as method return type

I'm using javaparser-1.0.8 and I'm trying to generate the following generic method. public T get(int param) { return (T) doSomeMagic(param); } I have the following code that is supposed to build the method: public static…
ejohansson
  • 2,832
  • 1
  • 23
  • 30
2
votes
2 answers

Javaparser comment expression

I'm trying to comment all calls to a custom method inside a java file using Javaparser. My first approach is to use a ModifierVisitor: ModifierVisitor visitante = new ModifierVisitor() { @Override public…
Luis M. Villa
  • 159
  • 11
2
votes
1 answer

Use JavaParser to refactor a java file

Could you use the javaparser library to: Rename imported classes, methods and fields? For example: package org.example; import org.example.Test; public class Example { public void example() { Test t = new Test(); …
Ruiqi Li
  • 187
  • 14
2
votes
1 answer

Find if the current statement in an If/Else block is the last statement of the THEN branch

I am using JavaParser to parse through the contents of the following code and identify the sequence of statements: class X { void x() { int x = 1; if (x>4) { x=21; } else { if (x>1) { x=3; } else…
John Stef
  • 585
  • 1
  • 4
  • 16
2
votes
1 answer

JavaParser how to get classname in CompilationUnit

I have a path to a .java class like "./src/module/car.java" and i parse it like this: File sourceFileCar = new File("./src/module/car.java"); CompilationUnit cuCar = StaticJavaParser.parse(sourceFileCar); How can I get the class name in the…
nixfuerdiecharts
  • 313
  • 2
  • 14
2
votes
1 answer

Javaparser: get generic class of a method return type like List

I'm processing a Java source file using the java-parser library. I have this situation in the analyzed source code: List getList() { return EnumSet.allOf(Allergen.class).stream() .filter(this::isAllergicTo) …
2
votes
1 answer

Resolve a type in a context

I have a Spring boot project and I want to parse it and file the dependencies between classes I am using the JavaSymbolSolver to find out the Class Name public static void main(String[] args) throws Exception { Set
1
2
3
12 13