How to determine all temporary (but not static) dependencies of a Java source by reading the code?

Let's say I wanted to say that this Java source folder contains all the source code needed to run any of the programs contained in the source folder. Just showing that compiling the entire source folder will be insufficient because maybe some of these programs use reflection to create objects. That way, I was able to search all over the code and search for calls newInstance()to find out which classes should be present at runtime. But what about calls Class.forName(...)that don't participate in calls on newInstance()? Better check them out. But how many such things do I need to check?

Is there any exhaustive list that I could advise to make sure I look at every Java way so that such a run-time dependency can be introduced? Revised, is there some list of operations, so if I can show that no source in the source folder uses these operations (and this folder compiles), that all (code) dependencies are present?

If such a list does not exist, can we run it in this thread and make every effort to cover all the operations that we know about?

Edit

I would like to narrow the question a bit. What interests me the most is that when compiling the code base, all the dependencies are present. It seems to me that I need to first compile the code base, and then check if any of the code ever calls certain methods (for example, newInstance) that may introduce a run-time dependency. If no such methods are found, I am sure that all the required code is present in the original form, and running the program will not lead to creation ClassNotFoundException.

+4
source share
3 answers

Answer to the original question

There is no broad way. You can do this as far as I know. I mean the following code:

public static Object calleableFromAnywhere(Object o) throws IllegalAccessException, InstantiationException {
    Object ret = null;
    if(!Objects.isNull(o)){
        ret = o.getClass().newInstance();
    }
    return ret;
}

, .

, :

Constructor<?> constructor = o.getClass().getConstructor();
//there could be a lot of constructors enclosing different paramethers

Object something = o.getClass().newInstance();
//default constructor called

, ... .

, Class.forName(...) String, .

, String, . - , .

TL.DR.: , . newInstance, , . , 100%.

/

a newInstance . . ( , newInstance, .) , - , , .

, :

public static Class getClass(String name) throws ClassNotFoundException {
    return Class.forName(name);
}

name , . , .

public Class getClassExample1(String name) throws ClassNotFoundException {
    return this.getClass().getClassLoader().loadClass(name);
}

JavaDoc ClassNotFoundException :

  • @see java.lang.Class # forName (java.lang.String)
  • @see java.lang.ClassLoader # findSystemClass (java.lang.String)
  • @see java.lang.ClassLoader # loadClass (java.lang.String, boolean)

, , , someInstance.getClass(name).

NoClassDefFoundError. , , , maven, , .

TL.DR.V2: , , ( ), .

+3

, , , . , - , .

- , (, - URL- JDBC, JDBC). , , .

TL;DR: ,

+1

- , Java- , , , : AnnotationProcessing 101

+1
source

Source: https://habr.com/ru/post/1677723/


All Articles