Skip to content

Commit 2aa295e

Browse files
committed
Merge pull request androidannotations#526 from excilys/272_Otto_integration
Otto integration. Fixes androidannotations#272
2 parents bd8cd24 + 26199a5 commit 2aa295e

13 files changed

Lines changed: 386 additions & 12 deletions

File tree

AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/AndroidAnnotationProcessor.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package org.androidannotations;
1717

1818
import static org.androidannotations.helper.AndroidManifestFinder.ANDROID_MANIFEST_FILE_OPTION;
19+
import static org.androidannotations.helper.CanonicalNameConstants.PRODUCE;
20+
import static org.androidannotations.helper.CanonicalNameConstants.SUBSCRIBE;
1921
import static org.androidannotations.helper.ModelConstants.TRACE_OPTION;
2022

2123
import java.io.IOException;
@@ -164,6 +166,7 @@
164166
import org.androidannotations.processing.OptionsMenuProcessor;
165167
import org.androidannotations.processing.OrmLiteDaoProcessor;
166168
import org.androidannotations.processing.PrefProcessor;
169+
import org.androidannotations.processing.ProduceProcessor;
167170
import org.androidannotations.processing.ResProcessor;
168171
import org.androidannotations.processing.RestServiceProcessor;
169172
import org.androidannotations.processing.RoboGuiceProcessor;
@@ -172,6 +175,7 @@
172175
import org.androidannotations.processing.SeekBarTouchStartProcessor;
173176
import org.androidannotations.processing.SeekBarTouchStopProcessor;
174177
import org.androidannotations.processing.SharedPrefProcessor;
178+
import org.androidannotations.processing.SubscribeProcessor;
175179
import org.androidannotations.processing.SystemServiceProcessor;
176180
import org.androidannotations.processing.TextChangeProcessor;
177181
import org.androidannotations.processing.TouchProcessor;
@@ -229,6 +233,7 @@
229233
import org.androidannotations.validation.OptionsMenuValidator;
230234
import org.androidannotations.validation.OrmLiteDaoValidator;
231235
import org.androidannotations.validation.PrefValidator;
236+
import org.androidannotations.validation.ProduceValidator;
232237
import org.androidannotations.validation.ResValidator;
233238
import org.androidannotations.validation.RestServiceValidator;
234239
import org.androidannotations.validation.RoboGuiceValidator;
@@ -238,6 +243,7 @@
238243
import org.androidannotations.validation.SeekBarTouchStartValidator;
239244
import org.androidannotations.validation.SeekBarTouchStopValidator;
240245
import org.androidannotations.validation.SharedPrefValidator;
246+
import org.androidannotations.validation.SubscribeValidator;
241247
import org.androidannotations.validation.SystemServiceValidator;
242248
import org.androidannotations.validation.TextChangeValidator;
243249
import org.androidannotations.validation.TouchValidator;
@@ -429,13 +435,16 @@ private ModelValidator buildModelValidator(IRClass rClass, AndroidSystemServices
429435
*/
430436
modelValidator.register(new AfterViewsValidator(processingEnv));
431437
modelValidator.register(new TraceValidator(processingEnv));
438+
modelValidator.register(new SubscribeValidator(processingEnv));
439+
modelValidator.register(new ProduceValidator(processingEnv));
432440
modelValidator.register(new RunnableValidator(UiThread.class.getName(), processingEnv));
433441
modelValidator.register(new RunnableValidator(Background.class.getName(), processingEnv));
434442
modelValidator.register(new InstanceStateValidator(processingEnv));
435443
modelValidator.register(new OrmLiteDaoValidator(processingEnv, rClass));
436444
modelValidator.register(new HttpsClientValidator(processingEnv, rClass));
437445
modelValidator.register(new OnActivityResultValidator(processingEnv, rClass));
438446
modelValidator.register(new HierarchyViewerSupportValidator(processingEnv, androidManifest));
447+
439448
return modelValidator;
440449
}
441450

