diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/ItemClickHandler.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/ItemClickHandler.java index 71833927db..acaba415e5 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/ItemClickHandler.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/ItemClickHandler.java @@ -86,7 +86,7 @@ protected void processParameters(EComponentWithViewSupportHolder holder, JMethod call.arg(cast(parameterClass, invoke(onItemClickParentParam, "getAdapter").invoke("getItem").arg(onItemClickPositionParam))); if (parameterClass.isParameterized()) { - listenerMethod.annotate(SuppressWarnings.class).param("value", "unchecked"); + codeModelHelper.addSuppressWarnings(listenerMethod, "unchecked"); } } } diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/ItemLongClickHandler.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/ItemLongClickHandler.java index b3eb4a45db..d3e2d77be9 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/ItemLongClickHandler.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/ItemLongClickHandler.java @@ -93,7 +93,7 @@ protected void processParameters(EComponentWithViewSupportHolder holder, JMethod call.arg(cast(parameterClass, invoke(onItemClickParentParam, "getAdapter").invoke("getItem").arg(onItemClickPositionParam))); if (parameterClass.isParameterized()) { - listenerMethod.annotate(SuppressWarnings.class).param("value", "unchecked"); + codeModelHelper.addSuppressWarnings(listenerMethod, "unchecked"); } } } diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/RoboGuiceHandler.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/RoboGuiceHandler.java index 85a7e4cdb7..6641dba9fb 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/RoboGuiceHandler.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/RoboGuiceHandler.java @@ -96,7 +96,7 @@ private void listenerFields(Element element, EActivityHolder holder) { for (String listenerClassName : listenerClasses) { JClass listenerClass = refClass(listenerClassName); JFieldVar listener = holder.getGeneratedClass().field(JMod.PRIVATE, listenerClass, "listener" + i + "_"); - listener.annotate(SuppressWarnings.class).param("value", "unused"); + codeModelHelper.addSuppressWarnings(listener, "unused"); listener.annotate(classes().INJECT); i++; } diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/ViewByIdHandler.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/ViewByIdHandler.java index a85f485905..450fd8cd7a 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/ViewByIdHandler.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/ViewByIdHandler.java @@ -22,6 +22,7 @@ import javax.lang.model.type.TypeMirror; import org.androidannotations.annotations.ViewById; +import org.androidannotations.helper.APTCodeModelHelper; import org.androidannotations.helper.AndroidManifest; import org.androidannotations.helper.IdAnnotationHelper; import org.androidannotations.helper.IdValidatorHelper; @@ -37,9 +38,11 @@ public class ViewByIdHandler extends BaseAnnotationHandler { private IdAnnotationHelper annotationHelper; + private APTCodeModelHelper codeModelHelper; public ViewByIdHandler(ProcessingEnvironment processingEnvironment) { super(ViewById.class, processingEnvironment); + codeModelHelper = new APTCodeModelHelper(); } @Override @@ -66,10 +69,9 @@ public void process(Element element, EComponentWithViewSupportHolder holder) { String fieldName = element.getSimpleName().toString(); TypeMirror uiFieldTypeMirror = element.asType(); - String typeQualifiedName = uiFieldTypeMirror.toString(); JFieldRef idRef = annotationHelper.extractOneAnnotationFieldRef(processHolder, element, IRClass.Res.ID, true); - JClass viewClass = refClass(typeQualifiedName); + JClass viewClass = codeModelHelper.typeMirrorToJClass(uiFieldTypeMirror, holder); JFieldRef fieldRef = ref(fieldName); holder.assignFindViewById(idRef, viewClass, fieldRef); diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/APTCodeModelHelper.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/APTCodeModelHelper.java index bae02662e8..634bb8eb0b 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/APTCodeModelHelper.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/APTCodeModelHelper.java @@ -24,6 +24,7 @@ import java.lang.annotation.Inherited; import java.lang.reflect.Field; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; @@ -51,7 +52,9 @@ import org.androidannotations.holder.GeneratedClassHolder; import com.sun.codemodel.JAnnotatable; +import com.sun.codemodel.JAnnotationArrayMember; import com.sun.codemodel.JAnnotationUse; +import com.sun.codemodel.JAnnotationValue; import com.sun.codemodel.JBlock; import com.sun.codemodel.JClass; import com.sun.codemodel.JCodeModel; @@ -527,4 +530,28 @@ public TypeMirror getActualType(Element element, GeneratedClassHolder holder) { TypeMirror type = actualTypes.get(element.asType().toString()); return type == null ? element.asType() : type; } + + public void addSuppressWarnings(JAnnotatable generatedElement, String annotationValue) { + Collection annotations = generatedElement.annotations(); + for (JAnnotationUse annotationUse : annotations) { + if (annotationUse.getAnnotationClass().fullName().equals(SuppressWarnings.class.getCanonicalName())) { + JAnnotationValue value = annotationUse.getAnnotationMembers().values().iterator().next(); + StringWriter code = new StringWriter(); + JFormatter formatter = new JFormatter(code); + formatter.g(value); + if (!code.toString().contains(annotationValue)) { + if (value instanceof JAnnotationArrayMember) { + ((JAnnotationArrayMember) value).param(annotationValue); + } else { + String foundValue = code.toString().substring(1, code.toString().length() - 1); + JAnnotationArrayMember newParamArray = annotationUse.paramArray("value"); + newParamArray.param(foundValue).param(annotationValue); + } + } + return; + } + } + + generatedElement.annotate(SuppressWarnings.class).param("value", annotationValue); + } } diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/BundleHelper.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/BundleHelper.java index 5d2bb44f27..6eade2f86c 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/BundleHelper.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/BundleHelper.java @@ -221,9 +221,7 @@ public JExpression getExpressionToRestoreFromBundle(JClass variableClass, JExpre expressionToRestore = JExpr.cast(variableClass, expressionToRestore); if (restoreCallNeedsSuppressWarning()) { - if (!codeModelHelper.hasAnnotation(method, SuppressWarnings.class)) { - method.annotate(SuppressWarnings.class).param("value", "unchecked"); - } + codeModelHelper.addSuppressWarnings(method, "unchecked"); } } return expressionToRestore; diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/EComponentWithViewSupportHolder.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/EComponentWithViewSupportHolder.java index b3bec017e9..e0d485bf86 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/EComponentWithViewSupportHolder.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/EComponentWithViewSupportHolder.java @@ -33,6 +33,7 @@ import org.androidannotations.api.view.HasViews; import org.androidannotations.api.view.OnViewChangedListener; import org.androidannotations.api.view.OnViewChangedNotifier; +import org.androidannotations.helper.APTCodeModelHelper; import org.androidannotations.helper.ViewNotifierHelper; import org.androidannotations.process.ProcessHolder; @@ -48,7 +49,9 @@ public abstract class EComponentWithViewSupportHolder extends EComponentHolder { + protected APTCodeModelHelper codeModelHelper = new APTCodeModelHelper(); protected ViewNotifierHelper viewNotifierHelper; + private JMethod onViewChanged; private JBlock onViewChangedBody; private JBlock onViewChangedBodyBeforeFindViews; private JVar onViewChangedHasViewsParam; @@ -88,7 +91,7 @@ public JVar getOnViewChangedHasViewsParam() { protected void setOnViewChanged() { getGeneratedClass()._implements(OnViewChangedListener.class); - JMethod onViewChanged = getGeneratedClass().method(PUBLIC, codeModel().VOID, "onViewChanged"); + onViewChanged = getGeneratedClass().method(PUBLIC, codeModel().VOID, "onViewChanged"); onViewChanged.annotate(Override.class); onViewChangedBody = onViewChanged.body(); onViewChangedBodyBeforeFindViews = onViewChangedBody.block(); @@ -116,6 +119,10 @@ public void assignFindViewById(JFieldRef idRef, JClass viewClass, JFieldRef fiel assignExpression = findViewById(idRef); if (viewClass != null && viewClass != classes().VIEW) { assignExpression = cast(viewClass, assignExpression); + + if (viewClass.isParameterized()) { + codeModelHelper.addSuppressWarnings(onViewChanged, "unchecked"); + } } foundViewsHolders.put(idRefString, new FoundViewHolder(this, viewClass, fieldRef, block)); } diff --git a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/EViewHolder.java b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/EViewHolder.java index fb2b97c972..96541e8b3a 100644 --- a/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/EViewHolder.java +++ b/AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/EViewHolder.java @@ -22,8 +22,6 @@ import static javax.lang.model.element.ElementKind.CONSTRUCTOR; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; import java.util.List; import javax.lang.model.element.Element; @@ -33,8 +31,6 @@ import org.androidannotations.process.ProcessHolder; -import com.sun.codemodel.JAnnotationArrayMember; -import com.sun.codemodel.JAnnotationUse; import com.sun.codemodel.JBlock; import com.sun.codemodel.JClass; import com.sun.codemodel.JExpr; @@ -71,19 +67,7 @@ public EViewHolder(ProcessHolder processHolder, TypeElement annotatedElement) th private void addSuppressWarning() { generatedClass.javadoc().append(SUPPRESS_WARNING_COMMENT); - Collection annotations = getGeneratedClass().annotations(); - for (JAnnotationUse annotationUse : annotations) { - if (annotationUse.getAnnotationClass().fullName().equals(SuppressWarnings.class.getCanonicalName())) { - if (!Arrays.asList(getAnnotatedElement().getAnnotation(SuppressWarnings.class).value()).contains("unused")) { - JAnnotationArrayMember value = (JAnnotationArrayMember) annotationUse.getAnnotationMembers().get("value"); - value.param("unused"); - } - - return; - } - } - - generatedClass.annotate(SuppressWarnings.class).param("value", "unused"); + codeModelHelper.addSuppressWarnings(getGeneratedClass(), "unused"); } private void createConstructorAndBuilder() { diff --git a/AndroidAnnotations/androidannotations/src/test/java/org/androidannotations/viewbyid/GenericViewByIdActivity.java b/AndroidAnnotations/androidannotations/src/test/java/org/androidannotations/viewbyid/GenericViewByIdActivity.java new file mode 100644 index 0000000000..1f9d96efee --- /dev/null +++ b/AndroidAnnotations/androidannotations/src/test/java/org/androidannotations/viewbyid/GenericViewByIdActivity.java @@ -0,0 +1,35 @@ +/** + * Copyright (C) 2010-2015 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.viewbyid; + +import org.androidannotations.annotations.EActivity; +import org.androidannotations.annotations.ViewById; +import org.androidannotations.eviewgroup.SomeGenericViewGroup; + +import android.app.Activity; + +@EActivity +public class GenericViewByIdActivity extends Activity { + + @ViewById + SomeGenericViewGroup view; + + @ViewById + SomeGenericViewGroup view2; + + @ViewById + SomeGenericViewGroup view3; +} diff --git a/AndroidAnnotations/androidannotations/src/test/java/org/androidannotations/viewbyid/R.java b/AndroidAnnotations/androidannotations/src/test/java/org/androidannotations/viewbyid/R.java new file mode 100644 index 0000000000..47a1d32c8e --- /dev/null +++ b/AndroidAnnotations/androidannotations/src/test/java/org/androidannotations/viewbyid/R.java @@ -0,0 +1,9 @@ +package org.androidannotations.viewbyid; + +public class R { + public static final class id { + public static final int view = 0x7f06000a; + public static final int view2 = 0x7f06000b; + public static final int view3 = 0x7f06000c; + } +} diff --git a/AndroidAnnotations/androidannotations/src/test/java/org/androidannotations/viewbyid/ViewByIdTest.java b/AndroidAnnotations/androidannotations/src/test/java/org/androidannotations/viewbyid/ViewByIdTest.java new file mode 100644 index 0000000000..61f50879b2 --- /dev/null +++ b/AndroidAnnotations/androidannotations/src/test/java/org/androidannotations/viewbyid/ViewByIdTest.java @@ -0,0 +1,36 @@ +/** + * Copyright (C) 2010-2015 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.viewbyid; + +import org.androidannotations.AndroidAnnotationProcessor; +import org.androidannotations.utils.AAProcessorTestHelper; +import org.junit.Before; +import org.junit.Test; + +public class ViewByIdTest extends AAProcessorTestHelper { + + @Before + public void setUp() { + addManifestProcessorParameter(ViewByIdTest.class); + addProcessor(AndroidAnnotationProcessor.class); + } + + @Test + public void viewByIdGenericViewCompiles() { + assertCompilationSuccessful(compileFiles(GenericViewByIdActivity.class)); + } + +} diff --git a/AndroidAnnotations/androidannotations/src/test/resources/org/androidannotations/viewbyid/AndroidManifest.xml b/AndroidAnnotations/androidannotations/src/test/resources/org/androidannotations/viewbyid/AndroidManifest.xml new file mode 100644 index 0000000000..9ed70604bf --- /dev/null +++ b/AndroidAnnotations/androidannotations/src/test/resources/org/androidannotations/viewbyid/AndroidManifest.xml @@ -0,0 +1,28 @@ + + + + + + + + +