Skip to content

Commit 87c7896

Browse files
committed
rebinding on the implementation generated class rather then interface in @NonConfigurationInstance. Improved integration tests to validate change. androidannotations#361
1 parent b80fe2d commit 87c7896

File tree

13 files changed

+152
-45
lines changed

13 files changed

+152
-45
lines changed

AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/helper/AnnotationHelper.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@
2020
import java.lang.reflect.Method;
2121
import java.util.ArrayList;
2222
import java.util.List;
23+
import java.util.Map;
2324

2425
import javax.annotation.processing.ProcessingEnvironment;
2526
import javax.lang.model.element.AnnotationMirror;
27+
import javax.lang.model.element.AnnotationValue;
2628
import javax.lang.model.element.Element;
29+
import javax.lang.model.element.ExecutableElement;
2730
import javax.lang.model.element.Modifier;
2831
import javax.lang.model.element.NestingKind;
2932
import javax.lang.model.element.TypeElement;
33+
import javax.lang.model.type.DeclaredType;
3034
import javax.lang.model.type.MirroredTypeException;
3135
import javax.lang.model.type.TypeMirror;
3236
import javax.lang.model.util.Elements;
@@ -304,4 +308,27 @@ public String actionName(Class<? extends Annotation> target) {
304308
return target.getSimpleName() + "ed";
305309
}
306310

311+
public DeclaredType extractAnnotationClassParameter(Element element, Class<? extends Annotation> target) {
312+
313+
AnnotationMirror annotationMirror = findAnnotationMirror(element, target);
314+
315+
Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues = annotationMirror.getElementValues();
316+
317+
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : elementValues.entrySet()) {
318+
/*
319+
* "value" is unset when the default value is used
320+
*/
321+
if ("value".equals(entry.getKey().getSimpleName().toString())) {
322+
323+
AnnotationValue annotationValue = entry.getValue();
324+
325+
DeclaredType annotationClass = (DeclaredType) annotationValue.getValue();
326+
327+
return annotationClass;
328+
}
329+
}
330+
331+
return null;
332+
}
333+
307334
}

AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/helper/TargetAnnotationHelper.java

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,9 @@
1616
package com.googlecode.androidannotations.helper;
1717

1818
import java.lang.annotation.Annotation;
19-
import java.util.Map;
2019

2120
import javax.annotation.processing.ProcessingEnvironment;
22-
import javax.lang.model.element.AnnotationMirror;
23-
import javax.lang.model.element.AnnotationValue;
2421
import javax.lang.model.element.Element;
25-
import javax.lang.model.element.ExecutableElement;
2622
import javax.lang.model.type.DeclaredType;
2723

2824
public class TargetAnnotationHelper extends AnnotationHelper implements HasTarget {
@@ -45,26 +41,7 @@ public <T> T extractAnnotationParameter(Element element, String methodName) {
4541
}
4642

4743
public DeclaredType extractAnnotationClassParameter(Element element) {
48-
49-
AnnotationMirror annotationMirror = findAnnotationMirror(element, target);
50-
51-
Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues = annotationMirror.getElementValues();
52-
53-
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : elementValues.entrySet()) {
54-
/*
55-
* "value" is unset when the default value is used
56-
*/
57-
if ("value".equals(entry.getKey().getSimpleName().toString())) {
58-
59-
AnnotationValue annotationValue = entry.getValue();
60-
61-
DeclaredType annotationClass = (DeclaredType) annotationValue.getValue();
62-
63-
return annotationClass;
64-
}
65-
}
66-
67-
return null;
44+
return extractAnnotationClassParameter(element, target);
6845
}
6946

7047
@Override

AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/processing/NonConfigurationInstanceProcessor.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import javax.annotation.processing.ProcessingEnvironment;
3030
import javax.lang.model.element.Element;
3131
import javax.lang.model.element.TypeElement;
32+
import javax.lang.model.type.DeclaredType;
33+
import javax.lang.model.type.TypeMirror;
3234

3335
import com.googlecode.androidannotations.annotations.Bean;
3436
import com.googlecode.androidannotations.annotations.NonConfigurationInstance;
@@ -137,7 +139,19 @@ public void process(Element element, JCodeModel codeModel, EBeanHolder holder) t
137139

