Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6572413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:00:10+00:00 2026-05-25T15:00:10+00:00

I am currently working on an academic project which employs an ASTVisitor to create

  • 0

I am currently working on an academic project which employs an ASTVisitor to create a basic calltree.

For this purpose it is needed to associate the invocation of a method with its declaration.

EDIT: Problem is solved to a large extent: The described error appears only JUnit-Tests, but but not in the standalone-plugin. It seemingly has to do with the unit-testing workflow of ASTVisitors. I will further investigate the reason in some time and post the answer here.

The code below shows a minimal visitor which prints invocations and associated declarations to console:

import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.MethodInvocation;

/**
* Visits all method invocations and prints the declaration of the caller to
* console.
*/
public class InvocationLoggerASTVisitor extends ASTVisitor {

@Override
public boolean visit(MethodInvocation methodInvocation) {

if (methodInvocation.resolveMethodBinding() != null) {
    IMethodBinding declarationOfInvokedMethod = methodInvocation
    .resolveMethodBinding().getMethodDeclaration();

    System.out.println(String.format(
        "invocation of \"%s\" is resolved to declaration \"%s\"",
        methodInvocation, declarationOfInvokedMethod));

}
return super.visit(methodInvocation);
}

}

The visitor works for some tested scenarios.
But strangely, when applying it to compilation units which contain overloaded methods, some invocations get associated with wrong declarations. Lets visit the following code:

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashSet;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class OverloadedMethodsAndRawTypes implements ICallGraphTestSource {

void f(HashSet objects) {
f(new ArrayDeque(objects));
}

void f(ArrayDeque objects) {
f(new ArrayList(objects));
}

void f(ArrayList objects) {
f(new HashSet(objects));
}

}

When visiting this compilation unit, the console output is:

invocation of "f(new ArrayDeque(objects))" is resolved to declaration "void f(HashSet) "
invocation of "f(new ArrayList(objects))" is resolved to declaration "void f(HashSet )"
invocation of "f(new HashSet(list))" is resolved to declaration "void f(HashSet) "

I would expect this output:

invocation of "f(new ArrayDeque(objects))" is resolved to declaration "void f(ArrayDeque) "
invocation of "f(new ArrayList(objects))" is resolved to declaration "void f(ArrayList )"
invocation of "f(new HashSet(list))" is resolved to declaration "void f(HashSet) "

I’ve noticed, that invocations of overloaded methods always resolve to the first occurring declaration matching the invoked method name, which seems not right to me.

To prove, that method overloading is to blame, i attach the same code without method overloading:

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashSet;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class RawTypes implements ICallGraphTestSource {

void f_set(HashSet objects) {
    f_deque(new ArrayDeque(objects));
}

void f_deque(ArrayDeque objects) {
    f_list(new ArrayList(objects));
}

void f_list(ArrayList objects) {
    f_set(new HashSet(objects));
}

}

When visited, it yields the correct output:

invocation of "f_deque(new ArrayDeque(objects))" is resolved to declaration "void f_deque(ArrayDeque) "
invocation of "f_list(new ArrayList(objects))" is resolved to declaration "void f_list(ArrayList) "
invocation of "f_set(new HashSet(list))" is resolved to declaration "void f_set(HashSet) "

My ASTParser is configured as following:

public static ASTNode getAST(ICompilationUnit compilationUnit) {
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(compilationUnit);
parser.setResolveBindings(true);
parser.setBindingsRecovery(true);
parser.setProject(getJavaProject());
return parser.createAST(null);
}

EDIT: The described setup does not work in JUnit-Tests, but it does work as standalone-plugin. The reason must be therefor in the getJavaProject-Method, where a temporary project is created.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:00:11+00:00Added an answer on May 25, 2026 at 3:00 pm

    Since the erroneous behaviour only happens in test cases, the creation of the AST seems to be not set up properly.

    I found a testing setup which works:
    For each test I read the CompilationUnit as text from the file system and create a new project programmatically, following an approach derived from a description here.
    As you see in the ASTParser configuration in the original question, an IJavaProject-instance is passed using parser.setProject(IJavaProject), holding an environment to resolve invocations properly.

    This instance has to be configured with a classpath:

    /**
     * @param project
     *            a project with JavaNature
     * @return a java project with classpath set to the default JRELibrary
     * @throws JavaModelException
     */
    private static IJavaProject createJavaProject(IProject project)
        throws JavaModelException {
    
    IJavaProject javaProject = JavaCore.create(project);
    javaProject.setRawClasspath(PreferenceConstants.getDefaultJRELibrary(),
        null);
    
    return javaProject;
    }
    

    In my case I need only the default JRE-library. In your case there may be need to augment the classpath. In any case, this solves the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im currently working on a project which uses jqGrid with multiple subgrids. I'm trying
I am currently working on an academic project, developing in Java and XML .
Im currently working on a c# project that uses another .net library. This library
Currently working on a pretty basic hosted PHP app which I'm in the process
Currently working on database part of android project. The main aim of the project
Currently working on something which uses ajax for some pagination. What I'm looking to
Im currently working on troubleshooting an old job which is taking long in running
Im currently working on a site which will contain a products catalog. I am
Im currently working on a Python/Pygame module to wrap some basic sprite animation. Animation
Im currently working on a project that requires the following. I need to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.