Current position:  Home > Default > Can't find class

Can't find class

Time:November 30
A have made an annotation processing test class using netbeans 6 and jsr 269
public class CustoWizardAnnotationsTest extends AbstractProcessor {
public boolean process(Set<? extends TypeElement> typeElements, RoundEnvironment roundEnv) {
This is working, but I have a problem using it, I tried 3 different scenarios:
1) add the test class to the project which it is tested on. The problem is you can't compile the project anymore because the test is not available anymore after a full clean. I can't find an option to tell the project to compile the testclass first and then start using it.
2) Make a separate project for the testclass. The problem is that I need some classes from the main project in the test, so I need to add the classes from the main project as a library. Same problem here after a clean of both projects, the testclass can't compile as the libraries of the main project are not available, and the main project can't compile as the testclass is not available.
3) same as option 2, but now using the java source files as a library. Now I still can compile the testclass when the main project was cleaned, but then another problem occurs. As the testclass is compiling some libraries, it will keep using them, thus resolving in sometimes using too old libraries for running the tests on. It can be solved by recompiling the testclass every time before the main project is compiled, but this doesn't seem a very good solution to me...
Can anybody help me out of this problem?
The main project however does not have a dependency on the testclass, but it seems the testclass needs to be compiled before the main project can be compiled, as that class is used during compile time, and that's the problem I'm encountering.
I hope this provides some more information?You might to need to explain why that bit happens. What error messages are you getting when you compile?
But you might not if these guesses at what is happening lead you in the right direction.
*1* It might be that you are using the autodiscovery mechanism, and your META-INF/services/javax.annotation.processing.Processor file (containing the fully qualified name of your Processor) is in the src tree while the Processor itself is in the test tree, which would mean javac would look for your processor when it builds the main part of the project.
*2* Another scenario which I have encountered, which might be what you are seeing is a sort of bootstrapping catch22. This occurs where you have a META-INF/services/javax.annotation.processing.Processor file pointing to your processor, and they are both in the src tree. It can happen that the META-INF file is also on the classpath (in the build/classes dir as a result of a previous build) so when compiling the processor, it tries to run that same processor but can't find it because it hasn't yet been successfully compiled. A CLEAN_AND_BUILD will normally bypass this issue each time it occurs. If this is biting you often, it might be best to delay coding the META-INF file till the processor is working correctly (or just temporarily comment out the name of your processor in that file) - just use the explicit processor option on javac command line when testing it.
*3* Yet another scenario might be that you are trying to compile the enum, annotation, processor and all the code that uses the enum/annotation pair all at the same time, and you are expecting the processor to run as part of that compilation. This is not going to work at least after a clean.
You will need to build the processor before compiling the source that uses the enum/annotation. So that's 2 projects. Where you put the enum and annotation is up to you, but if you put them in the main project, you'll need to write your processor so that it doesn't have a dependency on them.
That means you shouldn't use Element.getAnnotation(Class) method, because you can't refer to the annotation class. You can use Element.getAnnotationMirrors() to obtain a model of the annotation and use Names to find it which breaks your dependency on the annotation. (There is an easier way which I can explain in a separate post if need be - just ask).