Skip to content
This repository has been archived by the owner on Feb 26, 2023. It is now read-only.

Commit

Permalink
Re-add Otto annotation handlers
Browse files Browse the repository at this point in the history
We copy all non-AA annotations when we override a method. This can help
Otto, because it does not read annotations from parent classes. But if a
method is only annotated with Otto annotation, so we do not override it,
the annotation will not be read Otto and the methods will not be called.
To help that case, we re-add Otto handlers, which override (and
annotate) Otto annotated methods in the generated subclass.
  • Loading branch information
WonderCsabo committed Sep 10, 2014
1 parent 2465468 commit 7d7213f
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,22 @@
*/
package org.androidannotations.handler;

import org.androidannotations.handler.rest.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.processing.ProcessingEnvironment;

import org.androidannotations.handler.rest.DeleteHandler;
import org.androidannotations.handler.rest.GetHandler;
import org.androidannotations.handler.rest.HeadHandler;
import org.androidannotations.handler.rest.OptionsHandler;
import org.androidannotations.handler.rest.PostHandler;
import org.androidannotations.handler.rest.PutHandler;
import org.androidannotations.handler.rest.RestHandler;
import org.androidannotations.handler.rest.RestServiceHandler;
import org.androidannotations.helper.AndroidManifest;
import org.androidannotations.helper.OptionsHelper;
import org.androidannotations.holder.GeneratedClassHolder;
Expand All @@ -25,9 +40,6 @@
import org.androidannotations.process.ProcessHolder;
import org.androidannotations.rclass.IRClass;

import javax.annotation.processing.ProcessingEnvironment;
import java.util.*;

