Java by API/java.lang/Field — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 14:40, 31 мая 2010

Field: getModifiers()

 
/* 
ArrayList, serialVersionUID, long, private static final
ArrayList, elementData, [Ljava.lang.Object;, private transient
ArrayList, size, int, private
Arrays, INSERTIONSORT_THRESHOLD, int, private static final
BitSet, ADDRESS_BITS_PER_UNIT, int, private static final
BitSet, BITS_PER_UNIT, int, private static final
BitSet, BIT_INDEX_MASK, int, private static final
BitSet, WORD_MASK, long, private static final
BitSet, bits, [J, private
BitSet, unitsInUse, int, private transient
BitSet, serialVersionUID, long, private static final
BitSet, trailingZeroTable, [B, private static final
Collections, BINARYSEARCH_THRESHOLD, int, private static final
Collections, REVERSE_THRESHOLD, int, private static final
Collections, SHUFFLE_THRESHOLD, int, private static final
Collections, FILL_THRESHOLD, int, private static final
Collections, ROTATE_THRESHOLD, int, private static final
Collections, COPY_THRESHOLD, int, private static final
Collections, REPLACEALL_THRESHOLD, int, private static final
Collections, INDEXOFSUBLIST_THRESHOLD, int, private static final
Collections, r, java.util.Random, private static
Collections, EMPTY_SET, java.util.Set, public static final
Collections, EMPTY_LIST, java.util.List, public static final
Collections, EMPTY_MAP, java.util.Map, public static final
Collections, REVERSE_ORDER, java.util.ruparator, private static final
HashMap, DEFAULT_INITIAL_CAPACITY, int, static final
HashMap, MAXIMUM_CAPACITY, int, static final
HashMap, DEFAULT_LOAD_FACTOR, float, static final
HashMap, table, [Ljava.util.HashMap$Entry;, transient
HashMap, size, int, transient
HashMap, threshold, int, 
HashMap, loadFactor, float, final
HashMap, modCount, int, transient volatile
HashMap, NULL_KEY, java.lang.Object, static final
HashMap, entrySet, java.util.Set, private transient
HashMap, serialVersionUID, long, private static final
HashSet, serialVersionUID, long, static final
HashSet, map, java.util.HashMap, private transient
HashSet, PRESENT, java.lang.Object, private static final
WeakHashMap, DEFAULT_INITIAL_CAPACITY, int, private static final
WeakHashMap, MAXIMUM_CAPACITY, int, private static final
WeakHashMap, DEFAULT_LOAD_FACTOR, float, private static final
WeakHashMap, table, [Ljava.util.WeakHashMap$Entry;, private
WeakHashMap, size, int, private
WeakHashMap, threshold, int, private
WeakHashMap, loadFactor, float, private final
WeakHashMap, queue, java.lang.ref.ReferenceQueue, private final
WeakHashMap, modCount, int, private volatile
WeakHashMap, NULL_KEY, java.lang.Object, private static final
WeakHashMap, entrySet, java.util.Set, private transient
 *
 **/
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class MainClass {
  public static void main(String args[]) throws Exception {
    String names[] = { "ArrayList", "Arrays", "BitSet", "Collection", "Collections", "Dictionary",
        "Enumeration", "HashMap", "HashSet", "WeakHashMap" };
    for (int i = 0, n = names.length; i < n; i++) {
      String className = "java.util." + names[i];
      Class theClass = Class.forName(className);
      Field fields[] = theClass.getDeclaredFields();
      for (int j = 0, m = fields.length; j < m; j++) {
        System.out.println(names[i] + ", " + fields[j].getName() + ", "
            + fields[j].getType().getName() + ", " + Modifier.toString(fields[j].getModifiers()));
      }
    }
  }
}





