Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Commit 017b589

Browse files
committed
Merge pull request #862 from excilys/858_namedGenerics
Handle named generics on methods
2 parents 58614d2 + 6221980 commit 017b589

3 files changed

Lines changed: 66 additions & 1 deletion

File tree

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/APTCodeModelHelper.java‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import javax.lang.model.element.Element;
2424
import javax.lang.model.element.ExecutableElement;
2525
import javax.lang.model.element.TypeElement;
26+
import javax.lang.model.element.TypeParameterElement;
2627
import javax.lang.model.element.VariableElement;
2728
import javax.lang.model.type.ArrayType;
2829
import javax.lang.model.type.DeclaredType;
@@ -109,7 +110,6 @@ public Parameter(String name, JClass jClass) {
109110
public JMethod overrideAnnotatedMethod(ExecutableElement executableElement, GeneratedClassHolder holder) {
110111

111112
String methodName = executableElement.getSimpleName().toString();
112-
113113
JClass returnType = typeMirrorToJClass(executableElement.getReturnType(), holder);
114114

115115
List<Parameter> parameters = new ArrayList<Parameter>();
@@ -139,6 +139,21 @@ public JMethod overrideAnnotatedMethod(ExecutableElement executableElement, Gene
139139
method._throws(thrownType);
140140
}
141141

142+
for (TypeParameterElement typeParameter : executableElement.getTypeParameters()) {
143+
List<? extends TypeMirror> bounds = typeParameter.getBounds();
144+
145+
JClass jClassBounds;
146+
if (bounds.isEmpty()) {
147+
jClassBounds = holder.classes().OBJECT;
148+
} else {
149+
// Currently Codemodel can't generate generics with multiple
150+
// classes like this <T extends Number & Serializable>.
151+
// So we only take the first class
152+
jClassBounds = typeMirrorToJClass(bounds.get(0), holder);
153+
}
154+
method.generify(typeParameter.toString(), jClassBounds);
155+
}
156+
142157
callSuperMethod(method, holder, method.body());
143158

144159
return method;

‎AndroidAnnotations/functional-test-1-5/AndroidManifest.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
<activity android:name="org.androidannotations.test15.AwaitingResultActivity_" />
8383
<activity android:name=".HierarchyViewerActivity_" />
8484
<activity android:name="org.androidannotations.test15.CustomTitleActivity_" />
85+
<activity android:name=".ActivityWithGenerics_" />
8586

8687
<receiver android:name="org.androidannotations.test15.ereceiver.MyReceiver_" />
8788

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright (C) 2010-2013 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.test15;
17+
18+
import java.util.List;
19+
20+
import org.androidannotations.annotations.Background;
21+
import org.androidannotations.annotations.EActivity;
22+
import org.androidannotations.annotations.UiThread;
23+
24+
import android.app.Activity;
25+
26+
@EActivity
27+
public class ActivityWithGenerics extends Activity {
28+
29+
// @UiThread
30+
// <T, S extends Number & List<String>> void emptyUiMethod(T param, S param2) {
31+
// // Not possible due to Codemodel's constraints
32+
// }
33+
34+
@UiThread
35+
<T, S extends Number> void emptyUiMethod(T param) {
36+
37+
}
38+
39+
@Background
40+
<T, S extends Number> void emptyBackgroundMethod(T param) {
41+
42+
}
43+
44+
@Background
45+
<T extends Number> void emptyBackgroundMethod(T param, List<T> param2) {
46+
47+
}
48+
49+
}

0 commit comments

Comments
 (0)