Java reflection Methods

1, the constructor method to give

  1. Constructor getConstructor (Class [] params) - parameters obtained using special types of public constructors,
  2. Constructor [] getConstructors () - all obtained class public constructors
  3. Constructor getDeclaredConstructor (Class [] params) - Constructor obtained using type specific parameters (regardless of access priority)
  4. Constructor [] getDeclaredConstructors () - all the constructors class obtained (regardless of access priority)

2, the method of obtaining field information

  1. Field getField (String name) - Public obtain field named
  2. Field [] getFields () - get all the public fields of the class
  3. Field getDeclaredField (String name) - get the class declaration of the named field
  4. Field [] getDeclaredFields () - get all the fields declared by the class

3. A method for obtaining information

  1. Method getMethod (String name, Class [] params) - type specific parameters used to obtain public method named
  2. Method [] getMethods () - get all public methods in the class
  3. Method getDeclaredMethod (String name, Class [] params) - close-up parameter types, the class declaration is obtained a method of naming
  4. Method [] getDeclaredMethods () - get all methods declared by the class
Summary: ======== >>>>>>>> with DeclaredMethod acquisition method of the present kind of private field does not include a value related to the parent, the method can only be obtained with no public class fields according to the present method, parent field and related method

4: method to get annotated

The Annotation 1. [] Annotations = (the Annotation []) class1.getAnnotations (); // Get all annotations class object 
2. Annotation the Annotation = (the Annotation) class1.getAnnotation (Deprecated.class); // Get the specified class object annotation
genericSuperclass class1.getGenericSuperclass the type = 3. (); // get the object class direct superclass
4. the type the type [] = interfaceTypes class1.getGenericInterfaces (); type // set all the acquired object class interfaces

5: other expansion method

  1. boolean isPrimitive = class1.isPrimitive (); // determines whether base-type
  2. boolean isArray = class1.isArray (); // determines whether collections
  3. boolean isAnnotation = class1.isAnnotation (); // determines whether the annotation class
  4. boolean isInterface = class1.isInterface (); // determines whether the interface class
  5. boolean isEnum = class1.isEnum (); // determines whether an enumeration class
  6. boolean isAnonymousClass = class1.isAnonymousClass (); // determines whether anonymous inner classes
  7. boolean isAnnotationPresent = class1.isAnnotationPresent (Deprecated.class); // determines whether a modified class annotated
  8. String className = class1.getName (); // get class names include the package name path
  9. Package aPackage = class1.getPackage (); // class of acquiring package information
  10. String simpleName = class1.getSimpleName (); // get class class name
  11. int modifiers = class1.getModifiers (); // get access to class
  12. Class<?>[] declaredClasses = class1.getDeclaredClasses();//内部类
  13. Class<?> declaringClass = class1.getDeclaringClass();//外部类
  14. getSuperclass (): Gets the class of the parent class
  15. getInterfaces (): Gets an interface implemented certain

6. type having

  • ParameterizedType: shows a parameterized type, such as Collection <String>
  • GenericArrayType: represents an element type is an array type parameters or type of variable types
  • TypeVariable: the interface is the common parent of all types of variables
  • WildcardType: represents a wildcard type expression, such as? ,? extends Number ,? super Integer. (Wildcard is a word: is the "wildcard")

 

 

Guess you like

Origin www.cnblogs.com/dgwblog/p/11701256.html