138140
boolean hasBeanAnnotation = element.getAnnotation(Bean.class) != null;
139141
if (hasBeanAnnotation) {
140-
JClass fieldGeneratedBeanClass = holder.refClass(fieldType.fullName() + GENERATION_SUFFIX);
142+
143+
DeclaredType targetAnnotationClassValue = annotationHelper.extractAnnotationClassParameter(element, Bean.class);
144+
145+
TypeMirror elementType;
146+
if (targetAnnotationClassValue != null) {
147+
elementType = targetAnnotationClassValue;
148+
} else {
149+
elementType = element.asType();
150+
}
151+
152+
String typeQualifiedName = elementType.toString();
153+
154+
JClass fieldGeneratedBeanClass = holder.refClass(typeQualifiedName + GENERATION_SUFFIX);
141155

142156
ncHolder.initIfNonConfigurationNotNullBody.invoke(cast(fieldGeneratedBeanClass, field), "rebind").arg(_this());
143157
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.googlecode.androidannotations.ebean;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import com.googlecode.androidannotations.AndroidAnnotationProcessor;
7+
import com.googlecode.androidannotations.utils.AAProcessorTestHelper;
8+
9+
public class EBeanTest extends AAProcessorTestHelper {
10+
11+
@Before
12+
public void setup() {
13+
addManifestProcessorParameter(EBeanTest.class);
14+
addProcessor(AndroidAnnotationProcessor.class);
15+
}
16+
17+
@Test
18+
public void activity_subclass_in_manifest_compiles() {
19+
assertCompilationSuccessful(compileFiles(SomeActivity.class, SomeImplementation.class));
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.googlecode.androidannotations.ebean;
2+
3+
import android.app.Activity;
4+
5+
import com.googlecode.androidannotations.annotations.Bean;
6+
import com.googlecode.androidannotations.annotations.EActivity;
7+
import com.googlecode.androidannotations.annotations.NonConfigurationInstance;
8+
9+
@EActivity
10+
public class SomeActivity extends Activity {
11+
12+
@NonConfigurationInstance
13+
@Bean(SomeImplementation.class)
14+
SomeInterface someInterface;
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.googlecode.androidannotations.ebean;
2+
3+
import com.googlecode.androidannotations.annotations.EBean;
4+
5+
@EBean
6+
public class SomeImplementation implements SomeInterface {
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.googlecode.androidannotations.ebean;
2+
3+
public interface SomeInterface {
4+
5+
}

AndroidAnnotations/androidannotations/src/test/java/com/googlecode/androidannotations/manifest/AndroidManifestErrorsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class AndroidManifestErrorsTest extends AAProcessorTestHelper {
1313
@Before
1414
public void setup() {
1515
addManifestProcessorParameter(AndroidManifestErrorsTest.class);
16-
addProcessor(new AndroidAnnotationProcessor());
16+
addProcessor(AndroidAnnotationProcessor.class);
1717
}
1818

1919
@Test

AndroidAnnotations/androidannotations/src/test/java/com/googlecode/androidannotations/manifest/AndroidManifestFinderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class AndroidManifestFinderTest extends AAProcessorTestHelper {
1717

1818
@Before
1919
public void setup() {
20-
addProcessor(new AndroidAnnotationProcessor());
20+
addProcessor(AndroidAnnotationProcessor.class);
2121
}
2222

2323
@Test

AndroidAnnotations/androidannotations/src/test/java/com/googlecode/androidannotations/utils/AAProcessorTestHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public void addManifestProcessorParameter(Class<?> classOfPackagingContainingMan
1313
}
1414

1515
public File toGeneratedFile(Class<?> compiledClass) {
16-
File output = new File(OUTPUT_DIRECTORY, toPath(compiledClass.getPackage()) + "/" + compiledClass + ModelConstants.GENERATION_SUFFIX + SOURCE_FILE_SUFFIX);
16+
File output = new File(OUTPUT_DIRECTORY, toPath(compiledClass.getPackage()) + "/" + compiledClass.getSimpleName() + ModelConstants.GENERATION_SUFFIX + SOURCE_FILE_SUFFIX);
1717
return output;
1818
}
1919

0 commit comments

Comments
 (0)