Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Chinese News Board
@ralphcnb_twitter
Are you trying to say that the AST Nodes that are being retained in memory / on the Java Heap - permanently - are in fact being retained by the class LexicalPreservingPrinter AND NOT by the class StaticJavaParser? If so, how might one enforce that those AST's are nulled (set to null) so that they may be Garbage Collected ?
Chinese News Board
@ralphcnb_twitter
I was sort of hoping that Van Bruggen / Matazoid was out there... He seems to know JP the best on Gitter. Hey! Danny van Bruggen if you happen to read this, I'm trying to identify whether there is a way to clear the Heap of all AST's or Nodes after I am done using them. My Build Script uses (quite well, actually) Java Parser to parse well over 300 source code '.java' files - and all I can tell is that when the class Visitor / Handler I use "goes out of scope" for one '.java' source code file and moves on to the next one - all of the AST Nodes are retained in memory and cannot be garbage collected (from the last source-code file) I would just like to know if there were a way to 'null' their references in whichever JP class these references are being retrained.
Chinese News Board
@ralphcnb_twitter
Um... And here is the VoidVisitorAdapter class I use, here's The JavaDoc for it... Here are the other classes the retrieve the java.lang.String that I need for my HTML Documentation Tool.... Here is Callable, (JavaDoc), and 'Method' (JavaDoc), and 'Constructor' (JavaDoc), and 'Field' (JavaDoc)
Nuwan Sanjeewa Abeysiriwardana
@nuwansa
how do i include java sdk libraries to the symbol resolver. I'm getting following exception.
UnsolvedSymbolException{context='null', name='List', cause='null'}
jean pierre Lerbscher
@jlerbsc
Hi @nuwansa You have to initialize JavaSymbolSolver with ReflectionTypeSolver.
Nuwan Sanjeewa Abeysiriwardana
@nuwansa
@jlerbsc thanks
Déaglán Connolly Bree
@Deaglancb_gitlab
Hi everyone. I came across some behaviour but I'm not sure if it's a bug or if I'm missing something (RE how Lexical Preservation is intended to be used). Trying to addExtendedType to a class, I get an unsupported operation if there isn't a space before the opening brace of the class (which I want to add the extends to) with lexical preservation enabled. So public class Customer{} won't work, but if I change it to public class Customer {}I can add an extends no problem. The problem also goes away if I don't set the configuration of the parser to enable lexical preservation, and if I don't setup the LexicalPreservingPrinter for each Compilation Unit. If I'm not being clear I can post more details in the issues on Github, might be easier.
jean pierre Lerbscher
@jlerbsc
Hi @Deaglancb_gitlab Could you open an issue and provide a test case please. Thanks.
Déaglán Connolly Bree
@Deaglancb_gitlab
Done, cheers!
marcosvasousa
@marcosvasousa

I have classe bellow, so I would like to have output

OUTPUT:

WebServiceXpto

method_1

call_1

call_2

String x = "end";

method_2

call_2

String x = "end";

Maybe on an map having list of method as key and list os all call stack.

CLASSE:
public class WebServiceXpto {

public void method_1() {
call_1()
}

public void method_2() {
call_2()
}

public void call_1() {
call_2()
}

public void call_2() {
String x = "end";
}

}

jean pierre Lerbscher
@jlerbsc
Hi @marcosvasousa I think you have to implement your own visitor (which can inherit from VoidVisitorAdapter) and redefine visit() methods for ClassOrInterfaceDeclaration, MethodDeclaration, MethodCallExpr... to print your own output format.
marcosvasousa
@marcosvasousa

Hi @jlerbsc I tried but did't found solution. I din't found I way to have all children of MethodCallExpr or recursive have it
My MethodCallCheck extends VoidVisitorAdapter<Void>
public void visit(MethodCallExpr methodCallExpr, Void arg) {
super.visit(methodCallExpr, arg);
}

