Example usage for java.lang InstantiationException printStackTrace

List of usage examples for java.lang InstantiationException printStackTrace

Introduction

In this page you can find the example usage for java.lang InstantiationException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:Main.java

public static void replaceFragment(FragmentManager manager, Class<? extends Fragment> fragmentClass,
        boolean isAddToBackStack) {

    Fragment fragment = manager.findFragmentByTag(fragmentClass.getSimpleName());

    if (null == fragment) {
        try {//from  w  ww  .j a v a  2  s  .  c om

            fragment = fragmentClass.newInstance();

        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    FragmentTransaction ft = manager.beginTransaction();
    ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out, android.R.anim.fade_in,
            android.R.anim.fade_out);
    if (!fragment.isAdded()) {
        ft.replace(android.R.id.content, fragment, fragment.getClass().getSimpleName());
        if (isAddToBackStack) {
            ft.addToBackStack(null);
        }
    }
    ft.commit();

}

From source file:Main.java

@SuppressWarnings("unchecked")
public static final <E> E getInstance(Class<E> clazz) {
    if (INSTANCE_MAP.containsKey(clazz)) {
        return (E) INSTANCE_MAP.get(clazz);
    } else {//from w  w  w .  j a v a 2  s .c o m
        E instance;
        synchronized (clazz) {
            instance = (E) INSTANCE_MAP.get(clazz);
            if (instance == null) {
                try {
                    Constructor<E> constructor = clazz.getDeclaredConstructor();
                    constructor.setAccessible(true);
                    instance = constructor.newInstance();
                    INSTANCE_MAP.put(clazz, instance);
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        return instance;
    }
}

From source file:Main.java

public static Object newInstance(Class cls, Class<?>[] types, Object... params) {
    Constructor<?> mConstructor;
    try {/*from   w  w  w . ja v a 2s . c om*/
        return cls.getClass().newInstance();
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;

}

From source file:Main.java

public static Object newInstance(Object model) {
    Object instance = null;//from   w  w  w. j a v a2 s.  c  o  m
    try {
        Class<?> aClass = model.getClass();

        instance = aClass.newInstance();
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return instance;
}

From source file:Main.java

public static <T extends Fragment> T addFragmentToActivity(@NonNull FragmentManager fragmentManager,
        @NonNull Class<T> clz, @NonNull Integer frameId) {
    checkNotNull(fragmentManager);/*from w w  w  .  ja v  a  2 s  .c o m*/
    checkNotNull(clz);
    checkNotNull(frameId);
    T fragment = (T) fragmentManager.findFragmentById(frameId);
    if (fragment == null) {
        try {
            fragment = clz.newInstance();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.add(frameId, fragment);
            transaction.commit();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    return fragment;
}

From source file:Main.java

/**
 * Converts the given array to a set with the given type. Works like
 * {@link Arrays#asList(Object...)}./*from   ww w.  j av  a  2s  . c o m*/
 *
 * @param <T>
 *            The type of the set values.
 * @param setType
 *            The set type to return.
 * @param arr
 *            The array to convert into a set.
 * @return An object of the given set or, if, and only if, an error
 *         occurred, <code>null</code>.
 */
@SuppressWarnings("unchecked")
public static <T> Set<T> toSet(@SuppressWarnings("rawtypes") final Class<? extends Set> setType,
        final T... arr) {
    assert setType != null : "SetType cannot be null!";
    assert !Modifier.isAbstract(setType.getModifiers()) : "SetType cannot be abstract!";
    assert !setType.isInterface() : "SetType cannot be an interface!";
    assert arr != null : "Arr cannot be null!";

    Set<T> result = null;
    try {
        result = setType.newInstance();

        for (final T t : arr) {
            result.add(t);
        }
    } catch (final InstantiationException ex) {
        ex.printStackTrace();
    } catch (final IllegalAccessException ex) {
        ex.printStackTrace();
    }

    return result;
}

From source file:ClassUtil.java

/**
 * Create new instance of specified class and type
 *
 * @param clazz of instance/*from  ww w .j  av a  2  s . com*/
 * @param <T> type of object
 * @return new Class instance
 */
public static <T> T getInstance(Class<T> clazz) {
    T t = null;
    try {
        t = clazz.newInstance();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    return t;
}

From source file:Main.java

public static <T> T converMap2Object(Map<String, String> map, Class<T> cls) {

    Field[] fields = cls.getDeclaredFields();

    T t = null;/*  www. j  a  v  a  2 s.co  m*/
    try {
        t = (T) cls.newInstance();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    for (Field f : fields) {
        f.setAccessible(true);
        invokeSet(t, f.getName(), map.get(f.getName()));
    }
    return t;
}

From source file:sintef.android.gravity.wizard.WizardTemplate.java

protected static <T extends WizardTemplate> T newInstance(Class<T> clazz, int resId, int position) {
    T f;// w  w w . j  a  v  a  2s . c o  m
    try {
        f = clazz.newInstance();
    } catch (java.lang.InstantiationException e) {
        e.printStackTrace();
        return null;
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        return null;
    }
    Bundle args = new Bundle();
    args.putInt(RES_ID, resId);
    args.putInt(POSITION, position);
    f.setArguments(args);
    return f;
}

From source file:Main.java

/**
 * This method loads fragment in a backstack.
 * //ww  w.  j a  v a 2 s  .  c om
 * @param fragmentActivity
 * @param fragmentContainerId
 * @param fragmentClass
 * @param bundle
 * @param tag
 * @return true if loaded successfully, false otherwise
 */
public static boolean loadFragmentInBackstack(FragmentActivity fragmentActivity, int fragmentContainerId,
        Class<? extends Fragment> fragmentClass, Bundle bundle, String tag) {
    // TODO Auto-generated method stub
    boolean status = false;
    try {
        FragmentManager fragmentManager = fragmentActivity.getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out,
                android.R.anim.fade_in, android.R.anim.fade_out);
        Fragment fragment = fragmentClass.newInstance();
        fragment.setArguments(bundle);
        fragmentTransaction.replace(fragmentContainerId, fragment, tag).addToBackStack(null).commit();
        // finish pending transactions
        fragmentManager.executePendingTransactions();
        status = true;
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return status;
}