Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
ridhij93
@ridhij93
Thanks!
ridhij93
@ridhij93
Can I also find out which classes a class extends or interfaces it implements?
Danny van Bruggen
@matozoid
For that you'll need the symbol solver. The book(let) explains how all of that works.
nashid
@nashid
Did anyone attempt to build a control flow analysis with Java Parser? Are there examples available?
I would be also interested to know anyone built a basic Reaching definition data-flow analysis with Java Parser? Any help is appreciated.
Bart Naudts
@bnaudts
Hi Nashid, while not the immediate goal of the project, the static code analyser that I'm working on (e2immu.org) uses JavaParser and does more than basic control+data flow analysis.
16 replies
Danny van Bruggen
@matozoid
Danny van Bruggen
@matozoid
That one (mine :-) ) is something I wrote after users kept asking for a control flow analyser. I have no use for it myself, so I just implemented what seemed useful. Feedback would be very welcome :-)
9 replies
maggieCC
@mattycc
Hi everybody. Do you know is there any method to get the type of parameters? For example, "m.create(name)", the type of "name" is NameStr. Can we get the type which is NameStr with Javaparser? Or the only way is to search for the whole java file and find the declaration place?
Danny van Bruggen
@matozoid
@mattycc that's the job of the symbol solver. The book has a manual for it.
maggieCC
@mattycc
Thanks so much. I've tried that but got an error of UnsolvedSymbolException even I added the whole java package and jar path to the combinationTypeSolver.
Danny van Bruggen
@matozoid
ygq1001
@ygq1001
hi, there. I'm trying to build a code property graph in method level by using JavaParser. However, for now, I have no idea about how to build CFG and PDG while the AST built.
9 replies
Ryan Warren
@ryantbwarren
Hi, I am trying to extract the number of JavaTokens present in a given TokenRange. My current approach is to iterate over the TokenRange with a counter but I wasn't sure if there might be a better solution? Many thanks!
2 replies
ygq1001
@ygq1001
Hi, I want to know if there is a way to compare two nodes and decide whether they are the same node. Is there a unique identifier for the node?
2 replies
ygq1001
@ygq1001
well, I'm trying to get all tokens in nodes. I find that the tokens contain whitespace like \r\n and comment. I have set parser configuration "ParserConfiguration().setAttributeComments(false);" . But comments still are in the tokenrange. Is there a way to ignore these unimportant tokens? Thanks
2 replies
maggieCC
@mattycc
Hi there, do you know is there any way to do pretty writing into a new file? I can use PrettyPrint to get a pretty output. But when using BufferWriter to write, I cannot get java source code as pretty as I want.
like setColoumAlignFirstMethodChain() will not work when writing into a new file
Danny van Bruggen
@matozoid
Not sure why there would be a difference - if you can can get the pretty printed string, you can write it to a file. Are you doing something in between?
varivoda
@varivoda

Hi there! Thanks for the great job with java parser!

I'm new in this tool and I can't add new annotation to my test method. I have compilationUnit for my class and do the next

compilationUnit.findAll(MethodDeclaration.class).forEach(methodDeclaration -> methodDeclaration.getAnnotations().add(new MarkerAnnotationExpr("Nullable")));

But it doesn't work. How can I solve it? Thanks a lot!

2 replies
maggieCC
@mattycc

Not sure why there would be a difference - if you can can get the pretty printed string, you can write it to a file. Are you doing something in between?

Thanks for your reponse. Yeah, I got the expected output last week, thanks! But I still have small question for the layout. For example, in the original source codes, it looks like:

this.workflowDefinitionManager.putWorkflowDefinition(putWorkflowDefinitionRequest.getId(),
                putWorkflowDefinitionRequest.getAssumeRoleId());

This line is too long, so it just start a new line. However, in the souce codes after transformation, it cannot maintain the original layout, it will be

this.workflowDefinitionManager.putWorkflowDefinition(putWorkflowDefinitionRequest.getId(), putWorkflowDefinitionRequest.getAssumeRoleId());

Which is not an expected change. And also for those original null character lines, it will delete them automatically, like from

import xxx

import XXX

to

import xxx
import XXX

For those two unexpected edits, is there a proper way to solve it and preserve the original layout in prettyprint configuration or other methods? Thanks!

