Skip to content
This repository has been archived by the owner on Feb 26, 2023. It is now read-only.

add @AfterExtras #928

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,39 @@
/**
* Copyright (C) 2010-2014 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;

/**
* Methods annotated with @{@link AfterExtras} will be called after the Extras
* from an Intent have been injected.
*
* Any code depending on injected extras should be done in an
* {@link AfterExtras} annotated method.
*
* The method must have zero parameters.
*
* There may be several methods annotated with @{@link AfterExtras} in the same
* class.
*
*/
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
public @interface AfterExtras {
}
@@ -0,0 +1,53 @@
/**
* 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.handler;

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

import org.androidannotations.annotations.AfterExtras;
import org.androidannotations.holder.EActivityHolder;
import org.androidannotations.model.AnnotationElements;
import org.androidannotations.process.IsValid;

public class AfterExtrasHandler extends BaseAnnotationHandler<EActivityHolder> {

public AfterExtrasHandler(ProcessingEnvironment processingEnvironment) {
super(AfterExtras.class, processingEnvironment);
}

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

ExecutableElement executableElement = (ExecutableElement) element;

validatorHelper.returnTypeIsVoid(executableElement, valid);

validatorHelper.isNotPrivate(element, valid);

validatorHelper.doesntThrowException(executableElement, valid);

validatorHelper.param.zeroParameter(executableElement, valid);
}

@Override
public void process(Element element, EActivityHolder holder) {
String methodName = element.getSimpleName().toString();
holder.getInjectExtrasBlock().invoke(methodName);
}
}
Expand Up @@ -109,6 +109,7 @@ public AnnotationHandlers(ProcessingEnvironment processingEnvironment) {
add(new IgnoredWhenDetachedHandler(processingEnvironment));
/* After injection methods must be after injections */
add(new AfterInjectHandler(processingEnvironment));
add(new AfterExtrasHandler(processingEnvironment));
add(new AfterViewsHandler(processingEnvironment));

if (optionsHelper.shouldLogTrace()) {
Expand Down
@@ -0,0 +1,63 @@
/**
* Copyright (C) 2010-2014 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.afterextras;

import static org.fest.assertions.Assertions.assertThat;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.util.ActivityController;

import android.content.Intent;

@RunWith(RobolectricTestRunner.class)
public class AfterExtrasActivityTest {

ActivityController<AfterExtrasActivity_> activityController;
AfterExtrasActivity_ activity;
Intent intent;

@Before
public void setup() {
activityController = ActivityController.of(AfterExtrasActivity_.class);
activity = activityController.get();

intent = AfterExtrasActivity_.intent(activity).extraDataSet(true).get();
}

@Test
public void afterExtra_called_activity_after_setIntent() {
activityController.create();
assertThat(activity.extraDataSet).isFalse();
assertThat(activity.afterExtrasCalled).isFalse();

activity.setIntent(intent);
assertThat(activity.extraDataSet).isTrue();
assertThat(activity.afterExtrasCalled).isTrue();
}

@Test
public void afterExtra_called_activity_in_onCreate() throws Exception {
assertThat(activity.extraDataSet).isFalse();
assertThat(activity.afterExtrasCalled).isFalse();

activityController.withIntent(intent).create();
assertThat(activity.extraDataSet).isTrue();
assertThat(activity.afterExtrasCalled).isTrue();
}
}
@@ -0,0 +1,40 @@
/**
* Copyright (C) 2010-2014 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.afterextras;

import org.androidannotations.annotations.AfterExtras;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.test15.R;

import android.app.Activity;

@EActivity(R.layout.main)
public class AfterExtrasActivity extends Activity {

public static final String EXTRA_DATA_KEY = "EXTRA_DATA";

@Extra(EXTRA_DATA_KEY)
public boolean extraDataSet = false;

public boolean afterExtrasCalled = false;

@AfterExtras
void afterExtras() {
afterExtrasCalled = true;
}

}