diff --git a/AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/annotations/FragmentById.java b/AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/annotations/FragmentById.java index b493ae691e..940739216b 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/annotations/FragmentById.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/annotations/FragmentById.java @@ -42,7 +42,7 @@ * android:layout_height="match_parent" > * * <fragment - * android:id="@+id/myFragment" + * android:id="@+id/myFragment" * android:name="mypackage.MyFragment_" * android:layout_width="match_parent" * android:layout_height="match_parent" /> @@ -55,15 +55,52 @@ * // all injected fragment will be the same * * @FragmentById - * public MyFragment myFragment; + * public MyFragment myFragment; * - * @FragmentById(R.id.myFragment) + * @FragmentById(R.id.myFragment) * public MyFragment myFragment2; * } * * * * + *

+ * To use the getChildFragmentManager() to inject the + * Fragment, set the {@link #childFragment()} annotation parameter + * to true. You can only do this if the annotated field is in a + * class which extends android.app.Fragment or + * android.support.v4.app.Fragment and the + * getChildFragmentManager() method is available. + *

+ * + *
+ * + * Example : + * + *
+ * <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ *     android:layout_width="match_parent"
+ *     android:layout_height="match_parent" >
+ * 
+ *     <fragment
+ *         android:id="@+id/myChildFragment"
+ *         android:name="mypackage.MyChildFragment_"
+ *         android:layout_width="match_parent"
+ *         android:layout_height="match_parent" />
+ * </LinearLayout>
+ * 
+ * 
+ * @EFragment(R.layout.parentfragment)
+ * public class MyParentFragment extends Fragment {
+ * 
+ * 	@FragmentById(childFragment = true)
+ * 	MyChildFragment myFragment;
+ * 
+ * }
+ * 
+ * + *
+ * * @see EFragment * @see FragmentArg * @see FragmentByTag @@ -85,4 +122,14 @@ * @return the resource name of the Fragment */ String resName() default ""; + + /** + * Whether to use getChildFragmentManager() or + * getFragmentManager() to obtain the Fragment. Only can be + * true when injecting into a Fragment. + * + * @return true to use getChildFragmentManager(), + * false to use getFragmentManager() + */ + boolean childFragment() default false; } diff --git a/AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/annotations/FragmentByTag.java b/AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/annotations/FragmentByTag.java index fe5696fddf..15a01b66e4 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/annotations/FragmentByTag.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/annotations/FragmentByTag.java @@ -43,7 +43,7 @@ * * <fragment * android:id="@+id/myFragment" - * android:tag="myFragmentTag" + * android:tag="myFragmentTag" * android:name="mypackage.MyFragment_" * android:layout_width="match_parent" * android:layout_height="match_parent" /> @@ -56,14 +56,52 @@ * // all injected fragment will be the same * * @FragmentByTag - * public MyFragment myFragmentTag; + * public MyFragment myFragmentTag; * - * @FragmentByTag("myFragmentTag") + * @FragmentByTag("myFragmentTag") * public MyFragment myFragmentTag2; * } * * * + * + *