Field: getName()

 
/* 
ArrayList, serialVersionUID, long, private static final
ArrayList, elementData, [Ljava.lang.Object;, private transient
ArrayList, size, int, private
Arrays, INSERTIONSORT_THRESHOLD, int, private static final
BitSet, ADDRESS_BITS_PER_UNIT, int, private static final
BitSet, BITS_PER_UNIT, int, private static final
BitSet, BIT_INDEX_MASK, int, private static final
BitSet, WORD_MASK, long, private static final
BitSet, bits, [J, private
BitSet, unitsInUse, int, private transient
BitSet, serialVersionUID, long, private static final
BitSet, trailingZeroTable, [B, private static final
Collections, BINARYSEARCH_THRESHOLD, int, private static final
Collections, REVERSE_THRESHOLD, int, private static final
Collections, SHUFFLE_THRESHOLD, int, private static final
Collections, FILL_THRESHOLD, int, private static final
Collections, ROTATE_THRESHOLD, int, private static final
Collections, COPY_THRESHOLD, int, private static final
Collections, REPLACEALL_THRESHOLD, int, private static final
Collections, INDEXOFSUBLIST_THRESHOLD, int, private static final
Collections, r, java.util.Random, private static
Collections, EMPTY_SET, java.util.Set, public static final
Collections, EMPTY_LIST, java.util.List, public static final
Collections, EMPTY_MAP, java.util.Map, public static final
Collections, REVERSE_ORDER, java.util.ruparator, private static final
HashMap, DEFAULT_INITIAL_CAPACITY, int, static final
HashMap, MAXIMUM_CAPACITY, int, static final
HashMap, DEFAULT_LOAD_FACTOR, float, static final
HashMap, table, [Ljava.util.HashMap$Entry;, transient
HashMap, size, int, transient
HashMap, threshold, int, 
HashMap, loadFactor, float, final
HashMap, modCount, int, transient volatile
HashMap, NULL_KEY, java.lang.Object, static final
HashMap, entrySet, java.util.Set, private transient
HashMap, serialVersionUID, long, private static final
HashSet, serialVersionUID, long, static final
HashSet, map, java.util.HashMap, private transient
HashSet, PRESENT, java.lang.Object, private static final
WeakHashMap, DEFAULT_INITIAL_CAPACITY, int, private static final
WeakHashMap, MAXIMUM_CAPACITY, int, private static final
WeakHashMap, DEFAULT_LOAD_FACTOR, float, private static final
WeakHashMap, table, [Ljava.util.WeakHashMap$Entry;, private
WeakHashMap, size, int, private
WeakHashMap, threshold, int, private
WeakHashMap, loadFactor, float, private final
WeakHashMap, queue, java.lang.ref.ReferenceQueue, private final
WeakHashMap, modCount, int, private volatile
WeakHashMap, NULL_KEY, java.lang.Object, private static final
WeakHashMap, entrySet, java.util.Set, private transient
 *
 **/
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class MainClass {
  public static void main(String args[]) throws Exception {
    String names[] = { "ArrayList", "Arrays", "BitSet", "Collection", "Collections", "Dictionary",
        "Enumeration", "HashMap", "HashSet", "WeakHashMap" };
    for (int i = 0, n = names.length; i < n; i++) {
      String className = "java.util." + names[i];
      Class theClass = Class.forName(className);
      Field fields[] = theClass.getDeclaredFields();
      for (int j = 0, m = fields.length; j < m; j++) {
        System.out.println(names[i] + ", " + fields[j].getName() + ", "
            + fields[j].getType().getName() + ", " + Modifier.toString(fields[j].getModifiers()));
      }
    }
  }
}





