Skip to content

Commit 57744db

Browse files
committed
Create @InjectMenu to inject menu
1 parent 7b4bef4 commit 57744db

4 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright (C) 2010-2015 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.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+
* <p>
25+
* Should be used on {@link EActivity} or {@link EFragment} annotated classes to
26+
* inject the menu.
27+
* </p>
28+
* <p>
29+
* The field MUST be of type {@link android.view.Menu}.
30+
* </p>
31+
*
32+
* <blockquote>
33+
*
34+
* Example :
35+
*
36+
* <pre>
37+
* &#064;EActivity
38+
* public class MyActivity extends Activity {
39+
*
40+
* &#064;InjectMenu
41+
* Menu menu;
42+
*
43+
* }
44+
* </pre>
45+
*
46+
* </blockquote>
47+
*/
48+
@Retention(RetentionPolicy.CLASS)
49+
@Target(ElementType.FIELD)
50+
public @interface InjectMenu {
51+
}

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/handler/AnnotationHandlers.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public AnnotationHandlers(ProcessingEnvironment processingEnvironment) {
101101
add(new OptionsHandler(processingEnvironment));
102102
add(new AppHandler(processingEnvironment));
103103
add(new BeanHandler(processingEnvironment));
104+
add(new InjectMenuHandler(processingEnvironment));
104105
add(new OptionsMenuHandler(processingEnvironment));
105106
add(new OptionsMenuItemHandler(processingEnvironment));
106107
add(new OptionsItemHandler(processingEnvironment));
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
3+
* <p/>
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+
* <p/>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p/>
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.handler;
17+
18+
19+
import com.sun.codemodel.JBlock;
20+
import com.sun.codemodel.JExpr;
21+
import com.sun.codemodel.JVar;
22+
import org.androidannotations.annotations.InjectMenu;
23+
import org.androidannotations.holder.HasOptionsMenu;
24+
import org.androidannotations.model.AnnotationElements;
25+
import org.androidannotations.process.IsValid;
26+
27+
import javax.annotation.processing.ProcessingEnvironment;
28+
import javax.lang.model.element.Element;
29+
30+
public class InjectMenuHandler extends BaseAnnotationHandler<HasOptionsMenu> {
31+
32+
public InjectMenuHandler(ProcessingEnvironment processingEnvironment) {
33+
super(InjectMenu.class, processingEnvironment);
34+
}
35+
36+
@Override
37+
public void validate(Element element, AnnotationElements validatedElements, IsValid valid) {
38+
validatorHelper.enclosingElementHasEActivityOrEFragment(element, validatedElements, valid);
39+
40+
validatorHelper.isDeclaredType(element, valid);
41+
42+
validatorHelper.extendsMenu(element, valid);
43+
44+
validatorHelper.isNotPrivate(element, valid);
45+
}
46+
47+
@Override
48+
public void process(Element element, HasOptionsMenu holder) {
49+
String fieldName = element.getSimpleName().toString();
50+
JBlock body = holder.getOnCreateOptionsMenuMethodBody();
51+
JVar menuParam = holder.getOnCreateOptionsMenuMenuParam();
52+
53+
body.assign(JExpr._this().ref(fieldName), menuParam);
54+
}
55+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,16 @@ public void extendsMenuItem(Element element, IsValid valid) {
532532
}
533533
}
534534

535+
public void extendsMenu(Element element, IsValid valid) {
536+
Element enclosingElement = element.getEnclosingElement();
537+
String enclosingQualifiedName = enclosingElement.asType().toString();
538+
TypeElement enclosingTypeElement = annotationHelper.typeElementFromQualifiedName(enclosingQualifiedName);
539+
540+
if (enclosingTypeElement != null) {
541+
extendsType(element, CanonicalNameConstants.MENU, valid);
542+
}
543+
}
544+
535545
public void extendsOrmLiteDao(Element element, IsValid valid, OrmLiteHelper ormLiteHelper) {
536546
if (!valid.isValid()) {
537547
// we now that the element is invalid. early exit as the reason

0 commit comments

Comments
 (0)