Skip to content

Instantly share code, notes, and snippets.

@edefazio
Created August 4, 2019 18:22
Show Gist options
  • Save edefazio/6ed134a627aacec68ca93cebe3cc5b16 to your computer and use it in GitHub Desktop.
Save edefazio/6ed134a627aacec68ca93cebe3cc5b16 to your computer and use it in GitHub Desktop.
Recalculating the positions after adding things to a compilationUnit
package org.jdraft.mr;
import com.github.javaparser.*;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.utils.PositionUtils;
import junit.framework.TestCase;
import org.jdraft._field;
import org.jdraft._method;
import java.util.Optional;
import java.util.stream.StreamSupport;
public class PosUtils extends TestCase {
public void testFF() {
String s = "public class A{\n\n\n"+ "}";
CompilationUnit cu = StaticJavaParser.parse(s);
cu.getType(0).addMember( _field.of("public static final int ID=100;").getFieldDeclaration() );
cu.getType(0).addMember( _method.of("public static void main(String[] args){ System.out.println(1); }").ast() );
cu.recalculatePositions();
System.out.println( "***** RECALC PosUtil ");
cu.walk(Node.class, n-> System.out.println(n.getClass()+ " "+ n.getRange().get()));
}
}
@edefazio
Copy link
Author

edefazio commented Aug 4, 2019

Output:

***** RECALC PosUtil
class com.github.javaparser.ast.CompilationUnit (line 1,col 1)-(line 4,col 1)
class com.github.javaparser.ast.body.ClassOrInterfaceDeclaration (line 1,col 1)-(line 4,col 1)
class com.github.javaparser.ast.Modifier (line 1,col 1)-(line 1,col 6)
class com.github.javaparser.ast.expr.SimpleName (line 1,col 14)-(line 1,col 14)
class com.github.javaparser.ast.body.FieldDeclaration (line 2,col 1)-(line 2,col 31)
class com.github.javaparser.ast.Modifier (line 2,col 1)-(line 2,col 6)
class com.github.javaparser.ast.Modifier (line 2,col 8)-(line 2,col 13)
class com.github.javaparser.ast.Modifier (line 2,col 15)-(line 2,col 19)
class com.github.javaparser.ast.body.VariableDeclarator (line 2,col 25)-(line 2,col 30)
class com.github.javaparser.ast.type.PrimitiveType (line 2,col 21)-(line 2,col 23)
class com.github.javaparser.ast.expr.SimpleName (line 2,col 25)-(line 2,col 26)
class com.github.javaparser.ast.expr.IntegerLiteralExpr (line 2,col 28)-(line 2,col 30)
class com.github.javaparser.ast.body.MethodDeclaration (line 1,col 1)-(line 1,col 64)
class com.github.javaparser.ast.Modifier (line 1,col 1)-(line 1,col 6)
class com.github.javaparser.ast.Modifier (line 1,col 8)-(line 1,col 13)
class com.github.javaparser.ast.expr.SimpleName (line 1,col 20)-(line 1,col 23)
class com.github.javaparser.ast.body.Parameter (line 1,col 25)-(line 1,col 37)
class com.github.javaparser.ast.type.ArrayType (line 1,col 25)-(line 1,col 32)
class com.github.javaparser.ast.type.ClassOrInterfaceType (line 1,col 25)-(line 1,col 30)
class com.github.javaparser.ast.expr.SimpleName (line 1,col 25)-(line 1,col 30)
class com.github.javaparser.ast.expr.SimpleName (line 1,col 34)-(line 1,col 37)
class com.github.javaparser.ast.type.VoidType (line 1,col 15)-(line 1,col 18)
class com.github.javaparser.ast.stmt.BlockStmt (line 1,col 39)-(line 1,col 64)
class com.github.javaparser.ast.stmt.ExpressionStmt (line 1,col 41)-(line 1,col 62)
class com.github.javaparser.ast.expr.MethodCallExpr (line 1,col 41)-(line 1,col 61)
class com.github.javaparser.ast.expr.FieldAccessExpr (line 1,col 41)-(line 1,col 50)
class com.github.javaparser.ast.expr.NameExpr (line 1,col 41)-(line 1,col 46)
class com.github.javaparser.ast.expr.SimpleName (line 1,col 41)-(line 1,col 46)
class com.github.javaparser.ast.expr.SimpleName (line 1,col 48)-(line 1,col 50)
class com.github.javaparser.ast.expr.SimpleName (line 1,col 52)-(line 1,col 58)
class com.github.javaparser.ast.expr.IntegerLiteralExpr (line 1,col 60)-(line 1,col 60)
public class A {

public static final int ID = 100;

public static void main(String[] args) {
    System.out.println(1);
}

}

