Skip to content

Commit 1802403

Browse files
committed
Introduce AndroidAnnotationsEnvironment
1 parent d6ec9ef commit 1802403

134 files changed

Lines changed: 1310 additions & 1360 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎AndroidAnnotations/androidannotations-core/androidannotations-test/AndroidManifest.xml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
<activity android:name=".WindowFeatureActivity_" />
7272
<activity android:name=".ExtendingActivity_" />
7373
<activity android:name=".ebean.BeanInjectedActivity_" />
74-
<activity android:name=".rest.MyServiceActivity_" />
7574
<activity android:name=".instancestate.SaveInstanceStateActivity_" />
7675
<activity android:name=".afterinject.AfterInjectActivity_" />
7776
<activity android:name=".nonconfiguration.NonConfigurationActivity_" />

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/AndroidAnnotationProcessor.java‎

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
import java.io.FileNotFoundException;
1919
import java.io.IOException;
2020
import java.net.URL;
21+
import java.util.ArrayList;
2122
import java.util.Arrays;
2223
import java.util.Iterator;
2324
import java.util.List;
2425
import java.util.Properties;
26+
import java.util.ServiceLoader;
2527
import java.util.Set;
2628

2729
import javax.annotation.processing.AbstractProcessor;
@@ -36,7 +38,6 @@
3638
import org.androidannotations.exception.ValidationException;
3739
import org.androidannotations.exception.VersionMismatchException;
3840
import org.androidannotations.generation.CodeModelGenerator;
39-
import org.androidannotations.handler.AnnotationHandlers;
4041
import org.androidannotations.helper.AndroidManifest;
4142
import org.androidannotations.helper.AndroidManifestFinder;
4243
import org.androidannotations.helper.ErrorHelper;
@@ -68,11 +69,12 @@ public class AndroidAnnotationProcessor extends AbstractProcessor {
6869
private final Properties propertiesApi = new Properties();
6970
private final TimeStats timeStats = new TimeStats();
7071
private final ErrorHelper errorHelper = new ErrorHelper();
71-
private AnnotationHandlers annotationHandlers;
72+
private AndroidAnnotationsEnvironment androidAnnotationsEnv;
7273

7374
@Override
7475
public synchronized void init(ProcessingEnvironment processingEnv) {
7576
super.init(processingEnv);
77+
androidAnnotationsEnv = new AndroidAnnotationsEnvironment(processingEnv);
7678

7779
ModelConstants.init(processingEnv);
7880

@@ -89,25 +91,40 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
8991

9092
LOGGER.info("Initialize AndroidAnnotations {} with options {}", getAAProcessorVersion(), processingEnv.getOptions());
9193

92-
annotationHandlers = new AnnotationHandlers(processingEnv);
93-
loadPlugins(processingEnv);
94+
List<AndroidAnnotationsPlugin> plugins = loadPlugins();
95+
LOGGER.info("Plugins loaded: {}", Arrays.toString(plugins.toArray()));
96+
androidAnnotationsEnv.setPlugins(plugins);
9497
}
9598

96-
private void loadPlugins(ProcessingEnvironment processingEnv) {
97-
List<AndroidAnnotationsPlugin> plugins = AndroidAnnotationsPlugin.load();
98-
LOGGER.info("Plugins loaded: {}", Arrays.toString(plugins.toArray()));
99-
for (AndroidAnnotationsPlugin plugin : plugins) {
100-
plugin.addHandlers(annotationHandlers, processingEnv);
99+
private List<AndroidAnnotationsPlugin> loadPlugins() {
100+
ServiceLoader<AndroidAnnotationsPlugin> serviceLoader = ServiceLoader.load(AndroidAnnotationsPlugin.class, AndroidAnnotationsPlugin.class.getClassLoader());
101+
List<AndroidAnnotationsPlugin> plugins = new ArrayList<>();
102+
plugins.add(new CorePlugin());
103+
for (AndroidAnnotationsPlugin plugin : serviceLoader) {
104+
plugins.add(plugin);
101105
}
106+
return plugins;
102107
}
103108

104-
private void checkApiAndCoreVersions() throws VersionMismatchException {
105-
String apiVersion = getAAApiVersion();
106-
String coreVersion = getAAProcessorVersion();
109+
private void loadPropertyFile() throws FileNotFoundException {
110+
String filename = "androidannotations.properties";
111+
try {
112+
URL url = getClass().getClassLoader().getResource(filename);
113+
properties.load(url.openStream());
114+
} catch (Exception e) {
115+
LOGGER.error("Core property file {} couldn't be parsed");
116+
throw new FileNotFoundException("Core property file " + filename + " couldn't be parsed.");
117+
}
118+
}
107119

108-
if (!apiVersion.equals(coreVersion)) {
109-
LOGGER.error("AndroidAnnotations version for API ({}) and core ({}) doesn't match. Please check your classpath", apiVersion, coreVersion);
110-
throw new VersionMismatchException("AndroidAnnotations version for API (" + apiVersion + ") and core (" + coreVersion + ") doesn't match. Please check your classpath");
120+
private void loadApiPropertyFile() throws FileNotFoundException {
121+
String filename = "androidannotations-api.properties";
122+
try {
123+
URL url = EActivity.class.getClassLoader().getResource(filename);
124+
propertiesApi.load(url.openStream());
125+
} catch (Exception e) {
126+
LOGGER.error("API property file {} couldn't be parsed");
127+
throw new FileNotFoundException("API property file " + filename + " couldn't be parsed. Please check your classpath and verify that AA-API's version is at least 3.0");
111128
}
112129
}
113130

@@ -142,25 +159,13 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
142159
return true;
143160
}
144161

145-
private void loadPropertyFile() throws FileNotFoundException {
146-
String filename = "androidannotations.properties";
147-
try {
148-
URL url = getClass().getClassLoader().getResource(filename);
149-
properties.load(url.openStream());
150-
} catch (Exception e) {
151-
LOGGER.error("Core property file {} couldn't be parsed");
152-
throw new FileNotFoundException("Core property file " + filename + " couldn't be parsed.");
153-
}
154-
}
162+
private void checkApiAndCoreVersions() throws VersionMismatchException {
163+
String apiVersion = getAAApiVersion();
164+
String coreVersion = getAAProcessorVersion();
155165

156-
private void loadApiPropertyFile() throws FileNotFoundException {
157-
String filename = "androidannotations-api.properties";
158-
try {
159-
URL url = EActivity.class.getClassLoader().getResource(filename);
160-
propertiesApi.load(url.openStream());
161-
} catch (Exception e) {
162-
LOGGER.error("API property file {} couldn't be parsed");
163-
throw new FileNotFoundException("API property file " + filename + " couldn't be parsed. Please check your classpath and verify that AA-API's version is at least 3.0");
166+
if (!apiVersion.equals(coreVersion)) {
167+
LOGGER.error("AndroidAnnotations version for API ({}) and core ({}) doesn't match. Please check your classpath", apiVersion, coreVersion);
168+
throw new VersionMismatchException("AndroidAnnotations version for API (" + apiVersion + ") and core (" + coreVersion + ") doesn't match. Please check your classpath");
164169
}
165170
}
166171

@@ -195,9 +200,10 @@ private void processThrowing(Set<? extends TypeElement> annotations, RoundEnviro
195200

196201
AndroidSystemServices androidSystemServices = new AndroidSystemServices();
197202

198-
annotationHandlers.setAndroidEnvironment(rClass, androidSystemServices, androidManifest);
203+
androidAnnotationsEnv.setAndroidEnvironment(rClass, androidSystemServices, androidManifest);
199204

200205
AnnotationElements validatedModel = validateAnnotations(extractedModel);
206+
androidAnnotationsEnv.setValidatedModel(validatedModel);
201207

202208
ModelProcessor.ProcessResult processResult = processAnnotations(validatedModel);
203209

@@ -247,16 +253,15 @@ private Option<IRClass> findRClasses(AndroidManifest androidManifest) throws IOE
247253

248254
private AnnotationElements validateAnnotations(AnnotationElementsHolder extractedModel) throws ValidationException {
249255
timeStats.start("Validate Annotations");
250-
ModelValidator modelValidator = new ModelValidator(annotationHandlers);
256+
ModelValidator modelValidator = new ModelValidator(androidAnnotationsEnv);
251257
AnnotationElements validatedAnnotations = modelValidator.validate(extractedModel);
252258
timeStats.stop("Validate Annotations");
253259
return validatedAnnotations;
254260
}
255261

256262
private ModelProcessor.ProcessResult processAnnotations(AnnotationElements validatedModel) throws Exception {
257263
timeStats.start("Process Annotations");
258-
annotationHandlers.setValidatedModel(validatedModel);
259-
ModelProcessor modelProcessor = new ModelProcessor(processingEnv, annotationHandlers);
264+
ModelProcessor modelProcessor = new ModelProcessor(androidAnnotationsEnv);
260265
ModelProcessor.ProcessResult processResult = modelProcessor.process(validatedModel);
261266
timeStats.stop("Process Annotations");
262267
return processResult;
@@ -290,7 +295,7 @@ private void handleException(Set<? extends TypeElement> annotations, RoundEnviro
290295

291296
@Override
292297
public Set<String> getSupportedAnnotationTypes() {
293-
return annotationHandlers.getSupportedAnnotationTypes();
298+
return androidAnnotationsEnv.getSupportedAnnotationTypes();
294299
}
295300

296301
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/**
2+
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations;
17+
18+
import org.androidannotations.handler.AnnotationHandler;
19+
import org.androidannotations.handler.AnnotationHandlers;
20+
import org.androidannotations.handler.GeneratingAnnotationHandler;
21+
import org.androidannotations.helper.AndroidManifest;
22+
import org.androidannotations.helper.OptionsHelper;
23+
import org.androidannotations.holder.GeneratedClassHolder;
24+
import org.androidannotations.model.AndroidSystemServices;
25+
import org.androidannotations.model.AnnotationElements;
26+
import org.androidannotations.plugin.AndroidAnnotationsPlugin;
27+
import org.androidannotations.process.ProcessHolder;
28+
import org.androidannotations.rclass.IRClass;
29+
30+
import javax.annotation.processing.ProcessingEnvironment;
31+
import java.util.List;
32+
import java.util.Set;
33+
34+
public class AndroidAnnotationsEnvironment {
35+
36+
private final ProcessingEnvironment processingEnvironment;
37+
private final OptionsHelper options;
38+
private final AnnotationHandlers annotationHandlers;
39+
40+
private List<AndroidAnnotationsPlugin> plugins;
41+
42+
private IRClass rClass;
43+
private AndroidSystemServices androidSystemServices;
44+
private AndroidManifest androidManifest;
45+
46+
private AnnotationElements validatedModel;
47+
48+
private ProcessHolder processHolder;
49+
50+
AndroidAnnotationsEnvironment(ProcessingEnvironment processingEnvironment) {
51+
this.processingEnvironment = processingEnvironment;
52+
options = new OptionsHelper(processingEnvironment);
53+
annotationHandlers = new AnnotationHandlers();
54+
}
55+
56+
public void setPlugins(List<AndroidAnnotationsPlugin> plugins) {
57+
this.plugins = plugins;
58+
for (AndroidAnnotationsPlugin plugin : plugins) {
59+
plugin.addHandlers(annotationHandlers, this);
60+
}
61+
}
62+
63+
public void setAndroidEnvironment(IRClass rClass, AndroidSystemServices androidSystemServices, AndroidManifest androidManifest) {
64+
this.rClass = rClass;
65+
this.androidSystemServices = androidSystemServices;
66+
this.androidManifest = androidManifest;
67+
}
68+
69+
public void setValidatedModel(AnnotationElements validatedModel) {
70+
this.validatedModel = validatedModel;
71+
}
72+
73+
public void setProcessHolder(ProcessHolder processHolder) {
74+
this.processHolder = processHolder;
75+
}
76+
77+
public ProcessingEnvironment getProcessingEnvironment() {
78+
return processingEnvironment;
79+
}
80+
81+
public OptionsHelper getOptions() {
82+
return options;
83+
}
84+
85+
public Set<String> getSupportedAnnotationTypes() {
86+
return annotationHandlers.getSupportedAnnotationTypes();
87+
}
88+
89+
public List<AnnotationHandler<? extends GeneratedClassHolder>> getHandlers() {
90+
return annotationHandlers.get();
91+
}
92+
93+
public List<AnnotationHandler<? extends GeneratedClassHolder>> getDecoratingHandlers() {
94+
return annotationHandlers.getDecorating();
95+
}
96+
97+
public List<GeneratingAnnotationHandler<? extends GeneratedClassHolder>> getGeneratingHandlers() {
98+
return annotationHandlers.getGenerating();
99+
}
100+
101+
public IRClass getRClass() {
102+
return rClass;
103+
}
104+
105+
public AndroidSystemServices getAndroidSystemServices() {
106+
return androidSystemServices;
107+
}
108+
109+
public AndroidManifest getAndroidManifest() {
110+
return androidManifest;
111+
}
112+
113+
public AnnotationElements getValidatedModel() {
114+
if (validatedModel == null) {
115+
throw new IllegalStateException("ValidatedModel is only available after validation");
116+
}
117+
return validatedModel;
118+
}
119+
120+
public ProcessHolder getProcessHolder() {
121+
return processHolder;
122+
}
123+
}

0 commit comments

Comments
 (0)