From c4bc3991ae542b44b7a0d7c1070a77f239be55e7 Mon Sep 17 00:00:00 2001 From: Damien Date: Sun, 22 Sep 2013 21:36:47 +0200 Subject: [PATCH 1/3] Add a check on API and core versions --- .../androidannotations-api/pom.xml | 14 +++++++ .../androidannotations-api.properties} | 0 .../org.eclipse.core.resources.prefs | 2 +- AndroidAnnotations/androidannotations/pom.xml | 4 -- .../AndroidAnnotationProcessor.java | 42 ++++++++++++++++--- .../exception/VersionMismatchException.java | 38 +++++++++++++++++ .../resources/androidannotations.properties | 17 ++++++++ AndroidAnnotations/pom.xml | 11 ++++- 8 files changed, 115 insertions(+), 13 deletions(-) rename AndroidAnnotations/{androidannotations/src/main/resources/androidannotations-version.properties => androidannotations-api/src/main/resources/androidannotations-api.properties} (100%) create mode 100644 AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/exception/VersionMismatchException.java create mode 100644 AndroidAnnotations/androidannotations/src/main/resources/androidannotations.properties diff --git a/AndroidAnnotations/androidannotations-api/pom.xml b/AndroidAnnotations/androidannotations-api/pom.xml index 7744a43615..d56464c833 100644 --- a/AndroidAnnotations/androidannotations-api/pom.xml +++ b/AndroidAnnotations/androidannotations-api/pom.xml @@ -43,6 +43,10 @@ 1.6 + + org.apache.maven.plugins + maven-resources-plugin + maven-javadoc-plugin @@ -66,6 +70,16 @@ + + + + src/main/resources + true + + **/*.properties + + + diff --git a/AndroidAnnotations/androidannotations/src/main/resources/androidannotations-version.properties b/AndroidAnnotations/androidannotations-api/src/main/resources/androidannotations-api.properties similarity index 100% rename from AndroidAnnotations/androidannotations/src/main/resources/androidannotations-version.properties rename to AndroidAnnotations/androidannotations-api/src/main/resources/androidannotations-api.properties diff --git a/AndroidAnnotations/androidannotations/.settings/org.eclipse.core.resources.prefs b/AndroidAnnotations/androidannotations/.settings/org.eclipse.core.resources.prefs index d25a71a2d6..02b3260d3f 100644 --- a/AndroidAnnotations/androidannotations/.settings/org.eclipse.core.resources.prefs +++ b/AndroidAnnotations/androidannotations/.settings/org.eclipse.core.resources.prefs @@ -2,7 +2,7 @@ eclipse.preferences.version=1 encoding//src/main/java=UTF-8 encoding//src/main/java/rebel.xml=UTF-8 encoding//src/main/resources=UTF-8 -encoding//src/main/resources/androidannotations-version.properties=utf-8 +encoding//src/main/resources/androidannotations.properties=utf-8 encoding//src/main/resources/rebel.xml=UTF-8 encoding//src/test/java=UTF-8 encoding//src/test/resources=UTF-8 diff --git a/AndroidAnnotations/androidannotations/pom.xml b/AndroidAnnotations/androidannotations/pom.xml index c5b2d0aa85..36e030f5a1 100644 --- a/AndroidAnnotations/androidannotations/pom.xml +++ b/AndroidAnnotations/androidannotations/pom.xml @@ -80,10 +80,6 @@ org.apache.maven.plugins maven-resources-plugin - 2.5 - - ${project.build.sourceEncoding} - diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/AndroidAnnotationProcessor.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/AndroidAnnotationProcessor.java index d80624f0bf..f5b517e78d 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/AndroidAnnotationProcessor.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/AndroidAnnotationProcessor.java @@ -21,6 +21,7 @@ import static org.androidannotations.helper.ModelConstants.TRACE_OPTION; import static org.androidannotations.rclass.ProjectRClassFinder.RESOURCE_PACKAGE_NAME_OPTION; +import java.io.FileNotFoundException; import java.io.IOException; import java.net.URL; import java.util.Collections; @@ -124,6 +125,7 @@ import org.androidannotations.annotations.sharedpreferences.Pref; import org.androidannotations.annotations.sharedpreferences.SharedPref; import org.androidannotations.exception.ProcessingException; +import org.androidannotations.exception.VersionMismatchException; import org.androidannotations.generation.CodeModelGenerator; import org.androidannotations.helper.AndroidManifest; import org.androidannotations.helper.AndroidManifestFinder; @@ -285,6 +287,7 @@ public class AndroidAnnotationProcessor extends AbstractProcessor { private final Properties properties = new Properties(); + private final Properties propertiesApi = new Properties(); private final TimeStats timeStats = new TimeStats(); private final ErrorHelper errorHelper = new ErrorHelper(); @@ -296,11 +299,27 @@ public synchronized void init(ProcessingEnvironment processingEnv) { Messager messager = processingEnv.getMessager(); - loadPropertyFile(); + try { + loadPropertyFile(); + loadApiPropertyFile(); + checkApiAndCoreVersions(); + } catch (Exception e) { + messager.printMessage(Diagnostic.Kind.ERROR, "AndroidAnnotations processing failed: " + e.getMessage()); + } timeStats.setMessager(messager); messager.printMessage(Diagnostic.Kind.NOTE, "Starting AndroidAnnotations annotation processing"); + + } + + private void checkApiAndCoreVersions() throws VersionMismatchException { + String apiVersion = getAAApiVersion(); + String coreVersion = getAAProcessorVersion(); + + if (!apiVersion.equals(coreVersion)) { + throw new VersionMismatchException("AndroidAnnotation version for API (" + apiVersion + ") and core (" + coreVersion + " doesn't match. Please check your classpath)"); + } } @Override @@ -319,16 +338,23 @@ public boolean process(Set annotations, RoundEnvironment return true; } - private void loadPropertyFile() { - String filename = "androidannotations-version.properties"; + private void loadPropertyFile() throws FileNotFoundException { + String filename = "androidannotations.properties"; try { URL url = getClass().getClassLoader().getResource(filename); properties.load(url.openStream()); } catch (Exception e) { - e.printStackTrace(); + throw new FileNotFoundException(filename + " couldn't be parsed"); + } + } - Messager messager = processingEnv.getMessager(); - messager.printMessage(Diagnostic.Kind.NOTE, "AndroidAnnotations processing failed because " + filename + " couldn't be parsed : " + e.getLocalizedMessage()); + private void loadApiPropertyFile() throws FileNotFoundException { + String filename = "androidannotations-api.properties"; + try { + URL url = getClass().getClassLoader().getResource(filename); + propertiesApi.load(url.openStream()); + } catch (Exception e) { + throw new FileNotFoundException(filename + " couldn't be parsed"); } } @@ -336,6 +362,10 @@ private String getAAProcessorVersion() { return properties.getProperty("version", "3.0+"); } + private String getAAApiVersion() { + return propertiesApi.getProperty("version", null); + } + private void processThrowing(Set annotations, RoundEnvironment roundEnv) throws ProcessingException, Exception { if (nothingToDo(annotations, roundEnv)) { return; diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/exception/VersionMismatchException.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/exception/VersionMismatchException.java new file mode 100644 index 0000000000..1c1b58e1cb --- /dev/null +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/exception/VersionMismatchException.java @@ -0,0 +1,38 @@ +/** + * Copyright (C) 2010-2013 eBusiness Information, Excilys Group + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed To in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.androidannotations.exception; + +public class VersionMismatchException extends Exception { + + private static final long serialVersionUID = 1457334941140141471L; + + public VersionMismatchException() { + super(); + } + + public VersionMismatchException(String message, Throwable cause) { + super(message, cause); + } + + public VersionMismatchException(String message) { + super(message); + } + + public VersionMismatchException(Throwable cause) { + super(cause); + } + +} diff --git a/AndroidAnnotations/androidannotations/src/main/resources/androidannotations.properties b/AndroidAnnotations/androidannotations/src/main/resources/androidannotations.properties new file mode 100644 index 0000000000..46d482f4cf --- /dev/null +++ b/AndroidAnnotations/androidannotations/src/main/resources/androidannotations.properties @@ -0,0 +1,17 @@ +# +# Copyright (C) 2010-2013 eBusiness Information, Excilys Group +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy of +# the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed To in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations under +# the License. +# + +version=${project.version} \ No newline at end of file diff --git a/AndroidAnnotations/pom.xml b/AndroidAnnotations/pom.xml index 8243af898b..7f18f73c38 100644 --- a/AndroidAnnotations/pom.xml +++ b/AndroidAnnotations/pom.xml @@ -1,6 +1,5 @@ - + 4.0.0 @@ -196,6 +195,14 @@ maven-compiler-plugin 2.3.2 + + org.apache.maven.plugins + maven-resources-plugin + 2.5 + + ${project.build.sourceEncoding} + + org.apache.maven.plugins maven-jar-plugin From 50da2b916bf21a53533cb00264b3177a704af2cb Mon Sep 17 00:00:00 2001 From: Damien Date: Sun, 22 Sep 2013 22:09:54 +0200 Subject: [PATCH 2/3] Move versions check in process() method instead of init() and use the handleError() method in case of mismatch --- .../androidannotations/AndroidAnnotationProcessor.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/AndroidAnnotationProcessor.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/AndroidAnnotationProcessor.java index f5b517e78d..f55a3315d9 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/AndroidAnnotationProcessor.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/AndroidAnnotationProcessor.java @@ -302,9 +302,9 @@ public synchronized void init(ProcessingEnvironment processingEnv) { try { loadPropertyFile(); loadApiPropertyFile(); - checkApiAndCoreVersions(); } catch (Exception e) { messager.printMessage(Diagnostic.Kind.ERROR, "AndroidAnnotations processing failed: " + e.getMessage()); + throw new RuntimeException("AndroidAnnotations processing failed", e); } timeStats.setMessager(messager); @@ -326,7 +326,9 @@ private void checkApiAndCoreVersions() throws VersionMismatchException { public boolean process(Set annotations, RoundEnvironment roundEnv) { timeStats.clear(); timeStats.start("Whole Processing"); + try { + checkApiAndCoreVersions(); processThrowing(annotations, roundEnv); } catch (ProcessingException e) { handleException(annotations, roundEnv, e); @@ -344,7 +346,7 @@ private void loadPropertyFile() throws FileNotFoundException { URL url = getClass().getClassLoader().getResource(filename); properties.load(url.openStream()); } catch (Exception e) { - throw new FileNotFoundException(filename + " couldn't be parsed"); + throw new FileNotFoundException(filename + " couldn't be parsed."); } } @@ -354,7 +356,7 @@ private void loadApiPropertyFile() throws FileNotFoundException { URL url = getClass().getClassLoader().getResource(filename); propertiesApi.load(url.openStream()); } catch (Exception e) { - throw new FileNotFoundException(filename + " couldn't be parsed"); + throw new FileNotFoundException(filename + " couldn't be parsed. Please check your classpath and verify that AA-API's version is at least 3.0"); } } From f4075851bc9afa5cbc3c83d12a9dbfb5632a4944 Mon Sep 17 00:00:00 2001 From: Damien Date: Sun, 22 Sep 2013 22:10:30 +0200 Subject: [PATCH 3/3] Improve a little bit the message notification in case of errors if there is no elements --- .../AndroidAnnotationProcessor.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/AndroidAnnotationProcessor.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/AndroidAnnotationProcessor.java index f55a3315d9..2b853c141c 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/AndroidAnnotationProcessor.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/AndroidAnnotationProcessor.java @@ -26,6 +26,7 @@ import java.net.URL; import java.util.Collections; import java.util.HashSet; +import java.util.Iterator; import java.util.Map; import java.util.Properties; import java.util.Set; @@ -651,8 +652,16 @@ private void handleException(Set annotations, RoundEnviro * eclipse. */ - Element element = roundEnv.getElementsAnnotatedWith(annotations.iterator().next()).iterator().next(); - messager.printMessage(Diagnostic.Kind.ERROR, errorMessage, element); + Iterator iterator = annotations.iterator(); + if (iterator.hasNext()) { + Element element = roundEnv.getElementsAnnotatedWith(iterator.next()).iterator().next(); + messager.printMessage(Diagnostic.Kind.ERROR, errorMessage, element); + } else { + // Sometime this is a total mess and javac could not even find one + // element on which we could print the error. So we should just + // throw an exception and let it go. + throw new RuntimeException("An error occured and couldn't be printed on an element: " + errorMessage); + } } @Override