11 replies
ygq1001
@ygq1001
hi,is't possible to get a cfg-node's variables(read and written) through a simple way(Convenient API). I have used your cfg analysis code, and I want to build a data dependence graph based on it. Many thanks. @matozoid
1 reply
Shashank
@shankyty
Hi All,
I am planning to write Unit Test generator.
Whart would best apporach for it.
2 replies
ygq1001
@ygq1001
2021-07-21 04-01-10屏幕截图.png
@matozoid hi,when I use you data-flow-analyse code.I found something may be wrong. I found it just analyse try block but didn't analyse the catchclause. I can only get "87 START -> 90
90 STEP -> 91
91 STEP -> end"
Danny van Bruggen
@matozoid

@matozoid hi,when I use you data-flow-analyse code.I found something may be wrong. I found it just analyse try block but didn't analyse the catchclause. I can only get "87 START -> 90
90 STEP -> 91
91 STEP -> end"

Okay, open a bug report on that project please. (Or a PR with a fix ;-) )

11 replies
Danny van Bruggen
@matozoid
( holiday time for me )
roshansinghkande
@roshansinghkande

@ftomassetti @matozoid and Javaparser team
Thank you for this awesome library.

Can anyone please suggest a way on how I can achieve the below:

I am trying to migrate from an old library to a new one in some projects. I want to automate the refactoring of this migration.
Specifically I want to refactor the instantiation and population of a request object as below

Current code:

RequestDTO1 req= new RequestDTO1();
req.setParam1(variable1);
req.setParam2(variable2) .......

Target code:

RequestDTO2 req= new RequestDTO2(); // The RequestDTO2 class extends Hashmap
req.put("param1",variable1);
req.put("param2",variable2)......

In a nutshell, want to extract variable1 and variable2 from current code and use it in refactored code.

maggieCC
@mattycc
Hi everyone, do you know if LexicalPreservingPrinter ignores comments parts? I plan to change something in the comments. The compilationUnit varaible is what I want after transformation. However, when I use LexicalPreservingPrinter to print it out. The comment part is as same as the original one without any changes.
5 replies
I test it as following:
CompilationUnit compilationUnit = StaticJavaParser.parse(new FileInputStream(testPath));
        compilationUnit.getAllComments()
            .stream()
            .filter(m -> m.toString().contains("Test block comments"))
            .forEach(m -> {                            
                      String newCom = m.getContent().replace("block", "test");
                      Comment newComNode = m.setContent(newCom);
                      m.replace(m, newComNode);
            });
LexicalPreservingPrinter.setup(compilationUnit);
System.out.println(LexicalPreservingPrinter.print(compilationUnit));
wuguanyu
@wuguanyu
In our project, We use lombok. we use javaparser to analyze java project, we encounter one issue
Unsolved symbol : We are unable to find the method declaration corresponding,
In lombok, some code will generate in compile phase in class , not in java source code, do we have any solution to resolve
wuguanyu
@wuguanyu
@matozoid
Roger Howell
@MysterAitch
@wuguanyu -- see this issue and the others linked from it (short version, JavaParser doesn't run preprocessors such as Lombok - which means that the generated code doesn't yet exist therefore it cannot be parsed)
Danny van Bruggen
@matozoid
Back from holiday :-)
Bar Lerer
@barlerer_gitlab
I have a question.
I have created a simple visitor, that collects a SimpleName from method bodies.
The only issue I have with it is the order of the last attributes (i.e the function name, parameter name and class name)
Everything else appears in order, so why do these names come in last? and not first (As in the order they appear)?
1 reply
mbto
@mbto
Hello. How can I implement .toString() so that it prints exactly as it does in the original file? I have lost the indents in the entity classes.
1 reply
Screenshot_1
mbto
@mbto
Thanks, I extends DefaultPrettyPrinterVisitor and override public void visit(final ArrayInitializerExpr n, final Void arg) изображение
Michiel Borkent
@borkdude
Hey @matozoid ! Thanks for responding on Twitter
Danny van Bruggen
@matozoid
No problem! Sorry for not thinking of the dual license :-#
Siddarth Menon
@sidmenon28:matrix.org
[m]
How do you find static, abstract and private methods in an inheritance chain using the javaparser
1 reply
ygq1001
@ygq1001
I want to use symbol solver to resolve method call. The files are SARD juliet test suite cases, as the picture shows, there are two files, and they are in the same directory.However, both of them don't contain any package info, I have tried to operate as the doc said, but sth went wrong.So, how to resolve it?
5 replies
image.png
1.PNG
image.png
image.png