Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add @Extra in @EService #500

Closed
wants to merge 2 commits into from

3 participants

@DayS
Owner

Related to #466

@pyricau pyricau commented on the diff
...va/org/androidannotations/processing/EBeanHolder.java
@@ -36,6 +36,11 @@
public class EBeanHolder {
+ public static enum GeneratedClassType {
@pyricau Owner
pyricau added a note

I hope this class will be useful. Currently, this introduce a change to a lot of processors, just to let ExtraProcessor be able to distinguish between service and activity.

@DayS Owner
DayS added a note

I agree that it seems to be too much just for this case. But I think it could be great to have this information on every processor even if it's not used yet. I prefer that than a simple boolean isActivity or whatever

@pyricau Owner
pyricau added a note

By the way, did you know about the eBeanAnnotation in EBeanHolder ? You should have used it to determine GeneratedClassType.

@DayS Owner
DayS added a note

I agree. I didn't see that attribute before. I'll change that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@pyricau pyricau commented on the diff
...org/androidannotations/processing/ExtraProcessor.java
((55 lines not shown))
JVar methodParam = setIntentMethod.param(holder.classes().INTENT, "newIntent");
JBlock setIntentBody = setIntentMethod.body();
- setIntentBody.invoke(_super(), setIntentMethod).arg(methodParam);
- setIntentBody.invoke(initIntentMethod);
+ // Override only if it's an activity
+ if (holder.classType == GeneratedClassType.ACTIVITY) {
@pyricau Owner
pyricau added a note

Shouldn't we check for the whole injectOrOverrideSetIntent method ? I don't get why you added "setIntent()" to Service, is this useful in any way ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@pyricau pyricau commented on the diff
.../androidannotations/processing/EServiceProcessor.java
@@ -78,6 +81,21 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
onCreateBody.invoke(JExpr._super(), onCreate);
}
+ holder.initStartCommand = holder.generatedClass.method(PRIVATE, codeModel.VOID, "initStartCommand_");
@pyricau Owner
pyricau added a note

I don't get the need for this initStartCommand_ method. Here is what's generated :

    @Override
    public int onStartCommand(Intent intent, int flag, int startId) {
        initStartCommand_(intent);
        return super.onStartCommand(intent, flag, startId);
    }

    private void initStartCommand_(Intent intent) {
        if (intent!= null) {
            injectExtras_(intent.getExtras());
        }
    }

What about this instead:

    @Override
    public int onStartCommand(Intent intent, int flag, int startId) {
        if (intent!= null) {
            injectExtras_(intent.getExtras());
        }
        return super.onStartCommand(intent, flag, startId);
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@pyricau pyricau commented on the diff
...org/androidannotations/validation/ExtraValidator.java
@@ -43,12 +43,7 @@ public ExtraValidator(ProcessingEnvironment processingEnv) {
public boolean validate(Element element, AnnotationElements validatedElements) {
IsValid valid = new IsValid();
- /*
- * TODO since we override setIntent(), we should check that the
@pyricau Owner
pyricau added a note

This TODO is still valid, even if @Override is removed. We can't override a final method, regardless of putting @Override.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@pyricau pyricau commented on the diff
...org/androidannotations/test15/eservice/MyService.java
@@ -44,6 +45,9 @@
@OrmLiteDao(helper = DatabaseHelper.class, model = User.class)
UserDao userDao;
+
+ @Extra
+ Long someId;
@pyricau Owner
pyricau added a note

Is that useful ? We already have ExtraInjectedService.

@DayS Owner
DayS added a note

This one is useless. I added this one before ExtraInjectedService and I forgot to remove it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@pyricau
Owner

Most important note : I don't think this pull request actually solves #466.

@erickok wants to use @Extra in an IntentService. Let's read the onStartCommand() javadoc for IntentService:

You should not override this method for your IntentService. Instead, override onHandleIntent(Intent), which the system calls when the IntentService receives a start request.

And that's because onHandleIntent() executes in a background thread. onStartCommand() may be called multiple times while onHandleIntent() is executing.

So if the injected extras may change while executing onHandleIntent(). And you can expect concurrency issues too.

@erickok

I think @pyricau is right, I think. Luckily, intents are queued, that is, every Intent will be handled in serial (all executed on a single worker thread), so there should be no problem injecting the extras just before a new onHandleIntent() is started.

Here's a suggestion. Given the following simple annotated class:

@EService
public class ExtraInjectedService extends IntentService {

    @Extra("stringExtra")
    String stringExtra;

    @Override
    protected void onHandleIntent(Intent intent) {
        // Do some stuff...
        Log.d(ExtraInjectedService.class.getSimpleName(), "We now have access to injected String extra: " + stringExtra);
    }

}

Should generate something like:

public class ExtraInjectedService_ extends ExtraInjectedService {

    @Override
    protected void onHandleIntent(Intent intent) {
        extraString = intent.getStringExtra("stringExtra");
        super.onHandleIntent(Intent intent);
    }

}

And then also generale a Builder inner class in the ExtraInjectedService_. That should work right?

@pyricau
Owner

I started thinking like @erickok, but then something else occurred to me : we cannot reset the fields to their initial values.

Let's say we have this :

@EService
public class ExtraInjectedService extends IntentService {

    @Extra("stringExtra")
    String stringExtra = "foobar";

    @Override
    protected void onHandleIntent(Intent intent) {
        // Do some stuff...
        Log.d(ExtraInjectedService.class.getSimpleName(), "We now have access to injected String extra: " + stringExtra);
    }

}

If you start it twice, the first time with a "stringExtra", the second time without. Then the second time the field will be set to the stringExtra instead of "foobar" (default value).

I'm really started to think we shouldn't mix @Extra with services, because of their lifecycle.

@erickok

I can see your point, but don't activities have the same issue? If new intents are set on an existing activity instead of creating a new activity?

@pyricau
Owner

Damn it. We override setIntent() in activities and reinject extras there, but you're right, there's exactly the same problem. Hum..

@DayS
Owner

Well, it seems that this functionality is quite more complicated to implement than I expected...

What should we do about the reset field issue ?

@DayS
Owner

I'm closing this PR as we don't have any good solution.

@DayS DayS closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 8, 2013
  1. @DayS
  2. @DayS

    Allow @Extra in @EService

    DayS authored
This page is out of date. Refresh to see the latest.
Showing with 230 additions and 56 deletions.
  1. +8 −1 AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java
  2. +2 −1  AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EActivityProcessor.java
  3. +3 −1 ...idAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EApplicationProcessor.java
  4. +17 −1 AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EBeanHolder.java
  5. +2 −1  AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EBeanProcessor.java
  6. +3 −2 AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EBeansHolder.java
  7. +5 −4 AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EFragmentProcessor.java
  8. +3 −1 AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EProviderProcessor.java
  9. +3 −1 AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EReceiverProcessor.java
  10. +19 −2 AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EServiceProcessor.java
  11. +3 −1 AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EViewGroupProcessor.java
  12. +3 −1 AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EViewProcessor.java
  13. +51 −16 AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/ExtraProcessor.java
  14. +2 −1  AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/SharedPrefProcessor.java
  15. +2 −1  AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/rest/RestProcessor.java
  16. +1 −6 AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/validation/ExtraValidator.java
  17. +2 −2 AndroidAnnotations/functional-test-1-5/.factorypath
  18. +7 −6 AndroidAnnotations/functional-test-1-5/AndroidManifest.xml
  19. +83 −0 ...otations/functional-test-1-5/src/main/java/org/androidannotations/test15/eservice/ExtraInjectedService.java
  20. +10 −6 AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/eservice/MyService.java
  21. +1 −1  AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/prefs/PrefsActivity.java
View
9 AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java
@@ -169,6 +169,13 @@ public void enclosingElementHasEActivity(Element element, AnnotationElements val
hasClassAnnotation(element, enclosingElement, validatedElements, EActivity.class, valid);
}
+ public void enclosingElementHasEActivityOrEService(Element element, AnnotationElements validatedElements, IsValid valid) {
+ Element enclosingElement = element.getEnclosingElement();
+ @SuppressWarnings("unchecked")
+ List<Class<? extends Annotation>> validAnnotations = asList(EActivity.class, EService.class);
+ hasOneOfClassAnnotations(element, enclosingElement, validatedElements, validAnnotations, valid);
+ }
+
public void enclosingElementHasEActivityOrEFragment(Element element, AnnotationElements validatedElements, IsValid valid) {
Element enclosingElement = element.getEnclosingElement();
@SuppressWarnings("unchecked")
@@ -1181,7 +1188,7 @@ public void validateInterceptors(Element element, IsValid valid) {
TypeMirror clientHttpRequestInterceptorType = annotationHelper.typeElementFromQualifiedName(CLIENT_HTTP_REQUEST_INTERCEPTOR).asType();
TypeMirror clientHttpRequestInterceptorTypeErased = annotationHelper.getTypeUtils().erasure(clientHttpRequestInterceptorType);
List<DeclaredType> interceptors = annotationHelper.extractAnnotationClassArrayParameter(element, annotationHelper.getTarget(), "interceptors");
- if(interceptors == null) {
+ if (interceptors == null) {
return;
}
for (DeclaredType interceptorType : interceptors) {
View
3  ...idAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EActivityProcessor.java
@@ -39,6 +39,7 @@
import org.androidannotations.helper.AnnotationHelper;
import org.androidannotations.helper.CanonicalNameConstants;
import org.androidannotations.helper.ModelConstants;
+import org.androidannotations.processing.EBeanHolder.GeneratedClassType;
import org.androidannotations.rclass.IRClass;
import org.androidannotations.rclass.IRClass.Res;
@@ -101,7 +102,7 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
JDefinedClass generatedClass = codeModel._class(modifiers, subActivityQualifiedName, ClassType.CLASS);
- EBeanHolder holder = eBeansHolder.create(element, getTarget(), generatedClass);
+ EBeanHolder holder = eBeansHolder.create(element, getTarget(), generatedClass, GeneratedClassType.ACTIVITY);
JClass annotatedActivity = codeModel.directClass(annotatedActivityQualifiedName);
View
4 ...nnotations/androidannotations/src/main/java/org/androidannotations/processing/EApplicationProcessor.java
@@ -27,6 +27,8 @@
import org.androidannotations.annotations.EApplication;
import org.androidannotations.helper.ModelConstants;
+import org.androidannotations.processing.EBeanHolder.GeneratedClassType;
+
import com.sun.codemodel.ClassType;
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
@@ -52,7 +54,7 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
String generatedComponentQualifiedName = annotatedComponentQualifiedName + ModelConstants.GENERATION_SUFFIX;
JDefinedClass generatedClass = codeModel._class(PUBLIC | FINAL, generatedComponentQualifiedName, ClassType.CLASS);
- EBeanHolder holder = eBeansHolder.create(element, getTarget(), generatedClass);
+ EBeanHolder holder = eBeansHolder.create(element, getTarget(), generatedClass, GeneratedClassType.APPLICATION);
JClass annotatedComponent = codeModel.directClass(annotatedComponentQualifiedName);
View
18 AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EBeanHolder.java
@@ -36,6 +36,11 @@
public class EBeanHolder {
+ public static enum GeneratedClassType {
@pyricau Owner
pyricau added a note

I hope this class will be useful. Currently, this introduce a change to a lot of processors, just to let ExtraProcessor be able to distinguish between service and activity.

@DayS Owner
DayS added a note

I agree that it seems to be too much just for this case. But I think it could be great to have this information on every processor even if it's not used yet. I prefer that than a simple boolean isActivity or whatever

@pyricau Owner
pyricau added a note

By the way, did you know about the eBeanAnnotation in EBeanHolder ? You should have used it to determine GeneratedClassType.

@DayS Owner
DayS added a note

I agree. I didn't see that attribute before. I'll change that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ ACTIVITY, APPLICATION, BEAN, FRAGMENT, PROVIDER, RECEIVER, SERVICE, VIEW_GROUP, VIEW, REST, SHARED_PREF
+ }
+
+ public final GeneratedClassType classType;
public final JDefinedClass generatedClass;
/**
* Only defined on activities
@@ -48,9 +53,19 @@
*/
public JMethod afterSetContentView;
public JBlock extrasNotNullBlock;
+ public JMethod injectExtrasMethod;
+ public JMethod injectExtrasSimpleMethod;
public JVar extras;
public JVar resources;
+ /**
+ * Only defined in services
+ */
+ public JMethod initStartCommand;
+ public JVar initStartCommandIntent;
+ public JMethod onStartCommandMethod;
+ public JVar onStartCommandMethodIntent;
+
public JMethod cast;
public JFieldVar handler;
@@ -124,10 +139,11 @@
private final EBeansHolder eBeansHolder;
public final Class<? extends Annotation> eBeanAnnotation;
- public EBeanHolder(EBeansHolder eBeansHolder, Class<? extends Annotation> eBeanAnnotation, JDefinedClass generatedClass) {
+ public EBeanHolder(EBeansHolder eBeansHolder, Class<? extends Annotation> eBeanAnnotation, JDefinedClass generatedClass, GeneratedClassType classType) {
this.eBeansHolder = eBeansHolder;
this.eBeanAnnotation = eBeanAnnotation;
this.generatedClass = generatedClass;
+ this.classType = classType;
}
public Classes classes() {
View
3  AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EBeanProcessor.java
@@ -34,6 +34,7 @@
import org.androidannotations.annotations.EBean;
import org.androidannotations.helper.APTCodeModelHelper;
+import org.androidannotations.processing.EBeanHolder.GeneratedClassType;
import org.androidannotations.processing.EBeansHolder.Classes;
import com.sun.codemodel.ClassType;
@@ -65,7 +66,7 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
JDefinedClass generatedClass = codeModel._class(PUBLIC | FINAL, generatedBeanQualifiedName, ClassType.CLASS);
- EBeanHolder holder = eBeansHolder.create(element, getTarget(), generatedClass);
+ EBeanHolder holder = eBeansHolder.create(element, getTarget(), generatedClass, GeneratedClassType.BEAN);
JClass eBeanClass = codeModel.directClass(eBeanQualifiedName);
View
5 AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EBeansHolder.java
@@ -28,6 +28,7 @@
import javax.lang.model.element.Element;
import org.androidannotations.helper.CanonicalNameConstants;
+import org.androidannotations.processing.EBeanHolder.GeneratedClassType;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JClassAlreadyExistsException;
@@ -165,13 +166,13 @@ private void preloadJavaLangClasses() {
loadedClasses.put(Object.class.getName(), refClass(Object.class));
}
- public EBeanHolder create(Element element, Class<? extends Annotation> eBeanAnnotation, JDefinedClass generatedClass) {
+ public EBeanHolder create(Element element, Class<? extends Annotation> eBeanAnnotation, JDefinedClass generatedClass, GeneratedClassType classType) {
String qualifiedName = generatedClass.fullName();
originatingElements.add(qualifiedName, element);
- EBeanHolder activityHolder = new EBeanHolder(this, eBeanAnnotation, generatedClass);
+ EBeanHolder activityHolder = new EBeanHolder(this, eBeanAnnotation, generatedClass, classType);
eBeanHolders.put(element, activityHolder);
return activityHolder;
}
View
9 ...idAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EFragmentProcessor.java
@@ -15,7 +15,6 @@
*/
package org.androidannotations.processing;
-import static org.androidannotations.helper.ModelConstants.GENERATION_SUFFIX;
import static com.sun.codemodel.JExpr.FALSE;
import static com.sun.codemodel.JExpr._new;
import static com.sun.codemodel.JExpr._null;
@@ -25,6 +24,7 @@
import static com.sun.codemodel.JMod.PRIVATE;
import static com.sun.codemodel.JMod.PUBLIC;
import static com.sun.codemodel.JMod.STATIC;
+import static org.androidannotations.helper.ModelConstants.GENERATION_SUFFIX;
import java.lang.annotation.Annotation;
@@ -34,9 +34,11 @@
import org.androidannotations.annotations.EFragment;
import org.androidannotations.helper.IdAnnotationHelper;
+import org.androidannotations.processing.EBeanHolder.GeneratedClassType;
import org.androidannotations.processing.EBeansHolder.Classes;
import org.androidannotations.rclass.IRClass;
import org.androidannotations.rclass.IRClass.Res;
+
import com.sun.codemodel.ClassType;
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
@@ -72,7 +74,7 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
JDefinedClass generatedClass = codeModel._class(PUBLIC | FINAL, generatedBeanQualifiedName, ClassType.CLASS);
- EBeanHolder holder = eBeansHolder.create(element, getTarget(), generatedClass);
+ EBeanHolder holder = eBeansHolder.create(element, getTarget(), generatedClass, GeneratedClassType.FRAGMENT);
JClass eBeanClass = codeModel.directClass(beanQualifiedName);
@@ -142,11 +144,10 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
JBlock onViewCreatedBody = onViewCreated.body();
onViewCreatedBody.invoke(_super(), onViewCreated).arg(view).arg(savedInstanceState);
-
+
onViewCreatedBody.invoke(holder.afterSetContentView);
}
-
{
// findViewById
View
4 ...idAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EProviderProcessor.java
@@ -28,6 +28,8 @@
import org.androidannotations.annotations.EProvider;
import org.androidannotations.helper.ModelConstants;
+import org.androidannotations.processing.EBeanHolder.GeneratedClassType;
+
import com.sun.codemodel.ClassType;
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
@@ -53,7 +55,7 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
JDefinedClass generatedClass = codeModel._class(PUBLIC | FINAL, generatedComponentQualifiedName, ClassType.CLASS);
- EBeanHolder holder = eBeansHolder.create(element, getTarget(), generatedClass);
+ EBeanHolder holder = eBeansHolder.create(element, getTarget(), generatedClass, GeneratedClassType.PROVIDER);
JClass annotatedComponent = codeModel.directClass(annotatedComponentQualifiedName);
View
4 ...idAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EReceiverProcessor.java
@@ -26,7 +26,9 @@
import org.androidannotations.annotations.EReceiver;
import org.androidannotations.helper.ModelConstants;
+import org.androidannotations.processing.EBeanHolder.GeneratedClassType;
import org.androidannotations.processing.EBeansHolder.Classes;
+
import com.sun.codemodel.ClassType;
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
@@ -55,7 +57,7 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
JDefinedClass generatedClass = codeModel._class(PUBLIC | FINAL, generatedComponentQualifiedName, ClassType.CLASS);
- EBeanHolder holder = eBeansHolder.create(element, getTarget(), generatedClass);
+ EBeanHolder holder = eBeansHolder.create(element, getTarget(), generatedClass, GeneratedClassType.RECEIVER);
JClass annotatedComponent = codeModel.directClass(annotatedComponentQualifiedName);
View
21 ...oidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EServiceProcessor.java
@@ -28,6 +28,8 @@
import org.androidannotations.annotations.EService;
import org.androidannotations.helper.APTCodeModelHelper;
import org.androidannotations.helper.ModelConstants;
+import org.androidannotations.processing.EBeanHolder.GeneratedClassType;
+
import com.sun.codemodel.ClassType;
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
@@ -35,6 +37,7 @@
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JMethod;
+import com.sun.codemodel.JVar;
public class EServiceProcessor implements GeneratingElementProcessor {
@@ -60,7 +63,7 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
JDefinedClass generatedClass = codeModel._class(PUBLIC | FINAL, generatedComponentQualifiedName, ClassType.CLASS);
- EBeanHolder holder = eBeansHolder.create(element, getTarget(), generatedClass);
+ EBeanHolder holder = eBeansHolder.create(element, getTarget(), generatedClass, GeneratedClassType.SERVICE);
JClass annotatedComponent = codeModel.directClass(annotatedComponentQualifiedName);
@@ -78,6 +81,21 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
onCreateBody.invoke(JExpr._super(), onCreate);
}
+ holder.initStartCommand = holder.generatedClass.method(PRIVATE, codeModel.VOID, "initStartCommand_");
@pyricau Owner
pyricau added a note

I don't get the need for this initStartCommand_ method. Here is what's generated :

    @Override
    public int onStartCommand(Intent intent, int flag, int startId) {
        initStartCommand_(intent);
        return super.onStartCommand(intent, flag, startId);
    }

    private void initStartCommand_(Intent intent) {
        if (intent!= null) {
            injectExtras_(intent.getExtras());
        }
    }

What about this instead:

    @Override
    public int onStartCommand(Intent intent, int flag, int startId) {
        if (intent!= null) {
            injectExtras_(intent.getExtras());
        }
        return super.onStartCommand(intent, flag, startId);
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ holder.initStartCommandIntent = holder.initStartCommand.param(eBeansHolder.classes().INTENT, "intent");
+ {
+ // onStartCommand
+ holder.onStartCommandMethod = holder.generatedClass.method(PUBLIC, codeModel.INT, "onStartCommand");
+ holder.onStartCommandMethod.annotate(Override.class);
+ holder.onStartCommandMethodIntent = holder.onStartCommandMethod.param(eBeansHolder.classes().INTENT, "intent");
+ JVar flag = holder.onStartCommandMethod.param(codeModel.INT, "flag");
+ JVar startId = holder.onStartCommandMethod.param(codeModel.INT, "startId");
+
+ JBlock onStartCommandBody = holder.onStartCommandMethod.body();
+ onStartCommandBody.invoke(holder.initStartCommand).arg(holder.onStartCommandMethodIntent);
+ onStartCommandBody._return(JExpr._super().invoke(holder.onStartCommandMethod).arg(holder.onStartCommandMethodIntent).arg(flag).arg(startId));
+ }
+
{
/*
* Setting to null shouldn't be a problem as long as we don't allow
@@ -91,5 +109,4 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
aptCodeModelHelper.addServiceIntentBuilder(codeModel, holder);
}
-
}
View
4 ...dAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EViewGroupProcessor.java
@@ -30,9 +30,11 @@
import org.androidannotations.helper.APTCodeModelHelper;
import org.androidannotations.helper.IdAnnotationHelper;
import org.androidannotations.helper.ModelConstants;
+import org.androidannotations.processing.EBeanHolder.GeneratedClassType;
import org.androidannotations.processing.EBeansHolder.Classes;
import org.androidannotations.rclass.IRClass;
import org.androidannotations.rclass.IRClass.Res;
+
import com.sun.codemodel.ClassType;
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
@@ -95,7 +97,7 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
JDefinedClass generatedClass = codeModel._class(modifiers, generatedBeanQualifiedName, ClassType.CLASS);
- EBeanHolder holder = eBeansHolder.create(element, getTarget(), generatedClass);
+ EBeanHolder holder = eBeansHolder.create(element, getTarget(), generatedClass, GeneratedClassType.VIEW_GROUP);
JClass eBeanClass = codeModel.directClass(eBeanQualifiedName);
View
4 AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EViewProcessor.java
@@ -27,7 +27,9 @@
import org.androidannotations.annotations.EView;
import org.androidannotations.helper.APTCodeModelHelper;
import org.androidannotations.helper.ModelConstants;
+import org.androidannotations.processing.EBeanHolder.GeneratedClassType;
import org.androidannotations.processing.EBeansHolder.Classes;
+
import com.sun.codemodel.ClassType;
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
@@ -85,7 +87,7 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
}
JDefinedClass generatedClass = codeModel._class(modifiers, generatedBeanQualifiedName, ClassType.CLASS);
- EBeanHolder holder = eBeansHolder.create(element, getTarget(), generatedClass);
+ EBeanHolder holder = eBeansHolder.create(element, getTarget(), generatedClass, GeneratedClassType.VIEW);
JClass eBeanClass = codeModel.directClass(eBeanQualifiedName);
View
67 AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/ExtraProcessor.java
@@ -40,6 +40,7 @@
import org.androidannotations.annotations.Extra;
import org.androidannotations.helper.APTCodeModelHelper;
import org.androidannotations.helper.CaseHelper;
+import org.androidannotations.processing.EBeanHolder.GeneratedClassType;
import org.androidannotations.processing.EBeansHolder.Classes;
import com.sun.codemodel.JBlock;
@@ -93,6 +94,10 @@ public void process(Element element, JCodeModel codeModel, EBeanHolder holder) {
if (holder.extras == null) {
injectExtras(holder, codeModel);
+ injectExtrasSimple(holder, codeModel);
+ injectOrOverrideSetIntent(holder, codeModel);
+ injectExtrasOnInit(holder);
+ injectExtrasOnStartCommand(holder);
}
String staticFieldName;
@@ -178,43 +183,73 @@ private void generateCastMethod(JCodeModel codeModel, EBeanHolder holder) {
}
/**
- * Adds call to injectExtras_() in onCreate and setIntent() methods.
+ * Adds call to injectExtras_(Bundle) in onStartCommand and setIntent()
+ * methods methods.
*/
private void injectExtras(EBeanHolder holder, JCodeModel codeModel) {
-
Classes classes = holder.classes();
- JMethod injectExtrasMethod = holder.generatedClass.method(PRIVATE, codeModel.VOID, "injectExtras_");
+ holder.injectExtrasMethod = holder.generatedClass.method(PRIVATE, codeModel.VOID, "injectExtras_");
+ holder.extras = holder.injectExtrasMethod.param(classes.BUNDLE, "extras_");
+
+ JBlock injectExtrasBody = holder.injectExtrasMethod.body();
+
+ holder.extrasNotNullBlock = injectExtrasBody._if(holder.extras.ne(_null()))._then();
+ }
+
+ /**
+ * Adds call to injectExtras_() in onCreate method.
+ */
+ private void injectExtrasSimple(EBeanHolder holder, JCodeModel codeModel) {
+ if (holder.classType == GeneratedClassType.SERVICE) {
+ return;
+ }
- overrideSetIntent(holder, codeModel, injectExtrasMethod);
+ Classes classes = holder.classes();
- injectExtrasOnInit(holder, classes.INTENT, injectExtrasMethod);
+ holder.injectExtrasSimpleMethod = holder.generatedClass.method(PRIVATE, codeModel.VOID, "injectExtras_");
- JBlock injectExtrasBody = injectExtrasMethod.body();
+ JBlock injectExtrasBody = holder.injectExtrasSimpleMethod.body();
JVar intent = injectExtrasBody.decl(classes.INTENT, "intent_", invoke("getIntent"));
- holder.extras = injectExtrasBody.decl(classes.BUNDLE, "extras_");
- holder.extras.init(intent.invoke("getExtras"));
+ JVar extras = injectExtrasBody.decl(classes.BUNDLE, "extras_");
+ extras.init(intent.invoke("getExtras"));
- holder.extrasNotNullBlock = injectExtrasBody._if(holder.extras.ne(_null()))._then();
+ injectExtrasBody.invoke(holder.injectExtrasMethod).arg(extras);
}
- private void overrideSetIntent(EBeanHolder holder, JCodeModel codeModel, JMethod initIntentMethod) {
+ private void injectOrOverrideSetIntent(EBeanHolder holder, JCodeModel codeModel) {
if (holder.intentBuilderClass != null) {
-
JMethod setIntentMethod = holder.generatedClass.method(PUBLIC, codeModel.VOID, "setIntent");
- setIntentMethod.annotate(Override.class);
JVar methodParam = setIntentMethod.param(holder.classes().INTENT, "newIntent");
JBlock setIntentBody = setIntentMethod.body();
- setIntentBody.invoke(_super(), setIntentMethod).arg(methodParam);
- setIntentBody.invoke(initIntentMethod);
+ // Override only if it's an activity
+ if (holder.classType == GeneratedClassType.ACTIVITY) {
@pyricau Owner
pyricau added a note

Shouldn't we check for the whole injectOrOverrideSetIntent method ? I don't get why you added "setIntent()" to Service, is this useful in any way ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ setIntentMethod.annotate(Override.class);
+ setIntentBody.invoke(_super(), setIntentMethod).arg(methodParam);
+ }
+
+ JInvocation extras = methodParam.invoke("getExtras");
+ setIntentBody.invoke(holder.injectExtrasMethod).arg(extras);
}
}
- private void injectExtrasOnInit(EBeanHolder holder, JClass intentClass, JMethod injectExtrasMethod) {
- holder.init.body().invoke(injectExtrasMethod);
+ private void injectExtrasOnInit(EBeanHolder holder) {
+ if (holder.classType == GeneratedClassType.ACTIVITY) {
+ holder.init.body().invoke(holder.injectExtrasMethod);
+ }
+ }
+
+ private void injectExtrasOnStartCommand(EBeanHolder holder) {
+ if (holder.classType == GeneratedClassType.SERVICE && holder.onStartCommandMethod != null) {
+ JVar intent = holder.onStartCommandMethodIntent;
+ JBlock body = holder.initStartCommand.body();
+
+ body._if(intent.ne(JExpr._null())) //
+ ._then().invoke(holder.injectExtrasMethod).arg(intent.invoke("getExtras"));
+ }
}
}
View
3  ...dAnnotations/androidannotations/src/main/java/org/androidannotations/processing/SharedPrefProcessor.java
@@ -55,6 +55,7 @@
import org.androidannotations.api.sharedpreferences.StringPrefField;
import org.androidannotations.helper.CanonicalNameConstants;
import org.androidannotations.helper.ModelConstants;
+import org.androidannotations.processing.EBeanHolder.GeneratedClassType;
import com.sun.codemodel.ClassType;
import com.sun.codemodel.JBlock;
@@ -106,7 +107,7 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
String helperQualifiedName = interfaceQualifiedName + ModelConstants.GENERATION_SUFFIX;
JDefinedClass helperClass = codeModel._class(JMod.PUBLIC | JMod.FINAL, helperQualifiedName, ClassType.CLASS);
- eBeansHolder.create(typeElement, getTarget(), helperClass);
+ eBeansHolder.create(typeElement, getTarget(), helperClass, GeneratedClassType.SHARED_PREF);
helperClass._extends(SharedPreferencesHelper.class);
View
3  ...idAnnotations/androidannotations/src/main/java/org/androidannotations/processing/rest/RestProcessor.java
@@ -39,6 +39,7 @@
import org.androidannotations.annotations.rest.Rest;
import org.androidannotations.helper.AnnotationHelper;
import org.androidannotations.helper.ModelConstants;
+import org.androidannotations.processing.EBeanHolder.GeneratedClassType;
import org.androidannotations.processing.EBeansHolder;
import org.androidannotations.processing.GeneratingElementProcessor;
@@ -76,7 +77,7 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
String implementationName = interfaceName + ModelConstants.GENERATION_SUFFIX;
holder.restImplementationClass = codeModel._class(JMod.PUBLIC, implementationName, ClassType.CLASS);
- eBeansHolder.create(element, getTarget(), holder.restImplementationClass);
+ eBeansHolder.create(element, getTarget(), holder.restImplementationClass, GeneratedClassType.REST);
JClass interfaceClass = eBeansHolder.refClass(interfaceName);
holder.restImplementationClass._implements(interfaceClass);
View
7 AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/validation/ExtraValidator.java
@@ -43,12 +43,7 @@ public ExtraValidator(ProcessingEnvironment processingEnv) {
public boolean validate(Element element, AnnotationElements validatedElements) {
IsValid valid = new IsValid();
- /*
- * TODO since we override setIntent(), we should check that the
@pyricau Owner
pyricau added a note

This TODO is still valid, even if @Override is removed. We can't override a final method, regardless of putting @Override.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
- * setIntent() method can be overridden
- */
-
- validatorHelper.enclosingElementHasEActivity(element, validatedElements, valid);
+ validatorHelper.enclosingElementHasEActivityOrEService(element, validatedElements, valid);
validatorHelper.isNotPrivate(element, valid);
View
4 AndroidAnnotations/functional-test-1-5/.factorypath
@@ -1,6 +1,6 @@
<factorypath>
<factorypathentry kind="PLUGIN" id="org.eclipse.jst.ws.annotations.core" enabled="true" runInBatchMode="false"/>
- <factorypathentry kind="VARJAR" id="M2_REPO/org/androidannotations/androidannotations-api/3.0-SNAPSHOT/androidannotations-api-3.0-SNAPSHOT.jar" enabled="true" runInBatchMode="false"/>
- <factorypathentry kind="VARJAR" id="M2_REPO/org/androidannotations/androidannotations/3.0-SNAPSHOT/androidannotations-3.0-SNAPSHOT.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/sun/codemodel/codemodel/2.4.1/codemodel-2.4.1.jar" enabled="true" runInBatchMode="false"/>
+ <factorypathentry kind="WKSPJAR" id="/androidannotations-api/target/androidannotations-api-3.0-SNAPSHOT.jar" enabled="true" runInBatchMode="false"/>
+ <factorypathentry kind="WKSPJAR" id="/androidannotations/target/androidannotations-3.0-SNAPSHOT.jar" enabled="true" runInBatchMode="false"/>
</factorypath>
View
13 AndroidAnnotations/functional-test-1-5/AndroidManifest.xml
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
-
Copyright (C) 2010-2012 eBusiness Information, Excilys Group
Licensed under the Apache License, Version 2.0 (the "License"); you may not
@@ -14,7 +13,6 @@
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
-
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.androidannotations.test15"
@@ -24,13 +22,13 @@
<uses-sdk
android:minSdkVersion="3"
android:targetSdkVersion="8" />
-
- <uses-permission android:name="android.permission.INTERNET"/>
+
+ <uses-permission android:name="android.permission.INTERNET" />
<application
android:name="org.androidannotations.test15.roboguice.SampleRoboApplication_"
- android:icon="@drawable/icon"
android:debuggable="true"
+ android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity android:name="org.androidannotations.test15.eviewgroup.CustomFrameLayoutActivity_" >
<intent-filter>
@@ -89,6 +87,9 @@
<service
android:name="org.androidannotations.test15.eservice.MyService_"
android:exported="false" />
+ <service
+ android:name="org.androidannotations.test15.eservice.ExtraInjectedService_"
+ android:exported="false" />
</application>
-</manifest>
+</manifest>
View
83 ...tions/functional-test-1-5/src/main/java/org/androidannotations/test15/eservice/ExtraInjectedService.java
@@ -0,0 +1,83 @@
+/**
+ * Copyright (C) 2010-2012 eBusiness Information, Excilys Group
+ *
+ * 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.test15.eservice;
+
+import java.util.List;
+
+import org.androidannotations.annotations.Background;
+import org.androidannotations.annotations.EService;
+import org.androidannotations.annotations.Extra;
+import org.androidannotations.annotations.Trace;
+import org.androidannotations.annotations.UiThread;
+import org.androidannotations.test15.CustomData;
+
+import android.app.IntentService;
+import android.content.Intent;
+import android.os.IBinder;
+import android.util.Log;
+import android.widget.Toast;
+
+@EService
+public class ExtraInjectedService extends IntentService {
+
+ @Extra("stringExtra")
+ String stringExtra;
+
+ @Extra("arrayExtra")
+ CustomData[] arrayExtra;
+
+ @Extra("listExtra")
+ List<String> listExtra;
+
+ @Extra("intExtra")
+ int intExtra;
+
+ @Extra("byteArrayExtra")
+ byte[] byteArrayExtra;
+
+ @Extra
+ String extraWithoutValue;
+
+ public ExtraInjectedService() {
+ super(ExtraInjectedService.class.getSimpleName());
+ }
+
+ @Override
+ protected void onHandleIntent(Intent intent) {
+ // Do some stuff...
+
+ showToast();
+ workInBackground();
+ }
+
+ @Trace
+ @UiThread
+ void showToast() {
+ Toast.makeText(getApplicationContext(), "Hello World!", Toast.LENGTH_LONG).show();
+ }
+
+ @Trace
+ @Background
+ void workInBackground() {
+ Log.d(ExtraInjectedService.class.getSimpleName(), "Doing some background work.");
+ }
+
+ @Override
+ public IBinder onBind(Intent intent) {
+ return null;
+ }
+
+}
View
16 AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/eservice/MyService.java
@@ -15,15 +15,10 @@
*/
package org.androidannotations.test15.eservice;
-import android.app.IntentService;
-import android.app.NotificationManager;
-import android.content.Intent;
-import android.util.Log;
-import android.widget.Toast;
-
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EService;
+import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.OrmLiteDao;
import org.androidannotations.annotations.SystemService;
import org.androidannotations.annotations.Trace;
@@ -33,6 +28,12 @@
import org.androidannotations.test15.ormlite.User;
import org.androidannotations.test15.ormlite.UserDao;
+import android.app.IntentService;
+import android.app.NotificationManager;
+import android.content.Intent;
+import android.util.Log;
+import android.widget.Toast;
+
@EService
public class MyService extends IntentService {
@@ -44,6 +45,9 @@
@OrmLiteDao(helper = DatabaseHelper.class, model = User.class)
UserDao userDao;
+
+ @Extra
+ Long someId;
@pyricau Owner
pyricau added a note

Is that useful ? We already have ExtraInjectedService.

@DayS Owner
DayS added a note

This one is useless. I added this one before ExtraInjectedService and I forgot to remove it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
public MyService() {
super(MyService.class.getSimpleName());
View
2  AndroidAnnotations/functional-test-1-5/src/main/java/org/androidannotations/test15/prefs/PrefsActivity.java
@@ -32,7 +32,7 @@
@Pref
ActivityPrefs_ activityPrefs;
-
+
@Pref
ApplicationDefaultPrefs_ applicationDefaultPrefs;
Something went wrong with that request. Please try again.