Skip to content

Commit 8783de5

Browse files
committed
Handle named generics on methods
1 parent 58614d2 commit 8783de5

2 files changed

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

0 commit comments

Comments
 (0)