Skip to content

Commit 1dc834d

Browse files
committed
Merge pull request androidannotations#1245 from jiongxuan/943_release_openhelpermanager
Release database helper with @OrmLiteDao
2 parents ad7fbb0 + ee9e1a8 commit 1dc834d

9 files changed

Lines changed: 192 additions & 18 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.helper;
17+
18+
import static com.sun.codemodel.JExpr._null;
19+
20+
import org.androidannotations.holder.HasLifecycleMethods;
21+
import org.androidannotations.process.ProcessHolder;
22+
23+
import com.sun.codemodel.JBlock;
24+
import com.sun.codemodel.JFieldVar;
25+
26+
public class OrmLiteHelper {
27+
28+
public static void injectReleaseInDestroy(JFieldVar databaseHelperRef, HasLifecycleMethods holder, ProcessHolder.Classes classes) {
29+
JBlock destroyBody = holder.getOnDestroyBeforeSuperBlock();
30+
31+
destroyBody.staticInvoke(classes.OPEN_HELPER_MANAGER, "releaseHelper");
32+
destroyBody.assign(databaseHelperRef, _null());
33+
}
34+
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,27 @@
2727
import com.sun.codemodel.JMod;
2828
import com.sun.codemodel.JType;
2929
import com.sun.codemodel.JVar;
30+
3031
import org.androidannotations.api.SdkVersionHelper;
3132
import org.androidannotations.helper.ActionBarSherlockHelper;
3233
import org.androidannotations.helper.ActivityIntentBuilder;
3334
import org.androidannotations.helper.AndroidManifest;
3435
import org.androidannotations.helper.AnnotationHelper;
3536
import org.androidannotations.helper.CanonicalNameConstants;
3637
import org.androidannotations.helper.IntentBuilder;
38+
import org.androidannotations.helper.OrmLiteHelper;
3739
import org.androidannotations.process.ProcessHolder;
3840

41+
import java.util.ArrayList;
42+
import java.util.List;
43+
3944
import javax.lang.model.element.Element;
4045
import javax.lang.model.element.ExecutableElement;
4146
import javax.lang.model.element.Modifier;
4247
import javax.lang.model.element.TypeElement;
4348
import javax.lang.model.type.TypeKind;
49+
import javax.lang.model.type.TypeMirror;
4450
import javax.lang.model.util.ElementFilter;
45-
import java.util.ArrayList;
46-
import java.util.List;
4751

4852
import static com.sun.codemodel.JExpr.FALSE;
4953
import static com.sun.codemodel.JExpr.TRUE;
@@ -757,4 +761,11 @@ public JFieldVar getIntentFilterField(String[] actions, String[] dataSchemes) {
757761
return receiverRegistrationHolder.getIntentFilterField(actions, dataSchemes);
758762
}
759763

764+
@Override
765+
protected JFieldVar setDatabaseHelperRef(TypeMirror databaseHelperTypeMirror) {
766+
JFieldVar databaseHelperRef = super.setDatabaseHelperRef(databaseHelperTypeMirror);
767+
OrmLiteHelper.injectReleaseInDestroy(databaseHelperRef, this, classes());
768+
769+
return databaseHelperRef;
770+
}
760771
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public JFieldVar getDatabaseHelperRef(TypeMirror databaseHelperTypeMirror) {
111111
return databaseHelperRef;
112112
}
113113

114-
private JFieldVar setDatabaseHelperRef(TypeMirror databaseHelperTypeMirror) {
114+
protected JFieldVar setDatabaseHelperRef(TypeMirror databaseHelperTypeMirror) {
115115
JClass databaseHelperClass = refClass(databaseHelperTypeMirror.toString());
116116
String fieldName = CaseHelper.lowerCaseFirst(databaseHelperClass.name()) + ModelConstants.GENERATION_SUFFIX;
117117
JFieldVar databaseHelperRef = generatedClass.field(PRIVATE, databaseHelperClass, fieldName);

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,19 @@
2727
import com.sun.codemodel.JMod;
2828
import com.sun.codemodel.JTypeVar;
2929
import com.sun.codemodel.JVar;
30+
3031
import org.androidannotations.annotations.EFragment;
3132
import org.androidannotations.helper.ActionBarSherlockHelper;
3233
import org.androidannotations.helper.AnnotationHelper;
34+
import org.androidannotations.helper.OrmLiteHelper;
3335
import org.androidannotations.process.ProcessHolder;
3436

35-
import javax.lang.model.element.TypeElement;
3637
import java.util.ArrayList;
3738
import java.util.List;
3839

40+
import javax.lang.model.element.TypeElement;
41+
import javax.lang.model.type.TypeMirror;
42+
3943
import static com.sun.codemodel.JExpr.FALSE;
4044
import static com.sun.codemodel.JExpr.TRUE;
4145
import static com.sun.codemodel.JExpr._new;
@@ -540,4 +544,12 @@ public JBlock getOnDetachBeforeSuperBlock() {
540544
}
541545
return onDetachBeforeSuperBlock;
542546
}
547+
548+
@Override
549+
protected JFieldVar setDatabaseHelperRef(TypeMirror databaseHelperTypeMirror) {
550+
JFieldVar databaseHelperRef = super.setDatabaseHelperRef(databaseHelperTypeMirror);
551+
OrmLiteHelper.injectReleaseInDestroy(databaseHelperRef, this, classes());
552+
553+
return databaseHelperRef;
554+
}
543555
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
import com.sun.codemodel.*;
1919
import org.androidannotations.helper.AndroidManifest;
2020
import org.androidannotations.helper.IntentBuilder;
21+
import org.androidannotations.helper.OrmLiteHelper;
2122
import org.androidannotations.helper.ServiceIntentBuilder;
2223
import org.androidannotations.process.ProcessHolder;
2324

2425
import javax.lang.model.element.TypeElement;
26+
import javax.lang.model.type.TypeMirror;
2527

2628
import static com.sun.codemodel.JExpr._this;
2729
import static com.sun.codemodel.JMod.PRIVATE;
@@ -130,4 +132,12 @@ public JBlock getOnAttachAfterSuperBlock() {
130132
public JBlock getOnDetachBeforeSuperBlock() {
131133
return receiverRegistrationHolder.getOnDetachBeforeSuperBlock();
132134
}
135+
136+
@Override
137+
protected JFieldVar setDatabaseHelperRef(TypeMirror databaseHelperTypeMirror) {
138+
JFieldVar databaseHelperRef = super.setDatabaseHelperRef(databaseHelperTypeMirror);
139+
OrmLiteHelper.injectReleaseInDestroy(databaseHelperRef, this, classes());
140+
141+
return databaseHelperRef;
142+
}
133143
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.holder;
17+
18+
import com.sun.codemodel.JBlock;
19+
20+
public interface HasLifecycleMethods extends GeneratedClassHolder {
21+
22+
JBlock getOnCreateAfterSuperBlock();
23+
JBlock getOnDestroyBeforeSuperBlock();
24+
25+
JBlock getOnStartAfterSuperBlock();
26+
JBlock getOnStopBeforeSuperBlock();
27+
28+
JBlock getOnResumeAfterSuperBlock();
29+
JBlock getOnPauseBeforeSuperBlock();
30+
31+
JBlock getOnAttachAfterSuperBlock();
32+
JBlock getOnDetachBeforeSuperBlock();
33+
}

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,12 @@
1515
*/
1616
package org.androidannotations.holder;
1717

18-
import com.sun.codemodel.JBlock;
1918
import com.sun.codemodel.JExpression;
2019
import com.sun.codemodel.JFieldVar;
2120

22-
public interface HasReceiverRegistration extends GeneratedClassHolder {
21+
public interface HasReceiverRegistration extends HasLifecycleMethods {
2322

2423
JExpression getContextRef();
2524

2625
JFieldVar getIntentFilterField(String[] actions, String[] dataSchemas);
27-
28-
JBlock getOnCreateAfterSuperBlock();
29-
JBlock getOnDestroyBeforeSuperBlock();
30-
31-
JBlock getOnStartAfterSuperBlock();
32-
JBlock getOnStopBeforeSuperBlock();
33-
34-
JBlock getOnResumeAfterSuperBlock();
35-
JBlock getOnPauseBeforeSuperBlock();
36-
37-
JBlock getOnAttachAfterSuperBlock();
38-
JBlock getOnDetachBeforeSuperBlock();
3926
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.test15.ormlite;
17+
18+
import org.androidannotations.annotations.EActivity;
19+
import org.androidannotations.annotations.OrmLiteDao;
20+
21+
import android.app.Activity;
22+
23+
@EActivity
24+
public class OrmLiteOnDestroyActivity extends Activity {
25+
26+
@OrmLiteDao(helper = DatabaseHelper.class)
27+
UserDao userDao;
28+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.test15.ormlite;
17+
18+
import static org.fest.assertions.api.Assertions.assertThat;
19+
import static org.fest.reflect.core.Reflection.field;
20+
import static org.fest.reflect.core.Reflection.staticField;
21+
22+
import org.fest.reflect.field.Invoker;
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
import org.robolectric.Robolectric;
27+
import org.robolectric.RobolectricTestRunner;
28+
import org.robolectric.util.ActivityController;
29+
30+
import com.j256.ormlite.android.apptools.OpenHelperManager;
31+
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
32+
33+
@RunWith(RobolectricTestRunner.class)
34+
public class OrmLiteOnDestroyTest {
35+
36+
private ActivityController<OrmLiteOnDestroyActivity_> controller;
37+
private Invoker<Integer> instanceCountInvoker;
38+
39+
@Before
40+
public void setup() throws NoSuchFieldException, IllegalAccessException {
41+
staticField("helper").ofType(OrmLiteSqliteOpenHelper.class).in(OpenHelperManager.class).set(null);
42+
staticField("helperClass").ofType(Class.class).in(OpenHelperManager.class).set(null);
43+
staticField("wasClosed").ofType(boolean.class).in(OpenHelperManager.class).set(false);
44+
45+
instanceCountInvoker = staticField("instanceCount").ofType(int.class).in(OpenHelperManager.class);
46+
instanceCountInvoker.set(0);
47+
48+
controller = Robolectric.buildActivity(OrmLiteOnDestroyActivity_.class).create();
49+
}
50+
51+
@Test
52+
public void helper_is_destroyed() throws Exception {
53+
controller.destroy();
54+
55+
Object databaseHelper_ = field("databaseHelper_").ofType(OrmLiteSqliteOpenHelper.class).in(controller.get()).get();
56+
assertThat(databaseHelper_).isNull();
57+
assertThat(instanceCountInvoker.get()).isEqualTo(0);
58+
}
59+
}

0 commit comments

Comments
 (0)