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

JavaParser - save modified source code file with new name

I'm using JavaParser to modify java source code. My goal is to read a single java source code file (ArithmeticClassToBeMutated) and store it in a compilation unit. Then, i'd like to replace/mutate its arithmetic operators…
NicolasH
  • 1
  • 1
0
votes
1 answer

JavaParser add Annotation with single value

I'm tryng to parse a java file to add annotation to the classes. I have a class like Class A{ } And i want it to become like @Foo(Bar) ClassA{ } At the moment i'm using a visitor with like: public void visit(ClassOrInterfaceDeclaration n, Object…
Djamaica
  • 29
  • 4
0
votes
1 answer

Javaparser: how to annotate existing classes?

I am trying to annotate a class with a custom annotation. I have a simple class (Foo) and a ClassVisitor, which add the annotation to an existing class. public class Foo{ public static void main(String[] args) { SourceRoot sourceRoot =…
apê
  • 1
0
votes
0 answers

Javaparser package not importing after adding maven dependency

I'm working on a project with javaparser, I added the following maven dependencies - com.github.javaparser javaparser-core 3.24.0
fireWall
  • 1
  • 2
0
votes
1 answer

JavaParser removes empty lines

I am using JavaParser with with the following dependency: com.github.javaparser javaparser-core 3.24.0 When I try to use it with the following class…
Christian
  • 576
  • 1
  • 4
  • 16
0
votes
1 answer

ClassOrInterfaceDeclaration.getTokenRange() gets all the tokens for a file even after removing nodes from the class

When I remove "ClassOrInterfaceDeclaration" nodes from a class in CompilationUnit (JavaParser) from a parsed file that contains more than one class, ClassOrInterfaceDeclaration.getTokenRange() gets all the tokens even the removed nodes tokens. For…
RanaSamy
  • 87
  • 1
  • 5
0
votes
1 answer

count the "IfStmt" statement in java parser

I am using javaparser to parse a java file , when I count the "If" statement it display the output as an incremental number such that Eg: if there are 3 "if" statements then it displays as [ 1 2 3 ] I want to get only the total number of "IF"…
0
votes
1 answer

Java Parser comment statement

I am trying to comment a particulate statement. My first approach is to return a comment in case statement is an 'Expression Statement' and expression is a particular 'Method Call Expression'. new ModifierVisitor() { public Visitable…
Vinod Pahuja
  • 47
  • 12
0
votes
0 answers

Can soot perform program analysis for incomplete code

What I understand is Soot performs program analysis on the ByteCode. However, I have incomplete or slightly buggy-code like the following where the return type for the method is not correct: class D { public void m1() { int total = 0; …
Exploring
  • 2,493
  • 11
  • 56
  • 97
0
votes
1 answer

How to put annotations on a new line with JavaParser?

I want to add an annotation to a field with javaparser. public static String annotate() { String classString = new StringJoiner("\n") .add("class A {") .add("") .add(" @ExistingAnnotation") .add("…
peer
  • 4,171
  • 8
  • 42
  • 73
0
votes
1 answer

How to get an assignment from a method?

I want to collect methods in a class that set a certain field e.g. int i = 2;. I know how to get the Statements of a method: CompilationUnit cu = StaticJavaParser.parse("class A {private int i; public int SetI(int…
peer
  • 4,171
  • 8
  • 42
  • 73
0
votes
3 answers

Minimal java file

I want to compare two .java files and only check if they are identical. for example i consider the following 2 code blocks as identical public class A extends B { private int i; private int j; } public class A extends B { private int…
Zarathustra
  • 2,853
  • 4
  • 33
  • 62
0
votes
1 answer

JavaParser: How do I add a method call to a chained invocation?

Given a chained invocation (builder pattern): return new ThingBuilder().withA(a).withB(b).build(); How can I use JavaParser to add another chained invocation to end up with code like this: return new…
Ovesh
  • 5,209
  • 11
  • 53
  • 73
0
votes
2 answers

How to extract metadata from Java methods using AST?

Is there an AST tool that allows easily extract metadata from a Java method? For instance, using the following code snippet /* Checks if a target integer is present in the list of integers. */ public Boolean contains(Integer target, List
Celso França
  • 653
  • 8
  • 31
0
votes
1 answer

How to get variable names used inside a method of a class using javaparser

The class which I am analyzing: public class Users{ private String name = "Jacob"; private String address = "711 11th Ave, New York"; private int age = 28; public String verifyAgeLimit() { String msg = (this.age < 16) ?…
RajB009
  • 417
  • 2
  • 7
  • 19