Field: getType()

 
/* 
ArrayList, serialVersionUID, long, private static final
ArrayList, elementData, [Ljava.lang.Object;, private transient
ArrayList, size, int, private
Arrays, INSERTIONSORT_THRESHOLD, int, private static final
BitSet, ADDRESS_BITS_PER_UNIT, int, private static final
BitSet, BITS_PER_UNIT, int, private static final
BitSet, BIT_INDEX_MASK, int, private static final
BitSet, WORD_MASK, long, private static final
BitSet, bits, [J, private
BitSet, unitsInUse, int, private transient
BitSet, serialVersionUID, long, private static final
BitSet, trailingZeroTable, [B, private static final
Collections, BINARYSEARCH_THRESHOLD, int, private static final
Collections, REVERSE_THRESHOLD, int, private static final
Collections, SHUFFLE_THRESHOLD, int, private static final
Collections, FILL_THRESHOLD, int, private static final
Collections, ROTATE_THRESHOLD, int, private static final
Collections, COPY_THRESHOLD, int, private static final
Collections, REPLACEALL_THRESHOLD, int, private static final
Collections, INDEXOFSUBLIST_THRESHOLD, int, private static final
Collections, r, java.util.Random, private static
Collections, EMPTY_SET, java.util.Set, public static final
Collections, EMPTY_LIST, java.util.List, public static final
Collections, EMPTY_MAP, java.util.Map, public static final
Collections, REVERSE_ORDER, java.util.ruparator, private static final
HashMap, DEFAULT_INITIAL_CAPACITY, int, static final
HashMap, MAXIMUM_CAPACITY, int, static final
HashMap, DEFAULT_LOAD_FACTOR, float, static final
HashMap, table, [Ljava.util.HashMap$Entry;, transient
HashMap, size, int, transient
HashMap, threshold, int, 
HashMap, loadFactor, float, final
HashMap, modCount, int, transient volatile
HashMap, NULL_KEY, java.lang.Object, static final
HashMap, entrySet, java.util.Set, private transient
HashMap, serialVersionUID, long, private static final
HashSet, serialVersionUID, long, static final
HashSet, map, java.util.HashMap, private transient
HashSet, PRESENT, java.lang.Object, private static final
WeakHashMap, DEFAULT_INITIAL_CAPACITY, int, private static final
WeakHashMap, MAXIMUM_CAPACITY, int, private static final
WeakHashMap, DEFAULT_LOAD_FACTOR, float, private static final
WeakHashMap, table, [Ljava.util.WeakHashMap$Entry;, private
WeakHashMap, size, int, private
WeakHashMap, threshold, int, private
WeakHashMap, loadFactor, float, private final
WeakHashMap, queue, java.lang.ref.ReferenceQueue, private final
WeakHashMap, modCount, int, private volatile
WeakHashMap, NULL_KEY, java.lang.Object, private static final
WeakHashMap, entrySet, java.util.Set, private transient
 *
 **/
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
public class MainClass {
  public static void main(String args[]) throws Exception {
    String names[] = { "ArrayList", "Arrays", "BitSet", "Collection", "Collections", "Dictionary",
        "Enumeration", "HashMap", "HashSet", "WeakHashMap" };
    for (int i = 0, n = names.length; i < n; i++) {
      String className = "java.util." + names[i];
      Class theClass = Class.forName(className);
      Field fields[] = theClass.getDeclaredFields();
      for (int j = 0, m = fields.length; j < m; j++) {
        System.out.println(names[i] + ", " + fields[j].getName() + ", "
            + fields[j].getType().getName() + ", " + Modifier.toString(fields[j].getModifiers()));
      }
    }
  }
}





Field: setAccessible(boolean flag)

     
import java.lang.reflect.Field;
public class Main {
  public static void initIntFields(final Object obj) {
    try {
      Field[] fields = obj.getClass().getDeclaredFields();
      for (int idx = 0; idx < fields.length; idx++) {
        if (fields[idx].getType() == int.class) {
          fields[idx].setAccessible(true);
          fields[idx].setInt(obj, 0);
        }
      }
    } catch (final IllegalAccessException ex) {
      throw new RuntimeException(ex);
    }
  }
  public static final void main(final String[] args) {
    Integer value = new Integer("123");
    System.out.println("Before: " + value);
    initIntFields(value);
    System.out.println("After: " + value);
  }
}