Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Type and Method binding not working properly in some cases
Type and Method binding not working properly in some cases [message #1811010] Fri, 23 August 2019 14:11 Go to next message
Mohammad Rafid Ul Islam is currently offline Mohammad Rafid Ul IslamFriend
Messages: 9
Registered: August 2019
Junior Member
hello,
I am trying to build a call graph from source and I ran into two different scenarios.
First scenario:
I have multiple classes in one Java file. One of them is public and the others are without any access modifiers. Though it is not a good practice, it is syntactically correct. Also, I have another java file with a single public class. This class has a method named bar() and it invokes a method from one of the non-public classes of the other file. In this case, when I try to get the method binfding or the type binding of this method invocation or class instance creator, I get RecoveredTypeBinding instead of the source binding. And, I can't get any source data like ISourceRange or CompilationUnit from it.

Test case for this scenario:
I have a project say, ProjectA. Then it has two .java files one Main.java and other MyClass.java. in Main.java, I have one Main public class and other A, B, C, and D classes without any modifiers.
class A {
    
    public A foo(A x) {
        return x;
    }
}
class B extends A {
	public A foo(A x) {
        return new D();
    }
}
class C extends A {
	public A foo(A x) {
        D d = new D();
        int i = 1;
        d.foo(x, i);
        return this;
    }
}
class D extends A {
	public A foo(A x) {
        A b = new B();
        b.foo(x);
        return new A();
    }
    
	public A foo(A x, int i) {
        return x;
    }
}
public class Main {
    public static void main(String[] args) {
        A x = new A();
        int i = 10;
        while(i>0) {
            x = x.foo(new B()); /*<<<<<8,12,12,12, b*/
            i--;
        }
        A y = new C();
        y.foo(x);
        MyClass mc = new MyClass();
        mc.bar();
        System.out.println("END");
    }
}

and In MyClass.java
public class MyClass{
    int haha;
    
    public int bar() {
        A x = new D();
        x.foo(new C());
        return haha;
    }
}


Now, I have a method say,
getTypeDecOfClassInstance()
where I am iterating through every .java file in the project and visiting the ClassInstanceCreations it has. I am trying to get the binding of these ClassInstanceCreations, but getting RecoveredTypeBinding when I visit
new D()
and
new C()
inside the bar() method of MyClass instead of SourceTypeBinding. And I can't get ISourceRange or ICompilationUnit for them instead getting null.

Second scenario:
I have a project that has separate sub projects in different directories. And there are dependencies between projects described by pom.xml file. But when I try to build a call-graph out of it, I always get 0 callers for each method invocation.
I also tried to get the binding of the supertypes of each class and in this case, if the supertype is in another subproject, I sometimes get the source binding and sometimes get the RecoveredTypeBinding from which I can't get any source data like the above case.

Test case for this scenario:
I have a project say, ProjectA. Then it has two sub projects named, SubProjectA and SubProjectB.
In SubProjectA, I have A, B, C, D, and Main classes in different files.
public class A {
    
    public A foo(A x) {
        return x;
    }
}
public class B extends A {
	public A foo(A x) {
        return new D();
    }
}
public class C extends A {
	public A foo(A x) {
        D d = new D();
        int i = 1;
        d.foo(x, i);
        return this;
    }
}
public class D extends A {
	public A foo(A x) {
        A b = new B();
        b.foo(x);
        return new A();
    }
    
	public A foo(A x, int i) {
        return x;
    }
}
public class Main {
    public static void main(String[] args) {
        A x = new A();
        int i = 10;
        while(i>0) {
            x = x.foo(new B()); /*<<<<<8,12,12,12, b*/
            i--;
        }
        A y = new C();
        y.foo(x);
        MyClass mc = new MyClass();
        mc.bar();
        System.out.println("END");
    }
}

and in SubProjectB, I have MyClass.java
public class MyClass{
    int haha;
    
    public int bar() {
        A x = new D();
        x.foo(new C());
        return haha;
    }
}


Again, I am having the same problem with my
getTypeDecOfClassInstance()
method.

Thanks in Advance.

[Updated on: Fri, 30 August 2019 04:59]

Report message to a moderator

Re: Type and Method binding not working properly in some cases [message #1811125 is a reply to message #1811010] Tue, 27 August 2019 08:16 Go to previous messageGo to next message
Manoj N Palat is currently offline Manoj N PalatFriend
Messages: 19
Registered: October 2014
Junior Member
Hi,
Can you please provide a reproducible test case - that includes the a) the program and b) the input program.

Regards,
Manoj


Manoj N Palat
Eclipse Java Development Tools (JDT).
Re: Type and Method binding not working properly in some cases [message #1811276 is a reply to message #1811125] Fri, 30 August 2019 05:02 Go to previous messageGo to next message
Mohammad Rafid Ul Islam is currently offline Mohammad Rafid Ul IslamFriend
Messages: 9
Registered: August 2019
Junior Member
I have added test cases and some information about which method I am having the problem with. Please let me know if you have any insight into what is causing the problem.
Thanks!
Re: Type and Method binding not working properly in some cases [message #1814014 is a reply to message #1811276] Sun, 01 September 2019 13:21 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
It may depend on how you start lookup of bindings. If you pass only MyClass.java into the process, then JDT has no idea where to look for the secondary types (A-D), because there is no A.java etc.
This problem should vanish, if you explicitly pass Main.java, too. Can you please try?
Re: Type and Method binding not working properly in some cases [message #1814028 is a reply to message #1814014] Mon, 02 September 2019 05:52 Go to previous messageGo to next message
Mohammad Rafid Ul Islam is currently offline Mohammad Rafid Ul IslamFriend
Messages: 9
Registered: August 2019
Junior Member
Adding the classpath worked. But I actually want to run my code on bigger projects found on the git. They don't have classpath in their projects. So if I want to run my program on these projects and don't add the classpaths manually, how should I do it?
Re: Type and Method binding not working properly in some cases [message #1814048 is a reply to message #1814028] Mon, 02 September 2019 10:11 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33264
Registered: July 2009
Senior Member
I'm not the expert here, but if you think about it, you cannot expect javac to work unless you tell it where to find all the *.java/*.class files that are needed to compile the sources. So somehow you will need to synthesize a class path so that this processing you are doing has a scope in which to work in order to resolve imports...

Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Hot Swap
Next Topic:IMethod.getParameterTypes() - Source vs Binary method signatures
Goto Forum:
  


Current Time: Sat Dec 14 13:34:30 GMT 2024

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

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

Back to the top