Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
jean pierre Lerbscher
@jlerbsc
hi @ascopes because you modify the AST you can use a ModifierVisitor like this
ModifierVisitor v = new ModifierVisitor<Void>() {
        @Override
        public Visitable visit(IfStmt ifs, Void arg) {
            return null;   
        }
    };
    v.visit(cu, null);
Ashley
@ascopes
ah awesome, thanks
Danny van Bruggen
@matozoid
Also the functional traversal style has protection built in against that: https://javaparser.org/functional-tree-traversal/
Amartya Vats
@amartya2969
Hi!
I am trying to write a piece of code through which I can print all the methods that have been called in a constructor. For a non contructor method it is easy to do with MethodCallExpr, how to do the same for a constructor?
and check the class it extends from.
Discotek.ca
@discotekdotca_twitter
Hello, I have been testing JavaParser against the JDK's own code and have some issues. What is the best way for me to report them?
Discotek.ca
@discotekdotca_twitter
Sorry - forget it. I found the info I needed.
Danny van Bruggen
@matozoid
Well, that's quick :-)
I'm curious what that info was though (setting the language level maybe?)
Cheikh Saadbouh
@cheikh-sadbouh

hello everyone i am new to javaParser and i need some help .

i want :

  • delete some method parameters
  • replace statement A with statement B
class MethodChangerVisitor extends VoidVisitorAdapter<Void> {
      @Override
    public   void visit(MethodDeclaration n, Void arg) {

          System.out.println(n.getParameters().get(0).getType());
          System.out.println(n.getParameters().get(0).getName());
          System.out.println(n.getParameters().remove(0));//remove parameter not working
          //replace statement with other statement ?
          System.out.println(n.getBody().get().getStatements().get(0).asExpressionStmt().setExpression("int a = 5;"));

        super.visit(n, arg);
    }
jean pierre Lerbscher
@jlerbsc
Hi @cheikh-sadbouh why do you say "n.getParameters().remove(0)" doesn't work? Do you have an exception?
12 replies
ovesh
@ovesh

Hello!

I have a question about chained method calls.
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 ThingBuilder().withA(a).withB(b).withC(c).build();

Inserting it anywhere in the chain (before build()) is fine.

15 replies
mdproctor
@mdproctor
does anyone know of a Language Server Protocol impl, that can be driven from a JavaCC grammar?
3 replies
Yevgen Muntyan
@muntyan

Good morning. I am using LexicalPreservingPrinter to do some code modifications, and it works well, but I can't figure out how to insert whitespace where I need it. My goal is to replace code like

    function(new RuntimeException("abc"),
        anArgument,
        anotherArgument);

with

    new RuntimeException("abc",
        anArgument,
        anotherArgument);

But I can't get the newlines in the replacement, I can only get

