Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,9 +38,11 @@
public class ViewByIdHandler extends BaseAnnotationHandler<EComponentWithViewSupportHolder> {

private IdAnnotationHelper annotationHelper;
private APTCodeModelHelper codeModelHelper;

public ViewByIdHandler(ProcessingEnvironment processingEnvironment) {
super(ViewById.class, processingEnvironment);
codeModelHelper = new APTCodeModelHelper();
}

@Override
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<JAnnotationUse> 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)) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this because now we do not need the TypeElement passed to this method, which is not always available. I know this is ugly and an anti-pattern, but the other way is using 30 lines of reflective code to access the needed values from CodeModel. 😟

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with that.

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -71,19 +67,7 @@ public EViewHolder(ProcessHolder processHolder, TypeElement annotatedElement) th
private void addSuppressWarning() {
generatedClass.javadoc().append(SUPPRESS_WARNING_COMMENT);

Collection<JAnnotationUse> 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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CharSequence> view;

@ViewById
SomeGenericViewGroup<?> view2;

@ViewById
SomeGenericViewGroup<? extends CharSequence> view3;
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<!--

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.

-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.androidannotations.viewbyid"
android:versionCode="1"
android:versionName="1.0" >

<application>
<activity android:name="org.androidannotations.viewbyid.GenericViewByIdActivity_" />
</application>

</manifest>
Morty Proxy This is a proxified and sanitized view of the page, visit original site.