X Tutup
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
Expand Up @@ -42,7 +42,7 @@
* android:layout_height="match_parent" >
*
* <fragment
* android:id="@+id/myFragment"
* android:id="@+id/<b>myFragment</b>"
* android:name="mypackage.MyFragment_"
* android:layout_width="match_parent"
* android:layout_height="match_parent" /&gt;
Expand All @@ -55,15 +55,52 @@
* // all injected fragment will be the same
*
* &#064;FragmentById
* public MyFragment myFragment;
* public MyFragment <b>myFragment</b>;
*
* &#064;FragmentById(R.id.myFragment)
* &#064;FragmentById(R.id.<b>myFragment</b>)
* public MyFragment myFragment2;
* }
* </pre>
*
* </blockquote>
*
* <p>
* To use the <code>getChildFragmentManager()</code> to inject the
* <code>Fragment</code>, set the {@link #childFragment()} annotation parameter
* to <code>true</code>. You can only do this if the annotated field is in a
* class which extends <code>android.app.Fragment</code> or
* <code>android.support.v4.app.Fragment</code> and the
* <code>getChildFragmentManager()</code> method is available.
* </p>
*
* <blockquote>
*
* Example :
*
* <pre>
* &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
* android:layout_width="match_parent"
* android:layout_height="match_parent" &gt;
*
* &lt;fragment
* android:id="@+id/myChildFragment"
* android:name="mypackage.MyChildFragment_"
* android:layout_width="match_parent"
* android:layout_height="match_parent" /&gt;
* &lt;/LinearLayout&gt;
*
*
* &#064;EFragment(R.layout.parentfragment)
* public class MyParentFragment extends Fragment {
*
* &#064;FragmentById(<b>childFragment = true</b>)
* MyChildFragment myFragment;
*
* }
* </pre>
*
* </blockquote>
*
* @see EFragment
* @see FragmentArg
* @see FragmentByTag
Expand All @@ -85,4 +122,14 @@
* @return the resource name of the Fragment
*/
String resName() default "";

/**
* Whether to use <code>getChildFragmentManager()</code> or
* <code>getFragmentManager()</code> to obtain the Fragment. Only can be
* <code>true</code> when injecting into a <code>Fragment</code>.
*
* @return <code>true</code> to use <code>getChildFragmentManager()</code>,
* <code>false</code> to use <code>getFragmentManager()</code>
*/
boolean childFragment() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
*
* &lt;fragment
* android:id="@+id/myFragment"
* android:tag="myFragmentTag"
* android:tag="<b>myFragmentTag</b>"
* android:name="mypackage.MyFragment_"
* android:layout_width="match_parent"
* android:layout_height="match_parent" /&gt;
Expand All @@ -56,14 +56,52 @@
* // all injected fragment will be the same
*
* &#064;FragmentByTag
* public MyFragment myFragmentTag;
* public MyFragment <b>myFragmentTag</b>;
*
* &#064;FragmentByTag("myFragmentTag")
* &#064;FragmentByTag(<b>"myFragmentTag"</b>)
* public MyFragment myFragmentTag2;
* }
* </pre>
*
* </blockquote>
*
* <p>
* To use the <code>getChildFragmentManager()</code> to inject the
* <code>Fragment</code>, set the {@link #childFragment()} annotation parameter
* to <code>true</code>. You can only do this if the annotated field is in a
* class which extends <code>android.app.Fragment</code> or
* <code>android.support.v4.app.Fragment</code> and the
* <code>getChildFragmentManager()</code> method is available.
* </p>
*
* <blockquote>
*
* Example :
*
* <pre>
* &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
* android:layout_width="match_parent"
* android:layout_height="match_parent" &gt;
*
* &lt;fragment
* android:id="@+id/myChildFragment"
* android:tag="myChildFragment"
* android:name="mypackage.MyChildFragment_"
* android:layout_width="match_parent"
* android:layout_height="match_parent" /&gt;
* &lt;/LinearLayout&gt;
*
*
* &#064;EFragment(R.layout.parentfragment)
* public class MyParentFragment extends Fragment {
*
* &#064;FragmentByTag(<b>childFragment = true</b>)
* MyChildFragment myFragment;
*
* }
* </pre>
*
* </blockquote>
*
* @see EFragment
* @see FragmentArg
Expand All @@ -79,4 +117,14 @@
* @return the tag of the Fragment
*/
String value() default "";

/**
* Whether to use <code>getChildFragmentManager()</code> or
* <code>getFragmentManager()</code> to obtain the Fragment. Only can be
* <code>true</code> when injecting into a <code>Fragment</code>.
*
* @return <code>true</code> to use <code>getChildFragmentManager()</code>,
* <code>false</code> to use <code>getFragmentManager()</code>
*/
boolean childFragment() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<? extends JavaFileObject> 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<EComponentWithViewSupportHolder> {

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<EComponentWithViewSupportHolder> {
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);
}
}
Loading
X Tutup