Skip to content
Advertisement

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 example, the code snippet at positions 1 and 2 will give the same result, although I already removed nodes from ClassOrInterfaceDeclaration n.

Does anyone know how to get the correct tokens list?

JavaScript

Advertisement

Answer

Ater Adjusting the file that contains the node, you must update the source root by using method saveAll, to override the AST to your file, like that

JavaScript

Full Example:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement