Skip to content

Commit 36957ee

Browse files
committed
Check API and processor version pairs for all plugins
1 parent 5595fa3 commit 36957ee

14 files changed

Lines changed: 240 additions & 84 deletions

File tree

‎AndroidAnnotations/androidannotations-core/androidannotations-api/pom.xml‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,6 @@
7171
</executions>
7272
</plugin>
7373
</plugins>
74-
75-
<resources>
76-
<resource>
77-
<directory>src/main/resources</directory>
78-
<filtering>true</filtering>
79-
<includes>
80-
<include>**/*.properties</include>
81-
</includes>
82-
</resource>
83-
</resources>
8474
</build>
8575

8676
</project>

‎AndroidAnnotations/androidannotations-core/androidannotations/pom.xml‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,6 @@
9494
<artifactId>maven-resources-plugin</artifactId>
9595
</plugin>
9696
</plugins>
97-
98-
<resources>
99-
<resource>
100-
<directory>src/main/resources</directory>
101-
<excludes>
102-
<exclude>rebel.xml</exclude>
103-
</excludes>
104-
</resource>
105-
<resource>
106-
<directory>src/main/resources</directory>
107-
<filtering>true</filtering>
108-
<includes>
109-
<include>**/*.properties</include>
110-
</includes>
111-
</resource>
112-
</resources>
11397
</build>
11498

11599
</project>

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

Lines changed: 28 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717

1818
import java.io.FileNotFoundException;
1919
import java.io.IOException;
20-
import java.net.URL;
2120
import java.util.ArrayList;
2221
import java.util.Arrays;
2322
import java.util.Iterator;
2423
import java.util.List;
25-
import java.util.Properties;
2624
import java.util.ServiceLoader;
2725
import java.util.Set;
2826

@@ -33,12 +31,12 @@
3331
import javax.lang.model.element.Element;
3432
import javax.lang.model.element.TypeElement;
3533

36-
import org.androidannotations.annotations.EActivity;
3734
import org.androidannotations.exception.AndroidManifestNotFoundException;
3835
import org.androidannotations.exception.ProcessingException;
3936
import org.androidannotations.exception.RClassNotFoundException;
4037
import org.androidannotations.exception.ValidationException;
4138
import org.androidannotations.exception.VersionMismatchException;
39+
import org.androidannotations.exception.VersionNotFoundException;
4240
import org.androidannotations.generation.CodeModelGenerator;
4341
import org.androidannotations.helper.AndroidManifest;
4442
import org.androidannotations.helper.AndroidManifestFinder;
@@ -65,8 +63,8 @@ public class AndroidAnnotationProcessor extends AbstractProcessor {
6563

6664
private static final Logger LOGGER = LoggerFactory.getLogger(AndroidAnnotationProcessor.class);
6765

68-
private final Properties properties = new Properties();
69-
private final Properties propertiesApi = new Properties();
66+
private String coreVersion;
67+
7068
private final TimeStats timeStats = new TimeStats();
7169
private final ErrorHelper errorHelper = new ErrorHelper();
7270
private AndroidAnnotationsEnvironment androidAnnotationsEnv;
@@ -83,51 +81,34 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
8381
loggerContext.setEnvironment(androidAnnotationsEnv);
8482

8583
try {
86-
loadPropertyFile();
87-
loadApiPropertyFile();
88-
} catch (Exception e) {
89-
LOGGER.error("Can't load API or core properties files", e);
90-
}
84+
AndroidAnnotationsPlugin corePlugin = new CorePlugin();
85+
corePlugin.loadVersion();
86+
coreVersion = corePlugin.getVersion();
9187

92-
LOGGER.info("Initialize AndroidAnnotations {} with options {}", getAAProcessorVersion(), processingEnv.getOptions());
88+
LOGGER.info("Initialize AndroidAnnotations {} with options {}", coreVersion, processingEnv.getOptions());
9389

94-
List<AndroidAnnotationsPlugin> plugins = loadPlugins();
95-
androidAnnotationsEnv.setPlugins(plugins);
90+
List<AndroidAnnotationsPlugin> plugins = loadPlugins();
91+
plugins.add(0, corePlugin);
92+
androidAnnotationsEnv.setPlugins(plugins);
93+
} catch (Exception e) {
94+
LOGGER.error("Can't load plugins", e);
95+
}
9696
}
9797

