<commit_before>/*
 * Copyright 2013 <NAME>
 * Copyright 2014 <NAME> (@f2prateek)
 *
 * 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 com.jug6ernaut.saber;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import com.jug6ernaut.saber.internal.InjectExtraProcessor;

import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Extra injection utilities. Use this class to simplify getting extras.
 * <p>
 * Injecting extras from your activity is as easy as:
 * <pre><code>
 * public class ExampleActivity extends Activity {
 *   {@literal @}Preference("key") String extra;
 *
 *   {@literal @}Override protected void onCreate(Bundle savedInstanceState) {
 *     super.onCreate(savedInstanceState);
 *     Saber.inject(this);
 *   }
 * }
 * </code></pre>
 * You can inject an {@link #inject(Activity) activity directly}, {@link
 * #inject(android.app.Fragment) fragment directly}, or inject an
 * {@link #inject(Context, Object) bundle into another object}.
 * <p>
 * Be default, extras are required to be present in the bundle for field injections.
 * If an extra is optional add the {@link Nullable @Nullable} annotation.
 * <pre><code>
 * {@literal @}Nullable {@literal @}Preference("key") String extra;
 * </code></pre>
 * <p>
 * If you need to provide a default value for an extra, simply set an initial value
 * while declaring the field, combined with the {@link Nullable @Nullable} annotation.
 * <pre><code>
 * {@literal @}Nullable {@literal @}Preference("key") String extra = "default_value";
 * </code></pre>
 */
public class Saber {
  static final Map<Class<?>, Method> INJECTORS = new LinkedHashMap<Class<?>, Method>();
  static final Method NO_OP = null;
  private static final String TAG = "Saber";
  private static boolean debug = false;

  private Saber() {
    // No instances.
  }

  /** Control whether debug logging is enabled. */
  public static void setDebug(boolean debug) {
    Saber.debug = debug;
  }

  /**
   * Inject fields annotated with {@link Preference} in the specified {@link
   * android.app.Activity}.
   * The intent that called this activity will be used as the source of the extras bundle.
   *
   * @param target Target activity for field injection.
   * @throws Saber.UnableToInjectException if injection could not be
   * performed.
   * @see android.content.Intent#getExtras()
   */
  public static void inject(Activity target) {
    inject(target, target);
  }

  /**
   * Inject fields annotated with {@link Preference} in the specified {@link
   * android.app.Fragment}.
   * The arguments that this fragment was called with will be used as the source of the extras
   * bundle.
   *
   * @param target Target fragment for field injection.
   * @throws Saber.UnableToInjectException if injection could not be
   * performed.
   * @see android.app.Fragment#getArguments()
   */
  public static void inject(Fragment target) {
    inject(target.getActivity(), target);
  }

  static void inject(Context source, Object target) {
    Class<?> targetClass = target.getClass();
    try {
      if (debug) Log.d(TAG, "Looking up extra injector for " + targetClass.getName());
      Method inject = findInjectorForClass(targetClass);
      if (inject != null) {
        inject.invoke(null, source, target);
      }
    } catch (RuntimeException e) {
      throw e;
    } catch (Exception e) {
      throw new UnableToInjectException("Unable to inject extras for " + target, e);
    }
  }

  private static Method findInjectorForClass(Class<?> cls) throws NoSuchMethodException {
    Method inject = INJECTORS.get(cls);
    if (inject != null) {
      if (debug) Log.d(TAG, "HIT: Cached in injector map.");
      return inject;
    }
    String clsName = cls.getName();
    if (clsName.startsWith("android.") || clsName.startsWith("java.")) {
      if (debug) Log.d(TAG, "MISS: Reached framework class. Abandoning search.");
      return NO_OP;
    }<commit_msg>Expose base level inject method<commit_after>/*
 * Copyright 2013 <NAME>
 * Copyright 2014 <NAME> (@f2prateek)
 *
 * 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 com.jug6ernaut.saber;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import com.jug6ernaut.saber.internal.InjectExtraProcessor;

import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Extra injection utilities. Use this class to simplify getting extras.
 * <p>
 * Injecting extras from your activity is as easy as:
 * <pre><code>
 * public class ExampleActivity extends Activity {
 *   {@literal @}Preference("key") String extra;
 *
 *   {@literal @}Override protected void onCreate(Bundle savedInstanceState) {
 *     super.onCreate(savedInstanceState);
 *     Saber.inject(this);
 *   }
 * }
 * </code></pre>
 * You can inject an {@link #inject(Activity) activity directly}, {@link
 * #inject(android.app.Fragment) fragment directly}, or inject an
 * {@link #inject(Context, Object) bundle into another object}.
 * <p>
 * Be default, extras are required to be present in the bundle for field injections.
 * If an extra is optional add the {@link Nullable @Nullable} annotation.
 * <pre><code>
 * {@literal @}Nullable {@literal @}Preference("key") String extra;
 * </code></pre>
 * <p>
 * If you need to provide a default value for an extra, simply set an initial value
 * while declaring the field, combined with the {@link Nullable @Nullable} annotation.
 * <pre><code>
 * {@literal @}Nullable {@literal @}Preference("key") String extra = "default_value";
 * </code></pre>
 */
public class Saber {
  static final Map<Class<?>, Method> INJECTORS = new LinkedHashMap<Class<?>, Method>();
  static final Method NO_OP = null;
  private static final String TAG = "Saber";
  private static boolean debug = false;

  private Saber() {
    // No instances.
  }

  /** Control whether debug logging is enabled. */
  public static void setDebug(boolean debug) {
    Saber.debug = debug;
  }

  /**
   * Inject fields annotated with {@link Preference} in the specified {@link
   * android.app.Activity}.
   * The intent that called this activity will be used as the source of the extras bundle.
   *
   * @param target Target activity for field injection.
   * @throws Saber.UnableToInjectException if injection could not be
   * performed.
   * @see android.content.Intent#getExtras()
   */
  public static void inject(Activity target) {
    inject(target, target);
  }

  /**
   * Inject fields annotated with {@link Preference} in the specified {@link
   * android.app.Fragment}.
   * The arguments that this fragment was called with will be used as the source of the extras
   * bundle.
   *
   * @param target Target fragment for field injection.
   * @throws Saber.UnableToInjectException if injection could not be
   * performed.
   * @see android.app.Fragment#getArguments()
   */
  public static void inject(Fragment target) {
    inject(target.getActivity(), target);
  }

  public static void inject(Context source, Object target) {
    Class<?> targetClass = target.getClass();
    try {
      if (debug) Log.d(TAG, "Looking up extra injector for " + targetClass.getName());
      Method inject = findInjectorForClass(targetClass);
      if (inject != null) {
        inject.invoke(null, source, target);
      }
    } catch (RuntimeException e) {
      throw e;
    } catch (Exception e) {
      throw new UnableToInjectException("Unable to inject extras for " + target, e);
    }
  }

  private static Method findInjectorForClass(Class<?> cls) throws NoSuchMethodException {
    Method inject = INJECTORS.get(cls);
    if (inject != null) {
      if (debug) Log.d(TAG, "HIT: Cached in injector map.");
      return inject;
    }
    String clsName = cls.getName();
    if (clsName.startsWith("android.") || clsName.startsWith("java.")) {
      if (debug) Log.d(TAG, "MISS: Reached framework class. Abandoning search.");
      return NO_OP;
    }
{'fraction_non_alphanumeric': 0.1108599720159294, 'fraction_numerical': 0.0032289312237649337, 'mean_word_length': 3.688193743693239, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7584204', 'n_tokens_mistral': 2633, 'n_tokens_neox': 2583, 'n_words': 955}