Skip to content

Commit b3657d9

Browse files
Merge pull request androidannotations#474 from excilys/473_OptionsMenu
Fixed the generated code when multiple @optionsmenu are declared in the class hierarchy
2 parents d2e5d15 + 2de534f commit b3657d9

5 files changed

Lines changed: 93 additions & 22 deletions

File tree

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/EBeanHolder.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ public class EBeanHolder {
114114
public JMethod findNativeFragmentByTag;
115115
public JMethod findSupportFragmentByTag;
116116

117+
public JBlock onCreateOptionMenuMethodBody;
118+
public JVar onCreateOptionMenuMenuInflaterVariable;
119+
public JVar onCreateOptionMenuMenuParam;
120+
117121
private final EBeansHolder eBeansHolder;
118122
public final Class<? extends Annotation> eBeanAnnotation;
119123

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/OptionsMenuProcessor.java‎

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.androidannotations.processing;
1717

18-
import static com.sun.codemodel.JExpr.TRUE;
19-
import static com.sun.codemodel.JExpr._super;
2018
import static com.sun.codemodel.JExpr.invoke;
2119
import static com.sun.codemodel.JMod.PUBLIC;
2220

@@ -33,9 +31,11 @@
3331
import org.androidannotations.processing.EBeansHolder.Classes;
3432
import org.androidannotations.rclass.IRClass;
3533
import org.androidannotations.rclass.IRClass.Res;
34+
3635
import com.sun.codemodel.JBlock;
3736
import com.sun.codemodel.JClass;
3837
import com.sun.codemodel.JCodeModel;
38+
import com.sun.codemodel.JExpr;
3939
import com.sun.codemodel.JFieldRef;
4040
import com.sun.codemodel.JInvocation;
4141
import com.sun.codemodel.JMethod;
@@ -86,33 +86,48 @@ public void process(Element element, JCodeModel codeModel, EBeanHolder holder) {
8686
returnType = codeModel.BOOLEAN;
8787
}
8888

89-
JMethod method = holder.generatedClass.method(PUBLIC, returnType, "onCreateOptionsMenu");
90-
method.annotate(Override.class);
91-
JVar menuParam = method.param(menuClass, "menu");
89+
JBlock body = holder.onCreateOptionMenuMethodBody;
90+
JVar menuInflater = holder.onCreateOptionMenuMenuInflaterVariable;
91+
JVar menuParam = holder.onCreateOptionMenuMenuParam;
9292

93-
JBlock body = method.body();
93+
if (body == null) {
94+
JMethod method = holder.generatedClass.method(PUBLIC, returnType, "onCreateOptionsMenu");
95+
method.annotate(Override.class);
9496

95-
JVar menuInflater;
96-
if (isFragment) {
97-
menuInflater = method.param(menuInflaterClass, "inflater");
98-
} else {
99-
menuInflater = body.decl(menuInflaterClass, "menuInflater", invoke(getMenuInflaterMethodName));
97+
menuParam = method.param(menuClass, "menu");
98+
99+
JBlock methodBody = method.body();
100+
101+
if (isFragment) {
102+
menuInflater = method.param(menuInflaterClass, "inflater");
103+
} else {
104+
menuInflater = methodBody.decl(menuInflaterClass, "menuInflater", invoke(getMenuInflaterMethodName));
105+
}
106+
107+
body = methodBody.block();
108+
109+
JInvocation superCall = invoke(JExpr._super(), method);
110+
superCall.arg(menuParam);
111+
112+
if (isFragment) {
113+
superCall.arg(menuInflater);
114+
methodBody.add(superCall);
115+
} else {
116+
methodBody._return(superCall);
117+
}
118+
119+
if (isFragment) {
120+
holder.init.body().invoke("setHasOptionsMenu").arg(JExpr.TRUE);
121+
}
122+
123+
holder.onCreateOptionMenuMethodBody = body;
124+
holder.onCreateOptionMenuMenuInflaterVariable = menuInflater;
125+
holder.onCreateOptionMenuMenuParam = menuParam;
100126
}
101127

102128
for (JFieldRef optionsMenuRefId : fieldRefs) {
103129
body.invoke(menuInflater, "inflate").arg(optionsMenuRefId).arg(menuParam);
104130
}
105131

106-
JInvocation superCall = invoke(_super(), method).arg(menuParam);
107-
if (isFragment) {
108-
superCall.arg(menuInflater);
109-
body.add(superCall);
110-
} else {
111-
body._return(superCall);
112-
}
113-
114-
if (isFragment) {
115-
holder.init.body().invoke("setHasOptionsMenu").arg(TRUE);
116-
}
117132
}
118133
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<activity android:name=".TextWatchedActivity_" />
5959
<activity android:name=".ormlite.OrmLiteActivity_" />
6060
<activity android:name="org.androidannotations.test15.menu.OptionsMenuActivity_" />
61+
<activity android:name="org.androidannotations.test15.menu.OptionsMenuSubActivity_" />
6162
<activity android:name="org.androidannotations.test15.NoTitleFullscreenActivity_" />
6263
<activity android:name="org.androidannotations.test15.ExtendingActivity_" />
6364
<activity android:name="org.androidannotations.test15.ebean.BeanInjectedActivity_" />
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
4+
Copyright (C) 2010-2012 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+
21+
<item
22+
android:id="@+id/menu_settings"
23+
android:title="Settings"/>
24+
25+
</menu>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (C) 2010-2012 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.test15.menu;
17+
18+
import org.androidannotations.annotations.EActivity;
19+
import org.androidannotations.annotations.OptionsMenu;
20+
import org.androidannotations.test15.R;
21+
22+
@EActivity
23+
@OptionsMenu(R.menu.my_menu3)
24+
public class OptionsMenuSubActivity extends OptionsMenuActivity {
25+
26+
}

0 commit comments

Comments
 (0)