Java – Reason for a reflection error

eclipsejavareflection

I’m working on an Eclipse plug-in project.

Using this plug-in, users can create Eclipse Java projects with some specificities. For example, they can add Java classes’ names which will be saved in a file. These Java classes can be created on the src of the project or used from a jar file which must be added to the project classpath. In this case, the plug-in will use reflection to get some data from each class.

There are two different test cases that give the same error because the plug-in can't find the class to instantiate:

  • A jar contain a class having a name saved in the file is not added to the project classpath. So in this case the classpath is incomplete.

  • The user of the plug-in updated a jar in which its old version of the jar contained the named class, but the new version of jar does not (which could happen if class was deleted from the new version of the jar). In this case, the plug-in will not find the class but the classpath is complete.

So The plug-in must differentiate between the two test cases when it fails finding the class name using reflection.

How that can be done?

Best Answer

The only way I see for you to achieve this is to force the user to declare among the class name if the class is part of the project or part of the classpath and use that info to give the error if the reflexion fails to find the class.

I don't believe there is any other way to do this since the project it self will be part of the "classpath" and the classloader treats every class as equal

Related Topic