Search code examples
rascal

M3 model@uses: src to innerclass


Is there an easy way to translate the "src" loc of model@uses to a class?

The problem I am having is I tried to use the model@declarations to match and find the class by location, but matching between file:// and project:// (m3 model) is not 100% pure (ignoring .scheme), comparing

begin.line >=

and

end.line <=

still results in 2 classes, when a "src" line is in the innerclass.

To summarise: Is there a function that returns the class, e.g.

loc classLoc = getClass( |home:///Workspaces/Rascal/src/Fruit.java|(150,1,<11,12>,<11,13>) );

That will return |java+class://Fruit|, having line 11 be a line in class Fruit.


Solution

  • Sure. Consider this example Java code:

    public abstract class Fruit {
    
        private class X {
    
        }
    
        int main() {
            X x = new X();
            return 1;
        }
    }
    

    and consider I stored the M3 model in m and take the use of X located at this source location l = |home:///Workspaces/Rascal/rascal/src/org/rascalmpl/courses/example-project/src/Fruit.java|(150,1,<11,12>,<11,13>);

    Then this expression will tell you to which declaration the class points to:

    rascal>cl = m@uses[l];
    set[loc]: {|java+class:///Fruit/X|}
    

    To find out in which other class this class is then nested we invert the containment relation and look up the parent of the nested class like so:

    rascal>m@containment<to,from>[cl]
    set[loc]: {|java+class:///Fruit|}