diff --git a/.idea/dictionaries/tmtron.xml b/.idea/dictionaries/tmtron.xml index 9638546..fc22244 100644 --- a/.idea/dictionaries/tmtron.xml +++ b/.idea/dictionaries/tmtron.xml @@ -9,6 +9,7 @@ greenrobot javadocs mycila + ossrh proc slashstar technotes diff --git a/README.md b/README.md index 6a3b8b1..e7ac91d 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ An [Android Annotations](http://androidannotations.org/) plugin to support the [ -[![Build Status](https://travis-ci.org/tmtron/green-annotations.svg)](https://travis-ci.org/tmtron/green-annotations/builds) +[![Build Status](https://travis-ci.org/tmtron/green-annotations.svg?label=travis)](https://travis-ci.org/tmtron/green-annotations/builds) [![Maven Central](https://img.shields.io/maven-central/v/com.tmtron/green-annotations.svg?maxAge=2592000)](https://maven-badges.herokuapp.com/maven-central/com.tmtron/green-annotations) [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-green--annotations-green.svg?style=true)](https://android-arsenal.com/details/1/4405) [![Javadoc](https://javadoc-emblem.rhcloud.com/doc/com.tmtron/green-annotations/badge.svg)](http://www.javadoc.io/doc/com.tmtron/green-annotations/) [![license](https://img.shields.io/github/license/tmtron/green-annotations.svg?maxAge=2592000)](https://raw.githubusercontent.com/tmtron/green-annotations/develop/LICENSE) [![Join the chat at https://gitter.im/green-annotations/Lobby](https://badges.gitter.im/green-annotations/Lobby.svg)](https://gitter.im/green-annotations/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) ## Usage in your Android Studio project: @@ -14,8 +14,8 @@ In the `build.gradle` file of the module project: ```gradle dependencies { // Android Annotations for Greenrobot - apt 'com.tmtron:green-annotations:1.0.0' - compile 'com.tmtron:green-annotations-api:1.0.0' + apt 'com.tmtron:green-annotations:1.0.1' + compile 'com.tmtron:green-annotations-api:1.0.1' } ``` diff --git a/green-annotations-test/lint.xml b/green-annotations-test/lint.xml index 3518348..a5808ca 100644 --- a/green-annotations-test/lint.xml +++ b/green-annotations-test/lint.xml @@ -23,5 +23,8 @@ http://stackoverflow.com/questions/39685618/intellij-idea-element-ignore-is-not-allowed-here --> + + + diff --git a/green-annotations-test/src/main/AndroidManifest.xml b/green-annotations-test/src/main/AndroidManifest.xml index 20b197b..d9b2e2f 100644 --- a/green-annotations-test/src/main/AndroidManifest.xml +++ b/green-annotations-test/src/main/AndroidManifest.xml @@ -32,6 +32,7 @@ + diff --git a/green-annotations-test/src/main/java/com/tmtron/greenannotations/test/ActivityAfter.java b/green-annotations-test/src/main/java/com/tmtron/greenannotations/test/ActivityAfter.java index ee2e462..216af59 100644 --- a/green-annotations-test/src/main/java/com/tmtron/greenannotations/test/ActivityAfter.java +++ b/green-annotations-test/src/main/java/com/tmtron/greenannotations/test/ActivityAfter.java @@ -16,6 +16,7 @@ package com.tmtron.greenannotations.test; +import android.annotation.SuppressLint; import android.app.Activity; import android.widget.Toast; import com.tmtron.greenannotations.EventBusGreenRobot; @@ -27,13 +28,16 @@ /** * test class to show how to use EventBus with GreenAnnotations */ +@SuppressLint("Registered") +@SuppressWarnings({"WeakerAccess"}) @EActivity public class ActivityAfter extends Activity { + @SuppressWarnings("CanBeFinal") @EventBusGreenRobot EventBus eventBus; - void fireEvent(String message) { + void fireEvent(@SuppressWarnings("SameParameterValue") String message) { eventBus.post(new MessageEvent(message)); } diff --git a/green-annotations-test/src/main/java/com/tmtron/greenannotations/test/ActivityBefore.java b/green-annotations-test/src/main/java/com/tmtron/greenannotations/test/ActivityBefore.java index b35a7a6..f83f31d 100644 --- a/green-annotations-test/src/main/java/com/tmtron/greenannotations/test/ActivityBefore.java +++ b/green-annotations-test/src/main/java/com/tmtron/greenannotations/test/ActivityBefore.java @@ -26,6 +26,7 @@ /** * test class to show how to use EventBus without GreenAnnotations */ +@SuppressWarnings({"WeakerAccess"}) public class ActivityBefore extends Activity { EventBus eventBus; @@ -48,7 +49,7 @@ public void onStop() { super.onStop(); } - void fireEvent(String message) { + void fireEvent(@SuppressWarnings("SameParameterValue") String message) { eventBus.post(new MessageEvent(message)); } diff --git a/green-annotations-test/src/main/java/com/tmtron/greenannotations/test/Service4EventTest.java b/green-annotations-test/src/main/java/com/tmtron/greenannotations/test/Service4EventTest.java new file mode 100644 index 0000000..ea9d24c --- /dev/null +++ b/green-annotations-test/src/main/java/com/tmtron/greenannotations/test/Service4EventTest.java @@ -0,0 +1,59 @@ +/* + * Copyright © 2016 Martin Trummer (martin.trummer@tmtron.com) + * + * 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 com.tmtron.greenannotations.test; + +import android.app.Service; +import android.content.Intent; +import android.os.IBinder; +import com.tmtron.greenannotations.EventBusGreenRobot; +import org.androidannotations.annotations.EService; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; +import org.greenrobot.eventbus.ThreadMode; + +@SuppressWarnings({"WeakerAccess"}) +@EService +public class Service4EventTest extends Service { + static final String EVENT_IDENTIFIER = "service4EventTest-id"; + + @SuppressWarnings("all") + @EventBusGreenRobot + EventBus eventBus; + + void fireEvent() { + eventBus.post(new Event4Tests(EVENT_IDENTIFIER)); + } + + int startId = 0; + String eventIdentifier; + + @Subscribe(threadMode = ThreadMode.MAIN) + public void onEvent(Event4Tests event4Tests) { + eventIdentifier = event4Tests.identifier; + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + this.startId = startId; + return START_STICKY_COMPATIBILITY; + } + + @Override + public IBinder onBind(Intent intent) { + return null; + } +} diff --git a/green-annotations-test/src/test/java/com/tmtron/greenannotations/test/GreenEventBusActivityTest.java b/green-annotations-test/src/test/java/com/tmtron/greenannotations/test/GreenEventBusActivityTest.java index 5eef65f..4531048 100644 --- a/green-annotations-test/src/test/java/com/tmtron/greenannotations/test/GreenEventBusActivityTest.java +++ b/green-annotations-test/src/test/java/com/tmtron/greenannotations/test/GreenEventBusActivityTest.java @@ -20,12 +20,15 @@ import org.junit.runner.RunWith; import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; +import org.robolectric.util.ServiceController; import static org.fest.assertions.api.Assertions.assertThat; @RunWith(RobolectricTestRunner.class) public class GreenEventBusActivityTest { + private static final int START_ID = 17; + @Test public void testEventBusAssigned() { GreenEventBusActivity activity = Robolectric.setupActivity(GreenEventBusActivity_.class); @@ -54,4 +57,23 @@ public void testEventFired() { assertThat(activity.eventIdentifier).isEqualTo(Bean4EventTest.EVENT_IDENTIFIER); } + @Test + public void testServiceEventFired() { + ServiceController serviceController = Robolectric.buildService(Service4EventTest_.class).attach().create(); + Service4EventTest service = serviceController.get(); + + assertThat(service.eventBus).isNotNull(); + + assertThat(service.startId).isEqualTo(0); + serviceController.startCommand(0, START_ID); + assertThat(service.startId).isEqualTo(START_ID); + + assertThat(service.eventIdentifier).isNullOrEmpty(); + service.fireEvent(); + assertThat(service.eventIdentifier).isEqualTo(Service4EventTest.EVENT_IDENTIFIER); + + serviceController.destroy(); + } + + } diff --git a/green-annotations/green-annotations.iml b/green-annotations/green-annotations.iml index ccdaf96..43cc27f 100644 --- a/green-annotations/green-annotations.iml +++ b/green-annotations/green-annotations.iml @@ -31,11 +31,11 @@ - + diff --git a/green-annotations/src/main/java/com/tmtron/greenannotations/handler/EventBusGreenRobotHandler.java b/green-annotations/src/main/java/com/tmtron/greenannotations/handler/EventBusGreenRobotHandler.java index 03c5508..339fa6a 100644 --- a/green-annotations/src/main/java/com/tmtron/greenannotations/handler/EventBusGreenRobotHandler.java +++ b/green-annotations/src/main/java/com/tmtron/greenannotations/handler/EventBusGreenRobotHandler.java @@ -23,6 +23,7 @@ import org.androidannotations.handler.MethodInjectionHandler; import org.androidannotations.helper.InjectHelper; import org.androidannotations.holder.EComponentHolder; +import org.androidannotations.holder.EServiceHolder; import org.androidannotations.holder.HasLifecycleMethods; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; @@ -166,8 +167,15 @@ private boolean hasSubscribeAnnotation(Element element) { private void handleEventBusRegistration(IJAssignmentTarget fieldRef , HasLifecycleMethods holderWithLifecycleMethods) { - JBlock onStartBlock = holderWithLifecycleMethods.getOnStartAfterSuperBlock(); - JBlock onStopBlock = holderWithLifecycleMethods.getOnStopBeforeSuperBlock(); + JBlock onStartBlock; + JBlock onStopBlock; + if (holderWithLifecycleMethods instanceof EServiceHolder) { + onStartBlock = holderWithLifecycleMethods.getOnCreateAfterSuperBlock(); + onStopBlock = holderWithLifecycleMethods.getOnDestroyBeforeSuperBlock(); + } else { + onStartBlock = holderWithLifecycleMethods.getOnStartAfterSuperBlock(); + onStopBlock = holderWithLifecycleMethods.getOnStopBeforeSuperBlock(); + } onStartBlock.invoke(fieldRef, "register").arg(JExpr._this()); onStopBlock.invoke(fieldRef, "unregister").arg(JExpr._this()); } diff --git a/green-annotations/src/test/java/com/tmtron/greenannotations/EventBusGreenRobotTest.java b/green-annotations/src/test/java/com/tmtron/greenannotations/EventBusGreenRobotTest.java index 50f3d78..7872da9 100644 --- a/green-annotations/src/test/java/com/tmtron/greenannotations/EventBusGreenRobotTest.java +++ b/green-annotations/src/test/java/com/tmtron/greenannotations/EventBusGreenRobotTest.java @@ -22,6 +22,7 @@ import java.io.IOException; +@SuppressWarnings({"WeakerAccess"}) public class EventBusGreenRobotTest extends ProcessorTestHelper { @Before @@ -86,4 +87,17 @@ public void eBeanCompilationFails() throws IOException { assertCompilationErrorOn(SomeEbeanError.class, "@EventBusGreenRobot", compileResult); } + /** + * testcase for issue 12 + */ + @Test + public void eServiceCompiles() { + CompileResult compileResult = compileFiles( + SomeEService.class + ); + assertCompilationSuccessful(compileResult); + assertGeneratedClassContainsBusInitialisation(SomeEService.class); + assertGeneratedClassContainsBusRegistration(SomeEService.class); + } + } diff --git a/green-annotations/src/test/java/com/tmtron/greenannotations/ProcessorTestHelper.java b/green-annotations/src/test/java/com/tmtron/greenannotations/ProcessorTestHelper.java index 756736a..0355d62 100644 --- a/green-annotations/src/test/java/com/tmtron/greenannotations/ProcessorTestHelper.java +++ b/green-annotations/src/test/java/com/tmtron/greenannotations/ProcessorTestHelper.java @@ -39,12 +39,10 @@ void assertGeneratedClassContainsBusInitialisation(Class sourceClass) { } private final String[] codeBusRegister = new String[]{ - " super.onStart();", " this.eventBus.register(this);"}; private final String[] codeBusUnregister = new String[]{ - " this.eventBus.unregister(this);", - " super.onStop();"}; + " this.eventBus.unregister(this);"}; void assertGeneratedClassDoesNotContainBusRegistration(Class sourceClass) { assertGeneratedClassDoesNotContain(sourceClass, codeBusRegister); diff --git a/green-annotations/src/test/java/com/tmtron/greenannotations/SomeEService.java b/green-annotations/src/test/java/com/tmtron/greenannotations/SomeEService.java new file mode 100644 index 0000000..c7c5b16 --- /dev/null +++ b/green-annotations/src/test/java/com/tmtron/greenannotations/SomeEService.java @@ -0,0 +1,42 @@ +/* + * Copyright © 2016 Martin Trummer (martin.trummer@tmtron.com) + * + * 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 com.tmtron.greenannotations; + +import android.app.Service; +import android.content.Intent; +import android.os.IBinder; +import org.androidannotations.annotations.EService; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; + +@EService +class SomeEService extends Service { + + // used for testing only + @SuppressWarnings("unused") + @EventBusGreenRobot + public EventBus eventBus; + + @SuppressWarnings("EmptyMethod") + @Subscribe + public void handleEvent(final SomeEvent event) { } + + @Override + public IBinder onBind(Intent intent) { + return null; + } +} diff --git a/green-annotations/src/test/resources/com/tmtron/greenannotations/AndroidManifest.xml b/green-annotations/src/test/resources/com/tmtron/greenannotations/AndroidManifest.xml index 8760311..48f2099 100644 --- a/green-annotations/src/test/resources/com/tmtron/greenannotations/AndroidManifest.xml +++ b/green-annotations/src/test/resources/com/tmtron/greenannotations/AndroidManifest.xml @@ -21,6 +21,7 @@ + \ No newline at end of file