Skip to content
This repository was archived by the owner on Oct 22, 2020. It is now read-only.

Commit aa27040

Browse files
committed
#15 - Remove provided dependency on Ebean
1 parent 3ae374b commit aa27040

6 files changed

Lines changed: 124 additions & 84 deletions

File tree

pom.xml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@
1919

2020
<dependencies>
2121

22-
<!-- Look for DbJson annotations -->
23-
<dependency>
24-
<groupId>io.ebean</groupId>
25-
<artifactId>ebean</artifactId>
26-
<version>11.41.1</version>
27-
<scope>provided</scope>
28-
</dependency>
29-
3022
<dependency>
3123
<groupId>org.avaje.composite</groupId>
3224
<artifactId>composite-testing</artifactId>

src/main/java/io/ebean/querybean/generator/Constants.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ interface Constants {
88

99
String GENERATED_9 = "javax.annotation.processing.Generated";
1010
String GENERATED_8 = "javax.annotation.Generated";
11+
12+
String MAPPED_SUPERCLASS = "javax.persistence.MappedSuperclass";
13+
String INHERITANCE = "javax.persistence.Inheritance";
14+
String ENTITY = "javax.persistence.Entity";
15+
String EMBEDDABLE = "javax.persistence.Embeddable";
16+
17+
String DBARRAY = "io.ebean.annotation.DbArray";
18+
String DBJSON = "io.ebean.annotation.DbJson";
19+
String DBJSONB = "io.ebean.annotation.DbJsonB";
20+
String DBNAME = "io.ebean.annotation.DbName";
21+
1122
String TQROOTBEAN = "io.ebean.typequery.TQRootBean";
1223
String TQASSOCBEAN = "io.ebean.typequery.TQAssocBean";
1324
String TQPROPERTY = "io.ebean.typequery.TQProperty";
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.ebean.querybean.generator;
2+
3+
import javax.lang.model.element.AnnotationMirror;
4+
import javax.lang.model.element.AnnotationValue;
5+
import javax.lang.model.element.ExecutableElement;
6+
import javax.lang.model.element.TypeElement;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.Set;
10+
11+
import static io.ebean.querybean.generator.Constants.DBNAME;
12+
13+
class FindDbName {
14+
15+
/**
16+
* Return the value of the DbName annotation or null if it isn't found on the element.
17+
*/
18+
static String value(TypeElement element) {
19+
20+
AnnotationMirror mirror = findDbNameMirror(element);
21+
if (mirror != null) {
22+
return readDbNameValue(mirror);
23+
}
24+
return null;
25+
}
26+
27+
private static String readDbNameValue(AnnotationMirror mirror) {
28+
29+
final Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues = mirror.getElementValues();
30+
final Set<? extends Map.Entry<? extends ExecutableElement, ? extends AnnotationValue>> entries = elementValues.entrySet();
31+
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : entries) {
32+
if ("value".equals(entry.getKey().getSimpleName().toString())) {
33+
return (String) entry.getValue().getValue();
34+
}
35+
}
36+
return null;
37+
}
38+
39+
private static AnnotationMirror findDbNameMirror(TypeElement element) {
40+
final List<? extends AnnotationMirror> mirrors = element.getAnnotationMirrors();
41+
for (AnnotationMirror mirror : mirrors) {
42+
final String name = mirror.getAnnotationType().asElement().toString();
43+
if (DBNAME.equals(name)) {
44+
return mirror;
45+
}
46+
}
47+
return null;
48+
}
49+
}

src/main/java/io/ebean/querybean/generator/ProcessingContext.java

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package io.ebean.querybean.generator;
22

3-
import io.ebean.annotation.DbArray;
4-
import io.ebean.annotation.DbJson;
5-
import io.ebean.annotation.DbJsonB;
6-
73
import javax.annotation.processing.Messager;
84
import javax.annotation.processing.ProcessingEnvironment;
95
import javax.lang.model.SourceVersion;
6+
import javax.lang.model.element.AnnotationMirror;
107
import javax.lang.model.element.Element;
118
import javax.lang.model.element.ElementKind;
129
import javax.lang.model.element.Modifier;
@@ -18,12 +15,7 @@
1815
import javax.lang.model.util.ElementFilter;
1916
import javax.lang.model.util.Elements;
2017
import 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;
2518
import javax.tools.Diagnostic;
26-
import java.lang.annotation.Annotation;
2719
import java.util.ArrayList;
2820
import java.util.List;
2921
import 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

src/main/java/io/ebean/querybean/generator/Processor.java

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
import javax.lang.model.SourceVersion;
77
import javax.lang.model.element.Element;
88
import javax.lang.model.element.TypeElement;
9-
import javax.persistence.Embeddable;
10-
import javax.persistence.Entity;
119
import java.util.LinkedHashSet;
1210
import java.util.Set;
1311

