SpoonLabs / gumtree-spoon-ast-diff

Computes the AST difference (aka edit script) between two Spoon Java source code abstract syntax trees
http://hal.archives-ouvertes.fr/hal-01054552
Apache License 2.0
161 stars 42 forks source link

Offsets are all zeros [0,0] #323

Closed pouryafard75 closed 1 month ago

pouryafard75 commented 1 month ago

Hello, I am just getting started with Spoon.

I am trying to add GT-Spoon as one of the tools for our DiffBenchmark. However, I stumbled upon a problem which is having zeros as all the offsets. Our benchmark is originally built for gumtree-jdt visitor but we want to find the eqv subtree for other visitors by relying on the offsets.

I have tried the ASTComperator with the annotation example, and tried to print the generated trees using the following code:

MappingStore mappingsComp = result.getMappingsComp();
        System.out.println(mappingsComp.src.toTreeString());
        System.out.println(mappingsComp.dst.toTreeString());

However the offsets are all shown as zero:

root [0,0]
    RootPac: unnamed package [0,0]
        Class: Test [0,0]
            Method: hashCode [0,0]
                RETURN_TYPE: int [0,0]
                Modifiers_Method [0,0]
                    Modifier: public [0,0]
                Annotation: java.lang.Override [0,0]
                Return: return [0,0]
                    UnaryOperator: NEG [0,0]
                        Literal: 1 [0,0]
root [0,0]
    RootPac: unnamed package [0,0]
        Class: Test [0,0]
            Method: hashCode [0,0]
                RETURN_TYPE: int [0,0]
                Modifiers_Method [0,0]
                    Modifier: public [0,0]
                Annotation: java.lang.Deprecated [0,0]
                Return: return [0,0]
                    UnaryOperator: NEG [0,0]
                        Literal: 1 [0,0]
algomaster99 commented 1 month ago

Hi! Thanks for the bug report! It seems that we don't support node position in the tree created using Gumtree APIs even though Gumtree provides with APIs such as .setPos and .setLength.

May I ask what kind of position are you interested in? Should it be line number-based or column-based?

pouryafard75 commented 1 month ago

I would like to have the exact offsets similar to the way that GT jdt visitor provides. [Startoffset-EndOffset]

algomaster99 commented 1 month ago

I believe I could add integers returned by these two APIs then. I understand that it gives the index of start and end character.

pouryafard75 commented 1 month ago

That would be perfect. I couldnt make it work because the position was from NoSourcePosition, but I am pretty sure you know how to deal with that.

algomaster99 commented 1 month ago

work because the position was from NoSourcePosition

That would be the case for implicit elements like default constructor, but for other elements, it should work. Gumtree tree based on Spoon should not contain implicit elements in my opinion. It is a good point and I will add a test for it.