Package: ModelGenProcessor
ModelGenProcessor
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ModelGenProcessor() |
|
|
|
|
|
||||||||||||||||||||
getSupportedSourceVersion() |
|
|
|
|
|
||||||||||||||||||||
init(ProcessingEnvironment) |
|
|
|
|
|
||||||||||||||||||||
isAnnotatedWithNonEntity(Element) |
|
|
|
|
|
||||||||||||||||||||
isPropertyPersistent(Element) |
|
|
|
|
|
||||||||||||||||||||
process(Set, RoundEnvironment) |
|
|
|
|
|
Coverage
1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.modelgen;
19:
20:
21: import cz.cvut.kbss.jopa.modelgen.classmodel.Field;
22: import cz.cvut.kbss.jopa.modelgen.classmodel.MappingAnnotations;
23: import cz.cvut.kbss.jopa.modelgen.classmodel.MetamodelClass;
24:
25: import javax.annotation.processing.AbstractProcessor;
26: import javax.annotation.processing.Messager;
27: import javax.annotation.processing.ProcessingEnvironment;
28: import javax.annotation.processing.RoundEnvironment;
29: import javax.annotation.processing.SupportedAnnotationTypes;
30: import javax.annotation.processing.SupportedOptions;
31: import javax.lang.model.SourceVersion;
32: import javax.lang.model.element.AnnotationMirror;
33: import javax.lang.model.element.Element;
34: import javax.lang.model.element.TypeElement;
35: import javax.tools.Diagnostic;
36: import java.util.HashMap;
37: import java.util.List;
38: import java.util.Map;
39: import java.util.Set;
40:
41: /**
42: * Annotation processor that finds JOPA entities and mapped superclasses and generates a static metamodel based on
43: * them.
44: */
45: @SupportedAnnotationTypes({"cz.cvut.kbss.jopa.model.annotations.OWLClass",
46: "cz.cvut.kbss.jopa.model.annotations.MappedSuperclass"})
47: @SupportedOptions({
48: ModelGenProcessor.OUTPUT_DIRECTORY_PARAM,
49: ModelGenProcessor.SOURCE_PACKAGE_PARAM,
50: ModelGenProcessor.DEBUG_PARAM
51: })
52: public class ModelGenProcessor extends AbstractProcessor {
53: public static final String OUTPUT_DIRECTORY_PARAM = "outputDirectory";
54: public static final String SOURCE_PACKAGE_PARAM = "sourcePackage";
55: public static final String DEBUG_PARAM = "debugOption";
56: Messager messager;
57:
58: private Map<String, MetamodelClass> classes;
59:
60: private String sourcePackage;
61: private String outputDirectory;
62: private boolean debugOption;
63:
64: @Override
65: public void init(ProcessingEnvironment env) {
66: super.init(env);
67: this.messager = env.getMessager();
68: messager.printMessage(Diagnostic.Kind.NOTE, "Initializing ModelGenProcessor.");
69: this.classes = new HashMap<>();
70: sourcePackage = env.getOptions().get(SOURCE_PACKAGE_PARAM);
71: outputDirectory = env.getOptions().get(OUTPUT_DIRECTORY_PARAM);
72: debugOption = Boolean.parseBoolean(env.getOptions().get(DEBUG_PARAM));
73: }
74:
75: @Override
76: public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
77:• for (TypeElement te : annotations) {
78:• for (Element elParent : roundEnv.getElementsAnnotatedWith(te)) {
79:• if (!isAnnotatedWithNonEntity(elParent) && (sourcePackage == null || elParent.asType().toString()
80:• .contains(sourcePackage))) {
81: MetamodelClass parentClass = new MetamodelClass(elParent);
82:
83:• if (debugOption) {
84: messager.printMessage(Diagnostic.Kind.NOTE,
85: "\t - Started processing class '" + parentClass.getName() + "'");
86: }
87: List<? extends Element> properties = elParent.getEnclosedElements();
88:• for (Element elProperty : properties) {
89:• if (isPropertyPersistent(elProperty)) {
90: Field field = new Field(elProperty, elParent);
91:• if (debugOption) {
92: messager.printMessage(Diagnostic.Kind.NOTE,
93: "\t\t - Processing field '" + field.getName() + "'");
94: }
95: parentClass.addField(field);
96: }
97: }
98: classes.put(elParent.toString(), parentClass);
99:• if (debugOption) {
100: messager.printMessage(Diagnostic.Kind.NOTE,
101: "\t - Finished processing class '" + parentClass.getName() + "'");
102: }
103: }
104: }
105: }
106:• if (debugOption) {
107: messager.printMessage(Diagnostic.Kind.NOTE, "Generating output files.");
108: }
109: final OutputFilesGenerator outputGenerator = new OutputFilesGenerator(outputDirectory, debugOption, messager);
110: outputGenerator.generateOutputFiles(classes.values());
111: return true;
112: }
113:
114: private static boolean isPropertyPersistent(Element param) {
115: boolean containsWanted = false;
116: List<? extends AnnotationMirror> paramAnnotations = param.getAnnotationMirrors();
117:• if (!paramAnnotations.isEmpty()) {
118:• for (AnnotationMirror paramAnnotation : paramAnnotations) {
119:• if (paramAnnotation.toString().contains("cz.cvut.kbss.jopa.model.annotations.Transient")) {
120: return false;
121: }
122:• for (MappingAnnotations anEnum : MappingAnnotations.values()) {
123:• if (paramAnnotation.toString().contains(anEnum.getAnnotation())) {
124: containsWanted = true;
125: break;
126: }
127: }
128:
129: }
130: }
131: return containsWanted;
132: }
133:
134: @Override
135: public SourceVersion getSupportedSourceVersion() {
136: return SourceVersion.latestSupported();
137: }
138:
139: public boolean isAnnotatedWithNonEntity(Element element) {
140: TypeElement typeElement = (TypeElement) element;
141: List<? extends AnnotationMirror> annotations = typeElement.getAnnotationMirrors();
142:• for (AnnotationMirror annotation : annotations) {
143:• if ("cz.cvut.kbss.jopa.model.annotations.util.NonEntity".equals(annotation.getAnnotationType()
144: .toString())) {
145: return true;
146: }
147: }
148: return false;
149: }
150: }