Skip to content

EclipseClassNameMapping

marchof edited this page Aug 25, 2012 · 1 revision

To facilitate looking up coverage nodes for Java elements in eclipse we need to create a way to map from one type to another. This is made more difficult by the way Eclipse stores its type information. Types live within a compilation unit (a source file) and inner classes are contained within those types. Anonymous types are also visible from within type instances.

Example class names:

  • Secondary$1$1.class
  • Secondary$1.class
  • Secondary$2.class
  • Secondary.class
  • Structure$1.class
  • Structure$1Local.class
  • Structure$2Local.class
  • Structure$Foo$1$Bar.class
  • Structure$Foo$1.class
  • Structure$Foo.class
  • Structure.class

Created from this compilation unit:

public class Structure {
  Runnable r = new Runnable() {
    public void run() {
      System.err.println("primary runnable");
    }
  };

  public void blah() {
    class Local {
    }
  }

  public void blah2() {
    class Local {
    }
  }

  private class Foo {
    Runnable r = new Runnable() {
      public void run() {
        System.err.println("inner runnable");
      }
		
      class Bar {
      }
    };
  }
}

class Secondary {
  public Secondary() {
    Runnable r = new Runnable() {
      public void run() {
        System.err.println("anonymous runnable");
        new Runnable() {
          public void run() {
            System.err.println("runnable in runnable");
          }
        };
      }
    };
	 
    Runnable r2 = new Runnable() {
      public void run() {
        System.err.println("anonymous runnable 2");
      }
    };
  }
}

We should be able to easily may primary types (types where the name matches the compilation unit) by calling getType on the owning package fragment. We can then parse though the class name to lookup each type in turn, either by name, or by position if it is anonymous. This is possible because eclipse seem to provide unique class numbers within a type. Anonymous and local types will be tricky as they are contained within a method. We probably won't know the name of the enclosing method, so would have to scan though each method in the most enclosing type to find them

Finding methods should be fairly easy as we should be able to remember the enclosing type, which should reduce the search scope significantly.

Binary Types

Binary types are much easier as we can just call getClassFiles on the package to get a list of all classes. Each class will have exactly one type in it. The class file names will map directly to the name on disk.