11package io .ebean .querybean .generator ;
22
3- import io .ebean .annotation .DbArray ;
4- import io .ebean .annotation .DbJson ;
5- import io .ebean .annotation .DbJsonB ;
6-
73import javax .annotation .processing .Messager ;
84import javax .annotation .processing .ProcessingEnvironment ;
95import javax .lang .model .SourceVersion ;
6+ import javax .lang .model .element .AnnotationMirror ;
107import javax .lang .model .element .Element ;
118import javax .lang .model .element .ElementKind ;
129import javax .lang .model .element .Modifier ;
1815import javax .lang .model .util .ElementFilter ;
1916import javax .lang .model .util .Elements ;
2017import javax .lang .model .util .Types ;
21- import javax .persistence .Embeddable ;
22- import javax .persistence .Entity ;
23- import javax .persistence .Inheritance ;
24- import javax .persistence .MappedSuperclass ;
2518import javax .tools .Diagnostic ;
26- import java .lang .annotation .Annotation ;
2719import java .util .ArrayList ;
2820import java .util .List ;
2921import java .util .Set ;
@@ -45,13 +37,13 @@ class ProcessingContext implements Constants {
4537
4638 private final PropertyTypeMap propertyTypeMap = new PropertyTypeMap ();
4739
48- ProcessingContext (ProcessingEnvironment processingEnv , String generatedSources ) {
49- this .generatedSources = generatedSources ;
40+ ProcessingContext (ProcessingEnvironment processingEnv ) {
5041 this .typeUtils = processingEnv .getTypeUtils ();
5142 this .messager = processingEnv .getMessager ();
5243 this .elementUtils = processingEnv .getElementUtils ();
5344 boolean jdk8 = processingEnv .getSourceVersion ().compareTo (SourceVersion .RELEASE_8 ) <= 0 ;
5445 this .generatedAnnotation = generatedAnnotation (jdk8 );
46+ this .generatedSources = initGeneratedSources (processingEnv );
5547 }
5648
5749 private String generatedAnnotation (boolean jdk8 ) {
@@ -61,25 +53,13 @@ private String generatedAnnotation(boolean jdk8) {
6153 return isTypeAvailable (GENERATED_9 ) ? GENERATED_9 : null ;
6254 }
6355
64- private boolean isTypeAvailable (String canonicalName ) {
65- return null != elementUtils .getTypeElement (canonicalName );
56+ private String initGeneratedSources (ProcessingEnvironment processingEnv ) {
57+ String generatedDir = processingEnv .getOptions ().get ("kapt.kotlin.generated" );
58+ return (generatedDir != null ) ? generatedDir : "target/generated-sources/kapt/compile" ;
6659 }
6760
68- /**
69- * Find the annotation searching the inheritance hierarchy.
70- */
71- <A extends Annotation > A findAnnotation (TypeElement element , Class <A > anno ) {
72-
73- final A annotation = element .getAnnotation (anno );
74- if (annotation != null ) {
75- return annotation ;
76- }
77- final TypeMirror typeMirror = element .getSuperclass ();
78- if (typeMirror .getKind () == TypeKind .NONE ) {
79- return null ;
80- }
81- final TypeElement element1 = (TypeElement )typeUtils .asElement (typeMirror );
82- return findAnnotation (element1 , anno );
61+ private boolean isTypeAvailable (String canonicalName ) {
62+ return null != elementUtils .getTypeElement (canonicalName );
8363 }
8464
8565 /**
@@ -148,30 +128,49 @@ private boolean isStaticOrTransient(VariableElement field) {
148128 return (modifiers .contains (Modifier .STATIC ) || modifiers .contains (Modifier .TRANSIENT ));
149129 }
150130
131+ private static boolean hasAnnotations (Element element , String ... annotations ) {
132+ return getAnnotation (element , annotations ) != null ;
133+ }
134+
135+ private static AnnotationMirror getAnnotation (Element element , String ... annotations ) {
136+ if (element == null ) {
137+ return null ;
138+ }
139+ for (AnnotationMirror annotationMirror : element .getAnnotationMirrors ()) {
140+ final String name = annotationMirror .getAnnotationType ().asElement ().toString ();
141+ for (String annotation : annotations ) {
142+ if (annotation .equals (name )) {
143+ return annotationMirror ;
144+ }
145+ }
146+ }
147+ return null ;
148+ }
149+
151150 private boolean isMappedSuperOrInheritance (Element mappedSuper ) {
152- return mappedSuper .getAnnotation (MappedSuperclass .class ) != null
153- || mappedSuper .getAnnotation (Inheritance .class ) != null ;
151+ return hasAnnotations (mappedSuper , MAPPED_SUPERCLASS , INHERITANCE );
154152 }
155153
156154 private boolean isEntityOrEmbedded (Element mappedSuper ) {
157- return mappedSuper != null
158- && (mappedSuper .getAnnotation (Entity .class ) != null
159- || mappedSuper .getAnnotation (Embeddable .class ) != null );
155+ return hasAnnotations (mappedSuper , ENTITY , EMBEDDABLE );
156+ }
157+
158+ boolean isEntity (Element element ) {
159+ return hasAnnotations (element , ENTITY );
160160 }
161161
162162 /**
163163 * Return true if it is a DbJson field.
164164 */
165165 private static boolean dbJsonField (Element field ) {
166- return (field .getAnnotation (DbJson .class ) != null
167- || field .getAnnotation (DbJsonB .class ) != null );
166+ return hasAnnotations (field , DBJSON , DBJSONB );
168167 }
169168
170169 /**
171170 * Return true if it is a DbArray field.
172171 */
173172 private static boolean dbArrayField (Element field ) {
174- return (field . getAnnotation ( DbArray . class ) != null );
173+ return hasAnnotations (field , DBARRAY );
175174 }
176175
177176 PropertyType getPropertyType (VariableElement field ) {
@@ -248,18 +247,18 @@ private PropertyType createPropertyTypeAssoc(String fullName) {
248247
249248 String [] split = Split .split (fullName );
250249 String propertyName = "QAssoc" + split [1 ];
251- String packageName = packageAppend (split [0 ], "query.assoc" );
250+ String packageName = packageAppend (split [0 ]);
252251 return new PropertyTypeAssoc (propertyName , packageName );
253252 }
254253
255254 /**
256255 * Prepend the package to the suffix taking null into account.
257256 */
258- private String packageAppend (String origPackage , String suffix ) {
257+ private String packageAppend (String origPackage ) {
259258 if (origPackage == null ) {
260- return suffix ;
259+ return "query.assoc" ;
261260 } else {
262- return origPackage + "." + suffix ;
261+ return origPackage + "." + "query.assoc" ;
263262 }
264263 }
265264
0 commit comments