Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package pocketknife;
  
  import android.os.Bundle;
  import android.util.Log;
 
 
 import static pocketknife.internal.GeneratedAdapters.ANDROID_PREFIX;
 import static pocketknife.internal.GeneratedAdapters.BUNDLE_ADAPTER_SUFFIX;
 import static pocketknife.internal.GeneratedAdapters.INTENT_ADAPTER_SUFFIX;
 import static pocketknife.internal.GeneratedAdapters.JAVA_PREFIX;
 
 
 public final class PocketKnife {
     private static final String TAG = "PocketKnife";
     private static boolean debug;
 
     private PocketKnife() {
         throw new AssertionError("No instances.");
     }

    
Control whether debug logging ins enabled
 
     public static void setDebug(boolean debug) {
         . = debug;
     }

    
Save annotated fields in the specified target to the android.os.Bundle.

Parameters:
target Target class for field saving.
bundle Bundle to save the field values.
 
     public static <T> void saveInstanceState(T targetBundle bundle) {
         @SuppressWarnings("unchecked")
         BundleBinding<T> binding = (BundleBinding<T>) getBundleBinding(target.getClass().getClassLoader(), target.getClass());
         if (binding != null) {
             binding.saveInstanceState(targetbundle);
         }
     }

    
Restore annotated fields in the specified target from the android.os.Bundle.

Parameters:
target Target class to restore fields
bundle Bundle to restore field values.
 
     public static <T> void restoreInstanceState(T targetBundle bundle) {
         @SuppressWarnings("unchecked")
         BundleBinding<T> binding = (BundleBinding<T>) getBundleBinding(target.getClass().getClassLoader(), target.getClass());
         if (binding != null) {
             binding.restoreInstanceState(targetbundle);
         }
     }

    
Inject annotated fields in the specified android.app.Fragment from its arguments.

Parameters:
fragment fragment to inject the arguments;
 
     public static void injectArguments(android.app.Fragment fragment) {
         injectArguments(fragmentfragment.getArguments());
     }

    
Inject annotated fields in the specified android.support.v4.app.Fragment from its arguments.

Parameters:
fragment fragment to inject the arguments;
 
     public static void injectArguments(android.support.v4.app.Fragment fragment) {
         injectArguments(fragmentfragment.getArguments());
     }

    
Inject annotated fields in the specified target from the android.os.Bundle.

Parameters:
target Target object for inject arguments
bundle Bundle containing arguments;
 
     public static <T> void injectArguments(T targetBundle bundle) {
         @SuppressWarnings("unchecked")
         BundleBinding<T> binding = (BundleBinding<T>) getBundleBinding(target.getClass().getClassLoader(), target.getClass());
         if (binding != null) {
             binding.injectArguments(targetbundle);
         }
     }

    
Inject annotated field in the specified android.app.Activity from its intent.

Parameters:
activity activity to inject the extras.
 
    public static void injectExtras(Activity activity) {
        injectExtras(activityactivity.getIntent());
    }

    
Inject annotated fields in the specified target from the android.content.Intent.

Parameters:
target Target object to inject the extras.
intent Intent containing the extras.
    public static <T> void injectExtras(T targetIntent intent) {
        @SuppressWarnings("unchecked")
        IntentBinding<T> binding = (IntentBinding<T>) getIntentBinding(target.getClass().getClassLoader(), target.getClass());
        if (binding != null) {
            binding.injectExtras(targetintent);
        }
    }
    private static BundleBinding<?> getBundleBinding(ClassLoader classLoaderClass<?> cls) {
        String clsName = cls.getName();
        if (clsName.startsWith() || clsName.startsWith()) {
            if () {
                Log.d("MISS: Reached framework class. Abandoning search.");
            }
            return null;
        }
        Class<?> adapterClass = loadClass(classLoaderclsName.concat());
        if (!adapterClass.equals(Void.class)) {
            if () {
                Log.d("Found loadable bundle adapter for " + clsName);
            }
            try {
                @SuppressWarnings("unchecked")
                Constructor<BundleBinding<?>> constructor = (Constructor<BundleBinding<?>>) adapterClass.getConstructor();
                return constructor.newInstance();
            } catch (NoSuchMethodException e) {
                throw new IllegalStateException(
                        "Couldn't find default constructor in the generated bundle adapter for class "
                                + clsName);
            } catch (InvocationTargetException e) {
                throw new IllegalStateException(
                        "Could not create an instance of the bundle adapter for class " + clsNamee);
            } catch (InstantiationException e) {
                throw new IllegalStateException(
                        "Could not create an instance of the bundle adapter for class " + clsNamee);
            } catch (IllegalAccessException e) {
                throw new IllegalStateException(
                        "Could not create an instance of the bundle adapter for class " + clsNamee);
            }
        }
        // Search for Parent Class adapter
        if () {
            Log.d(, String.format("%s not found. Trying superclass %s"clsNamecls.getSuperclass().getName()));
        }
        return getBundleBinding(classLoadercls.getSuperclass());
    }
    private static IntentBinding<?> getIntentBinding(ClassLoader classLoaderClass cls) {
        String clsName = cls.getName();
        if (clsName.startsWith() || clsName.startsWith()) {
            if () {
                Log.d("MISS: Reached framework class. Abandoning search.");
            }
            return null;
        }
        Class<?> adapterClass = loadClass(classLoaderclsName.concat());
        if (!adapterClass.equals(Void.class)) {
            if () {
                Log.d("Found loadable intent adapter for " + clsName);
            }
            try {
                @SuppressWarnings("unchecked")
                Constructor<IntentBinding<?>> constructor = (Constructor<IntentBinding<?>>) adapterClass.getConstructor();
                return constructor.newInstance();
            } catch (NoSuchMethodException e) {
                throw new IllegalStateException(
                        "Couldn't find default constructor in the generated intent adapter for class "
                                + clsName);
            } catch (InvocationTargetException e) {
                throw new IllegalStateException(
                        "Could not create an instance of the intent adapter for class " + clsNamee);
            } catch (InstantiationException e) {
                throw new IllegalStateException(
                        "Could not create an instance of the intent adapter for class " + clsNamee);
            } catch (IllegalAccessException e) {
                throw new IllegalStateException(
                        "Could not create an instance of the intent adapter for class " + clsNamee);
            }
        }
        // Search for Parent Class adapter
        if () {
            Log.d(, String.format("%s not found. Trying superclass %s"clsNamecls.getSuperclass().getName()));
        }
        return getIntentBinding(classLoadercls.getSuperclass());
    }
    private static Class<?> loadClass(ClassLoader classLoaderString name) {
        // a null classloader is the system classloader.
        if (classLoader == null) {
            if () {
                Log.d("Class loader is null using system class loader");
            }
            classLoader = ClassLoader.getSystemClassLoader();
        }
        return .get(classLoader).get(name);
    }
    private static final Memoizer<ClassLoaderMemoizer<StringClass<?>>> CACHES = new Memoizer<ClassLoaderMemoizer<StringClass<?>>>() {
        @Override
        protected Memoizer<StringClass<?>> create(final ClassLoader classLoader) {
            return new Memoizer<StringClass<?>>() {
                @Override
                protected Class<?> create(String className) {
                    try {
                        Class<?> cls = classLoader.loadClass(className);
                        if () {
                            Log.d("Successfully loaded class " + className);
                        }
                        return cls;
                    } catch (ClassNotFoundException e) {
                        if () {
                            Log.d("Failed to load class " + className);
                        }
                        return Void.class// Cache the failure (negative case).
                    }
                }
            };
        }
    };
New to GrepCode? Check out our FAQ X