    new RuntimeException("abc", anArgument, anotherArgument);

Is it possible to do what I want?

12 replies
Roger Howell
@MysterAitch
v3.17.0 released -- it'll be available as soon as bintray/sonatype/maven central do their thing!
Big shout out to @jlerbsc for another bumper release of bug fixes! 🥳🥳
Nicholas Smith
@SmiddyPence

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.

Sure, no problem.

Devan-Kerman
@Devan-Kerman

hey, given a FieldDeclaration, how would I replace the initiailizer, so for example

public class Foo {
       public int a = Something.compute(3, 5);
}

to

public class Foo {
       public int a = Impl.init();
}

I have the FieldDeclaration instance already

Roger Howell
@MysterAitch
Iirc the field declaration has a list of variable initialisations -- that's where you can access the right hand side of the assignment operators
(it's a list because you can have int x, b, abc;)
Devan-Kerman
@Devan-Kerman
yea I think I got it
for (VariableDeclarator variable : n.getVariables()) {
variable.setInitializer("Impl.init()");
}
very nice api, thank you
Roger Howell
@MysterAitch
Brill! Glad you've got it! :)
For reference, in case you've not seen it, this article might be helpful :) https://javaparser.org/inspecting-an-ast/
MarcosCosmos
@MarcosCosmos
Hey has anyone got a generic method/solution for getting the position/range in the source for attributes, or at the least, their tokens?
48 replies
mdproctor
@mdproctor
We are back to developing our java language extensions. One problem we have, is backwards compatibility - we want to make this optional. In short how we handle newlines, we would like to make configurable on a property. Does JavaCC have anything to enable that?
23 replies
Roger Howell
@MysterAitch
Could I ask for some sets of eyes on #2886 please? Specifically, will these changes in Java need to be acted upon in javaparser? It's not a language change, but I don't understand nests and some of the other things it talks about... equally, it could end up being something that is handled transparently for us by javassist...

Assuming that hidden classes (#2886) don't affect anything (I don't think it does, but I don't entirely understand the JEP), we can then formally tick javaparser off as supporting java 15... which would be rather nice! :D

After that, it's just records to go for java 16 support :)

Danny van Bruggen
@matozoid
Looks like some way to support people who want to write code that generates classes at runtime, so it is user code, running on the JVM, that is generating byte code to run on the same JVM, which can then be used only with reflection. Symbol solving is all about static, compile-time analysis, so this JEP would only be of interest if JP also had a JVM implementation :-)
Don't forget about javaparser/javaparser#2512 which has half the record syntax already! Feel free to take over from me.
The private project I'm working on has a dependency on JP since a few days ago, so I now have a stake in the project again :-#
fahd
@debbicce_gitlab
is there a javaparser jar file that I can add to my project configurations in Eclipse?
3 replies
Denis
@dencns
Using Gradle & Eclipse. Tried running this https://javaparser.org/inspecting-an-ast/, but I get the following error: Caused by: java.lang.NoClassDefFoundError: com/github/javaparser/ast/Node. Any tips to fix it? In the end I just want to change it to see the AST of a simple print of helloworld.
5 replies
Denis
@dencns
Tested it within intellij. it works there.
Luke McMahon
@ll16l4m_gitlab
Hi, is there a way to findAll for specific logical operators? For example, if I wanted to findAll(&&)?
3 replies
mdproctor
@mdproctor
@ftomasse_twitter I've just been told you are well connected to a JavaCC committer? I might be great if we could get an introduction, as we want to ask about LSP. One reason why we were interested in JavaCC21, is LSP. But if there was a way to get that into JavaCC itself, we have less of a need for JavaCC21.
Federico Tomassetti
@ftomassetti
I would strongely discourage to look into JavaCC21 as you would be strongely dependant on the person that behave the worst among all the persons I encountered online. So I would consider it risky for the future of the project and not ideal on the personal level
That said, I have talked the other day with Francis Andre', and I would be happy to write an introduction. Could you share your email with me, so I will put you in CC?
tagging @mdproctor
Federico Tomassetti
@ftomassetti
Screenshot 2020-12-03 at 11.52.46.png
@mdproctor this is just for you to get an idea of the kind of interactions you can expect, totally unprovoked
Danny van Bruggen
@matozoid
Yeah, sadly he already met the guy in the latest email torrent :-#
Federico Tomassetti
@ftomassetti
Sorry to hear that. Meeting this person made me much more careful. I have a ton of interactions every day and I was completely caught by surprise by the behavior of this guy
Betty-Wong
@Betty-Wong
Let me get this straight. You're saying that I should make the technical decision to use JavaCC21, or actually, not use it, I guess, because you have a beef with the main developer? Is there some other reason, like something that has some technical grounds?
Federico Tomassetti
@ftomassetti
@Betty-Wong of course you should do what you think it is best for you. However, I think it is useful to share experiences that were problematic. We are not reporting an heated discussion or a disagreement. We are talking about a person who insulted in very disturbing ways several persons, unprovoked. He involved people totally external to the conversation and forked projects out of spite for the persons he was attacking. I think this could have an impact on the future of the projects, because I can suppose that building a team of contributors and a community with this behavior may prove difficult and this can reflect on the project
If you do not find this relevant to you, you should obviously feel free to ignore we reporting our experiences
I can say that it was a really surprising experience for me. I interact with a ton of people every single day and I have never been attacked in such a way. I can guarantee you that it has been very unpleasant, and I wish something had warned me before I interacted with this person, for this reason I thought useful and fair to share my experience