Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Problems with the ISourceRange of the IType of an anonymous class
Problems with the ISourceRange of the IType of an anonymous class [message #1835202] Fri, 27 November 2020 04:55 Go to next message
Mohammad Rafid Ul Islam is currently offline Mohammad Rafid Ul IslamFriend
Messages: 9
Registered: August 2019
Junior Member
When I get the ISourceRange for an AnonymousClassDeclaration with the following code snippet, I am getting the startposition and length of the ClassInstanceCreation which is the parent of the AnonymousClassDecalration.

AnonymousClassDeclaration anonClassDec = (AnonymousClassDeclaration) node;
ITypeBinding binding = anonClassDec.resolveBinding();
IType type  = (IType) binding.getJavaElement();

try {
         ISourceRange sourceRange = type.getSourceRange();
         return new Pair(sourceRange.getOffset(), sourceRange.getLength());
} catch (Exception e) {
       e.printStackTrace();
}


Here, Pair is a custom class that stores two objects/primitives.

Is it intended to be this way? Or is it a bug?

The test case:
public class MyClass {
    String s = "haha";
    public void main(String args[]) {
        M m = new M() {
          @Override
          public String foo() {
              return s;
          }
        };
    }
}


Using JDT core 3.17

[Updated on: Fri, 27 November 2020 05:03]

Report message to a moderator

Re: Problems with the ISourceRange of the IType of an anonymous class [message #1835205 is a reply to message #1835202] Fri, 27 November 2020 06:11 Go to previous messageGo to next message
Jay Arthanareeswaran is currently offline Jay ArthanareeswaranFriend
Messages: 128
Registered: July 2009
Senior Member
When you request the model for the corresponding element, it can only return the type declaration and not the class body declaration. On the other hand, if you request for the source start of the AnonymousClassDeclaration, you do get the correct range.
So, looks like this is expected behavior.
Re: Problems with the ISourceRange of the IType of an anonymous class [message #1835282 is a reply to message #1835205] Mon, 30 November 2020 05:25 Go to previous message
Mohammad Rafid Ul Islam is currently offline Mohammad Rafid Ul IslamFriend
Messages: 9
Registered: August 2019
Junior Member
Thanks for clarifying that.
Previous Topic:How to disable parameters being added as null?
Next Topic:Eclipse suggestion pop-ups bug
Goto Forum:
  


Current Time: Fri Mar 29 02:02:24 GMT 2024

Powered by FUDForum. Page generated in 0.02314 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top