Skip to content
Browse files

Everything is merged with nativeFragments. emmby comments taken into …

…account.
  • Loading branch information...
1 parent 9672c19 commit 464c45a0735d90af07f4809dc852dce526a58378 @stephanenicolas stephanenicolas committed Jan 19, 2014
View
6 roboguice/.project
@@ -15,9 +15,15 @@
<arguments>
</arguments>
</buildCommand>
+ <buildCommand>
+ <name>net.sf.eclipsecs.core.CheckstyleBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
+ <nature>net.sf.eclipsecs.core.CheckstyleNature</nature>
</natures>
</projectDescription>
View
7 roboguice/src/main/java/roboguice/config/DefaultRoboModule.java
@@ -60,11 +60,12 @@
*
* @author Mike Burton
*/
+@SuppressWarnings("PMD")
public class DefaultRoboModule extends AbstractModule {
public static final String GLOBAL_EVENT_MANAGER_NAME = "GlobalEventManager";
@SuppressWarnings("rawtypes")
- protected static final Class ACCOUNT_MANAGER_CLASS;
+ protected static final Class ACCOUNT_MANAGER_CLASS;
static {
Class<?> c = null;
@@ -191,8 +192,8 @@ protected void configure() {
}
@SuppressWarnings("unchecked")
- private void bindDynamicBindings() {
- // Compatibility library bindings
+ private void bindDynamicBindings() {
+ // Compatibility library bindings
if(FragmentUtil.hasSupport) {
bind(FragmentUtil.supportFrag.fragmentManagerType()).toProvider(FragmentUtil.supportFrag.fragmentManagerProviderType());
}
View
39 roboguice/src/main/java/roboguice/fragment/FragmentUtil.java
@@ -10,39 +10,48 @@
* on whether or not the underlying app uses support library or not.
* @author Charles Munger
*/
-@SuppressWarnings({ "unchecked", "rawtypes" }) //Need an unchecked conversion
-public class FragmentUtil {
+@SuppressWarnings({ "unchecked", "rawtypes","PMD" }) //Need an unchecked conversion
+public final class FragmentUtil {
public static final String SUPPORT_PACKAGE = "android.support.v4.app.";
public static final String NATIVE_PACKAGE = "android.app.";
-
+
+ //CHECKSTYLE :OFF
+ @edu.umd.cs.findbugs.annotations.SuppressWarnings("MS_SHOULD_BE_REFACTORED_TO_BE_FINAL")
public static f nativeFrag = null;
+ @edu.umd.cs.findbugs.annotations.SuppressWarnings("MS_SHOULD_BE_REFACTORED_TO_BE_FINAL")
public static f supportFrag = null;
+ @edu.umd.cs.findbugs.annotations.SuppressWarnings("MS_SHOULD_BE_REFACTORED_TO_BE_FINAL")
public static Class<? extends Activity> supportActivity = null;
+ @edu.umd.cs.findbugs.annotations.SuppressWarnings("MS_SHOULD_BE_REFACTORED_TO_BE_FINAL")
public static boolean hasNative = false;
+ @edu.umd.cs.findbugs.annotations.SuppressWarnings("MS_SHOULD_BE_REFACTORED_TO_BE_FINAL")
public static boolean hasSupport = false;
-
- public static interface f<fragType,fragManagerType> {
- public View getView(fragType frag);
- public fragType findFragmentById(fragManagerType fm, int id);
- public fragType findFragmentByTag(fragManagerType fm, String tag);
- public Class<fragType> fragmentType();
- public Class<fragManagerType> fragmentManagerType();
- public Class<Provider<fragManagerType>> fragmentManagerProviderType();
+ //CHECKSTYLE :ON
+
+ public interface f<fragType,fragManagerType> {
+ View getView(fragType frag);
+ fragType findFragmentById(fragManagerType fm, int id);
+ fragType findFragmentByTag(fragManagerType fm, String tag);
+ Class<fragType> fragmentType();
+ Class<fragManagerType> fragmentManagerType();
+ Class<Provider<fragManagerType>> fragmentManagerProviderType();
}
-
-
+
static {
try {
nativeFrag = (f) Class.forName("roboguice.fragment.provided.NativeFragmentUtil").newInstance();
hasNative = nativeFrag != null;
} catch (Throwable e) {}
-
+
try {
supportFrag = (f) Class.forName("roboguice.fragment.support.SupportFragmentUtil").newInstance();
supportActivity = (Class<? extends Activity>) Class.forName(SUPPORT_PACKAGE+"FragmentActivity");
hasSupport = supportFrag != null && supportActivity != null;
} catch (Throwable e) {}
-
+ }
+
+ private FragmentUtil() {
+ //private utility class constructor
}
}
View
46 roboguice/src/main/java/roboguice/fragment/provided/NativeFragmentUtil.java
@@ -20,15 +20,15 @@
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressWarnings("unchecked") //No point in seeing warnings when you're being ambiguous on purpose
public class NativeFragmentUtil implements f<Fragment,FragmentManager> {
- //Incredible hack required to ensure that classes are loaded at construction time
- //I.E. so that it fails fast if they're not found. TODO fix this
-
- //I thought about just depending on the API level, but that broke Robolectric.
- public NativeFragmentUtil() throws ClassNotFoundException {
- Class.forName(Fragment.class.getName());
- Class.forName(FragmentManager.class.getName());
- }
-
+ //Incredible hack required to ensure that classes are loaded at construction time
+ //I.E. so that it fails fast if they're not found. TODO fix this
+
+ //I thought about just depending on the API level, but that broke Robolectric.
+ public NativeFragmentUtil() throws ClassNotFoundException {
+ Class.forName(Fragment.class.getName());
+ Class.forName(FragmentManager.class.getName());
+ }
+
@Override
public View getView(Fragment frag) {
return frag.getView();
@@ -54,20 +54,20 @@ public Fragment findFragmentByTag(FragmentManager fm, String tag) {
return FragmentManager.class;
}
- @SuppressWarnings("rawtypes") //not technically a Class<Provider<FragmentManager>>
- @Override
- public Class fragmentManagerProviderType() {
- return FragmentManagerProvider.class;
- }
-
- @ContextSingleton
- public static class FragmentManagerProvider implements Provider<FragmentManager> {
- @Inject protected Activity activity;
+ @SuppressWarnings("rawtypes") //not technically a Class<Provider<FragmentManager>>
+ @Override
+ public Class fragmentManagerProviderType() {
+ return FragmentManagerProvider.class;
+ }
- @Override
- public FragmentManager get() {
- return activity.getFragmentManager();
- }
- }
+ @ContextSingleton
+ public static class FragmentManagerProvider implements Provider<FragmentManager> {
+ @Inject protected Activity activity;
+
+ @Override
+ public FragmentManager get() {
+ return activity.getFragmentManager();
+ }
+ }
}
View
42 roboguice/src/main/java/roboguice/fragment/support/SupportFragmentUtil.java
@@ -17,14 +17,14 @@
* @author Charles Munger
*/
@SuppressWarnings("unchecked") //we're being ambiguous on purpose
-public class SupportFragmentUtil implements f<Fragment, FragmentManager>{
- // Incredible hack required to ensure that classes are loaded at construction time
- // I.E. so that it fails fast if they're not found. TODO fix this
- public SupportFragmentUtil() throws ClassNotFoundException {
- Class.forName(Fragment.class.getName());
- Class.forName(FragmentManager.class.getName());
- }
-
+public class SupportFragmentUtil implements f<Fragment, FragmentManager> {
+ // Incredible hack required to ensure that classes are loaded at construction time
+ // I.E. so that it fails fast if they're not found. TODO fix this
+ public SupportFragmentUtil() throws ClassNotFoundException {
+ Class.forName(Fragment.class.getName());
+ Class.forName(FragmentManager.class.getName());
+ }
+
@Override
public View getView(Fragment frag) {
return frag.getView();
@@ -50,20 +50,20 @@ public Fragment findFragmentByTag(FragmentManager fm, String tag) {
return FragmentManager.class;
}
- @SuppressWarnings("rawtypes")
- @Override
- public Class fragmentManagerProviderType() {
- return FragmentManagerProvider.class;
- }
+ @SuppressWarnings("rawtypes")
+ @Override
+ public Class fragmentManagerProviderType() {
+ return FragmentManagerProvider.class;
+ }
- @ContextSingleton
- public static class FragmentManagerProvider implements Provider<FragmentManager> {
- @Inject protected Activity activity;
+ @ContextSingleton
+ public static class FragmentManagerProvider implements Provider<FragmentManager> {
+ @Inject protected Activity activity;
- @Override
- public FragmentManager get() {
- return ((FragmentActivity)activity).getSupportFragmentManager();
- }
- }
+ @Override
+ public FragmentManager get() {
+ return ((FragmentActivity)activity).getSupportFragmentManager();
+ }
+ }
}
View
6 roboguice/src/main/java/roboguice/inject/ContextScopedRoboInjector.java
@@ -276,14 +276,14 @@ public void injectViewMembers(android.app.Fragment fragment) {
injectViews(fragment);
}
- private void injectViews(Object fragment) {
- synchronized (ContextScope.class) {
+ private void injectViews(Object fragment) {
+ synchronized (ContextScope.class) {
scope.enter(context);
try {
ViewMembersInjector.injectViews(fragment);
} finally {
scope.exit(context);
}
}
- }
+ }
}
View
216 roboguice/src/main/java/roboguice/inject/ViewListener.java
@@ -15,123 +15,130 @@
*/
package roboguice.inject;
-import java.lang.annotation.Annotation;
-import java.lang.ref.WeakReference;
-import java.lang.reflect.Field;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
-import java.util.ArrayList;
-import java.util.WeakHashMap;
+import android.app.Activity;
+import android.content.Context;
+import android.view.View;
-import javax.inject.Singleton;
+import roboguice.fragment.FragmentUtil;
+import roboguice.fragment.FragmentUtil.f;
import com.google.inject.MembersInjector;
import com.google.inject.Provider;
import com.google.inject.TypeLiteral;
import com.google.inject.spi.TypeEncounter;
import com.google.inject.spi.TypeListener;
-import edu.umd.cs.findbugs.annotations.SuppressWarnings;
-
-import android.app.Activity;
-import android.content.Context;
-import android.view.View;
+import javax.inject.Singleton;
+import java.lang.annotation.Annotation;
+import java.lang.ref.WeakReference;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.WeakHashMap;
@Singleton
+@SuppressWarnings({"unchecked","PMD"})
public class ViewListener implements TypeListener {
-
- @SuppressWarnings(value="MS_SHOULD_BE_REFACTORED_TO_BE_FINAL")
- protected static Class fragmentClass = null;
- @SuppressWarnings(value="MS_SHOULD_BE_REFACTORED_TO_BE_FINAL")
- protected static Class fragmentManagerClass = null;
- @SuppressWarnings(value="MS_SHOULD_BE_REFACTORED_TO_BE_FINAL")
- protected static Method fragmentGetViewMethod = null;
- @SuppressWarnings(value="MS_SHOULD_BE_REFACTORED_TO_BE_FINAL")
- protected static Method fragmentFindFragmentByIdMethod = null;
- @SuppressWarnings(value="MS_SHOULD_BE_REFACTORED_TO_BE_FINAL")
- protected static Method fragmentFindFragmentByTagMethod = null;
-
- static {
- try {
- fragmentClass = Class.forName("android.support.v4.app.Fragment");
- fragmentManagerClass = Class.forName("android.support.v4.app.FragmentManager");
- fragmentGetViewMethod = fragmentClass.getDeclaredMethod("getView");
- fragmentFindFragmentByIdMethod = fragmentManagerClass.getMethod("findFragmentById", int.class);
- fragmentFindFragmentByTagMethod = fragmentManagerClass.getMethod("findFragmentByTag", String.class);
- } catch (Throwable exception) {
- exception.printStackTrace();
- }
- }
-
+
+ @Override
public <I> void hear(TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter) {
for (Class<?> c = typeLiteral.getRawType(); c != Object.class; c = c.getSuperclass())
- for (Field field : c.getDeclaredFields())
+ for (Field field : c.getDeclaredFields()) {
if (field.isAnnotationPresent(InjectView.class))
if (Modifier.isStatic(field.getModifiers()))
throw new UnsupportedOperationException("Views may not be statically injected");
else if (!View.class.isAssignableFrom(field.getType()))
throw new UnsupportedOperationException("You may only use @InjectView on fields descended from type View");
else if (Context.class.isAssignableFrom(field.getDeclaringClass()) && !Activity.class.isAssignableFrom(field.getDeclaringClass()))
throw new UnsupportedOperationException("You may only use @InjectView in Activity contexts");
- else
- typeEncounter.register(new ViewMembersInjector<I>(field, field.getAnnotation(InjectView.class), typeEncounter));
+ else {
+ final f<?,?> utils = FragmentUtil.hasSupport
+ && (FragmentUtil.supportActivity.isAssignableFrom(field.getDeclaringClass())
+ || FragmentUtil.supportFrag.fragmentType().isAssignableFrom(field.getDeclaringClass()))
+ ? FragmentUtil.supportFrag : FragmentUtil.nativeFrag;
+
+ typeEncounter.register(new ViewMembersInjector<I>(
+ field, field.getAnnotation(InjectView.class),
+ typeEncounter, utils));
+ }
else if (field.isAnnotationPresent(InjectFragment.class))
- if (Modifier.isStatic(field.getModifiers()))
+ if (!FragmentUtil.hasNative && !FragmentUtil.hasSupport) {
+ throw new RuntimeException(new ClassNotFoundException("No fragment classes were available"));
+ } else if (Modifier.isStatic(field.getModifiers())) {
throw new UnsupportedOperationException("Fragments may not be statically injected");
- else if (fragmentClass != null && !fragmentClass.isAssignableFrom(field.getType()))
- throw new UnsupportedOperationException("You may only use @InjectFragment on fields descended from type Fragment");
- else if (Context.class.isAssignableFrom(field.getDeclaringClass()) && !Activity.class.isAssignableFrom(field.getDeclaringClass()))
- throw new UnsupportedOperationException("You may only use @InjectFragment in Activity contexts");
- else
- typeEncounter.register(new ViewMembersInjector<I>(field, field.getAnnotation(InjectFragment.class), typeEncounter));
+ } else {
+ final boolean assignableFromNative = FragmentUtil.hasNative && FragmentUtil.nativeFrag.fragmentType().isAssignableFrom(field.getType());
+ final boolean assignableFromSupport = FragmentUtil.hasSupport && FragmentUtil.supportFrag.fragmentType().isAssignableFrom(field.getType());
+ final boolean isSupportActivity = FragmentUtil.hasSupport && FragmentUtil.supportActivity.isAssignableFrom(field.getDeclaringClass());
+ final boolean isNativeActivity = !isSupportActivity && Activity.class.isAssignableFrom(field.getDeclaringClass());
+
+ if (isNativeActivity && assignableFromNative || isSupportActivity && assignableFromSupport) {
+ typeEncounter.register(new ViewMembersInjector<I>(field, field.getAnnotation(InjectFragment.class), typeEncounter, isNativeActivity ? FragmentUtil.nativeFrag:FragmentUtil.supportFrag));
+ } else if (isNativeActivity && !assignableFromNative) {
+ // Error messages - these filters are comprehensive. The
+ // final else block will never execute.
+ throw new UnsupportedOperationException(
+ "You may only use @InjectFragment in native activities if fields are descended from type android.app.Fragment");
+ } else if (!isSupportActivity && !isNativeActivity) {
+ throw new UnsupportedOperationException("You may only use @InjectFragment in Activity contexts");
+ } else if (isSupportActivity && !assignableFromSupport) {
+ throw new UnsupportedOperationException(
+ "You may only use @InjectFragment in support activities if fields are descended from type android.support.v4.app.Fragment");
+ } else {
+ throw new RuntimeException("This should never happen.");
+ }
+ }
+ }
}
+
/**
- * This class gets twice as many providers as necessary to do its job, look
- * into optimizations in the future if this is a bottleneck
+ * This class gets twice as many providers as necessary to do its job, look into optimizations in the future if this is a bottleneck
*/
public static class ViewMembersInjector<T> implements MembersInjector<T> {
- @SuppressWarnings("MS_SHOULD_BE_FINAL")
- protected static WeakHashMap<Object, ArrayList<ViewMembersInjector<?>>> viewMembersInjectors = new WeakHashMap<Object, ArrayList<ViewMembersInjector<?>>>();
+ @edu.umd.cs.findbugs.annotations.SuppressWarnings("MS_SHOULD_BE_FINAL")
+ protected static WeakHashMap<Object,ArrayList<ViewMembersInjector<?>>> viewMembersInjectors = new WeakHashMap<Object, ArrayList<ViewMembersInjector<?>>>();
protected Field field;
protected Annotation annotation;
protected WeakReference<T> instanceRef;
- protected Provider fragmentManagerProvider;
+ @SuppressWarnings("rawtypes")
+ protected FragmentUtil.f fragUtils;
+ @SuppressWarnings("rawtypes")
+ protected Provider fragManager;
protected Provider<Activity> activityProvider;
- public ViewMembersInjector(Field field, Annotation annotation, TypeEncounter<T> typeEncounter) {
+ public ViewMembersInjector(Field field, Annotation annotation, TypeEncounter<T> typeEncounter, FragmentUtil.f<?,?> utils) {
this.field = field;
this.annotation = annotation;
this.activityProvider = typeEncounter.getProvider(Activity.class);
- if (fragmentManagerClass != null)
- this.fragmentManagerProvider = typeEncounter.getProvider(fragmentManagerClass);
+ if( utils !=null ) {
+ this.fragUtils = utils;
+ this.fragManager = typeEncounter.getProvider(utils.fragmentManagerType());
+ }
}
/**
- * This is called when instance is injected by guice. Because the views
- * may or may not be set up yet, we don't do the real view injection
- * until later.
- *
- * @param instance
- * the instance being injected by guice
+ * This is called when instance is injected by guice. Because the views may or may not be set up yet,
+ * we don't do the real view injection until later.
+ *
+ * @param instance the instance being injected by guice
*/
public void injectMembers(T instance) {
synchronized (ViewMembersInjector.class) {
final Activity activity = activityProvider.get();
- final Object key = fragmentClass != null && fragmentClass.isInstance(instance) ? instance : activity;
- if (key == null)
+ final Object key = fragUtils != null && fragUtils.fragmentType().isInstance(instance) ? instance : activity;
+ if( key==null )
return;
// Add a view injector for the key
ArrayList<ViewMembersInjector<?>> injectors = viewMembersInjectors.get(key);
- if (injectors == null) {
+ if( injectors==null ) {
injectors = new ArrayList<ViewMembersInjector<?>>();
viewMembersInjectors.put(key, injectors);
}
@@ -141,36 +148,33 @@ public void injectMembers(T instance) {
}
}
- public void reallyInjectMembers(Object activityOrFragment) {
- if (annotation instanceof InjectView)
+ public void reallyInjectMembers( Object activityOrFragment ) {
+ if( annotation instanceof InjectView )
reallyInjectMemberViews(activityOrFragment);
else
reallyInjectMemberFragments(activityOrFragment);
}
/**
* This is when the view references are actually evaluated.
- *
- * I don't like all the hacks I had to put into this method. Instance is
- * the object you're injecting into. ActivityOrFragment is the activity
- * or fragment that you're injecting views into. Instance must equal
- * activityOrFragment is activityOrFragment is a fragment, but they may
- * differ if activityOrFragment is an activity. They should be allowed
- * to differ so that you can inject views into arbitrary objects, but I
- * don't know how to determine whether to get the view from the fragment
- * or the activity for an arbitrary object, so I'm artificially limiting
- * injection targets to the fragment itself for fragments.
*
- * @param activityOrFragment
- * an activity or fragment
+ * I don't like all the hacks I had to put into this method. Instance is the object you're
+ * injecting into. ActivityOrFragment is the activity or fragment that you're injecting
+ * views into. Instance must equal activityOrFragment is activityOrFragment is a fragment,
+ * but they may differ if activityOrFragment is an activity. They should be allowed to differ
+ * so that you can inject views into arbitrary objects, but I don't know how to determine whether
+ * to get the view from the fragment or the activity for an arbitrary object, so I'm artificially
+ * limiting injection targets to the fragment itself for fragments.
+ *
+ * @param activityOrFragment an activity or fragment
*/
protected void reallyInjectMemberViews(Object activityOrFragment) {
- final T instance = fragmentClass != null && fragmentClass.isInstance(activityOrFragment) ? (T) activityOrFragment : instanceRef.get();
- if (instance == null)
+ final T instance = fragUtils != null && fragUtils.fragmentType().isInstance(activityOrFragment) ? (T)activityOrFragment : instanceRef.get();
+ if( instance==null )
return;
- if (activityOrFragment instanceof Context && !(activityOrFragment instanceof Activity))
+ if( activityOrFragment instanceof Context && !(activityOrFragment instanceof Activity ))
throw new UnsupportedOperationException("Can't inject view into a non-Activity context");
View view = null;
@@ -179,46 +183,38 @@ protected void reallyInjectMemberViews(Object activityOrFragment) {
final InjectView injectView = (InjectView) annotation;
final int id = injectView.value();
- if (id >= 0)
- view = fragmentClass != null && fragmentClass.isInstance(activityOrFragment) ? ((View) fragmentGetViewMethod.invoke(activityOrFragment))
- .findViewById(id) : ((Activity) activityOrFragment).findViewById(id);
+ if( id>=0 )
+ view = fragUtils != null && fragUtils.fragmentType().isInstance(activityOrFragment) ? fragUtils.getView(activityOrFragment).findViewById(id) : ((Activity)activityOrFragment).findViewById(id);
else
- view = fragmentClass != null && fragmentClass.isInstance(activityOrFragment) ? ((View) fragmentGetViewMethod.invoke(activityOrFragment))
- .findViewWithTag(injectView.tag()) : ((Activity) activityOrFragment).getWindow().getDecorView().findViewWithTag(injectView.tag());
+ view = fragUtils !=null && fragUtils.fragmentType().isInstance(activityOrFragment) ? fragUtils.getView(activityOrFragment).findViewWithTag(injectView.tag()) : ((Activity)activityOrFragment).getWindow().getDecorView().findViewWithTag(injectView.tag());
if (view == null && Nullable.notNullable(field))
- throw new NullPointerException(String.format("Can't inject null value into %s.%s when field is not @Nullable", field.getDeclaringClass(),
- field.getName()));
+ throw new NullPointerException(String.format("Can't inject null value into %s.%s when field is not @Nullable", field.getDeclaringClass(), field.getName()));
field.setAccessible(true);
field.set(instance, view);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
- } catch (InvocationTargetException e) {
- throw new RuntimeException(e);
-
- } catch (IllegalArgumentException f) {
+ } catch (IllegalArgumentException f) {
throw new IllegalArgumentException(String.format("Can't assign %s value %s to %s field %s", view != null ? view.getClass() : "(null)", view,
field.getType(), field.getName()), f);
}
}
/**
* This is when the view references are actually evaluated.
- *
- * @param activityOrFragment
- * an activity or fragment
+ * @param activityOrFragment an activity or fragment
*/
protected void reallyInjectMemberFragments(Object activityOrFragment) {
final T instance = instanceRef.get();
- if (instance == null)
+ if( instance==null )
return;
- if (activityOrFragment instanceof Context && !(activityOrFragment instanceof Activity))
+ if( activityOrFragment instanceof Context && !(activityOrFragment instanceof Activity ))
throw new UnsupportedOperationException("Can't inject fragment into a non-Activity context");
Object fragment = null;
@@ -227,41 +223,35 @@ protected void reallyInjectMemberFragments(Object activityOrFragment) {
final InjectFragment injectFragment = (InjectFragment) annotation;
final int id = injectFragment.value();
- if (id >= 0)
- fragment = fragmentFindFragmentByIdMethod.invoke(fragmentManagerProvider.get(), id);
-
+ if( id>=0 )
+ fragment = fragUtils.findFragmentById(fragManager.get(), id);
else
- fragment = fragmentFindFragmentByTagMethod.invoke(fragmentManagerProvider.get(), injectFragment.tag());
+ fragment = fragUtils.findFragmentByTag(fragManager.get(),injectFragment.tag());
if (fragment == null && Nullable.notNullable(field))
- throw new NullPointerException(String.format("Can't inject null value into %s.%s when field is not @Nullable", field.getDeclaringClass(),
- field.getName()));
+ throw new NullPointerException(String.format("Can't inject null value into %s.%s when field is not @Nullable", field.getDeclaringClass(), field.getName()));
field.setAccessible(true);
field.set(instance, fragment);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
- } catch (InvocationTargetException e) {
- throw new RuntimeException(e);
-
} catch (IllegalArgumentException f) {
- throw new IllegalArgumentException(String.format("Can't assign %s value %s to %s field %s", fragment != null ? fragment.getClass() : "(null)",
- fragment, field.getType(), field.getName()), f);
+ throw new IllegalArgumentException(String.format("Can't assign %s value %s to %s field %s", fragment != null ? fragment.getClass() : "(null)", fragment,
+ field.getType(), field.getName()), f);
}
}
protected static void injectViews(Object activityOrFragment) {
- synchronized (ViewMembersInjector.class) {
+ synchronized ( ViewMembersInjector.class ) {
final ArrayList<ViewMembersInjector<?>> injectors = viewMembersInjectors.get(activityOrFragment);
- if (injectors != null)
- for (ViewMembersInjector<?> viewMembersInjector : injectors)
+ if(injectors!=null)
+ for(ViewMembersInjector<?> viewMembersInjector : injectors)
viewMembersInjector.reallyInjectMembers(activityOrFragment);
}
}
-
}
}
View
13 roboguice/src/test/java/roboguice/fragment/provided/FragmentInjectionTest.java
@@ -7,6 +7,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
@@ -34,8 +35,7 @@ public void shadowActivityGetApplicationContextShouldNotReturnNull() {
@Test
public void shouldInjectPojosAndViewsIntoFragments() {
- final ActivityA activity = new ActivityA();
- activity.onCreate(null);
+ final ActivityA activity = Robolectric.buildActivity(ActivityA.class).create().start().resume().get();
activity.fragmentRef.onViewCreated(activity.fragmentRef.onCreateView(null,null,null), null);
assertNotNull(activity.fragmentRef.ref);
@@ -109,6 +109,15 @@ protected void onCreate(Bundle savedInstanceState) {
View ref;
@Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ }
+
+ @Override
+ public void onViewCreated(View view, Bundle savedInstanceState) {
+ super.onViewCreated(view, savedInstanceState);
+ }
+ @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ref = new View(getActivity());
ref.setId(101);
View
1 test_suppressions.xml
@@ -7,4 +7,5 @@
<suppressions>
<suppress checks="ConstantName*" files="src[\\/]test[\\/].*" />
<suppress checks="VisibilityModifier|StaticVariableName" files=".*RoboGuice.*" />
+ <suppress checks="VisibilityModifier|StaticVariableName|TypeName" files=".*FragmentUtil.*" />
</suppressions>

0 comments on commit 464c45a

Please sign in to comment.
Something went wrong with that request. Please try again.