Java examples for Reflection:Object
Tests if the given object is Immutable.
import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; import java.util.function.Function; import java.util.function.Predicate; public class Main{ public static void main(String[] argv) throws Exception{ Object object = "java2s.com"; System.out.println(isImmutable(object)); }/*w ww . j av a2 s. c o m*/ private static final Set<Class> IMMUTABLE_CLASSES = Collections .unmodifiableSet(new HashSet<Class>(Arrays.asList( Boolean.class, Byte.class, Short.class, Integer.class, Long.class, Float.class, Double.class, Class.class, BigInteger.class, BigDecimal.class, Currency.class))); /** /** * Tests if the given object is Immutable. An object is considered Immutable either: <ol> * <li>One of the following * classes in the Standard Java Library: .</li> * <li>Marked with the org.paritybits.pantheon.common.Immutable<li> annotation. * @param object The object under test. * @return True if immutable * @throws NullPointerException If object is null. */ public static boolean isImmutable(final Object object) { Class objClass = object.getClass(); return IMMUTABLE_CLASSES.contains(objClass) || objClass.isAnnotationPresent(Immutable.class) || objClass.isAnnotationPresent(Idempotent.class); } /** * Allows a contains test to be made for any type of Iterable object. If the iterable object * is a collection the contains method will be used, otherwise a naive iteration over * all the items will be used. * * @param iterable The iterable object that could contain the element * @param o element whose presence in this collection is to be tested. * @param <T> The type of object being used. * @return True if item was returned by the iterator. * @throws NullPointerException if iterable or item are null. */ public static <T> boolean contains(final Iterable<T> iterable, final T o) { if (iterable instanceof Collection) return ((Collection) iterable).contains(o); for (T t : iterable) if (t.equals(o)) return true; return false; } }