1412
/**
1513
* Process compiled entity beans and generates 'query beans' for them.
1614
*/
17-
public class Processor extends AbstractProcessor {
15+
public class Processor extends AbstractProcessor implements Constants {
1816

1917
private static final String GENERATE_KOTLIN_CODE_OPTION = "generate.kotlin.code";
2018
private static final String KAPT_KOTLIN_GENERATED_OPTION = "kapt.kotlin.generated";
2119

20+
private ProcessingContext processingContext;
21+
2222
public Processor() {
2323
}
2424

@@ -34,14 +34,15 @@ public Set<String> getSupportedOptions() {
3434
@Override
3535
public synchronized void init(ProcessingEnvironment processingEnv) {
3636
super.init(processingEnv);
37+
this.processingContext = new ProcessingContext(processingEnv);
3738
}
3839

3940
@Override
4041
public Set<String> getSupportedAnnotationTypes() {
4142

4243
Set<String> annotations = new LinkedHashSet<>();
43-
annotations.add(Entity.class.getCanonicalName());
44-
annotations.add(Embeddable.class.getCanonicalName());
44+
annotations.add(ENTITY);
45+
annotations.add(EMBEDDABLE);
4546
return annotations;
4647
}
4748

@@ -53,42 +54,29 @@ public SourceVersion getSupportedSourceVersion() {
5354
@Override
5455
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
5556

56-
int entityCount = 0;
57-
int embeddableCount = 0;
58-
59-
//processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "options[" + processingEnv.getOptions() + "]");
60-
61-
String generatedDir = processingEnv.getOptions().get("kapt.kotlin.generated");
62-
if (generatedDir == null) {
63-
generatedDir = "target/generated-sources/kapt/compile";
64-
}
65-
66-
ProcessingContext context = new ProcessingContext(processingEnv, generatedDir);
67-
68-
for (Element element : roundEnv.getElementsAnnotatedWith(Entity.class)) {
69-
generateQueryBeans(context, element);
70-
entityCount++;
71-
}
57+
int count = 0;
7258

73-
for (Element element : roundEnv.getElementsAnnotatedWith(Embeddable.class)) {
74-
generateQueryBeans(context, element);
75-
embeddableCount++;
59+
for (TypeElement annotation : annotations) {
60+
for (Element element : roundEnv.getElementsAnnotatedWith(annotation)) {
61+
generateQueryBeans(element);
62+
count++;
63+
}
7664
}
7765

78-
if (entityCount > 0 || embeddableCount > 0) {
79-
context.logNote("Generated query beans for [" + entityCount + "] entities [" + embeddableCount + "] embeddable");
66+
if (count > 0) {
67+
processingContext.logNote("Generated " + count + " query beans");
8068
}
8169

8270
return true;
8371
}
8472

85-
private void generateQueryBeans(ProcessingContext context, Element element) {
73+
private void generateQueryBeans(Element element) {
8674
try {
87-
SimpleQueryBeanWriter beanWriter = new SimpleQueryBeanWriter((TypeElement) element, context);
75+
SimpleQueryBeanWriter beanWriter = new SimpleQueryBeanWriter((TypeElement) element, processingContext);
8876
beanWriter.writeRootBean();
8977
beanWriter.writeAssocBean();
9078
} catch (Exception e) {
91-
context.logError(element, "Error generating query beans: " + e);
79+
processingContext.logError(element, "Error generating query beans: " + e);
9280
}
9381
}
9482
}

src/main/java/io/ebean/querybean/generator/SimpleQueryBeanWriter.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package io.ebean.querybean.generator;
22

33

4-
import io.ebean.annotation.DbName;
5-
64
import javax.lang.model.element.TypeElement;
75
import javax.lang.model.element.VariableElement;
8-
import javax.persistence.Entity;
96
import java.io.File;
107
import java.io.FileWriter;
118
import java.io.IOException;
@@ -62,6 +59,7 @@ class SimpleQueryBeanWriter {
6259

6360
private final ProcessingContext processingContext;
6461

62+
private final boolean isEntity;
6563
private final String dbName;
6664
private final String beanFullName;
6765
private final LangAdapter langAdapter;
@@ -81,12 +79,15 @@ class SimpleQueryBeanWriter {
8179
this.generatedSourcesDir = processingContext.generatedSourcesDir();
8280
this.element = element;
8381
this.processingContext = processingContext;
84-
85-
DbName name = processingContext.findAnnotation(element, DbName.class);
86-
this.dbName = (name == null) ? null : name.value();
8782
this.beanFullName = element.getQualifiedName().toString();
8883
this.destPackage = derivePackage(beanFullName) + ".query";
8984
this.shortName = deriveShortName(beanFullName);
85+
this.isEntity = processingContext.isEntity(element);
86+
this.dbName = findDbName();
87+
}
88+
89+
private String findDbName() {
90+
return FindDbName.value(element);
9091
}
9192

9293
private LangAdapter lang() {
@@ -165,7 +166,7 @@ private void translateKotlinImportTypes() {
165166
}
166167

167168
private boolean isEntity() {
168-
return element.getAnnotation(Entity.class) != null;
169+
return isEntity;
169170
}
170171

171172
/**

0 commit comments

Comments
 (0)