This is my MethodCheck extends VoidVisitorAdapter<Void>
@Override
public void visit(MethodDeclaration methodDeclaration, Void arg) {
super.visit(methodDeclaration, arg);

    LOG.info("> MethodDeclaration: " + methodDeclaration.getNameAsString());

    NodeList<Statement> nodeList = methodDeclaration.getBody().get().getStatements();
    if (nodeList != null) {
        startCheck = false;

        for (Statement statement : nodeList) {
            // LOG.info("> Statement: " + statement + " - " + statement.isExpressionStmt());

            VoidVisitor<Void> methodCallCheck = new MethodCallCheck(packageNameBase, classNameBase, methodDeclaration.getNameAsString());
                statement.accept(methodCallCheck, arg);
        }
    }
}
jean pierre Lerbscher
@jlerbsc
Hi @marcosvasousa What you have represented in your example is an internal stack trace of the class but i think you want to extend it to all the classes of a project. I don't have time to implement your algorithm but what I recommend at first glance is to use the plain capacity of the visitor pattern and use the second argument to propagate an execution context which keep track of current class declaration, method declaration, method call expression for each class and use graph to modelize each relationship... Personnally i use this technique to implement a java class/package/module relationship in 3D representation (example http://www.jperf.com/jp/). Unfortunately i can't help you more without spending a lot more time on it, but I think you need to look further into these solutions or even study others if this one does not allow you to achieve your objective.
makbro
@makbro
Good evening, everyone! If someone would be so kind as to help me setup javaparser into NetBeans I would appreciate it. I want Netbeans to playback some feedback sounds whenever the parser detects an error as the user types code. How would I go about to do that? If there is already a video or resource explaining how to do that let me know.
And if there is someone I can contact directly that can help me, please let me know.
Danny van Bruggen
@matozoid
@makbro I think you want to look into creating Netbeans plugins for that - Netbeans already parses the code and shows the errors, so there must be a parser and some handling in there that you might be able to plug into. This project, JavaParser, is not in use in Netbeans.
makbro
@makbro
@matozoid I will do that. Thank you!
Chinese News Board
@ralphcnb_twitter
@matozoid !!! How do I garbage collect an AST after I am done with it? I have nary a clue where to start looking for that. I load AST's by asking the class StaticJavaParser for a CompilationUnit cu, and then have a straightforward VoidVisitor retrieve several String's from the AST... Is there any simple way to clear the AST out of the Java Heap ("memory") after my build code moves on from one source file to the next source-file ? Would you like to help ?? (Specifically, I mean System.gc() doesn't get the AST's out of memory !!
Danny van Bruggen
@matozoid
@ralphcnb_twitter --> https://visualvm.github.io/ :+1:
Chinese News Board
@ralphcnb_twitter
I know about the Memory Analyzer (you brought it up, before)... Umm... Do you know how in C/C++ you have to say free(ptr) or dealloc(ptr) (I haven't done C/C++ in 20 years)... But in Java as long as you say objReference = null you are guaranteed that the Object objReference (whatever it was) will be cleared from the Heap by an invokation of System.gc()(because there are no references to it) ... How in the world can I make sure the AST pointers that are being retained by ether LexicalPreservingPrinter or StaticJavaParser are set to null, so that System.gc() will clean the heap out after my code is through with an AST? I've got to clean that out in my build process - when the build moves from one source-file AST and one to the next source-file AST
Danny van Bruggen
@matozoid
Hey @ralphcnb_twitter, I'm not active on this project anymore. It looks like the problem (if any, GC is something that is best left to do its thing most of the time) is in your code, and I don't have time to look at that. The few times the GC was failing to do its job was always because a reference to the object to collect was accidentally still in a map or list somewhere.
If you are not running out of memory, there should be no reason to worry. If you depend on finalize() or so, then stop doing so.
Chinese News Board
@ralphcnb_twitter

You quit Java Parser ... for real? Haven't you been doing this for years and years? I mean, I almost bought your book, but I'm trying not to get into the internals of the JP Code - I used it, and it (Java Parser) worked great. It gets used on hundreds of source-files every time I do a build... I just don't want to memorize the whole JP GitHub Build - I want to concentrate on my build instead... Does that make sense? (Use the API, rather than becoming a JP Developer) I know I mentioned that the JP Jar I use is javaparser-core-3.14.159265359.jar and I literally got it from http://jar-downloader.com

Yeah, you said the right thing, and I have tried to explain that I extract (a bunch / a lot) of java.lang.String using a VoidVisitor -- but after the Visitor completes there is a bunch of stuff in memory, and I'm not keeping any references to the JavaParser classes at all - only java.lang.String ... But OK... If you have quit this project, that's fine... It's been real.... Take care...

Matthew Kramer
@matthewthekramer
Hey all, sorry if this is a silly question, but I've recently been investigating if javaparser would be a good substitute for a dependency in a project I'm working on. I would like to traverse an AST and save context to certain nodes (i.e. saving names of variables in the same scope to a statement node) for later visitor operations. Currently, the only way I see this would be possible is creating subclasses for some nodes, and replacing the nodes with the subclass during a visit. However, this seems a bit messy to me as you would then have to cast the node later to see its context. Is there a better way of doing this? Am I missing something obvious? I've looked a bit around the javadocs and read the book but it doesn't seem obvious to me. Thanks
Danny van Bruggen
@matozoid
Hi @matthewthekramer - you can annotate the nodes with any kind of information you like using the setData/getData methods.
Lucas Gontijo
@gontijolucas
Hi.
I would like to translate the book "JavaParser: Visited" into Brazilian Portuguese, has anyone translated this book? do you know if it is allowed? who would i have to talk to? thank you.
Danny van Bruggen
@matozoid
@SmiddyPence is/was the book guy.
Luca Molteni
@lucamolteni
Hi guys, I haven't been checking this channel for a while
so @matozoid aren't you on the project anymore? woah that's big news
I hope there's still someone then :D
Little trivia: what is the difference between the twos? (without looking at the source code
     StringLiteralExpr a = new StringLiteralExpr("String with \"");
     StringLiteralExpr b = new StringLiteralExpr().setString("String with \"");
Cameron Braid
@cameronbraid
I have a method call chain like the following, a.b().c() and I want to insert an extra method call in the chain to make it like a.b().d().c(). How do I do that ? I tried using new MethodCallExpr making d's scope be b and c's scope be d but when printing I get
Exception in thread "main" java.util.NoSuchElementException: No value present
        at java.base/java.util.Optional.get(Optional.java:148)
        at com.github.javaparser.printer.lexicalpreservation.PhantomNodeLogic.isPhantomNode(PhantomNodeLogic.java:58)
        at com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter.findNodeForToken(LexicalPreservingPrinter.java:364)
        at com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter.findNodeForToken(LexicalPreservingPrinter.java:375)
        at com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter.findNodeForToken(LexicalPreservingPrinter.java:375)
My actualy code is :
          MethodCallExpr nameMethod = cu.findAll(MethodCallExpr.class)
          .stream()
          .filter(mc->
            mc.getNameAsString().equals("name") &&
            mc.getArguments().size() == 1 &&
            mc.getArgument(0).isLiteralStringValueExpr() && 
            mc.getArgument(0).asLiteralStringValueExpr().getValue().equals(propertyName)
          ).findFirst().get();
          MethodCallExpr wireClassMethod = nameMethod.findAncestor(MethodCallExpr.class, a->a.getNameAsString().equals("wireClass")).get();
          MethodCallExpr wireTypeRef = new MethodCallExpr(wireClassMethod.getScope().get(), "wireTypeReference", NodeList.nodeList(new StringLiteralExpr("test")));
          wireClassMethod.setScope(wireTypeRef);
It works when using PrettyPrinter but that really messes up my code so I need to preserve the lexical structure
Cameron Braid
@cameronbraid
Found it.. need to run LexicalPreservingPrinter.setup(cu) BEFORE doing mutations on the compilation unit
jean pierre Lerbscher
@jlerbsc
Hi @cameronbraid we can't help you with so little information. The least thing would be to provide a minimalist test case.
Danny van Bruggen
@matozoid
@gontijolucas - no response from @SmiddyPence, so I say go ahead! I'm not aware of translations. @ftomassetti is author #3 but I doubt he will protest.
@lucamolteni - I'm in the background, supporting the new team :-) --> @maartenc @jlerbsc @MysterAitch <--
@lucamolteni I bet the answer to your riddle is something about how helpful JavaParser is trying to be with literals, and how it is failing to be consistent :-)
Danny van Bruggen
@matozoid
@maartenc @jlerbsc @mysteraitch, @lucamolteni works on RedHat's Drools (https://www.drools.org/) which was prototyped with JavaParser, and if I'm not mistaken it is still based on it. Pretty cool if you ask me.
Federico Tomassetti
@ftomassetti
I confirm I will not protest 🙂
Damn the issue was commented 7 years ago. I feel oooold
Matthew Kramer
@matthewthekramer
@matozoid Got it, thanks for the response!
Nguyen, Thanh
@thanhnguyen12
Hello, I have been playing with JavaParser serialization and DotPrinter (which are very cool tools). The JSON and Dot outputs are different in terms of child order. For example, JSON prints the children of BinaryExpr as (left, operator, right) while DotPrinter print (operator, left, right). I was wondering if there is a grammar that I can follow to make these consistent with each other? Thank you.
Chinese News Board
@ralphcnb_twitter
@ftomassetti ... Do you know anything about it? Do you know what I could 'null' so that an AST is cleared from memory (Java Heap) after I'm done with a CompilationUnit generated by StaticJavaParser ???
As I explained, I parse around 330 '.java' source-code files with JP - to extract source-code for code-hiliting in a JavaDoc HTML Extention Tool - it works fine - but memory heap grows and grows. @ftomassetti ...
jean pierre Lerbscher
@jlerbsc
@ralphcnb_twitter perhaps you can do a PhantomNodeLogic.cleanUpCache() to limit memory footprint.
Chinese News Board
@ralphcnb_twitter

Hey, thanks, just got back. I will try that. All I know is that after using an AST to retrieve stuff (String's), if I try to do something like ...

.forEach((Node nChild) -> 
            { try { nChild.removeForced(); ...

It starts throwing exceptions. It seems like this could be a trivial operation I'm trying to do, but I cannot figure it out. Thanks for the advice, will try later today. For System.gc() to actually do its magic, the references have to be null...