Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

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
DayS committed Apr 21, 2013
commit 5e38b20eb92e3c1dbe172b67a1cdde7866e04372
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 "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import org.androidannotations.annotations.OnActivityResult;
import org.androidannotations.annotations.OptionsItem;
import org.androidannotations.annotations.OptionsMenu;
import org.androidannotations.annotations.OptionsMenuItem;
import org.androidannotations.annotations.OrmLiteDao;
import org.androidannotations.annotations.RoboGuice;
import org.androidannotations.annotations.RootContext;
Expand Down Expand Up @@ -165,6 +166,7 @@
import org.androidannotations.processing.NonConfigurationInstanceProcessor;
import org.androidannotations.processing.OnActivityResultProcessor;
import org.androidannotations.processing.OptionsItemProcessor;
import org.androidannotations.processing.OptionsMenuItemProcessor;
import org.androidannotations.processing.OptionsMenuProcessor;
import org.androidannotations.processing.OrmLiteDaoProcessor;
import org.androidannotations.processing.PrefProcessor;
Expand Down Expand Up @@ -234,6 +236,7 @@
import org.androidannotations.validation.NonConfigurationInstanceValidator;
import org.androidannotations.validation.OnActivityResultValidator;
import org.androidannotations.validation.OptionsItemValidator;
import org.androidannotations.validation.OptionsMenuItemValidator;
import org.androidannotations.validation.OptionsMenuValidator;
import org.androidannotations.validation.OrmLiteDaoValidator;
import org.androidannotations.validation.PrefValidator;
Expand Down Expand Up @@ -420,6 +423,7 @@ private ModelValidator buildModelValidator(IRClass rClass, AndroidSystemServices
modelValidator.register(new AcceptValidator(processingEnv));
modelValidator.register(new AppValidator(processingEnv));
modelValidator.register(new OptionsMenuValidator(processingEnv, rClass));
modelValidator.register(new OptionsMenuItemValidator(processingEnv, rClass));
modelValidator.register(new OptionsItemValidator(processingEnv, rClass));
modelValidator.register(new NoTitleValidator(processingEnv));
modelValidator.register(new CustomTitleValidator(processingEnv, rClass));
Expand Down Expand Up @@ -515,6 +519,7 @@ private ModelProcessor buildModelProcessor(IRClass rClass, AndroidSystemServices
modelProcessor.register(new OptionsProcessor(processingEnv, restImplementationsHolder));
modelProcessor.register(new AppProcessor());
modelProcessor.register(new OptionsMenuProcessor(processingEnv, rClass));
modelProcessor.register(new OptionsMenuItemProcessor(processingEnv, rClass));
modelProcessor.register(new OptionsItemProcessor(processingEnv, rClass));
modelProcessor.register(new NoTitleProcessor());
modelProcessor.register(new CustomTitleProcessor(processingEnv, rClass));
Expand Down Expand Up @@ -633,6 +638,7 @@ public Set<String> getSupportedAnnotationTypes() {
Accept.class, //
FromHtml.class, //
OptionsMenu.class, //
OptionsMenuItem.class, //
OptionsItem.class, //
HtmlRes.class, //
NoTitle.class, //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public class ValidatorHelper {

private static final List<String> ANDROID_FRAGMENT_QUALIFIED_NAMES = asList(CanonicalNameConstants.FRAGMENT, CanonicalNameConstants.SUPPORT_V4_FRAGMENT);

private static final List<String> ANDROID_MENU_ITEM_QUALIFIED_NAMES = asList(CanonicalNameConstants.MENU_ITEM, CanonicalNameConstants.SHERLOCK_MENU_ITEM);

private static final String METHOD_NAME_SET_ROOT_URL = "setRootUrl";

private static final List<String> VALID_PREF_RETURN_TYPES = Arrays.asList("int", "boolean", "float", "long", CanonicalNameConstants.STRING);
Expand Down Expand Up @@ -497,6 +499,10 @@ public void extendsContext(Element element, IsValid valid) {
extendsType(element, CanonicalNameConstants.CONTEXT, valid);
}

public void extendsMenuItem(Element element, IsValid valid) {
extendsOneOfTypes(element, ANDROID_MENU_ITEM_QUALIFIED_NAMES, valid);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if extended SherlockActivity contains android MenuItem or vice versa? I haven't tried, but it seems that the user will get an error in generated folder.

Copy link
Copy Markdown
Contributor Author

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.

}

public void extendsOrmLiteDaoWithValidModelParameter(Element element, IsValid valid) {
TypeMirror elementType = element.asType();

Expand Down
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;
}
}

}
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));

}
}
Loading