Skip to content

Commit f5f9742

Browse files
author
Drozdov Artyom
committed
fix: wrong JClass resolving in EViewHolder: to get JClass from TypeMirror there is a APTCodeModelHelper.typeMirrorToJClass. It handles several corner cases with generics, while ProcessHolder.refClass - don't.
fix: factories in generated ViewGroup's now correctly generified
1 parent d0997d9 commit f5f9742

6 files changed

Lines changed: 162 additions & 4 deletions

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@ public JMethod overrideAnnotatedMethod(ExecutableElement executableElement, Gene
216216
return method;
217217
}
218218

219+
public void generifyStaticHelper(GeneratedClassHolder holder, JMethod staticHelper, TypeElement annotatedClass) {
220+
for (TypeParameterElement param : annotatedClass.getTypeParameters()) {
221+
JClass bounds = typeBoundsToJClass(holder, param.getBounds());
222+
staticHelper.generify(param.getSimpleName().toString(), bounds);
223+
}
224+
}
225+
219226
private JMethod findAlreadyGeneratedMethod(ExecutableElement executableElement, GeneratedClassHolder holder) {
220227
JDefinedClass definedClass = holder.getGeneratedClass();
221228
String methodName = executableElement.getSimpleName().toString();
@@ -497,5 +504,4 @@ public JExpression litObject(Object o) {
497504
return lit((String) o);
498505
}
499506
}
500-
501507
}

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/holder/EViewHolder.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,17 @@ private void createConstructorAndBuilder() {
7171
for (ExecutableElement userConstructor : constructors) {
7272
JMethod copyConstructor = generatedClass.constructor(PUBLIC);
7373
JMethod staticHelper = generatedClass.method(PUBLIC | STATIC, generatedClass._extends(), "build");
74+
75+
codeModelHelper.generifyStaticHelper(this, staticHelper, getAnnotatedElement());
76+
7477
JBlock body = copyConstructor.body();
7578
JInvocation superCall = body.invoke("super");
7679
JInvocation newInvocation = JExpr._new(generatedClass);
7780
for (VariableElement param : userConstructor.getParameters()) {
7881
String paramName = param.getSimpleName().toString();
79-
String paramType = param.asType().toString();
80-
copyConstructor.param(refClass(paramType), paramName);
81-
staticHelper.param(refClass(paramType), paramName);
82+
JClass paramType = codeModelHelper.typeMirrorToJClass(param.asType(), this);
83+
copyConstructor.param(paramType, paramName);
84+
staticHelper.param(paramType, paramName);
8285
superCall.arg(JExpr.ref(paramName));
8386
newInvocation.arg(JExpr.ref(paramName));
8487
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright (C) 2010-2014 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.eviewgroup;
17+
18+
import org.androidannotations.AndroidAnnotationProcessor;
19+
import org.androidannotations.utils.AAProcessorTestHelper;
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
23+
public class EViewGroupTest extends AAProcessorTestHelper {
24+
@Before
25+
public void setup() {
26+
addManifestProcessorParameter(EViewGroupTest.class);
27+
addProcessor(AndroidAnnotationProcessor.class);
28+
}
29+
30+
@Test
31+
public void view_group_with_generic_compiles() {
32+
assertCompilationSuccessful(compileFiles(
33+
SomeGenericViewGroup.class,
34+
SomeGenericViewGroupExt.class));
35+
}
36+
37+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright (C) 2010-2014 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.eviewgroup;
17+
18+
import android.content.Context;
19+
import android.widget.FrameLayout;
20+
import org.androidannotations.annotations.EViewGroup;
21+
import org.androidannotations.annotations.UiThread;
22+
23+
@EViewGroup
24+
public class SomeGenericViewGroup<T extends CharSequence> extends FrameLayout {
25+
26+
interface Test<T> {
27+
void test(T t);
28+
}
29+
30+
private T object;
31+
private Test<T> testInterface;
32+
33+
public SomeGenericViewGroup(Context context) {
34+
super(context);
35+
}
36+
37+
public SomeGenericViewGroup(Context context, T object, Test<T> testInterface) {
38+
super(context);
39+
this.object = object;
40+
this.testInterface = testInterface;
41+
}
42+
43+
@UiThread
44+
void someGenericMethod(T type) {
45+
}
46+
47+
@UiThread
48+
void someGenericMethod2(T type) {
49+
}
50+
51+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright (C) 2010-2014 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.eviewgroup;
17+
18+
import android.content.Context;
19+
import org.androidannotations.annotations.EViewGroup;
20+
import org.androidannotations.annotations.UiThread;
21+
22+
@EViewGroup
23+
public class SomeGenericViewGroupExt extends SomeGenericViewGroup<String> {
24+
25+
public SomeGenericViewGroupExt(Context context) {
26+
super(context);
27+
}
28+
29+
public SomeGenericViewGroupExt(Context context, String object, Test<String> testInterface) {
30+
super(context, object, testInterface);
31+
}
32+
33+
@Override
34+
@UiThread
35+
void someGenericMethod(String type) {
36+
}
37+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
4+
Copyright (C) 2010-2014 eBusiness Information, Excilys Group
5+
6+
Licensed under the Apache License, Version 2.0 (the "License"); you may not
7+
use this file except in compliance with the License. You may obtain a copy of
8+
the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed To in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15+
License for the specific language governing permissions and limitations under
16+
the License.
17+
18+
-->
19+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
20+
package="org.androidannotations.testprocessor"
21+
android:versionCode="1"
22+
android:versionName="1.0">
23+
24+
</manifest>

0 commit comments

Comments
 (0)