@@ -521,6 +530,8 @@ private ModelProcessor buildModelProcessor(IRClass rClass, AndroidSystemServices
521530
if (traceActivated()) {
522531
modelProcessor.register(new TraceProcessor());
523532
}
533+
modelProcessor.register(new SubscribeProcessor());
534+
modelProcessor.register(new ProduceProcessor());
524535
modelProcessor.register(new UiThreadProcessor());
525536
modelProcessor.register(new BackgroundProcessor());
526537
modelProcessor.register(new AfterInjectProcessor());
@@ -652,6 +663,9 @@ public Set<String> getSupportedAnnotationTypes() {
652663
set.add(annotationClass.getName());
653664
}
654665

666+
set.add(SUBSCRIBE);
667+
set.add(PRODUCE);
668+
655669
supportedAnnotationNames = Collections.unmodifiableSet(set);
656670
}
657671
return supportedAnnotationNames;

AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/AnnotationHelper.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package org.androidannotations.helper;
1717

18+
import static org.androidannotations.helper.ModelConstants.GENERATION_SUFFIX;
19+
import static org.androidannotations.helper.ModelConstants.VALID_ENHANCED_COMPONENT_ANNOTATIONS;
20+
1821
import java.lang.annotation.Annotation;
1922
import java.lang.reflect.InvocationTargetException;
2023
import java.lang.reflect.Method;
@@ -372,4 +375,26 @@ public DeclaredType extractAnnotationClassParameter(Element element, String anno
372375
return extractAnnotationClassParameter(element, annotationName, "value");
373376
}
374377

378+
public boolean enclosingElementIsGenerated(Element element) {
379+
/*
380+
* TODO This isn't really safe, can we find a better way?
381+
*/
382+
Element enclosingElement = element.getEnclosingElement();
383+
return enclosingElement.getSimpleName().toString().endsWith(GENERATION_SUFFIX);
384+
}
385+
386+
public boolean enclosingElementHasEnhancedComponentAnnotation(Element element) {
387+
Element enclosingElement = element.getEnclosingElement();
388+
return hasOneOfClassAnnotations(enclosingElement, VALID_ENHANCED_COMPONENT_ANNOTATIONS);
389+
}
390+
391+
public boolean hasOneOfClassAnnotations(Element element, List<Class<? extends Annotation>> validAnnotations) {
392+
for (Class<? extends Annotation> validAnnotation : validAnnotations) {
393+
if (element.getAnnotation(validAnnotation) != null) {
394+
return true;
395+
}
396+
}
397+
return false;
398+
}
399+
375400
}

AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/CanonicalNameConstants.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ public final class CanonicalNameConstants {
162162
public static final String SCHEME_REGISTRY = "org.apache.http.conn.scheme.SchemeRegistry";
163163
public static final String SINGLE_CLIENT_CONN_MANAGER = "org.apache.http.impl.conn.SingleClientConnManager";
164164

165+
/*
166+
* Otto
167+
*/
168+
public static final String SUBSCRIBE = "com.squareup.otto.Subscribe";
169+
public static final String PRODUCE = "com.squareup.otto.Produce";
170+
165171
private CanonicalNameConstants() {
166172
}
167173

AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/ModelConstants.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,33 @@
1515
*/
1616
package org.androidannotations.helper;
1717

18+
import static java.util.Arrays.asList;
19+
20+
import java.lang.annotation.Annotation;
21+
import java.util.List;
22+
23+
import org.androidannotations.annotations.EActivity;
24+
import org.androidannotations.annotations.EApplication;
25+
import org.androidannotations.annotations.EBean;
26+
import org.androidannotations.annotations.EFragment;
27+
import org.androidannotations.annotations.EProvider;
28+
import org.androidannotations.annotations.EReceiver;
29+
import org.androidannotations.annotations.EService;
30+
import org.androidannotations.annotations.EView;
31+
import org.androidannotations.annotations.EViewGroup;
32+
1833
public abstract class ModelConstants {
1934

2035
public static final String GENERATION_SUFFIX = "_";
2136

2237
public static final String TRACE_OPTION = "trace";
2338

39+
@SuppressWarnings("unchecked")
40+
public static final List<Class<? extends Annotation>> VALID_ENHANCED_VIEW_SUPPORT_ANNOTATIONS = asList(EActivity.class, EViewGroup.class, EView.class, EBean.class, EFragment.class);
41+
42+
@SuppressWarnings("unchecked")
43+
public static final List<Class<? extends Annotation>> VALID_ENHANCED_COMPONENT_ANNOTATIONS = asList(EApplication.class, EActivity.class, EViewGroup.class, EView.class, EBean.class, EService.class, EReceiver.class, EProvider.class, EFragment.class);
44+
2445
private ModelConstants() {
2546
}
2647

AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/ValidatorHelper.java

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import static org.androidannotations.helper.CanonicalNameConstants.HTTP_MESSAGE_CONVERTER;
2626
import static org.androidannotations.helper.CanonicalNameConstants.INTERNET_PERMISSION;
2727
import static org.androidannotations.helper.ModelConstants.GENERATION_SUFFIX;
28+
import static org.androidannotations.helper.ModelConstants.VALID_ENHANCED_COMPONENT_ANNOTATIONS;
29+
import static org.androidannotations.helper.ModelConstants.VALID_ENHANCED_VIEW_SUPPORT_ANNOTATIONS;
2830

2931
import java.lang.annotation.Annotation;
3032
import java.util.ArrayList;
@@ -49,14 +51,8 @@
4951
import javax.lang.model.util.Elements;
5052

5153
import org.androidannotations.annotations.EActivity;
52-
import org.androidannotations.annotations.EApplication;
5354
import org.androidannotations.annotations.EBean;
5455
import org.androidannotations.annotations.EFragment;
55-
import org.androidannotations.annotations.EProvider;
56-
import org.androidannotations.annotations.EReceiver;
57-
import org.androidannotations.annotations.EService;
58-
import org.androidannotations.annotations.EView;
59-
import org.androidannotations.annotations.EViewGroup;
6056
import org.androidannotations.annotations.Trace;
6157
import org.androidannotations.annotations.ViewById;
6258
import org.androidannotations.annotations.rest.Delete;
@@ -91,12 +87,6 @@ public class ValidatorHelper {
9187

9288
private static final Collection<Integer> VALID_LOG_LEVELS = Arrays.asList(LOG_VERBOSE, LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR);
9389

94-
@SuppressWarnings("unchecked")
95-
private static final List<Class<? extends Annotation>> VALID_ENHANCED_VIEW_SUPPORT_ANNOTATIONS = asList(EActivity.class, EViewGroup.class, EView.class, EBean.class, EFragment.class);
96-
97-
@SuppressWarnings("unchecked")
98-
private static final List<Class<? extends Annotation>> VALID_ENHANCED_COMPONENT_ANNOTATIONS = asList(EApplication.class, EActivity.class, EViewGroup.class, EView.class, EBean.class, EService.class, EReceiver.class, EProvider.class, EFragment.class);
99-
10090
protected final TargetAnnotationHelper annotationHelper;
10191

10292
public ValidatorHelper(TargetAnnotationHelper targetAnnotationHelper) {
@@ -159,6 +149,13 @@ public void isNotPrivate(Element element, IsValid valid) {
159149
}
160150
}
161151

152+
public void isPublic(Element element, IsValid valid) {
153+
if (!annotationHelper.isPublic(element)) {
154+
valid.invalidate();
155+
annotationHelper.printAnnotationError(element, "%s cannot be used on a non public element");
156+
}
157+
}
158+
162159
public void enclosingElementHasEBeanAnnotation(Element element, AnnotationElements validatedElements, IsValid valid) {
163160
Element enclosingElement = element.getEnclosingElement();
164161
hasClassAnnotation(element, enclosingElement, validatedElements, EBean.class, valid);
@@ -467,6 +464,15 @@ public void returnTypeIsVoid(ExecutableElement executableElement, IsValid valid)
467464
}
468465
}
469466

467+
public void returnTypeIsNotVoid(ExecutableElement executableElement, IsValid valid) {
468+
TypeMirror returnType = executableElement.getReturnType();
469+
470+
if (returnType.getKind() == TypeKind.VOID) {
471+
valid.invalidate();
472+
annotationHelper.printAnnotationError(executableElement, "%s can only be used on a method with a return type non void");
473+
}
474+
}
475+
470476
public void zeroOrOneParameter(ExecutableElement executableElement, IsValid valid) {
471477
List<? extends VariableElement> parameters = executableElement.getParameters();
472478

@@ -682,6 +688,14 @@ public void extendsType(Element element, String typeQualifiedName, IsValid valid
682688
}
683689
}
684690

691+
public void hasExactlyOneParameter(ExecutableElement executableElement, IsValid valid) {
692+
List<? extends VariableElement> parameters = executableElement.getParameters();
693+
if (parameters.size() != 1) {
694+
valid.invalidate();
695+
annotationHelper.printAnnotationError(executableElement, "%s can only be used on a method with exactly one parameter, instead of " + parameters.size());
696+
}
697+
}
698+
685699
public void hasOneOrTwoParametersAndFirstIsBoolean(ExecutableElement executableElement, IsValid valid) {
686700
List<? extends VariableElement> parameters = executableElement.getParameters();
687701

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright (C) 2010-2013 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.processing;
17+
18+
import static org.androidannotations.helper.CanonicalNameConstants.PRODUCE;
19+
20+
import javax.lang.model.element.Element;
21+
import javax.lang.model.element.ExecutableElement;
22+
23+
import org.androidannotations.helper.APTCodeModelHelper;
24+
25+
import com.sun.codemodel.JClassAlreadyExistsException;
26+
import com.sun.codemodel.JCodeModel;
27+
import com.sun.codemodel.JMethod;
28+
29+
public class ProduceProcessor implements DecoratingElementProcessor {
30+
31+
private final APTCodeModelHelper helper = new APTCodeModelHelper();
32+
33+
@Override
34+
public String getTarget() {
35+
return PRODUCE;
36+
}
37+
38+
@Override
39+
public void process(Element element, JCodeModel codeModel, EBeanHolder holder) throws JClassAlreadyExistsException {
40+
41+
ExecutableElement executableElement = (ExecutableElement) element;
42+
43+
JMethod delegatingMethod = helper.overrideAnnotatedMethod(executableElement, holder);
44+
45+
delegatingMethod.annotate(holder.refClass(PRODUCE));
46+
}
47+
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright (C) 2010-2013 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.processing;
17+
18+
import static org.androidannotations.helper.CanonicalNameConstants.SUBSCRIBE;
19+
20+
import javax.lang.model.element.Element;
21+
import javax.lang.model.element.ExecutableElement;
22+
23+
import org.androidannotations.helper.APTCodeModelHelper;
24+
25+
import com.sun.codemodel.JClassAlreadyExistsException;
26+
import com.sun.codemodel.JCodeModel;
27+
import com.sun.codemodel.JMethod;
28+
29+
public class SubscribeProcessor implements DecoratingElementProcessor {
30+
31+
private final APTCodeModelHelper helper = new APTCodeModelHelper();
32+
33+
@Override
34+
public String getTarget() {
35+
return SUBSCRIBE;
36+
}
37+
38+
@Override
39+
public void process(Element element, JCodeModel codeModel, EBeanHolder holder) throws JClassAlreadyExistsException {
40+
41+
ExecutableElement executableElement = (ExecutableElement) element;
42+
43+
JMethod delegatingMethod = helper.overrideAnnotatedMethod(executableElement, holder);
44+
45+
delegatingMethod.annotate(holder.refClass(SUBSCRIBE));
46+
}
47+
48+
}

0 commit comments

Comments
 (0)