98-
private List<AndroidAnnotationsPlugin> loadPlugins() {
98+
private List<AndroidAnnotationsPlugin> loadPlugins() throws FileNotFoundException, VersionNotFoundException {
9999
ServiceLoader<AndroidAnnotationsPlugin> serviceLoader = ServiceLoader.load(AndroidAnnotationsPlugin.class, AndroidAnnotationsPlugin.class.getClassLoader());
100100
List<AndroidAnnotationsPlugin> plugins = new ArrayList<>();
101101
for (AndroidAnnotationsPlugin plugin : serviceLoader) {
102102
plugins.add(plugin);
103+
104+
if (plugin.shouldCheckApiAndProcessorVersions()) {
105+
plugin.loadVersion();
106+
}
103107
}
104108
LOGGER.info("Plugins loaded: {}", Arrays.toString(plugins.toArray()));
105-
plugins.add(0, new CorePlugin());
106109
return plugins;
107110
}
108111

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-
}
119-
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");
128-
}
129-
}
130-
131112
@Override
132113
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
133114
timeStats.clear();
@@ -141,7 +122,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
141122
}
142123

143124
try {
144-
checkApiAndCoreVersions();
125+
checkApiAndProcessorVersions();
145126
processThrowing(annotations, roundEnv);
146127
} catch (ValidationException e) {
147128
// We do nothing, errors have been printed by ModelValidator
@@ -159,24 +140,16 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
159140
return true;
160141
}
161142

