vendredi 31 mars 2023

Is it technically possible for Intellij to statically know which exact class' method will be invoked on java.lang.Method m.invoke(instance) call

Let's say I have the following ReflectionTest.java file:

import java.lang.Class;
import java.lang.reflect.*;

public class ReflectionTest {
public static void main(String[] args) {
    try {

        // create an object of Dog
        Dog d1 = new Dog();

        // create an object of Class
        // using getClass()
        Class obj = d1.getClass();

        // using object of Class to
        // get all the declared methods of Dog
        Method[] methods = obj.getDeclaredMethods();

        // create an object of the Method class
        for (Method m : methods) {

            // get names of methods
            System.out.println("Method Name: " + m.getName());

            // get the access modifier of methods
            int modifier = m.getModifiers();
            System.out.println("Modifier: " + Modifier.toString(modifier));

            // get the return types of method
            System.out.println("Return Types: " + m.getReturnType());

            // invoke the method
            m.invoke(d1);
            System.out.println(" ");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

I am wondering, technically speaking, can a static analysis tool figure out, in 100% of the cases, which actual method of which specific class will be invoked by m.invoke(d1); and navigate me there? Kind of like the functionality IntelliJ already provides by holding the Cmd key and clicking with the left button of the mouse on a method - you get redirected to the actual method's implementation or get asked to choose one of all implementations in working with an interface, for example.

I am mostly interested in actual theoretical language level explanation as to what is possible and what not and why.





Aucun commentaire:

Enregistrer un commentaire