What its the first, the annotated class (egg) or used class (chicken)?

110 views Asked by At

certainly I have not read something fundamental, and it seems very strange, but I wonder.

Suppose you use

@SharedPref
public interface SharedPreferencesInterface {
    @DefaultBoolean(true)
    boolean showDeviceName();

I have the IDE (idea) configured with Gradle, and I generated the SharedPreferencesInterface_ class that I can use in another class as

@Pref
SharedPreferencesInterface_ prefs;

But suppose someone now download the project, how can the use? Because the class where used SharedPreferencesInterface_ not compile because the class does not exist, and the class does not exist because compilation errors ...

How it's made? Surely there is a way ... configured to compile certain classes first?

Help is appreciated.

A greeting.

1

There are 1 answers

5
WonderCsabo On BEST ANSWER

But suppose someone now download the project, how can the use? Because the class where used SharedPreferencesInterface_ not compile because the class does not exist, and the class does not exist because compilation errors ...

This is the same situation when you compile a project in a full build (when no classes are generated yet). Actually Gradle always does a full build currently in Android projects. No configuration is needed at all in addition to the standard AndroidAnnotaions config.

Actually this works because the compiler does not fully compiles your class before passing it to annotations processing. It is clear it should not to, because the class may reference generated classes, which are only available after the processing. So first the compiler creates a model of the classes, only parses the structure of the them (fields, methods, return types, parameter types, etc), but not the implementations. Also it allows missing types even on fields. If it finds a missing type, it assigns to TypeKind.ERROR, but the name of the type is still available for the annotation processor. After the processor is done, it generates the missing class, so the kind of the class is no longer TypeKind.ERROR, and the compilation can succeed.