public class AnnotationHandlers {

private List<AnnotationHandler<? extends GeneratedClassHolder>> annotationHandlers = new ArrayList<AnnotationHandler<? extends GeneratedClassHolder>>();
Expand Down Expand Up @@ -99,6 +111,8 @@ public AnnotationHandlers(ProcessingEnvironment processingEnvironment) {
add(new SeekBarTouchStartHandler(processingEnvironment));
add(new SeekBarTouchStopHandler(processingEnvironment));
add(new ServiceActionHandler(processingEnvironment));
add(new ProduceHandler(processingEnvironment));
add(new SubscribeHandler(processingEnvironment));
add(new InstanceStateHandler(processingEnvironment));
add(new HttpsClientHandler(processingEnvironment));
add(new OnActivityResultHandler(processingEnvironment));
Expand All @@ -115,14 +129,19 @@ public AnnotationHandlers(ProcessingEnvironment processingEnvironment) {
if (optionsHelper.shouldLogTrace()) {
add(new TraceHandler(processingEnvironment));
}
/* UIThreadHandler and BackgroundHandler must be after TraceHandler and IgnoredWhenDetached */
/*
* UIThreadHandler and BackgroundHandler must be after TraceHandler and
* IgnoredWhenDetached
*/
add(new UiThreadHandler(processingEnvironment));
add(new BackgroundHandler(processingEnvironment));

/* SupposeUiThreadHandler and SupposeBackgroundHandler must be
after all handlers that modifies generated method body */
add(new SupposeUiThreadHandler(processingEnvironment));
add(new SupposeBackgroundHandler(processingEnvironment));
/*
* SupposeUiThreadHandler and SupposeBackgroundHandler must be after all
* handlers that modifies generated method body
*/
add(new SupposeUiThreadHandler(processingEnvironment));
add(new SupposeBackgroundHandler(processingEnvironment));
}

private void add(AnnotationHandler<? extends GeneratedClassHolder> annotationHandler) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Copyright (C) 2010-2014 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.handler;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;

import org.androidannotations.helper.APTCodeModelHelper;
import org.androidannotations.helper.CanonicalNameConstants;
import org.androidannotations.helper.TargetAnnotationHelper;
import org.androidannotations.holder.EComponentHolder;
import org.androidannotations.model.AnnotationElements;
import org.androidannotations.process.IsValid;

public class ProduceHandler extends BaseAnnotationHandler<EComponentHolder> {

private final TargetAnnotationHelper annotationHelper;
private final APTCodeModelHelper codeModelHelper = new APTCodeModelHelper();

public ProduceHandler(ProcessingEnvironment processingEnvironment) {
super(CanonicalNameConstants.PRODUCE, processingEnvironment);
annotationHelper = new TargetAnnotationHelper(processingEnv, getTarget());
}

@Override
public void validate(Element element, AnnotationElements validatedElements, IsValid valid) {
if (!annotationHelper.enclosingElementHasEnhancedComponentAnnotation(element)) {
valid.invalidate();
return;
}

ExecutableElement executableElement = (ExecutableElement) element;

/*
* We check that twice to skip invalid annotated elements
*/
validatorHelper.enclosingElementHasEnhancedComponentAnnotation(executableElement, validatedElements, valid);

validatorHelper.returnTypeIsNotVoid(executableElement, valid);

validatorHelper.isPublic(element, valid);

validatorHelper.doesntThrowException(executableElement, valid);

validatorHelper.isNotFinal(element, valid);

validatorHelper.param.zeroParameter(executableElement, valid);
}

@Override
public void process(Element element, EComponentHolder holder) throws Exception {
ExecutableElement executableElement = (ExecutableElement) element;

codeModelHelper.overrideAnnotatedMethod(executableElement, holder);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Copyright (C) 2010-2014 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.handler;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;

import org.androidannotations.helper.APTCodeModelHelper;
import org.androidannotations.helper.CanonicalNameConstants;
import org.androidannotations.helper.TargetAnnotationHelper;
import org.androidannotations.holder.EComponentHolder;
import org.androidannotations.model.AnnotationElements;
import org.androidannotations.process.IsValid;

public class SubscribeHandler extends BaseAnnotationHandler<EComponentHolder> {

private final TargetAnnotationHelper annotationHelper;
private final APTCodeModelHelper codeModelHelper = new APTCodeModelHelper();

public SubscribeHandler(ProcessingEnvironment processingEnvironment) {
super(CanonicalNameConstants.SUBSCRIBE, processingEnvironment);
annotationHelper = new TargetAnnotationHelper(processingEnv, getTarget());
}

@Override
public void validate(Element element, AnnotationElements validatedElements, IsValid valid) {
if (!annotationHelper.enclosingElementHasEnhancedComponentAnnotation(element)) {
valid.invalidate();
return;
}

ExecutableElement executableElement = (ExecutableElement) element;

/*
* We check that twice to skip invalid annotated elements
*/
validatorHelper.enclosingElementHasEnhancedComponentAnnotation(executableElement, validatedElements, valid);

validatorHelper.returnTypeIsVoid(executableElement, valid);

validatorHelper.isPublic(element, valid);

validatorHelper.doesntThrowException(executableElement, valid);

validatorHelper.isNotFinal(element, valid);

validatorHelper.param.hasExactlyOneParameter(executableElement, valid);
}

@Override
public void process(Element element, EComponentHolder holder) throws Exception {
ExecutableElement executableElement = (ExecutableElement) element;

codeModelHelper.overrideAnnotatedMethod(executableElement, holder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package org.androidannotations.helper;

import static org.androidannotations.helper.ModelConstants.GENERATION_SUFFIX;
import static org.androidannotations.helper.ModelConstants.VALID_ENHANCED_COMPONENT_ANNOTATIONS;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -48,8 +51,6 @@

import com.sun.codemodel.JFieldRef;

import static org.androidannotations.helper.ModelConstants.GENERATION_SUFFIX;

public class AnnotationHelper {

public static final String DEFAULT_FIELD_NAME_VALUE = "value";
Expand Down Expand Up @@ -91,9 +92,9 @@ public String generatedClassQualifiedNameFromQualifiedName(String qualifiedName)
TypeElement type = typeElementFromQualifiedName(qualifiedName);
if (type.getNestingKind() == NestingKind.MEMBER) {
String parentGeneratedClass = generatedClassQualifiedNameFromQualifiedName(type.getEnclosingElement().asType().toString());
return parentGeneratedClass+"."+type.getSimpleName().toString()+GENERATION_SUFFIX;
return parentGeneratedClass + "." + type.getSimpleName().toString() + GENERATION_SUFFIX;
} else {
return qualifiedName+GENERATION_SUFFIX;
return qualifiedName + GENERATION_SUFFIX;
}
}

Expand Down Expand Up @@ -173,7 +174,7 @@ public Types getTypeUtils() {
/**
* Returns a list of {@link JFieldRef} linking to the R class, based on the
* given annotation
*
*
* @see #extractAnnotationResources(Element, String, IRInnerClass, boolean)
*/
public List<JFieldRef> extractAnnotationFieldRefs(ProcessHolder holder, Element element, String annotationName, IRInnerClass rInnerClass, boolean useElementName) {
Expand All @@ -195,7 +196,7 @@ public List<JFieldRef> extractAnnotationFieldRefs(ProcessHolder holder, Element
* set using the value() parameter of the annotation (as int or int[]), the
* resName() parameter of the annotation (as String or String[]), the
* element name.
*
*
* @param element
* the annotated element
* @param annotationName
Expand Down Expand Up @@ -427,6 +428,11 @@ public boolean hasOneOfClassAnnotations(Element element, Class<? extends Annotat
return hasOneOfClassAnnotations(element, annotations);
}

public boolean enclosingElementHasEnhancedComponentAnnotation(Element element) {
Element enclosingElement = element.getEnclosingElement();
return hasOneOfClassAnnotations(enclosingElement, VALID_ENHANCED_COMPONENT_ANNOTATIONS);
}

public boolean hasOneOfClassAnnotations(Element element, List<Class<? extends Annotation>> validAnnotations) {
for (Class<? extends Annotation> validAnnotation : validAnnotations) {
if (element.getAnnotation(validAnnotation) != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (C) 2010-2014 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.otto;

import static org.fest.assertions.Assertions.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class OttoActivityTest {

@Test
public void testOttoMethodsCalled() {
OttoActivity activity = Robolectric.setupActivity(OttoActivity_.class);

assertThat(activity.lastEvent).isNotNull();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright (C) 2010-2014 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.otto;

public class Event {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (C) 2010-2014 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.otto;

import org.androidannotations.annotations.EActivity;

import android.app.Activity;
import android.os.Bundle;

import com.squareup.otto.Bus;
import com.squareup.otto.Produce;
import com.squareup.otto.Subscribe;

@EActivity
public class OttoActivity extends Activity {

Event lastEvent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Bus bus = new Bus();
bus.register(this);
}

@Subscribe
public void onEvent(Event event) {
lastEvent = event;
}

@Produce
public Event produceEvent() {
return new Event();
}
}

0 comments on commit 7d7213f

Please sign in to comment.