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?
private void RemoveOtherClassesFromSameFile(ClassOrInterfaceDeclaration n) {
n.getTokenRange() // 1
List<ClassOrInterfaceDeclaration> internalClasses = unmodifiableList(
n.getMembers().stream().filter(m -> m instanceof ClassOrInterfaceDeclaration)
.map(m -> (ClassOrInterfaceDeclaration) m).collect(toList()));
for (ClassOrInterfaceDeclaration c : internalClasses)
n.remove(c);
n.getTokenRange() // 2
}