'.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 ...
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...
I read through it, and it looked like the light at the end of the tunnel for me! I wasn't aware that there was a cache in LexicalPreservingPrinter
... I get this exception:
Torello/HTML/Tools/JavaDoc/JavaSourceCodeFile.java:30: error:
PhantomNodeLogic is not public in com.github.javaparser.printer.lexicalpreservation; cannot be accessed from outside package
import com.github.javaparser.printer.lexicalpreservation.PhantomNodeLogic;
^
Torello/HTML/Tools/JavaDoc/JavaSourceCodeFile.java:449: error:
PhantomNodeLogic is not public in com.github.javaparser.printer.lexicalpreservation; cannot be accessed from outside package
PhantomNodeLogic.cleanUpCache();
^
2 errors
Is there any chance I'm using an "older" jar file? If that's the source of what "feels like" a memory-leak, is there a newer JP JAR File that I would need? I have been using (for almost a year now)javaparser-core-3.14.159265359.jar
This looks very promising...
PhantomNodeLogic
is a public class, and that cleanUpCache()
is a public static method, so I couldn't really be able to say why my JDK is reporting that it isn't public so... If anyone knows the answer... I don't have a GitHub account, and haven't ever built JP before, would I have to do that or something?
Thanks!!! It worked... BTW, I wrote my own build tool for my library - it's not Maven or Gradle (It's this very large Java Package), so I just kept using the JP Jar I got off the Internet... It has always worked though... I got 3.16.2 (It's 6:50 AM right now, here in Dallas)...
**************************************************************************
Free Memory Available: 242,205,208
Total Memory Available: 255,524,864
Loading File: javadoc/Torello/Languages/Verbs.WebFiles.html
Fully-Qualified Name: Torello.Languages.Verbs.WebFiles
Built from Source-File: Torello/Languages/Verbs.java
Relative-URL String: ../../
Parsed JavaDoc '.html': Name [Verbs.WebFiles], Package [Torello.Languages], CIET [class]
Parsed Source '.java': Name [Verbs.WebFiles], Package [Torello.Languages], CIET [class]
'.html' File Parser Found: [000] Fields, [000] Constructors, [004] Methods
'.java' File, JavaParser Found: [000] Fields, [001] Constructors, [004] Methods
**************************************************************************
The garbage collector seems to have got it all... Thanks Mr. Lerbscher
... I know you mentioned LexicalPreservingPrinter
being the source of it, but I didn't quite get it...
Free Memory Available: 315,115,776
Total Memory Available: 709,603,328
Hi!
I am trying to write a piece of code that removes useless empty if-else statements from code. For example...
foo();
if (true) { } else { }
bar();
but I seem to be running into an issue which I am not sure if it is just me missing something, or whether it is a bug.
My code is as follows:
class EmptyIfVisitor : VoidVisitorAdapter<Unit>() {
override fun visit(ifStmt: IfStmt, arg: Unit?) {
super.visit(ifStmt, arg)
val condition = simplifyExpr(ifStmt.condition)
val thenStmt = ifStmt.thenStmt
val elseStmt = ifStmt.elseStmt.orElse(null)
if (condition is BooleanLiteralExpr) {
if (isEmptyStatement(thenStmt) && isEmptyStatement(elseStmt)) {
// We can happily remove this block, as it is doing absolutely nothing useful whatsoever.
logger.warn { "removing useless ${debugString(ifStmt, verbose = true)}" }
ifStmt.remove()
}
}
}
}
however, the ifStmt.remove()
is currently throwing a ConcurrentModificationException whenever it is hit, assumably because it is modifying the internal NodeList on the parent while iterating over it.
Is there some way to do what I want to do? The only other solution is to do ifStmt.replace(EmptyStmt())
but that really feels like a bodge to me.
java.util.ConcurrentModificationException
at java.util.ArrayList.forEach(ArrayList.java:1262)
at com.github.javaparser.ast.NodeList.forEach(NodeList.java:290)
at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:109)
at com.github.javaparser.ast.stmt.BlockStmt.accept(BlockStmt.java:76)
at com.github.javaparser.ast.visitor.VoidVisitorAdapter.lambda$visit$104(VoidVisitorAdapter.java:369)
at java.util.Optional.ifPresent(Optional.java:159)
at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:369)
at com.github.javaparser.ast.body.MethodDeclaration.accept(MethodDeclaration.java:106)
at com.github.javaparser.ast.visitor.VoidVisitorAdapter.lambda$visit$31(VoidVisitorAdapter.java:154)
at java.util.ArrayList.forEach(ArrayList.java:1259)
at com.github.javaparser.ast.NodeList.forEach(NodeList.java:290)
at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:154)
at com.github.javaparser.ast.body.ClassOrInterfaceDeclaration.accept(ClassOrInterfaceDeclaration.java:100)
at com.github.javaparser.ast.visitor.VoidVisitorAdapter.lambda$visit$43(VoidVisitorAdapter.java:175)
at java.util.ArrayList.forEach(ArrayList.java:1259)
at com.github.javaparser.ast.NodeList.forEach(NodeList.java:290)
at com.github.javaparser.ast.visitor.VoidVisitorAdapter.visit(VoidVisitorAdapter.java:175)
at com.github.javaparser.ast.CompilationUnit.accept(CompilationUnit.java:134)
at <my unit test that calls CompilationUnit#accept(EmptyIfVisitor(), null)>
at <other junit stuff>
hello everyone i am new to javaParser and i need some help .
i want :
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);
}
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.
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?
int x, b, abc;
)
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 :)