+ * To use the getChildFragmentManager() to inject the + * Fragment, set the {@link #childFragment()} annotation parameter + * to true. You can only do this if the annotated field is in a + * class which extends android.app.Fragment or + * android.support.v4.app.Fragment and the + * getChildFragmentManager() method is available. + *

+ * + *
+ * + * Example : + * + *
+ * <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ *     android:layout_width="match_parent"
+ *     android:layout_height="match_parent" >
+ * 
+ *     <fragment
+ *         android:id="@+id/myChildFragment"
+ *         android:tag="myChildFragment"
+ *         android:name="mypackage.MyChildFragment_"
+ *         android:layout_width="match_parent"
+ *         android:layout_height="match_parent" />
+ * </LinearLayout>
+ * 
+ * 
+ * @EFragment(R.layout.parentfragment)
+ * public class MyParentFragment extends Fragment {
+ * 
+ * 	@FragmentByTag(childFragment = true)
+ * 	MyChildFragment myFragment;
+ * 
+ * }
+ * 
+ * + *
* * @see EFragment * @see FragmentArg @@ -79,4 +117,14 @@ * @return the tag of the Fragment */ String value() default ""; + + /** + * Whether to use getChildFragmentManager() or + * getFragmentManager() to obtain the Fragment. Only can be + * true when injecting into a Fragment. + * + * @return true to use getChildFragmentManager(), + * false to use getFragmentManager() + */ + boolean childFragment() default false; } diff --git a/AndroidAnnotations/androidannotations-core/androidannotations-testutils/src/main/java/org/androidannotations/testutils/ProcessorTestHelper.java b/AndroidAnnotations/androidannotations-core/androidannotations-testutils/src/main/java/org/androidannotations/testutils/ProcessorTestHelper.java index 01a90468bb..99025df137 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations-testutils/src/main/java/org/androidannotations/testutils/ProcessorTestHelper.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations-testutils/src/main/java/org/androidannotations/testutils/ProcessorTestHelper.java @@ -31,6 +31,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.regex.Pattern; import javax.annotation.processing.Processor; import javax.tools.Diagnostic; @@ -166,18 +167,31 @@ public static void assertCompilationErrorOn(File expectedErrorClassFile, String assertCompilationDiagnostingOn(Kind.ERROR, expectedErrorClassFile, expectedContentInError, result); } + public static void assertCompilationErrorOn(String expectedClassName, String expectedContentInError, CompileResult result) throws IOException { + assertCompilationDiagnostingOn(Kind.ERROR, new File(expectedClassName + ".java"), expectedContentInError, result); + } + public static void assertCompilationWarningOn(File expectedErrorClassFile, String expectedContentInError, CompileResult result) throws IOException { assertCompilationDiagnostingOn(Kind.WARNING, expectedErrorClassFile, expectedContentInError, result); } private static void assertCompilationDiagnostingOn(Kind expectedDiagnosticKind, File expectedErrorClassFile, String expectedContentInError, CompileResult result) throws IOException { - String expectedErrorPath = expectedErrorClassFile.toURI().toString(); + String expectedErrorPath; + boolean fileNameOnly = expectedErrorClassFile.getPath().split(Pattern.quote(File.separator)).length == 1; + + if (fileNameOnly) { + // this is just the filename + expectedErrorPath = expectedErrorClassFile.getPath(); + } else { + expectedErrorPath = expectedErrorClassFile.toURI().toString(); + } + for (Diagnostic diagnostic : result.diagnostics) { if (diagnostic.getKind() == expectedDiagnosticKind) { JavaFileObject source = diagnostic.getSource(); if (source != null) { - if (expectedErrorPath.endsWith(source.toUri().toString())) { + if (expectedErrorPath.endsWith(source.toUri().toString()) || fileNameOnly && source.toUri().toString().endsWith(expectedErrorPath)) { CharSequence sourceContent = source.getCharContent(true); if (diagnostic.getPosition() != Diagnostic.NOPOS) { diff --git a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/AbstractFragmentByHandler.java b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/AbstractFragmentByHandler.java new file mode 100644 index 0000000000..7433856144 --- /dev/null +++ b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/AbstractFragmentByHandler.java @@ -0,0 +1,87 @@ +/** + * 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.internal.core.handler; + +import static com.sun.codemodel.JExpr.cast; +import static com.sun.codemodel.JExpr.invoke; +import static com.sun.codemodel.JExpr.ref; + +import javax.lang.model.element.Element; +import javax.lang.model.element.TypeElement; +import javax.lang.model.type.TypeMirror; + +import org.androidannotations.AndroidAnnotationsEnvironment; +import org.androidannotations.ElementValidation; +import org.androidannotations.helper.CanonicalNameConstants; +import org.androidannotations.holder.EComponentWithViewSupportHolder; +import org.androidannotations.holder.EFragmentHolder; + +import com.sun.codemodel.JBlock; +import com.sun.codemodel.JExpression; +import com.sun.codemodel.JMethod; + +public abstract class AbstractFragmentByHandler extends CoreBaseAnnotationHandler { + + protected String findFragmentMethodName; + + public AbstractFragmentByHandler(Class targetClass, AndroidAnnotationsEnvironment environment, String findFragmentMethodName) { + super(targetClass, environment); + this.findFragmentMethodName = findFragmentMethodName; + } + + @Override + protected void validate(Element element, ElementValidation validation) { + validatorHelper.enclosingElementHasEnhancedViewSupportAnnotation(element, validation); + + validatorHelper.extendsFragment(element, validation); + + validatorHelper.isNotPrivate(element, validation); + + coreValidatorHelper.childFragmentUsedOnlyIfEnclosingClassIsFragment(element, validation); + + if (validation.isValid()) { + coreValidatorHelper.getChildFragmentManagerMethodIsAvailable(element, validation); + } + } + + @Override + public final void process(Element element, EComponentWithViewSupportHolder holder) throws Exception { + TypeMirror elementType = element.asType(); + String typeQualifiedName = elementType.toString(); + TypeElement nativeFragmentElement = annotationHelper.typeElementFromQualifiedName(CanonicalNameConstants.FRAGMENT); + boolean isNativeFragment = nativeFragmentElement != null && annotationHelper.isSubtype(elementType, nativeFragmentElement.asType()); + + String fieldName = element.getSimpleName().toString(); + JBlock methodBody = holder.getOnViewChangedBody(); + + if (holder instanceof EFragmentHolder) { + boolean childFragment = annotationHelper.extractAnnotationParameter(element, "childFragment"); + + String fragmentManagerGetter = childFragment ? "getChildFragmentManager" : "getFragmentManager"; + + methodBody.assign(ref(fieldName), cast(getJClass(typeQualifiedName), invoke(fragmentManagerGetter).invoke(findFragmentMethodName).arg(getFragmentId(element, fieldName)))); + } else { + JMethod findFragmentMethod = getFindFragmentMethod(isNativeFragment, holder); + + methodBody.assign(ref(fieldName), cast(getJClass(typeQualifiedName), invoke(findFragmentMethod).arg(getFragmentId(element, fieldName)))); + } + } + + protected abstract JMethod getFindFragmentMethod(boolean isNativeFragment, EComponentWithViewSupportHolder holder); + + protected abstract JExpression getFragmentId(Element element, String fieldName); + +} diff --git a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/FragmentByIdHandler.java b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/FragmentByIdHandler.java index 8bf79bd9b6..21797e231d 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/FragmentByIdHandler.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/FragmentByIdHandler.java @@ -15,66 +15,38 @@ */ package org.androidannotations.internal.core.handler; -import static com.sun.codemodel.JExpr.cast; -import static com.sun.codemodel.JExpr.invoke; -import static com.sun.codemodel.JExpr.ref; - import javax.lang.model.element.Element; -import javax.lang.model.element.TypeElement; -import javax.lang.model.type.TypeMirror; import org.androidannotations.AndroidAnnotationsEnvironment; import org.androidannotations.ElementValidation; import org.androidannotations.annotations.FragmentById; -import org.androidannotations.handler.BaseAnnotationHandler; -import org.androidannotations.helper.CanonicalNameConstants; import org.androidannotations.helper.IdValidatorHelper; import org.androidannotations.holder.EComponentWithViewSupportHolder; import org.androidannotations.rclass.IRClass; -import com.sun.codemodel.JBlock; -import com.sun.codemodel.JFieldRef; +import com.sun.codemodel.JExpression; import com.sun.codemodel.JMethod; -public class FragmentByIdHandler extends BaseAnnotationHandler { +public class FragmentByIdHandler extends AbstractFragmentByHandler { public FragmentByIdHandler(AndroidAnnotationsEnvironment environment) { - super(FragmentById.class, environment); + super(FragmentById.class, environment, "findFragmentById"); } @Override public void validate(Element element, ElementValidation validation) { - validatorHelper.enclosingElementHasEnhancedViewSupportAnnotation(element, validation); - - validatorHelper.extendsFragment(element, validation); + super.validate(element, validation); validatorHelper.resIdsExist(element, IRClass.Res.ID, IdValidatorHelper.FallbackStrategy.USE_ELEMENT_NAME, validation); - - validatorHelper.isNotPrivate(element, validation); } @Override - public void process(Element element, EComponentWithViewSupportHolder holder) { - - TypeMirror elementType = element.asType(); - String typeQualifiedName = elementType.toString(); - TypeElement nativeFragmentElement = annotationHelper.typeElementFromQualifiedName(CanonicalNameConstants.FRAGMENT); - boolean isNativeFragment = nativeFragmentElement != null && annotationHelper.isSubtype(elementType, nativeFragmentElement.asType()); - - JMethod findFragmentById; - if (isNativeFragment) { - findFragmentById = holder.getFindNativeFragmentById(); - } else { - findFragmentById = holder.getFindSupportFragmentById(); - } - - String fieldName = element.getSimpleName().toString(); - - JFieldRef idRef = annotationHelper.extractOneAnnotationFieldRef(element, IRClass.Res.ID, true); - - JBlock methodBody = holder.getOnViewChangedBody(); - - methodBody.assign(ref(fieldName), cast(getJClass(typeQualifiedName), invoke(findFragmentById).arg(idRef))); + protected JMethod getFindFragmentMethod(boolean isNativeFragment, EComponentWithViewSupportHolder holder) { + return isNativeFragment ? holder.getFindNativeFragmentById() : holder.getFindSupportFragmentById(); + } + @Override + protected JExpression getFragmentId(Element element, String fieldName) { + return annotationHelper.extractOneAnnotationFieldRef(element, IRClass.Res.ID, true); } } diff --git a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/FragmentByTagHandler.java b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/FragmentByTagHandler.java index bad57dfb02..f6a32eb999 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/FragmentByTagHandler.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/handler/FragmentByTagHandler.java @@ -15,62 +15,35 @@ */ package org.androidannotations.internal.core.handler; -import static com.sun.codemodel.JExpr.cast; -import static com.sun.codemodel.JExpr.invoke; import static com.sun.codemodel.JExpr.lit; -import static com.sun.codemodel.JExpr.ref; import javax.lang.model.element.Element; -import javax.lang.model.type.TypeMirror; import org.androidannotations.AndroidAnnotationsEnvironment; -import org.androidannotations.ElementValidation; import org.androidannotations.annotations.FragmentByTag; -import org.androidannotations.handler.BaseAnnotationHandler; -import org.androidannotations.helper.CanonicalNameConstants; import org.androidannotations.holder.EComponentWithViewSupportHolder; -import com.sun.codemodel.JBlock; +import com.sun.codemodel.JExpression; import com.sun.codemodel.JMethod; -public class FragmentByTagHandler extends BaseAnnotationHandler { +public class FragmentByTagHandler extends AbstractFragmentByHandler { public FragmentByTagHandler(AndroidAnnotationsEnvironment environment) { - super(FragmentByTag.class, environment); + super(FragmentByTag.class, environment, "findFragmentByTag"); } @Override - public void validate(Element element, ElementValidation validation) { - validatorHelper.enclosingElementHasEnhancedViewSupportAnnotation(element, validation); - - validatorHelper.extendsFragment(element, validation); - - validatorHelper.isNotPrivate(element, validation); + protected JMethod getFindFragmentMethod(boolean isNativeFragment, EComponentWithViewSupportHolder holder) { + return isNativeFragment ? holder.getFindNativeFragmentByTag() : holder.getFindSupportFragmentByTag(); } @Override - public void process(Element element, EComponentWithViewSupportHolder holder) { - - TypeMirror elementType = element.asType(); - String typeQualifiedName = elementType.toString(); - TypeMirror nativeFragmentType = annotationHelper.typeElementFromQualifiedName(CanonicalNameConstants.FRAGMENT).asType(); - boolean isNativeFragment = annotationHelper.isSubtype(elementType, nativeFragmentType); - - JMethod findFragmentByTag; - if (isNativeFragment) { - findFragmentByTag = holder.getFindNativeFragmentByTag(); - } else { - findFragmentByTag = holder.getFindSupportFragmentByTag(); - } - - String fieldName = element.getSimpleName().toString(); + protected JExpression getFragmentId(Element element, String fieldName) { FragmentByTag annotation = element.getAnnotation(FragmentByTag.class); String tagValue = annotation.value(); if (tagValue.equals("")) { tagValue = fieldName; } - - JBlock methodBody = holder.getOnViewChangedBody(); - methodBody.assign(ref(fieldName), cast(getJClass(typeQualifiedName), invoke(findFragmentByTag).arg(lit(tagValue)))); + return lit(tagValue); } } diff --git a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/helper/CoreValidatorHelper.java b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/helper/CoreValidatorHelper.java index 2173c72289..db9b61dc43 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/helper/CoreValidatorHelper.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/helper/CoreValidatorHelper.java @@ -35,6 +35,7 @@ import javax.lang.model.type.ErrorType; import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; +import javax.lang.model.util.ElementFilter; import javax.lang.model.util.Elements; import org.androidannotations.ElementValidation; @@ -407,4 +408,59 @@ public void extendsKeyEventCallback(Element element, ElementValidation validatio public void enclosingElementExtendsKeyEventCallback(Element element, ElementValidation validation) { extendsKeyEventCallback(element.getEnclosingElement(), validation); } + + public void childFragmentUsedOnlyIfEnclosingClassIsFragment(Element element, ElementValidation validation) { + boolean childFragment = annotationHelper.extractAnnotationParameter(element, "childFragment"); + + if (childFragment) { + TypeElement fragment = annotationHelper.getElementUtils().getTypeElement(CanonicalNameConstants.FRAGMENT); + TypeElement supportFragment = annotationHelper.getElementUtils().getTypeElement(CanonicalNameConstants.SUPPORT_V4_FRAGMENT); + + boolean enclosingElementIsFragment = false; + + TypeElement enclosingElement = (TypeElement) element.getEnclosingElement(); + + if (fragment != null && annotationHelper.isSubtype(enclosingElement, fragment)) { + enclosingElementIsFragment = true; + } else if (supportFragment != null && annotationHelper.isSubtype(enclosingElement, supportFragment)) { + enclosingElementIsFragment = true; + } + + if (!enclosingElementIsFragment) { + validation.addError(element, "The 'childFragmentManager' parameter only can be used if the class containing the annotated field is either subclass of " + + CanonicalNameConstants.FRAGMENT + " or " + CanonicalNameConstants.SUPPORT_V4_FRAGMENT); + } + } + } + + public void getChildFragmentManagerMethodIsAvailable(Element element, ElementValidation validation) { + boolean childFragment = annotationHelper.extractAnnotationParameter(element, "childFragment"); + + if (childFragment) { + TypeElement enclosingElement = (TypeElement) element.getEnclosingElement(); + + TypeElement fragment = annotationHelper.getElementUtils().getTypeElement(CanonicalNameConstants.FRAGMENT); + TypeElement supportFragment = annotationHelper.getElementUtils().getTypeElement(CanonicalNameConstants.SUPPORT_V4_FRAGMENT); + + if (supportFragment != null && annotationHelper.isSubtype(enclosingElement, supportFragment)) { + if (!methodIsAvailableIn(supportFragment, "getChildFragmentManager")) { + validation.addError(element, "The 'childFragmentManager' parameter only can be used if the getChildFragmentManager() method is available in " + + CanonicalNameConstants.SUPPORT_V4_FRAGMENT + ", update your support library version."); + } + } else if (fragment != null && annotationHelper.isSubtype(enclosingElement, fragment) && environment().getAndroidManifest().getMinSdkVersion() < 17) { + validation.addError(element, "The 'childFragmentManager' parameter only can be used if the getChildFragmentManager() method is available in " + + CanonicalNameConstants.FRAGMENT + " (from API 17). Increment 'minSdkVersion' or use " + CanonicalNameConstants.SUPPORT_V4_FRAGMENT + "."); + } + + } + } + + private boolean methodIsAvailableIn(TypeElement element, String methodName) { + for (Element method : ElementFilter.methodsIn(element.getEnclosedElements())) { + if (method.getSimpleName().contentEquals(methodName)) { + return true; + } + } + return false; + } } diff --git a/AndroidAnnotations/androidannotations-core/androidannotations/src/test/java/org/androidannotations/generation/ActivityWithChildFragment.java b/AndroidAnnotations/androidannotations-core/androidannotations/src/test/java/org/androidannotations/generation/ActivityWithChildFragment.java new file mode 100644 index 0000000000..b5a48c63be --- /dev/null +++ b/AndroidAnnotations/androidannotations-core/androidannotations/src/test/java/org/androidannotations/generation/ActivityWithChildFragment.java @@ -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.generation; + +import org.androidannotations.annotations.EActivity; +import org.androidannotations.annotations.FragmentByTag; + +import android.app.Activity; +import android.app.Fragment; + +@EActivity +public class ActivityWithChildFragment extends Activity { + + @FragmentByTag(childFragment = true) + Fragment child; +} diff --git a/AndroidAnnotations/androidannotations-core/androidannotations/src/test/java/org/androidannotations/generation/FragmentByChildFragmentManagerTest.java b/AndroidAnnotations/androidannotations-core/androidannotations/src/test/java/org/androidannotations/generation/FragmentByChildFragmentManagerTest.java new file mode 100644 index 0000000000..e41df12c1a --- /dev/null +++ b/AndroidAnnotations/androidannotations-core/androidannotations/src/test/java/org/androidannotations/generation/FragmentByChildFragmentManagerTest.java @@ -0,0 +1,81 @@ +/** + * 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.generation; + +import java.io.IOException; + +import org.androidannotations.internal.AndroidAnnotationProcessor; +import org.androidannotations.testutils.AAProcessorTestHelper; +import org.junit.Before; +import org.junit.Test; + +public class FragmentByChildFragmentManagerTest extends AAProcessorTestHelper { + + @Before + public void setUp() { + addProcessor(AndroidAnnotationProcessor.class); + } + + @Test + public void nativeFragmentByIdChildFragmentDoesNotCompileWithWithFroyo() throws IOException { + addManifestProcessorParameter(FragmentByChildFragmentManagerTest.class, "AndroidManifestMinFroyo.xml"); + + CompileResult result = compileFiles(toPath(FragmentByChildFragmentManagerTest.class, "Fragment.java"), NativeFragmentWithChild.class); + + assertCompilationErrorOn(NativeFragmentWithChild.class, "@FragmentByTag(childFragment = true)", result); + assertCompilationErrorCount(1, result); + } + + @Test + public void nativeFragmentByIdChildFragmentCompilesWithWithJB() { + addManifestProcessorParameter(FragmentByChildFragmentManagerTest.class, "AndroidManifestMinJB.xml"); + + CompileResult result = compileFiles(toPath(FragmentByChildFragmentManagerTest.class, "Fragment.java"), NativeFragmentWithChild.class); + + assertCompilationSuccessful(result); + } + + @Test + public void nativeFragmentByIdChildFragmentDoesNotCompileInActivity() throws IOException { + addManifestProcessorParameter(FragmentByChildFragmentManagerTest.class, "AndroidManifestMinJB.xml"); + + CompileResult result = compileFiles(toPath(FragmentByChildFragmentManagerTest.class, "Fragment.java"), ActivityWithChildFragment.class); + + assertCompilationErrorOn(ActivityWithChildFragment.class, "@FragmentByTag(childFragment = true)", result); + assertCompilationErrorCount(1, result); + } + + @Test + public void supportFragmentByIdChildFragmentCompilesWhenFragmentHasGetChildFragmentManager() { + addManifestProcessorParameter(FragmentByChildFragmentManagerTest.class, "AndroidManifestMinJB.xml"); + + CompileResult result = compileFiles(toPath(FragmentByChildFragmentManagerTest.class, "support/Fragment.java"), // + toPath(FragmentByChildFragmentManagerTest.class, "SupportFragmentWithChild.java")); + + assertCompilationSuccessful(result); + } + + @Test + public void supportFragmentByIdChildFragmentDoesNotCompileWhenFragmentDoesNotHaveGetChildFragmentManager() throws ClassNotFoundException, IOException { + addManifestProcessorParameter(FragmentByChildFragmentManagerTest.class, "AndroidManifestMinJB.xml"); + + CompileResult result = compileFiles(toPath(FragmentByChildFragmentManagerTest.class, "support/old/Fragment.java"), // + toPath(FragmentByChildFragmentManagerTest.class, "SupportFragmentWithChild.java")); + + assertCompilationErrorOn("SupportFragmentWithChild", "@FragmentByTag", result); + assertCompilationErrorCount(1, result); + } +} diff --git a/AndroidAnnotations/androidannotations-core/androidannotations/src/test/java/org/androidannotations/generation/NativeFragmentWithChild.java b/AndroidAnnotations/androidannotations-core/androidannotations/src/test/java/org/androidannotations/generation/NativeFragmentWithChild.java new file mode 100644 index 0000000000..dd5f2eea8a --- /dev/null +++ b/AndroidAnnotations/androidannotations-core/androidannotations/src/test/java/org/androidannotations/generation/NativeFragmentWithChild.java @@ -0,0 +1,28 @@ +/** + * 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.generation; + +import org.androidannotations.annotations.EFragment; +import org.androidannotations.annotations.FragmentByTag; + +import android.app.Fragment; + +@EFragment +public class NativeFragmentWithChild extends Fragment { + + @FragmentByTag(childFragment = true) + Fragment child; +} diff --git a/AndroidAnnotations/androidannotations-core/androidannotations/src/test/resources/org/androidannotations/generation/Fragment.java b/AndroidAnnotations/androidannotations-core/androidannotations/src/test/resources/org/androidannotations/generation/Fragment.java index 0752766000..918cc44cd6 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations/src/test/resources/org/androidannotations/generation/Fragment.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations/src/test/resources/org/androidannotations/generation/Fragment.java @@ -23,6 +23,26 @@ */ public class Fragment { + public void onCreate(android.os.Bundle savedInstanceState) { + + } + + public void onViewCreated(android.view.View view, android.os.Bundle savedInstanceState) { + + } + + public android.view.View onCreateView(android.view.LayoutInflater inflater, android.view.ViewGroup container, android.os.Bundle savedInstanceState) { + return null; + } + + public void onDestroyView() { + + } + + public void setArguments(android.os.Bundle args) { + + } + public Activity getActivity() { return null; } @@ -30,5 +50,20 @@ public Activity getActivity() { public void startActivityForResult(Intent intent, int flag) { } + + public FragmentManager getFragmentManager() { + return null; + } + + public FragmentManager getChildFragmentManager() { + return null; + } + + public abstract class FragmentManager { + + public abstract Fragment findFragmentById(int id); + + public abstract Fragment findFragmentByTag(String tag); + } } diff --git a/AndroidAnnotations/androidannotations-core/androidannotations/src/test/resources/org/androidannotations/generation/SupportFragmentWithChild.java b/AndroidAnnotations/androidannotations-core/androidannotations/src/test/resources/org/androidannotations/generation/SupportFragmentWithChild.java new file mode 100644 index 0000000000..d2db6495da --- /dev/null +++ b/AndroidAnnotations/androidannotations-core/androidannotations/src/test/resources/org/androidannotations/generation/SupportFragmentWithChild.java @@ -0,0 +1,28 @@ +/** + * 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.generation; + +import org.androidannotations.annotations.EFragment; +import org.androidannotations.annotations.FragmentByTag; + +import android.support.v4.app.Fragment; + +@EFragment +public class SupportFragmentWithChild extends Fragment { + + @FragmentByTag(childFragment = true) + Fragment child; +} diff --git a/AndroidAnnotations/androidannotations-core/androidannotations/src/test/resources/org/androidannotations/generation/support/Fragment.java b/AndroidAnnotations/androidannotations-core/androidannotations/src/test/resources/org/androidannotations/generation/support/Fragment.java index 135d03e15b..d95220c463 100644 --- a/AndroidAnnotations/androidannotations-core/androidannotations/src/test/resources/org/androidannotations/generation/support/Fragment.java +++ b/AndroidAnnotations/androidannotations-core/androidannotations/src/test/resources/org/androidannotations/generation/support/Fragment.java @@ -23,6 +23,26 @@ * classpath only on some unit tests methods */ public class Fragment { + + public void onCreate(android.os.Bundle savedInstanceState) { + + } + + public void onViewCreated(android.view.View view, android.os.Bundle savedInstanceState) { + + } + + public android.view.View onCreateView(android.view.LayoutInflater inflater, android.view.ViewGroup container, android.os.Bundle savedInstanceState) { + return null; + } + + public void onDestroyView() { + + } + + public void setArguments(android.os.Bundle args) { + + } public Activity getActivity() { return null; @@ -31,5 +51,20 @@ public Activity getActivity() { public void startActivityForResult(Intent intent, int flag) { } + + public FragmentManager getFragmentManager() { + return null; + } + + public FragmentManager getChildFragmentManager() { + return null; + } + + public abstract class FragmentManager { + + public abstract Fragment findFragmentById(int id); + + public abstract Fragment findFragmentByTag(String tag); + } } diff --git a/AndroidAnnotations/androidannotations-core/androidannotations/src/test/resources/org/androidannotations/generation/support/old/Fragment.java b/AndroidAnnotations/androidannotations-core/androidannotations/src/test/resources/org/androidannotations/generation/support/old/Fragment.java new file mode 100644 index 0000000000..74069718c6 --- /dev/null +++ b/AndroidAnnotations/androidannotations-core/androidannotations/src/test/resources/org/androidannotations/generation/support/old/Fragment.java @@ -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 android.support.v4.app; + +import android.app.Activity; +import android.content.Intent; + +public class Fragment { + + public void onCreate(android.os.Bundle savedInstanceState) { + + } + + public void onViewCreated(android.view.View view, android.os.Bundle savedInstanceState) { + + } + + public android.view.View onCreateView(android.view.LayoutInflater inflater, android.view.ViewGroup container, android.os.Bundle savedInstanceState) { + return null; + } + + public void onDestroyView() { + + } + + public void setArguments(android.os.Bundle args) { + + } + + public Activity getActivity() { + return null; + } + + public void startActivityForResult(Intent intent, int flag) { + + } + +}