162-
private void checkApiAndCoreVersions() throws VersionMismatchException {
163-
String apiVersion = getAAApiVersion();
164-
String coreVersion = getAAProcessorVersion();
165-
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");
143+
private void checkApiAndProcessorVersions() throws VersionMismatchException {
144+
for (AndroidAnnotationsPlugin plugin : androidAnnotationsEnv.getPlugins()) {
145+
if (plugin.shouldCheckApiAndProcessorVersions() && !plugin.getApiVersion().equals(plugin.getVersion())) {
146+
LOGGER.error("{} version for API ({}) and processor ({}) don't match. Please check your classpath", plugin.getName(), plugin.getApiVersion(), plugin.getVersion());
147+
throw new VersionMismatchException(plugin.getName() + "version for API (" + plugin.getApiVersion() + ") and core (" + plugin.getVersion()
148+
+ ") don't match. Please check your classpath");
149+
}
169150
}
170151
}
171152

172-
private String getAAProcessorVersion() {
173-
return properties.getProperty("version", "3.0+");
174-
}
175-
176-
private String getAAApiVersion() {
177-
return propertiesApi.getProperty("version", "unknown");
178-
}
179-
180153
private void processThrowing(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) throws Exception {
181154
if (nothingToDo(annotations, roundEnv)) {
182155
return;
@@ -258,13 +231,13 @@ private ModelProcessor.ProcessResult processAnnotations(AnnotationElements valid
258231
private void generateSources(ModelProcessor.ProcessResult processResult) throws IOException {
259232
timeStats.start("Generate Sources");
260233
LOGGER.info("Number of files generated by AndroidAnnotations: {}", processResult.codeModel.countArtifacts());
261-
CodeModelGenerator modelGenerator = new CodeModelGenerator(processingEnv.getFiler(), getAAProcessorVersion());
234+
CodeModelGenerator modelGenerator = new CodeModelGenerator(processingEnv.getFiler(), coreVersion);
262235
modelGenerator.generate(processResult);
263236
timeStats.stop("Generate Sources");
264237
}
265238

266239
private void handleException(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv, ProcessingException e) {
267-
String errorMessage = errorHelper.getErrorMessage(processingEnv, e, getAAProcessorVersion());
240+
String errorMessage = errorHelper.getErrorMessage(processingEnv, e, coreVersion);
268241

269242
/*
270243
* Printing exception as an error on a random element. The exception is

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,8 @@ public List<Class<? extends Annotation>> getGeneratingAnnotations() {
159159
public boolean isAndroidAnnotation(String annotationQualifiedName) {
160160
return getSupportedAnnotationTypes().contains(annotationQualifiedName);
161161
}
162+
163+
public List<AndroidAnnotationsPlugin> getPlugins() {
164+
return plugins;
165+
}
162166
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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.exception;
17+
18+
import org.androidannotations.plugin.AndroidAnnotationsPlugin;
19+
20+
public class VersionNotFoundException extends Exception {
21+
22+
private final AndroidAnnotationsPlugin plugin;
23+
24+
public VersionNotFoundException(AndroidAnnotationsPlugin plugin) {
25+
this.plugin = plugin;
26+
}
27+
28+
@Override
29+
public String getMessage() {
30+
return plugin.getName() + " does not declare its version!";
31+
}
32+
}

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/plugin/AndroidAnnotationsPlugin.java‎

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,26 @@
1515
*/
1616
package org.androidannotations.plugin;
1717

18-
import java.util.Collections;
19-
import java.util.List;
20-
2118
import org.androidannotations.AndroidAnnotationsEnvironment;
19+
import org.androidannotations.exception.VersionNotFoundException;
2220
import org.androidannotations.handler.AnnotationHandlers;
21+
import org.androidannotations.logger.Logger;
22+
import org.androidannotations.logger.LoggerFactory;
2323
import org.androidannotations.process.Option;
2424

25+
import java.io.FileNotFoundException;
26+
import java.net.URL;
27+
import java.util.Collections;
28+
import java.util.List;
29+
import java.util.Properties;
30+
2531
public abstract class AndroidAnnotationsPlugin {
2632

33+
private final Logger logger = LoggerFactory.getLogger(getClass());
34+
35+
private String version;
36+
private String apiVersion;
37+
2738
public abstract String getName();
2839
public abstract void addHandlers(AnnotationHandlers annotationHandlers, AndroidAnnotationsEnvironment androidAnnotationEnv);
2940

@@ -35,4 +46,43 @@ public String toString() {
3546
public List<Option> getSupportedOptions() {
3647
return Collections.emptyList();
3748
}
49+
50+
public boolean shouldCheckApiAndProcessorVersions() {
51+
return true;
52+
}
53+
54+
public final void loadVersion() throws FileNotFoundException, VersionNotFoundException {
55+
version = getVersionFromPropertyFile(getName().toLowerCase());
56+
apiVersion = getVersionFromPropertyFile(getName().toLowerCase() + "-api");
57+
}
58+
59+
private String getVersionFromPropertyFile(String name) throws FileNotFoundException, VersionNotFoundException {
60+
String filename = name + ".properties";
61+
Properties properties;
62+
try {
63+
URL url = getClass().getClassLoader().getResource(filename);
64+
properties = new Properties();
65+
properties.load(url.openStream());
66+
} catch (Exception e) {
67+
logger.error("Property file {} couldn't be parsed", filename);
68+
throw new FileNotFoundException("Property file " + filename + " couldn't be parsed.");
69+
}
70+
71+
String version = properties.getProperty("version");
72+
73+
if (version == null) {
74+
logger.error("{} plugin is missing 'version' property!", getName());
75+
throw new VersionNotFoundException(this);
76+
}
77+
78+
return version;
79+
}
80+
81+
public String getApiVersion() {
82+
return apiVersion;
83+
}
84+
85+
public String getVersion() {
86+
return version;
87+
}
3888
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
17+
version=${project.version}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
17+
version=${project.version}

‎AndroidAnnotations/androidannotations-otto/otto/src/main/java/org/androidannotations/otto/OttoPlugin.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public String getName() {
3030
return NAME;
3131
}
3232

33+
@Override
34+
public boolean shouldCheckApiAndProcessorVersions() {
35+
return false;
36+
}
37+
3338
@Override
3439
public void addHandlers(AnnotationHandlers annotationHandlers, AndroidAnnotationsEnvironment androidAnnotationEnv) {
3540
annotationHandlers.add(new SubscribeHandler(androidAnnotationEnv));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
17+
version=${project.version}

0 commit comments

Comments
 (0)