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 all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright (C) 2010-2015 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;

/**
* <p>
* Should be used on {@link EActivity} or {@link EFragment} annotated classes to
* inject the menu.
* </p>
* <p>
* The field MUST be of type {@link android.view.Menu}.
* </p>
*
* <blockquote>
*
* Example :
*
* <pre>
* &#064;EActivity
* public class MyActivity extends Activity {
*
* &#064;InjectMenu
* Menu menu;
*
* }
* </pre>
*
* </blockquote>
*/
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.FIELD)
public @interface InjectMenu {
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public AnnotationHandlers(ProcessingEnvironment processingEnvironment) {
add(new OptionsHandler(processingEnvironment));
add(new AppHandler(processingEnvironment));
add(new BeanHandler(processingEnvironment));
add(new InjectMenuHandler(processingEnvironment));
add(new OptionsMenuHandler(processingEnvironment));
add(new OptionsMenuItemHandler(processingEnvironment));
add(new OptionsItemHandler(processingEnvironment));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
* <p/>
* 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
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* 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.handler;


import com.sun.codemodel.JBlock;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JVar;
import org.androidannotations.annotations.InjectMenu;
import org.androidannotations.holder.HasOptionsMenu;
import org.androidannotations.model.AnnotationElements;
import org.androidannotations.process.IsValid;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;

public class InjectMenuHandler extends BaseAnnotationHandler<HasOptionsMenu> {

public InjectMenuHandler(ProcessingEnvironment processingEnvironment) {
super(InjectMenu.class, processingEnvironment);
}

@Override
public void validate(Element element, AnnotationElements validatedElements, IsValid valid) {
validatorHelper.enclosingElementHasEActivityOrEFragment(element, validatedElements, valid);

validatorHelper.isDeclaredType(element, valid);

validatorHelper.extendsMenu(element, valid);

validatorHelper.isNotPrivate(element, valid);
}

@Override
public void process(Element element, HasOptionsMenu holder) {
String fieldName = element.getSimpleName().toString();
JBlock body = holder.getOnCreateOptionsMenuMethodBody();
JVar menuParam = holder.getOnCreateOptionsMenuMenuParam();

body.assign(JExpr._this().ref(fieldName), menuParam);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,16 @@ public void extendsMenuItem(Element element, IsValid valid) {
}
}

public void extendsMenu(Element element, IsValid valid) {
Element enclosingElement = element.getEnclosingElement();
String enclosingQualifiedName = enclosingElement.asType().toString();
TypeElement enclosingTypeElement = annotationHelper.typeElementFromQualifiedName(enclosingQualifiedName);

if (enclosingTypeElement != null) {
extendsType(element, CanonicalNameConstants.MENU, valid);
}
}

public void extendsOrmLiteDao(Element element, IsValid valid, OrmLiteHelper ormLiteHelper) {
if (!valid.isValid()) {
// we now that the element is invalid. early exit as the reason
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (C) 2010-2015 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.menu;

import android.app.Activity;
import android.view.Menu;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.InjectMenu;

@EActivity
public class InjectMenuActivity extends Activity {

@InjectMenu
Menu menu;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright (C) 2010-2015 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.menu;


import org.androidannotations.AndroidAnnotationProcessor;
import org.androidannotations.utils.AAProcessorTestHelper;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class InjectMenuTest extends AAProcessorTestHelper {

@Before
public void setUp() {
addManifestProcessorParameter(InjectMenuActivity.class);
addProcessor(AndroidAnnotationProcessor.class);
}

@Test
public void testCompilation() throws IOException {
CompileResult result = compileFiles(InjectMenuActivity.class);
assertCompilationSuccessful(result);
assertGeneratedClassMatches(toGeneratedFile(InjectMenuActivity.class), " this.menu = menu;");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

<application>
<activity android:name="org.androidannotations.menu.OptionsMenuItemActivity_" />
<activity android:name="org.androidannotations.menu.InjectMenuActivity_" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<activity android:name=".receiver.ActivityWithReceiver_" />
<activity android:name="org.androidannotations.test15.menu.OptionsMenuActivity_" />
<activity android:name="org.androidannotations.test15.menu.OptionsMenuSubActivity_" />
<activity android:name="org.androidannotations.test15.menu.InjectMenuActivity_" />
<activity android:name="org.androidannotations.test15.NoTitleFullscreenActivity_" />
<activity android:name="org.androidannotations.test15.WindowFeatureActivity_" />
<activity android:name="org.androidannotations.test15.ExtendingActivity_" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (C) 2010-2015 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.test15.menu;

import android.app.Activity;
import android.view.Menu;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.InjectMenu;

@EActivity
public class InjectMenuActivity extends Activity {

@InjectMenu
Menu menu;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (C) 2010-2015 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.test15.menu;

import android.view.Menu;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;

import static org.fest.assertions.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

@RunWith(RobolectricTestRunner.class)
public class InjectMenuActivityTest {

private InjectMenuActivity_ injectMenuActivity;

@Before
public void setUp() {
injectMenuActivity = Robolectric.buildActivity(InjectMenuActivity_.class).create().get();
}

@Test
public void menuIsNull() {
assertThat(injectMenuActivity.menu).isNull();
}

@Test
public void testMenuInjectedFromOnCreateOptionsMenu() {
Menu menu = mock(Menu.class);
injectMenuActivity.onCreateOptionsMenu(menu);
assertThat(injectMenuActivity.menu).isSameAs(menu);
}
}