From 3ff98e4452011528c7afd6ec2d8b90dccf9cf0ef Mon Sep 17 00:00:00 2001 From: Adrian Rivero Date: Fri, 15 Feb 2019 19:32:38 +0100 Subject: [PATCH] Add Activity and Fragment scope for the @EBean annotation. --- .../androidannotations/annotations/EBean.java | 16 +++ .../api/bean/BeanHolder.java | 24 ++++ .../test/ebean/ActivityScopedBean.java | 22 ++++ .../test/ebean/BeanInjectedActivity.java | 40 +++++- .../test/ebean/CustomFragment.java | 9 ++ .../test/ebean/CyclicBeansFragment.java | 44 +++++++ .../test/ebean/DifferentFragment.java | 7 + .../test/ebean/FragmentScopedBean.java | 22 ++++ .../test/ebean/SomeCyclicActivityScopedA.java | 27 ++++ .../test/ebean/SomeCyclicActivityScopedB.java | 27 ++++ .../test/ebean/SomeCyclicFragmentScopedA.java | 27 ++++ .../test/ebean/SomeCyclicFragmentScopedB.java | 27 ++++ .../test/ebean/BeanInjectedActivityTest.java | 42 ++---- .../test/ebean/BeanScopesTest.java | 122 ++++++++++++++++++ .../test/ebean/CyclicBeansTest.java | 69 ++++++++++ .../test/ebean/CyclicSingletonTest.java | 39 ------ .../holder/EActivityHolder.java | 1 + .../holder/EBeanHolder.java | 69 ++++++++-- .../EComponentWithViewSupportHolder.java | 25 ++++ .../holder/EFragmentHolder.java | 1 + .../internal/core/handler/BeanHandler.java | 2 +- .../internal/core/handler/EBeanHandler.java | 6 +- .../ebean/ActivityScopedBean.java | 22 ++++ .../androidannotations/ebean/EBeanTest.java | 5 + .../ebean/FragmentScopedBean.java | 22 ++++ 25 files changed, 627 insertions(+), 90 deletions(-) create mode 100644 AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/api/bean/BeanHolder.java create mode 100644 AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/ActivityScopedBean.java create mode 100644 AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/CyclicBeansFragment.java create mode 100644 AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/FragmentScopedBean.java create mode 100644 AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/SomeCyclicActivityScopedA.java create mode 100644 AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/SomeCyclicActivityScopedB.java create mode 100644 AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/SomeCyclicFragmentScopedA.java create mode 100644 AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/SomeCyclicFragmentScopedB.java create mode 100644 AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/ebean/BeanScopesTest.java create mode 100644 AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/ebean/CyclicBeansTest.java delete mode 100644 AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/ebean/CyclicSingletonTest.java create mode 100644 AndroidAnnotations/androidannotations-core/androidannotations/src/test/java/org/androidannotations/ebean/ActivityScopedBean.java create mode 100644 AndroidAnnotations/androidannotations-core/androidannotations/src/test/java/org/androidannotations/ebean/FragmentScopedBean.java diff --git a/AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/annotations/EBean.java b/AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/annotations/EBean.java index d53213bb26..be419c261d 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/annotations/EBean.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/annotations/EBean.java @@ -110,6 +110,22 @@ enum Scope { */ Default, // + /** + * A new instance of the bean is created the first time it is needed in an + * Activity scope, it is then retained and the same instance is always returned + * from within the same Activity. Different Activities hold different copies of + * the bean. + */ + Activity, + + /** + * A new instance of the bean is created the first time it is needed in a + * Fragment scope, it is then retained and the same instance is always returned + * from within the same Fragment. Different Fragments hold different copies of + * the bean. + */ + Fragment, + /** * A new instance of the bean is created the first time it is needed, it is then * retained and the same instance is always returned. diff --git a/AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/api/bean/BeanHolder.java b/AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/api/bean/BeanHolder.java new file mode 100644 index 0000000000..44f11311d3 --- /dev/null +++ b/AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/api/bean/BeanHolder.java @@ -0,0 +1,24 @@ +/** + * Copyright (C) 2016-2019 the AndroidAnnotations project + * + * 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.api.bean; + +public interface BeanHolder { + + T getBean(Class key); + + void putBean(Class key, T value); + +} diff --git a/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/ActivityScopedBean.java b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/ActivityScopedBean.java new file mode 100644 index 0000000000..7fcbbcc50e --- /dev/null +++ b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/ActivityScopedBean.java @@ -0,0 +1,22 @@ +/** + * Copyright (C) 2016-2019 the AndroidAnnotations project + * + * 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.test.ebean; + +import org.androidannotations.annotations.EBean; + +@EBean(scope = EBean.Scope.Activity) +public class ActivityScopedBean { +} diff --git a/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/BeanInjectedActivity.java b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/BeanInjectedActivity.java index a7b9ae9823..7afbe61026 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/BeanInjectedActivity.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/BeanInjectedActivity.java @@ -33,17 +33,29 @@ public class BeanInjectedActivity extends Activity { @Bean public SomeSingleton singletonDependency; + @Bean + public ActivityScopedBean activityScopedDependency; + + @Bean + public FragmentScopedBean fragmentScopedDependency; + public EmptyDependency methodInjectedDependency; - public SomeSingleton methodInjectedSingleton; public SomeInterface methodInjectedInterface; + public SomeSingleton methodInjectedSingleton; + public ActivityScopedBean methodInjectedActivityScoped; + public FragmentScopedBean methodInjectedFragmentScoped; public EmptyDependency annotatedParamDependency; - public SomeSingleton annotatedParamSingleton; public SomeInterface annotatedParamInterface; + public SomeSingleton annotatedParamSingleton; + public ActivityScopedBean annotatedParamActivityScoped; + public FragmentScopedBean annotatedParamFragmentScoped; public EmptyDependency multiDependency; - public SomeSingleton multiDependencySingleton; public SomeInterface multiDependencyInterface; + public SomeSingleton multiDependencySingleton; + public ActivityScopedBean multiDependencyActivityScopedBean; + public FragmentScopedBean multiDependencyFragmentScopedBean; @Bean protected void injectDependency(EmptyDependency methodInjectedDependency) { @@ -60,6 +72,16 @@ protected void injectSingleton(SomeSingleton methodInjectedSingleton) { this.methodInjectedSingleton = methodInjectedSingleton; } + @Bean + protected void injectActivityScope(ActivityScopedBean methodInjectedActivityScoped) { + this.methodInjectedActivityScoped = methodInjectedActivityScoped; + } + + @Bean + protected void injectFragmentScope(FragmentScopedBean methodInjectedFragmentScoped) { + this.methodInjectedFragmentScoped = methodInjectedFragmentScoped; + } + protected void injectDependencyAnnotatedParam(@Bean EmptyDependency annotatedParamDependency) { this.annotatedParamDependency = annotatedParamDependency; } @@ -72,10 +94,20 @@ protected void injectSingletonAnnotatedParam(@Bean SomeSingleton annotatedParamS this.annotatedParamSingleton = annotatedParamSingleton; } + protected void injectActivityScopedAnnotatedParam(@Bean ActivityScopedBean annotatedParamActivityScoped) { + this.annotatedParamActivityScoped = annotatedParamActivityScoped; + } + + protected void injectFragmentScopedAnnotatedParam(@Bean FragmentScopedBean annotatedParamFragmentScoped) { + this.annotatedParamFragmentScoped = annotatedParamFragmentScoped; + } + protected void injectMultipleDependencies(@Bean EmptyDependency multiDependency, @Bean(SomeImplementation.class) SomeInterface multiDependencyInterface, - @Bean SomeSingleton multiDependencySingleton) { + @Bean SomeSingleton multiDependencySingleton, @Bean ActivityScopedBean multiDependencyActivityScopedBean, @Bean FragmentScopedBean multiDependecyFragmentScopedBean) { this.multiDependency = multiDependency; this.multiDependencyInterface = multiDependencyInterface; this.multiDependencySingleton = multiDependencySingleton; + this.multiDependencyActivityScopedBean = multiDependencyActivityScopedBean; + this.multiDependencyFragmentScopedBean = multiDependecyFragmentScopedBean; } } diff --git a/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/CustomFragment.java b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/CustomFragment.java index 350feb8e13..5afd432c1f 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/CustomFragment.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/CustomFragment.java @@ -32,4 +32,13 @@ public class CustomFragment extends Fragment { @Bean SomeBeanWithRootFragmentWithDifferentFragment someBeanWithDifferentFragment; + @Bean + ActivityScopedBean activityScopedBean; + + @Bean + FragmentScopedBean fragmentScopedBean1; + + @Bean + FragmentScopedBean fragmentScopedBean2; + } diff --git a/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/CyclicBeansFragment.java b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/CyclicBeansFragment.java new file mode 100644 index 0000000000..403aedca1e --- /dev/null +++ b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/CyclicBeansFragment.java @@ -0,0 +1,44 @@ +/** + * Copyright (C) 2016-2019 the AndroidAnnotations project + * + * 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.test.ebean; + +import org.androidannotations.annotations.Bean; +import org.androidannotations.annotations.EFragment; + +import android.app.Fragment; + +@EFragment +public class CyclicBeansFragment extends Fragment { + + @Bean + SomeCyclicSingletonA singletonA; + + @Bean + SomeCyclicSingletonB singletonB; + + @Bean + SomeCyclicActivityScopedA cyclicActivityScopedA; + + @Bean + SomeCyclicActivityScopedB cyclicActivityScopedB; + + @Bean + SomeCyclicFragmentScopedA cyclicFragmentScopedA; + + @Bean + SomeCyclicFragmentScopedB cyclicFragmentScopedB; + +} diff --git a/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/DifferentFragment.java b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/DifferentFragment.java index d3d4d71159..63e80900b4 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/DifferentFragment.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/DifferentFragment.java @@ -15,6 +15,7 @@ */ package org.androidannotations.test.ebean; +import org.androidannotations.annotations.Bean; import org.androidannotations.annotations.EFragment; import android.app.Fragment; @@ -22,4 +23,10 @@ @EFragment public class DifferentFragment extends Fragment { + @Bean + ActivityScopedBean activityScopedBean; + + @Bean + FragmentScopedBean fragmentScopedBean; + } diff --git a/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/FragmentScopedBean.java b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/FragmentScopedBean.java new file mode 100644 index 0000000000..f97d466561 --- /dev/null +++ b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/FragmentScopedBean.java @@ -0,0 +1,22 @@ +/** + * Copyright (C) 2016-2019 the AndroidAnnotations project + * + * 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.test.ebean; + +import org.androidannotations.annotations.EBean; + +@EBean(scope = EBean.Scope.Fragment) +public class FragmentScopedBean { +} diff --git a/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/SomeCyclicActivityScopedA.java b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/SomeCyclicActivityScopedA.java new file mode 100644 index 0000000000..e48248501c --- /dev/null +++ b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/SomeCyclicActivityScopedA.java @@ -0,0 +1,27 @@ +/** + * Copyright (C) 2016-2019 the AndroidAnnotations project + * + * 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.test.ebean; + +import org.androidannotations.annotations.Bean; +import org.androidannotations.annotations.EBean; + +@EBean(scope = EBean.Scope.Activity) +public class SomeCyclicActivityScopedA { + + @Bean + SomeCyclicActivityScopedB activityScopedB; + +} diff --git a/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/SomeCyclicActivityScopedB.java b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/SomeCyclicActivityScopedB.java new file mode 100644 index 0000000000..6bebc92916 --- /dev/null +++ b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/SomeCyclicActivityScopedB.java @@ -0,0 +1,27 @@ +/** + * Copyright (C) 2016-2019 the AndroidAnnotations project + * + * 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.test.ebean; + +import org.androidannotations.annotations.Bean; +import org.androidannotations.annotations.EBean; + +@EBean(scope = EBean.Scope.Activity) +public class SomeCyclicActivityScopedB { + + @Bean + SomeCyclicActivityScopedA activityScopedA; + +} diff --git a/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/SomeCyclicFragmentScopedA.java b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/SomeCyclicFragmentScopedA.java new file mode 100644 index 0000000000..a3fb439a16 --- /dev/null +++ b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/SomeCyclicFragmentScopedA.java @@ -0,0 +1,27 @@ +/** + * Copyright (C) 2016-2019 the AndroidAnnotations project + * + * 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.test.ebean; + +import org.androidannotations.annotations.Bean; +import org.androidannotations.annotations.EBean; + +@EBean(scope = EBean.Scope.Fragment) +public class SomeCyclicFragmentScopedA { + + @Bean + SomeCyclicFragmentScopedB fragmentScopedB; + +} diff --git a/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/SomeCyclicFragmentScopedB.java b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/SomeCyclicFragmentScopedB.java new file mode 100644 index 0000000000..7aa371e851 --- /dev/null +++ b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/main/java/org/androidannotations/test/ebean/SomeCyclicFragmentScopedB.java @@ -0,0 +1,27 @@ +/** + * Copyright (C) 2016-2019 the AndroidAnnotations project + * + * 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.test.ebean; + +import org.androidannotations.annotations.Bean; +import org.androidannotations.annotations.EBean; + +@EBean(scope = EBean.Scope.Fragment) +public class SomeCyclicFragmentScopedB { + + @Bean + SomeCyclicFragmentScopedA fragmentScopedA; + +} diff --git a/AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/ebean/BeanInjectedActivityTest.java b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/ebean/BeanInjectedActivityTest.java index dfb452c03a..b14fcc02d7 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/ebean/BeanInjectedActivityTest.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/ebean/BeanInjectedActivityTest.java @@ -1,5 +1,6 @@ /** * Copyright (C) 2010-2016 eBusiness Information, Excilys Group + * Copyright (C) 2016-2019 the AndroidAnnotations project * * 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 @@ -78,43 +79,24 @@ public void methodAnnotatedParamsDependencyWithAnnotationValueIsOfAnnotationValu assertThat(activity.annotatedParamInterface).isInstanceOf(SomeImplementation.class); } - @Test - public void singletonDependencyIsSameReference() { - SomeSingleton initialDependency = activity.singletonDependency; - - BeanInjectedActivity_ newActivity = Robolectric.buildActivity(BeanInjectedActivity_.class).create().get(); - - assertThat(newActivity.singletonDependency).isSameAs(initialDependency); - } - - @Test - public void methodInjectedSingletonDependencyIsSameReference() { - SomeSingleton initialDependency = activity.methodInjectedSingleton; - - BeanInjectedActivity_ newActivity = Robolectric.buildActivity(BeanInjectedActivity_.class).create().get(); - - assertThat(newActivity.methodInjectedSingleton).isSameAs(initialDependency); - } - - @Test - public void methodAnnotatedParamsSingletonDependencyIsSameReference() { - SomeSingleton initialDependency = activity.annotatedParamSingleton; - - BeanInjectedActivity_ newActivity = Robolectric.buildActivity(BeanInjectedActivity_.class).create().get(); - - assertThat(newActivity.annotatedParamSingleton).isSameAs(initialDependency); - } - @Test public void multipleDependenciesInjected() { assertThat(activity.multiDependency).isNotNull(); assertThat(activity.multiDependencyInterface).isNotNull(); assertThat(activity.multiDependencySingleton).isNotNull(); - + assertThat(activity.multiDependencyActivityScopedBean).isNotNull(); + assertThat(activity.multiDependencyFragmentScopedBean).isNotNull(); assertThat(activity.multiDependencyInterface).isInstanceOf(SomeImplementation.class); - SomeSingleton initialDependency = activity.multiDependencySingleton; BeanInjectedActivity_ newActivity = Robolectric.buildActivity(BeanInjectedActivity_.class).create().get(); - assertThat(newActivity.multiDependencySingleton).isSameAs(initialDependency); + + SomeSingleton initialSingletonDependency = activity.multiDependencySingleton; + assertThat(newActivity.multiDependencySingleton).isSameAs(initialSingletonDependency); + + ActivityScopedBean initialActivityScopedDependency = activity.multiDependencyActivityScopedBean; + assertThat(newActivity.multiDependencyActivityScopedBean).isNotSameAs(initialActivityScopedDependency); + + FragmentScopedBean initialFragmentScopedDependency = activity.multiDependencyFragmentScopedBean; + assertThat(newActivity.multiDependencyFragmentScopedBean).isNotSameAs(initialFragmentScopedDependency); } } diff --git a/AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/ebean/BeanScopesTest.java b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/ebean/BeanScopesTest.java new file mode 100644 index 0000000000..a459f1c4c2 --- /dev/null +++ b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/ebean/BeanScopesTest.java @@ -0,0 +1,122 @@ +/** + * Copyright (C) 2016-2019 the AndroidAnnotations project + * + * 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.test.ebean; + +import static org.fest.assertions.api.Assertions.assertThat; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; + +import android.app.Fragment; +import android.app.FragmentManager; +import android.app.FragmentTransaction; + +@RunWith(RobolectricTestRunner.class) +public class BeanScopesTest { + + private BeanInjectedActivity_ activity; + private CustomFragment_ fragment1; + private DifferentFragment_ fragment2; + + private void startFragment(Fragment fragment) { + FragmentManager fragmentManager = activity.getFragmentManager(); + FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); + fragmentTransaction.add(fragment, null); + fragmentTransaction.commit(); + } + + @Before + public void setUp() { + activity = Robolectric.buildActivity(BeanInjectedActivity_.class).create().get(); + + fragment1 = new CustomFragment_(); + fragment2 = new DifferentFragment_(); + + startFragment(fragment1); + startFragment(fragment2); + } + + @Test + public void singletonDependencyIsSameReference() { + SomeSingleton initialDependency = activity.singletonDependency; + + BeanInjectedActivity_ newActivity = Robolectric.buildActivity(BeanInjectedActivity_.class).create().get(); + + assertThat(newActivity.singletonDependency).isSameAs(initialDependency); + } + + @Test + public void methodInjectedSingletonDependencyIsSameReference() { + SomeSingleton initialDependency = activity.methodInjectedSingleton; + + BeanInjectedActivity_ newActivity = Robolectric.buildActivity(BeanInjectedActivity_.class).create().get(); + + assertThat(newActivity.methodInjectedSingleton).isSameAs(initialDependency); + } + + @Test + public void methodAnnotatedParamsSingletonDependencyIsSameReference() { + SomeSingleton initialDependency = activity.annotatedParamSingleton; + + BeanInjectedActivity_ newActivity = Robolectric.buildActivity(BeanInjectedActivity_.class).create().get(); + + assertThat(newActivity.annotatedParamSingleton).isSameAs(initialDependency); + } + + @Test + public void allSingletonDependenciesInSameActivityAreSameReference() { + assertThat(activity.singletonDependency).isSameAs(activity.annotatedParamSingleton); + assertThat(activity.singletonDependency).isSameAs(activity.methodInjectedSingleton); + assertThat(activity.singletonDependency).isSameAs(activity.multiDependencySingleton); + } + + @Test + public void allActivityScopedDependenciesInSameActivityAreSameReference() { + assertThat(activity.activityScopedDependency).isSameAs(activity.annotatedParamActivityScoped); + assertThat(activity.activityScopedDependency).isSameAs(activity.methodInjectedActivityScoped); + assertThat(activity.activityScopedDependency).isSameAs(activity.multiDependencyActivityScopedBean); + } + + @Test + public void allFragmentScopedDependenciesInSameActivityAreNotSameReference() { + assertThat(activity.fragmentScopedDependency).isNotSameAs(activity.annotatedParamFragmentScoped); + assertThat(activity.fragmentScopedDependency).isNotSameAs(activity.methodInjectedFragmentScoped); + assertThat(activity.fragmentScopedDependency).isNotSameAs(activity.multiDependencyFragmentScopedBean); + } + + @Test + public void allActivityScopedDependenciesInFragmentsAreSameReferenceAsInActivity() { + assertThat(activity.activityScopedDependency).isSameAs(fragment1.activityScopedBean); + assertThat(activity.activityScopedDependency).isSameAs(fragment2.activityScopedBean); + } + + @Test + public void allFragmentScopedDependenciesInFragmentAreSameReference() { + assertThat(fragment1.fragmentScopedBean1).isSameAs(fragment1.fragmentScopedBean2); + } + + @Test + public void fragmentScopedDependenciesAreNotSameReferenceIfNotOnSameFragment() { + assertThat(activity.fragmentScopedDependency).isNotSameAs(fragment1.fragmentScopedBean1); + assertThat(activity.fragmentScopedDependency).isNotSameAs(fragment1.fragmentScopedBean2); + assertThat(activity.fragmentScopedDependency).isNotSameAs(fragment2.fragmentScopedBean); + assertThat(fragment1.fragmentScopedBean1).isNotSameAs(fragment2.fragmentScopedBean); + } + +} diff --git a/AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/ebean/CyclicBeansTest.java b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/ebean/CyclicBeansTest.java new file mode 100644 index 0000000000..5432563d37 --- /dev/null +++ b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/ebean/CyclicBeansTest.java @@ -0,0 +1,69 @@ +/** + * Copyright (C) 2010-2016 eBusiness Information, Excilys Group + * Copyright (C) 2016-2019 the AndroidAnnotations project + * + * 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.test.ebean; + +import static org.fest.assertions.api.Assertions.assertThat; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; + +import android.app.Fragment; +import android.app.FragmentManager; +import android.app.FragmentTransaction; + +@RunWith(RobolectricTestRunner.class) +public class CyclicBeansTest { + + private BeanInjectedActivity_ activity; + private CyclicBeansFragment_ fragment; + + private void startFragment(Fragment fragment) { + FragmentManager fragmentManager = activity.getFragmentManager(); + FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); + fragmentTransaction.add(fragment, null); + fragmentTransaction.commit(); + } + + @Before + public void setUp() { + activity = Robolectric.buildActivity(BeanInjectedActivity_.class).create().get(); + fragment = new CyclicBeansFragment_(); + startFragment(fragment); + } + + @Test + public void cyclicSingleton() { + assertThat(fragment.singletonB).isSameAs(fragment.singletonA.singletonB); + assertThat(fragment.singletonA).isSameAs(fragment.singletonB.singletonA); + } + + @Test + public void cyclicActivityScoped() { + assertThat(fragment.cyclicActivityScopedA).isSameAs(fragment.cyclicActivityScopedB.activityScopedA); + assertThat(fragment.cyclicActivityScopedB).isSameAs(fragment.cyclicActivityScopedA.activityScopedB); + } + + @Test + public void cyclicFragmentScoped() { + assertThat(fragment.cyclicFragmentScopedA).isSameAs(fragment.cyclicFragmentScopedB.fragmentScopedA); + assertThat(fragment.cyclicFragmentScopedB).isSameAs(fragment.cyclicFragmentScopedA.fragmentScopedB); + } + +} diff --git a/AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/ebean/CyclicSingletonTest.java b/AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/ebean/CyclicSingletonTest.java deleted file mode 100644 index bc80c05a61..0000000000 --- a/AndroidAnnotations/androidannotations-core/androidannotations-test/src/test/java/org/androidannotations/test/ebean/CyclicSingletonTest.java +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Copyright (C) 2010-2016 eBusiness Information, Excilys Group - * Copyright (C) 2016-2019 the AndroidAnnotations project - * - * 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.test.ebean; - -import static org.fest.assertions.api.Assertions.assertThat; -import static org.robolectric.Robolectric.setupActivity; - -import org.androidannotations.test.EmptyActivityWithoutLayout_; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; - -@RunWith(RobolectricTestRunner.class) -public class CyclicSingletonTest { - - @Test - public void cyclicSingleton() { - EmptyActivityWithoutLayout_ context = setupActivity(EmptyActivityWithoutLayout_.class); - SomeCyclicSingletonA_ singletonA = SomeCyclicSingletonA_.getInstance_(context); - SomeCyclicSingletonB_ singletonB = SomeCyclicSingletonB_.getInstance_(context); - assertThat(singletonA.singletonB).isSameAs(singletonB); - assertThat(singletonB.singletonA).isSameAs(singletonA); - } - -} diff --git a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/EActivityHolder.java b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/EActivityHolder.java index a946cfb2e5..a5ad4c1f03 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/EActivityHolder.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/EActivityHolder.java @@ -113,6 +113,7 @@ public EActivityHolder(AndroidAnnotationsEnvironment environment, TypeElement an setSetContentView(); intentBuilder = new ActivityIntentBuilder(this, androidManifest); intentBuilder.build(); + implementBeanHolder(); } @Override diff --git a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/EBeanHolder.java b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/EBeanHolder.java index 1317b50702..f871f169d2 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/EBeanHolder.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/EBeanHolder.java @@ -18,6 +18,7 @@ import static com.helger.jcodemodel.JExpr._new; import static com.helger.jcodemodel.JExpr._null; +import static com.helger.jcodemodel.JExpr.cast; import static com.helger.jcodemodel.JMod.PRIVATE; import static com.helger.jcodemodel.JMod.PUBLIC; import static com.helger.jcodemodel.JMod.STATIC; @@ -30,11 +31,15 @@ import javax.lang.model.util.ElementFilter; import org.androidannotations.AndroidAnnotationsEnvironment; +import org.androidannotations.annotations.EBean; +import org.androidannotations.api.bean.BeanHolder; import com.helger.jcodemodel.AbstractJClass; +import com.helger.jcodemodel.IJExpression; import com.helger.jcodemodel.JBlock; import com.helger.jcodemodel.JExpr; import com.helger.jcodemodel.JFieldVar; +import com.helger.jcodemodel.JInvocation; import com.helger.jcodemodel.JMethod; import com.helger.jcodemodel.JVar; @@ -112,41 +117,54 @@ public void invokeInitInConstructors() { overloadedConstructorBody.invoke(getInit()); } - public void createFactoryMethod(boolean hasSingletonScope) { + public void createFactoryMethod(EBean.Scope scope) { AbstractJClass narrowedGeneratedClass = codeModelHelper.narrowGeneratedClass(generatedClass, annotatedElement.asType()); JMethod factoryMethod = generatedClass.method(PUBLIC | STATIC, narrowedGeneratedClass, GET_INSTANCE_METHOD_NAME); - codeModelHelper.generify(factoryMethod, annotatedElement); JVar factoryMethodContextParam = factoryMethod.param(getClasses().CONTEXT, "context"); JBlock factoryMethodBody = factoryMethod.body(); - /* - * Singletons are bound to the application context - */ - if (hasSingletonScope) { + switch (scope) { + case Default: + factoryMethodBody._return(_new(narrowedGeneratedClass).arg(factoryMethodContextParam)); + createOverloadedFactoryMethod(scope); + break; + + case Fragment: + JVar beanVar = factoryMethodBody.decl(getGeneratedClass(), "bean", _new(narrowedGeneratedClass).arg(factoryMethodContextParam)); + factoryMethodBody.invoke(beanVar, getInit()); + factoryMethodBody._return(beanVar); + + createOverloadedFactoryMethod(scope); + break; + + case Activity: + requestInstanceFromBeanHolder(factoryMethodContextParam, factoryMethodContextParam, factoryMethodBody); + beanVar = factoryMethodBody.decl(getGeneratedClass(), "bean", _new(narrowedGeneratedClass).arg(factoryMethodContextParam)); + factoryMethodBody.invoke(beanVar, getInit()); + factoryMethodBody._return(beanVar); + break; + + case Singleton: JFieldVar instanceField = generatedClass.field(PRIVATE | STATIC, generatedClass, "instance" + generationSuffix()); - JBlock creationBlock = factoryMethodBody // - ._if(instanceField.eq(_null())) // - ._then(); + JBlock creationBlock = factoryMethodBody._if(instanceField.eq(_null()))._then(); JVar previousNotifier = viewNotifierHelper.replacePreviousNotifierWithNull(creationBlock); creationBlock.assign(instanceField, _new(narrowedGeneratedClass).arg(factoryMethodContextParam.invoke("getApplicationContext"))); creationBlock.invoke(instanceField, getInit()); viewNotifierHelper.resetPreviousNotifier(creationBlock, previousNotifier); factoryMethodBody._return(instanceField); - } else { - factoryMethodBody._return(_new(narrowedGeneratedClass).arg(factoryMethodContextParam)); - createOverloadedFactoryMethod(); + break; } } - private void createOverloadedFactoryMethod() { + private void createOverloadedFactoryMethod(EBean.Scope scope) { AbstractJClass narrowedGeneratedClass = codeModelHelper.narrowGeneratedClass(generatedClass, annotatedElement.asType()); JMethod factoryMethod = generatedClass.method(PUBLIC | STATIC, narrowedGeneratedClass, GET_INSTANCE_METHOD_NAME); codeModelHelper.generify(factoryMethod, annotatedElement); @@ -155,9 +173,34 @@ private void createOverloadedFactoryMethod() { JVar factoryMethodRootFragmentParam = factoryMethod.param(getClasses().OBJECT, "rootFragment"); JBlock factoryMethodBody = factoryMethod.body(); + + if (scope == EBean.Scope.Fragment) { + requestInstanceFromBeanHolder(factoryMethodRootFragmentParam, factoryMethodContextParam, factoryMethodBody); + } + factoryMethodBody._return(_new(narrowedGeneratedClass).arg(factoryMethodContextParam).arg(factoryMethodRootFragmentParam)); } + private void requestInstanceFromBeanHolder(IJExpression fieldRef, IJExpression contextRef, JBlock block) { + AbstractJClass beanHolderClass = getJClass(BeanHolder.class); + + JBlock ifBlock = block._if(fieldRef._instanceof(beanHolderClass))._then(); + JVar beanHolderVar = ifBlock.decl(beanHolderClass, "beanHolder", cast(beanHolderClass, fieldRef)); + JVar beanVar = ifBlock.decl(getGeneratedClass(), "bean", beanHolderVar.invoke("getBean").arg(getGeneratedClass().dotclass())); + + JInvocation newBeanExpression = _new(getGeneratedClass()).arg(contextRef); + if (fieldRef != contextRef) { + newBeanExpression = newBeanExpression.arg(fieldRef); + } + + JBlock ifBeanNullBlock = ifBlock._if(beanVar.eq(_null()))._then(); + ifBeanNullBlock.assign(beanVar, newBeanExpression); + ifBeanNullBlock.add(beanHolderVar.invoke("putBean").arg(getGeneratedClass().dotclass()).arg(beanVar)); + ifBeanNullBlock.invoke(beanVar, getInit()); + + ifBlock._return(beanVar); + } + public void createRebindMethod() { JMethod rebindMethod = generatedClass.method(PUBLIC, getCodeModel().VOID, "rebind"); JVar contextParam = rebindMethod.param(getClasses().CONTEXT, "context"); diff --git a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/EComponentWithViewSupportHolder.java b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/EComponentWithViewSupportHolder.java index 03c722c501..0e6ff84b03 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/EComponentWithViewSupportHolder.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/EComponentWithViewSupportHolder.java @@ -33,6 +33,7 @@ import javax.lang.model.util.Elements; import org.androidannotations.AndroidAnnotationsEnvironment; +import org.androidannotations.api.bean.BeanHolder; import org.androidannotations.api.view.HasViews; import org.androidannotations.api.view.OnViewChangedListener; import org.androidannotations.api.view.OnViewChangedNotifier; @@ -44,6 +45,7 @@ import com.helger.jcodemodel.IJExpression; import com.helger.jcodemodel.JBlock; import com.helger.jcodemodel.JDefinedClass; +import com.helger.jcodemodel.JDirectClass; import com.helger.jcodemodel.JExpr; import com.helger.jcodemodel.JFieldRef; import com.helger.jcodemodel.JFieldVar; @@ -140,6 +142,29 @@ protected void setOnViewChanged() { getInitBodyInjectionBlock().add(notifierClass.staticInvoke("registerOnViewChangedListener").arg(_this())); } + protected void implementBeanHolder() { + getGeneratedClass()._implements(BeanHolder.class); + JDirectClass genericType = getCodeModel().directClass("T"); + + JFieldVar beansField = getGeneratedClass().field(PRIVATE | FINAL, getClasses().MAP.narrow(getClasses().CLASS.narrowAny(), getClasses().OBJECT), "beans_", + _new(getClasses().HASH_MAP.narrow(getClasses().CLASS.narrowAny(), getClasses().OBJECT))); + + JMethod getBeanMethod = getGeneratedClass().method(PUBLIC, genericType, "getBean"); + getBeanMethod.generify("T"); + getBeanMethod.annotate(Override.class); + + JVar keyParam = getBeanMethod.param(getClasses().CLASS.narrow(genericType), "key"); + getBeanMethod.body()._return(cast(genericType, beansField.invoke("get").arg(keyParam))); + + JMethod putBeanMethod = getGeneratedClass().method(PUBLIC, getCodeModel().VOID, "putBean"); + putBeanMethod.generify("T"); + putBeanMethod.annotate(Override.class); + + keyParam = putBeanMethod.param(getClasses().CLASS.narrow(genericType), "key"); + JVar valueParam = putBeanMethod.param(genericType, "value"); + putBeanMethod.body().add(beansField.invoke("put").arg(keyParam).arg(valueParam)); + } + public JInvocation findViewById(JFieldRef idRef) { JInvocation findViewById = invoke(getOnViewChangedHasViewsParam(), "internalFindViewById"); findViewById.arg(idRef); diff --git a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/EFragmentHolder.java b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/EFragmentHolder.java index a37ffdfd9e..52703c2486 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/EFragmentHolder.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/holder/EFragmentHolder.java @@ -96,6 +96,7 @@ public EFragmentHolder(AndroidAnnotationsEnvironment environment, TypeElement an setOnCreate(); setOnViewCreated(); setFragmentBuilder(); + implementBeanHolder(); } private void setOnCreate() { diff --git a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/BeanHandler.java b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/BeanHandler.java index 9090fd10a6..c93378ab1c 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/BeanHandler.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/BeanHandler.java @@ -86,7 +86,7 @@ public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, ECompon TypeElement declaredEBean = getProcessingEnvironment().getElementUtils().getTypeElement(typeQualifiedName); if (declaredEBean != null) { EBean annotation = declaredEBean.getAnnotation(EBean.class); - if (annotation.scope() == EBean.Scope.Default) { + if (annotation.scope() != EBean.Scope.Singleton && annotation.scope() != EBean.Scope.Activity) { beanInstance.arg(holder.getRootFragmentRef()); } } diff --git a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/EBeanHandler.java b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/EBeanHandler.java index 5a503d5c7e..b65035d832 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/EBeanHandler.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/EBeanHandler.java @@ -51,11 +51,11 @@ public void validate(Element element, ElementValidation valid) { public void process(Element element, EBeanHolder holder) { EBean eBeanAnnotation = element.getAnnotation(EBean.class); EBean.Scope eBeanScope = eBeanAnnotation.scope(); - boolean hasSingletonScope = eBeanScope == EBean.Scope.Singleton; + boolean hasDefaultScope = eBeanScope == EBean.Scope.Default; - holder.createFactoryMethod(hasSingletonScope); + holder.createFactoryMethod(eBeanScope); - if (!hasSingletonScope) { + if (hasDefaultScope) { holder.invokeInitInConstructors(); holder.createRebindMethod(); } diff --git a/AndroidAnnotations/androidannotations-core/androidannotations/src/test/java/org/androidannotations/ebean/ActivityScopedBean.java b/AndroidAnnotations/androidannotations-core/androidannotations/src/test/java/org/androidannotations/ebean/ActivityScopedBean.java new file mode 100644 index 0000000000..be1f23c690 --- /dev/null +++ b/AndroidAnnotations/androidannotations-core/androidannotations/src/test/java/org/androidannotations/ebean/ActivityScopedBean.java @@ -0,0 +1,22 @@ +/** + * Copyright (C) 2016-2019 the AndroidAnnotations project + * + * 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.ebean; + +import org.androidannotations.annotations.EBean; + +@EBean(scope = EBean.Scope.Activity) +public class ActivityScopedBean { +} diff --git a/AndroidAnnotations/androidannotations-core/androidannotations/src/test/java/org/androidannotations/ebean/EBeanTest.java b/AndroidAnnotations/androidannotations-core/androidannotations/src/test/java/org/androidannotations/ebean/EBeanTest.java index a258b18968..4383d711d8 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations/src/test/java/org/androidannotations/ebean/EBeanTest.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations/src/test/java/org/androidannotations/ebean/EBeanTest.java @@ -35,6 +35,11 @@ public void activitySubclassInManifestCompiles() { SomeBeanWithContextParamConstructor.class)); } + @Test + public void eBeansWithScopeCompile() { + assertCompilationSuccessful(compileFiles(SingletonBean.class, ActivityScopedBean.class, FragmentScopedBean.class)); + } + @Test public void eBeanOnInterfaceDoesNotCompile() { assertCompilationError(compileFiles(InterfaceWithEBean.class)); diff --git a/AndroidAnnotations/androidannotations-core/androidannotations/src/test/java/org/androidannotations/ebean/FragmentScopedBean.java b/AndroidAnnotations/androidannotations-core/androidannotations/src/test/java/org/androidannotations/ebean/FragmentScopedBean.java new file mode 100644 index 0000000000..079a5da46d --- /dev/null +++ b/AndroidAnnotations/androidannotations-core/androidannotations/src/test/java/org/androidannotations/ebean/FragmentScopedBean.java @@ -0,0 +1,22 @@ +/** + * Copyright (C) 2016-2019 the AndroidAnnotations project + * + * 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.ebean; + +import org.androidannotations.annotations.EBean; + +@EBean(scope = EBean.Scope.Fragment) +public class FragmentScopedBean { +}