javassist: issue with getMethods()

38 Views Asked by At

I've been trying to learn javassist to be used in a personal project. I am on JDK 11. However, when I attempt to read a class using ctClass.getMethod(), I get a RuntimeException that says it cannot find the file, yet I have no toruble doing ClassPool.getDefault(). This is my code for right now:

public static void main(String[] args) {

  String base = [absolute path to my project];
        
  ClassPool pool = ClassPool.getDefault();

  pool.insertClassPath(base);
        
  CtClass ctClass = pool.get(".src.main.java.Fibonacci");

  CtMethod[] ctMethods = ctClass.getMethods();

}

This is what Fibonacci looks like.

public class Fibonacci {

    public int Fib(int i) {
        if (i <= 1) return 1;
        return Fib(i - 1) + Fib(i - 2);
    }
}

This is the current exception that happens at CtMethod[] ctMethods = ctClass.getMethods();

Exception in thread "main" java.lang.RuntimeException: cannot find .src.main.java.Fibonacci: Fibonacci found in /src/main/java/Fibonacci.class
    at javassist.CtClassType.getClassFile3(CtClassType.java:208)
    at javassist.CtClassType.getClassFile2(CtClassType.java:173)
    at javassist.CtClassType.getInterfaces(CtClassType.java:763)
    at javassist.CtClassType.getMethods0(CtClassType.java:1161)
    at javassist.CtClassType.getMethods(CtClassType.java:1155)
    at Test_1.main(Test_1.java:18)

What am I doing wrong or what else do I have to include? Thank you.

I've made sure the Fibonacci.java has been compiled, I also tried other methods from ctClass such as ctClass.getDeclaredMethod("Fib"); which gives the same issue.

0

There are 0 best solutions below