Skip to content

Commit d897f2e

Browse files
committed
Support for @optionsmenu
1 parent 92052e5 commit d897f2e

File tree

9 files changed

+242
-9
lines changed

9 files changed

+242
-9
lines changed

AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/AndroidAnnotationProcessor.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.googlecode.androidannotations.annotations.ItemLongClick;
4545
import com.googlecode.androidannotations.annotations.ItemSelect;
4646
import com.googlecode.androidannotations.annotations.LongClick;
47+
import com.googlecode.androidannotations.annotations.OptionsMenu;
4748
import com.googlecode.androidannotations.annotations.RoboGuice;
4849
import com.googlecode.androidannotations.annotations.SystemService;
4950
import com.googlecode.androidannotations.annotations.Touch;
@@ -100,6 +101,7 @@
100101
import com.googlecode.androidannotations.processing.ItemSelectedProcessor;
101102
import com.googlecode.androidannotations.processing.LongClickProcessor;
102103
import com.googlecode.androidannotations.processing.ModelProcessor;
104+
import com.googlecode.androidannotations.processing.OptionsMenuProcessor;
103105
import com.googlecode.androidannotations.processing.PrefProcessor;
104106
import com.googlecode.androidannotations.processing.ResProcessor;
105107
import com.googlecode.androidannotations.processing.RoboGuiceProcessor;
@@ -130,6 +132,7 @@
130132
import com.googlecode.androidannotations.validation.ItemSelectedValidator;
131133
import com.googlecode.androidannotations.validation.LongClickValidator;
132134
import com.googlecode.androidannotations.validation.ModelValidator;
135+
import com.googlecode.androidannotations.validation.OptionsMenuValidator;
133136
import com.googlecode.androidannotations.validation.PrefValidator;
134137
import com.googlecode.androidannotations.validation.ResValidator;
135138
import com.googlecode.androidannotations.validation.RoboGuiceValidator;
@@ -191,7 +194,8 @@
191194
Options.class, //
192195
Post.class, //
193196
Put.class,
194-
FromHtml.class,
197+
FromHtml.class, //
198+
OptionsMenu.class, //
195199
HtmlRes.class})
196200
@SupportedSourceVersion(SourceVersion.RELEASE_6)
197201
public class AndroidAnnotationProcessor extends AnnotatedAbstractProcessor {
@@ -349,6 +353,7 @@ private ModelValidator buildModelValidator(IRClass rClass, AndroidSystemServices
349353
modelValidator.register(new PostValidator(processingEnv));
350354
modelValidator.register(new PutValidator(processingEnv));
351355
modelValidator.register(new AppValidator(processingEnv, androidManifest));
356+
modelValidator.register(new OptionsMenuValidator(processingEnv, rClass));
352357
return modelValidator;
353358
}
354359

@@ -390,7 +395,7 @@ private ModelProcessor buildModelProcessor(IRClass rClass, AndroidSystemServices
390395
modelProcessor.register(new RestProcessor(restImplementationHolder));
391396
modelProcessor.register(new GetProcessor(processingEnv, restImplementationHolder));
392397
modelProcessor.register(new AppProcessor());
393-
398+
modelProcessor.register(new OptionsMenuProcessor(rClass));
394399
return modelProcessor;
395400
}
396401

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright (C) 2010-2011 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 com.googlecode.androidannotations.annotations;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
/**
24+
* Should be used on Activity classes to set the id of the options menu.
25+
*
26+
* The activity must be annotated with {@link EActivity}.
27+
*
28+
* The annotation value should be one of R.menu.* fields.
29+
*
30+
*/
31+
@Retention(RetentionPolicy.SOURCE)
32+
@Target(ElementType.TYPE)
33+
public @interface OptionsMenu {
34+
int value();
35+
}

AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/helper/IdValidatorHelper.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,33 @@ public IdValidatorHelper(IdAnnotationHelper idAnnotationHelper) {
3939
public void idExists(Element element, Res res, IsValid valid) {
4040
idExists(element, res, true, valid);
4141
}
42-
42+
4343
public void idExists(Element element, Res res, boolean defaultUseName, IsValid valid) {
44+
idExists(element, res, defaultUseName, true, valid);
45+
}
46+
47+
public void idExists(Element element, Res res, boolean defaultUseName, boolean allowDefault, IsValid valid) {
4448

4549
Integer idValue = annotationHelper.extractAnnotationValue(element);
4650

47-
idExists(element, res, defaultUseName, valid, idValue);
51+
idExists(element, res, defaultUseName, allowDefault, valid, idValue);
4852
}
4953

5054
public void idsExists(Element element, Res res, IsValid valid) {
5155

5256
int [] idsValues = annotationHelper.extractAnnotationValue(element);
5357
if (idsValues[0] == Id.DEFAULT_VALUE) {
54-
idExists(element, res, true, valid, idsValues[0]);
58+
idExists(element, res, true, true, valid, idsValues[0]);
5559
} else {
5660
for (int idValue : idsValues) {
57-
idExists(element, res, false, valid, idValue);
61+
idExists(element, res, false, true, valid, idValue);
5862
}
5963
}
6064
}
6165

62-
private void idExists(Element element, Res res, boolean defaultUseName,
66+
private void idExists(Element element, Res res, boolean defaultUseName, boolean allowDefault,
6367
IsValid valid, Integer idValue) {
64-
if (idValue.equals(Id.DEFAULT_VALUE)) {
68+
if (allowDefault && idValue.equals(Id.DEFAULT_VALUE)) {
6569
if (defaultUseName) {
6670
String elementName = element.getSimpleName().toString();
6771
int lastIndex = elementName.lastIndexOf(annotationHelper
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Copyright (C) 2010-2011 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 com.googlecode.androidannotations.processing;
17+
18+
import static com.sun.codemodel.JExpr._super;
19+
import static com.sun.codemodel.JExpr.invoke;
20+
import static com.sun.codemodel.JMod.PUBLIC;
21+
22+
import java.lang.annotation.Annotation;
23+
24+
import javax.lang.model.element.Element;
25+
26+
import com.googlecode.androidannotations.annotations.OptionsMenu;
27+
import com.googlecode.androidannotations.rclass.IRClass;
28+
import com.googlecode.androidannotations.rclass.IRClass.Res;
29+
import com.googlecode.androidannotations.rclass.IRInnerClass;
30+
import com.sun.codemodel.JBlock;
31+
import com.sun.codemodel.JCodeModel;
32+
import com.sun.codemodel.JFieldRef;
33+
import com.sun.codemodel.JMethod;
34+
import com.sun.codemodel.JVar;
35+
36+
public class OptionsMenuProcessor implements ElementProcessor {
37+
38+
private final IRClass rClass;
39+
40+
public OptionsMenuProcessor(IRClass rClass) {
41+
this.rClass = rClass;
42+
}
43+
44+
@Override
45+
public Class<? extends Annotation> getTarget() {
46+
return OptionsMenu.class;
47+
}
48+
49+
@Override
50+
public void process(Element element, JCodeModel codeModel, EBeansHolder activitiesHolder) {
51+
EBeanHolder holder = activitiesHolder.getRelativeEBeanHolder(element);
52+
53+
OptionsMenu layoutAnnotation = element.getAnnotation(OptionsMenu.class);
54+
int layoutIdValue = layoutAnnotation.value();
55+
56+
IRInnerClass rInnerClass = rClass.get(Res.MENU);
57+
JFieldRef optionsMenuId = rInnerClass.getIdStaticRef(layoutIdValue, holder);
58+
59+
JMethod method = holder.eBean.method(PUBLIC, codeModel.BOOLEAN,"onCreateOptionsMenu");
60+
method.annotate(Override.class);
61+
JVar menuParam = method.param(holder.refClass("android.view.Menu"), "menu");
62+
63+
JBlock body = method.body();
64+
65+
JVar menuInflater = body.decl(holder.refClass("android.view.MenuInflater"), "menuInflater", invoke("getMenuInflater"));
66+
67+
body.invoke(menuInflater, "inflate").arg(optionsMenuId).arg(menuParam);
68+
69+
body._return(invoke(_super(), method).arg(menuParam));
70+
}
71+
72+
}

AndroidAnnotations/androidannotations/src/main/java/com/googlecode/androidannotations/rclass/IRClass.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
public interface IRClass {
2020

2121
public enum Res {
22-
LAYOUT, ID, STRING, ARRAY, COLOR, ANIM, BOOL, DIMEN, DRAWABLE, INTEGER, MOVIE;
22+
LAYOUT, ID, STRING, ARRAY, COLOR, ANIM, BOOL, DIMEN, DRAWABLE, INTEGER, MOVIE, MENU;
2323
public String rName() {
2424
return toString().toLowerCase();
2525
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Copyright (C) 2010-2011 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 com.googlecode.androidannotations.validation;
17+
18+
import java.lang.annotation.Annotation;
19+
20+
import javax.annotation.processing.ProcessingEnvironment;
21+
import javax.lang.model.element.Element;
22+
23+
import com.googlecode.androidannotations.annotations.OptionsMenu;
24+
import com.googlecode.androidannotations.helper.IdAnnotationHelper;
25+
import com.googlecode.androidannotations.helper.IdValidatorHelper;
26+
import com.googlecode.androidannotations.model.AnnotationElements;
27+
import com.googlecode.androidannotations.rclass.IRClass;
28+
import com.googlecode.androidannotations.rclass.IRClass.Res;
29+
30+
public class OptionsMenuValidator implements ElementValidator {
31+
32+
private IdValidatorHelper validatorHelper;
33+
34+
public OptionsMenuValidator(ProcessingEnvironment processingEnv, IRClass rClass) {
35+
IdAnnotationHelper annotationHelper = new IdAnnotationHelper(processingEnv, getTarget(), rClass);
36+
validatorHelper = new IdValidatorHelper(annotationHelper);
37+
}
38+
39+
@Override
40+
public Class<? extends Annotation> getTarget() {
41+
return OptionsMenu.class;
42+
}
43+
44+
@Override
45+
public boolean validate(Element element, AnnotationElements validatedElements) {
46+
47+
IsValid valid = new IsValid();
48+
49+
validatorHelper.hasEActivity(element, validatedElements, valid);
50+
51+
validatorHelper.idExists(element, Res.MENU, false, false, valid);
52+
53+
return valid.isValid();
54+
}
55+
56+
}

AndroidAnnotations/functional-test-1-5/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<activity android:name=".prefs.PrefsActivity_" />
3939
<activity android:name=".res.ResActivity_" />
4040
<activity android:name=".ThreadActivity_" />
41+
<activity android:name="com.googlecode.androidannotations.test15.menu.OptionsMenuActivity_" />
4142

4243

4344
</application>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
4+
Copyright (C) 2010-2011 eBusiness Information, Excilys Group
5+
6+
Licensed under the Apache License, Version 2.0 (the "License"); you may not
7+
use this file except in compliance with the License. You may obtain a copy of
8+
the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed To in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15+
License for the specific language governing permissions and limitations under
16+
the License.
17+
18+
-->
19+
<menu xmlns:android="http://schemas.android.com/apk/res/android">
20+
<item android:id="@+id/menu_refresh"
21+
android:title="Refresh"
22+
/>
23+
24+
<item android:id="@+id/menu_search"
25+
android:title="Search"
26+
/>
27+
28+
<item android:id="@+id/menu_share"
29+
android:title="Share"
30+
/>
31+
</menu>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (C) 2010-2011 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 com.googlecode.androidannotations.test15.menu;
17+
18+
import android.app.Activity;
19+
20+
import com.googlecode.androidannotations.annotations.EActivity;
21+
import com.googlecode.androidannotations.annotations.OptionsMenu;
22+
import com.googlecode.androidannotations.test15.R;
23+
24+
@EActivity
25+
@OptionsMenu(R.menu.main)
26+
public class OptionsMenuActivity extends Activity {
27+
28+
29+
}

0 commit comments

Comments
 (0)