class com.github.javaparser.ast.CompilationUnit (line 1,col 1)-(line 8,col 3)
class com.github.javaparser.ast.body.ClassOrInterfaceDeclaration (line 1,col 1)-(line 8,col 1)
class com.github.javaparser.ast.Modifier (line 1,col 1)-(line 1,col 6)
class com.github.javaparser.ast.expr.SimpleName (line 1,col 14)-(line 1,col 14)
class com.github.javaparser.ast.body.FieldDeclaration (line 3,col 5)-(line 3,col 37)
class com.github.javaparser.ast.Modifier (line 3,col 5)-(line 3,col 10)
class com.github.javaparser.ast.Modifier (line 3,col 12)-(line 3,col 17)
class com.github.javaparser.ast.Modifier (line 3,col 19)-(line 3,col 23)
class com.github.javaparser.ast.body.VariableDeclarator (line 3,col 29)-(line 3,col 36)
class com.github.javaparser.ast.type.PrimitiveType (line 3,col 25)-(line 3,col 27)
class com.github.javaparser.ast.expr.SimpleName (line 3,col 29)-(line 3,col 30)
class com.github.javaparser.ast.expr.IntegerLiteralExpr (line 3,col 34)-(line 3,col 36)
class com.github.javaparser.ast.body.MethodDeclaration (line 5,col 5)-(line 7,col 5)
class com.github.javaparser.ast.Modifier (line 5,col 5)-(line 5,col 10)
class com.github.javaparser.ast.Modifier (line 5,col 12)-(line 5,col 17)
class com.github.javaparser.ast.expr.SimpleName (line 5,col 24)-(line 5,col 27)
class com.github.javaparser.ast.body.Parameter (line 5,col 29)-(line 5,col 41)
class com.github.javaparser.ast.type.ArrayType (line 5,col 29)-(line 5,col 36)
class com.github.javaparser.ast.type.ClassOrInterfaceType (line 5,col 29)-(line 5,col 34)
class com.github.javaparser.ast.expr.SimpleName (line 5,col 29)-(line 5,col 34)
class com.github.javaparser.ast.expr.SimpleName (line 5,col 38)-(line 5,col 41)
class com.github.javaparser.ast.type.VoidType (line 5,col 19)-(line 5,col 22)
class com.github.javaparser.ast.stmt.BlockStmt (line 5,col 44)-(line 7,col 5)
class com.github.javaparser.ast.stmt.ExpressionStmt (line 6,col 9)-(line 6,col 30)
class com.github.javaparser.ast.expr.MethodCallExpr (line 6,col 9)-(line 6,col 29)
class com.github.javaparser.ast.expr.FieldAccessExpr (line 6,col 9)-(line 6,col 18)
class com.github.javaparser.ast.expr.NameExpr (line 6,col 9)-(line 6,col 14)
class com.github.javaparser.ast.expr.SimpleName (line 6,col 9)-(line 6,col 14)
class com.github.javaparser.ast.expr.SimpleName (line 6,col 16)-(line 6,col 18)
class com.github.javaparser.ast.expr.SimpleName (line 6,col 20)-(line 6,col 26)
class com.github.javaparser.ast.expr.IntegerLiteralExpr (line 6,col 28)-(line 6,col 28)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment