This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Repository navigation
Add a @OptionMenuItem annotation to inject MenuItem #567
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Add a @OptionMenuItem annotation to inject MenuItem
- Loading branch information
commit 5e38b20eb92e3c1dbe172b67a1cdde7866e04372
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
...roidannotations-api/src/main/java/org/androidannotations/annotations/OptionsMenuItem.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /** | ||
| * Copyright (C) 2010-2013 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.annotations; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * Should be used on option items fields in activity classes. | ||
| * | ||
| * The field MUST be of type {@link link android.view.MenuItem} or | ||
| * {@link com.actionbarsherlock.view.MenuItem}. | ||
| * <p/> | ||
| * The annotation value should be one of R.id.* fields. If not set, the method | ||
| * name will be used as the R.id.* field name. * | ||
| */ | ||
| @Retention(RetentionPolicy.CLASS) | ||
| @Target(ElementType.FIELD) | ||
| public @interface OptionsMenuItem { | ||
|
|
||
| int[] value() default ResId.DEFAULT_VALUE; | ||
|
|
||
| String[] resName() default ""; | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...tations/src/main/java/org/androidannotations/processing/AbstractOptionsMenuProcessor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /** | ||
| * Copyright (C) 2010-2013 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.processing; | ||
|
|
||
| import static com.sun.codemodel.JExpr.invoke; | ||
| import static com.sun.codemodel.JMod.PUBLIC; | ||
|
|
||
| import javax.annotation.processing.ProcessingEnvironment; | ||
| import javax.lang.model.element.Element; | ||
|
|
||
| import org.androidannotations.annotations.EFragment; | ||
| import org.androidannotations.helper.IdAnnotationHelper; | ||
| import org.androidannotations.helper.ThirdPartyLibHelper; | ||
| import org.androidannotations.processing.EBeansHolder.Classes; | ||
| import org.androidannotations.rclass.IRClass; | ||
|
|
||
| import com.sun.codemodel.JBlock; | ||
| import com.sun.codemodel.JClass; | ||
| import com.sun.codemodel.JCodeModel; | ||
| import com.sun.codemodel.JExpr; | ||
| import com.sun.codemodel.JInvocation; | ||
| import com.sun.codemodel.JMethod; | ||
| import com.sun.codemodel.JType; | ||
| import com.sun.codemodel.JVar; | ||
|
|
||
| public abstract class AbstractOptionsMenuProcessor implements DecoratingElementProcessor { | ||
|
|
||
| protected final ThirdPartyLibHelper libHelper; | ||
| protected final IdAnnotationHelper annotationHelper; | ||
|
|
||
| public AbstractOptionsMenuProcessor(ProcessingEnvironment processingEnv, IRClass rClass) { | ||
| annotationHelper = new IdAnnotationHelper(processingEnv, getTarget(), rClass); | ||
| libHelper = new ThirdPartyLibHelper(annotationHelper); | ||
| } | ||
|
|
||
| @Override | ||
| public void process(Element element, JCodeModel codeModel, EBeanHolder holder) { | ||
| if (holder.onCreateOptionMenuMethodBody == null) { | ||
| Classes classes = holder.classes(); | ||
|
|
||
| boolean isFragment = holder.eBeanAnnotation == EFragment.class; | ||
|
|
||
| JClass menuClass; | ||
| JClass menuInflaterClass; | ||
| String getMenuInflaterMethodName; | ||
| if (libHelper.usesActionBarSherlock(holder)) { | ||
| menuClass = classes.SHERLOCK_MENU; | ||
| menuInflaterClass = classes.SHERLOCK_MENU_INFLATER; | ||
| getMenuInflaterMethodName = "getSupportMenuInflater"; | ||
| } else { | ||
| menuClass = classes.MENU; | ||
| menuInflaterClass = classes.MENU_INFLATER; | ||
| getMenuInflaterMethodName = "getMenuInflater"; | ||
| } | ||
|
|
||
| JType returnType; | ||
| if (isFragment) { | ||
| returnType = codeModel.VOID; | ||
| } else { | ||
| returnType = codeModel.BOOLEAN; | ||
| } | ||
|
|
||
| JBlock body; | ||
| JVar menuInflater; | ||
| JVar menuParam; | ||
|
|
||
| JMethod method = holder.generatedClass.method(PUBLIC, returnType, "onCreateOptionsMenu"); | ||
| method.annotate(Override.class); | ||
|
|
||
| menuParam = method.param(menuClass, "menu"); | ||
|
|
||
| JBlock methodBody = method.body(); | ||
|
|
||
| if (isFragment) { | ||
| menuInflater = method.param(menuInflaterClass, "inflater"); | ||
| } else { | ||
| menuInflater = methodBody.decl(menuInflaterClass, "menuInflater", invoke(getMenuInflaterMethodName)); | ||
| } | ||
|
|
||
| body = methodBody.block(); | ||
|
|
||
| JInvocation superCall = invoke(JExpr._super(), method); | ||
| superCall.arg(menuParam); | ||
|
|
||
| if (isFragment) { | ||
| superCall.arg(menuInflater); | ||
| methodBody.add(superCall); | ||
| } else { | ||
| methodBody._return(superCall); | ||
| } | ||
|
|
||
| if (isFragment) { | ||
| holder.initBody.invoke("setHasOptionsMenu").arg(JExpr.TRUE); | ||
| } | ||
|
|
||
| holder.onCreateOptionMenuMethodBody = body; | ||
| holder.onCreateOptionMenuMenuInflaterVariable = menuInflater; | ||
| holder.onCreateOptionMenuMenuParam = menuParam; | ||
| } | ||
| } | ||
|
|
||
| } |
58 changes: 58 additions & 0 deletions
58
...annotations/src/main/java/org/androidannotations/processing/OptionsMenuItemProcessor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /** | ||
| * Copyright (C) 2010-2013 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.processing; | ||
|
|
||
| import static com.sun.codemodel.JExpr.ref; | ||
|
|
||
| import javax.annotation.processing.ProcessingEnvironment; | ||
| import javax.lang.model.element.Element; | ||
|
|
||
| import org.androidannotations.annotations.OptionsMenuItem; | ||
| import org.androidannotations.rclass.IRClass; | ||
| import org.androidannotations.rclass.IRClass.Res; | ||
|
|
||
| import com.sun.codemodel.JBlock; | ||
| import com.sun.codemodel.JCodeModel; | ||
| import com.sun.codemodel.JFieldRef; | ||
| import com.sun.codemodel.JVar; | ||
|
|
||
| /** | ||
| */ | ||
| public class OptionsMenuItemProcessor extends AbstractOptionsMenuProcessor { | ||
|
|
||
| public OptionsMenuItemProcessor(ProcessingEnvironment processingEnv, IRClass rClass) { | ||
| super(processingEnv, rClass); | ||
| } | ||
|
|
||
| @Override | ||
| public String getTarget() { | ||
| return OptionsMenuItem.class.getName(); | ||
| } | ||
|
|
||
| @Override | ||
| public void process(Element element, JCodeModel codeModel, EBeanHolder holder) { | ||
| super.process(element, codeModel, holder); | ||
|
|
||
| String fieldName = element.getSimpleName().toString(); | ||
| JBlock body = holder.onCreateOptionMenuMethodBody; | ||
| JVar menuParam = holder.onCreateOptionMenuMenuParam; | ||
|
|
||
| JFieldRef idsRef = annotationHelper.extractOneAnnotationFieldRef(holder, element, Res.ID, true); | ||
|
|
||
| body.assign(ref(fieldName), menuParam.invoke("findItem").arg(idsRef)); | ||
|
|
||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if extended
SherlockActivitycontains androidMenuItemor vice versa? I haven't tried, but it seems that the user will get an error in generated folder.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, you're right. We need to add a check on the super class.