KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > lang > reflect > Method

java.lang.reflect
Class Method

java.lang.Object
  extended by java.lang.reflect.AccessibleObject
      extended by java.lang.reflect.Method
All Implemented Interfaces:
AnnotatedElement, GenericDeclaration, Member
See Also:
Top Examples, Source Code, Class, Class.getMethods(), Class.getMethod(String, Class[]), Class.getDeclaredMethods(), Class.getDeclaredMethod(String Class[])

public boolean equals(Object obj)
See Also:
Hashtable, Object.hashCode()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
See Also:
AccessibleObject, AnnotatedElement
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Annotation[] getDeclaredAnnotations()
See Also:
AccessibleObject, AnnotatedElement
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Class<?> getDeclaringClass()
See Also:
Member
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Object getDefaultValue()
See Also:
Class, TypeNotPresentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Class<?>[] getExceptionTypes()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Type[] getGenericExceptionTypes()
See Also:
MalformedParameterizedTypeException, TypeNotPresentException, GenericSignatureFormatError
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Type[] getGenericParameterTypes()
See Also:
MalformedParameterizedTypeException, TypeNotPresentException, GenericSignatureFormatError
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Type getGenericReturnType()
See Also:
MalformedParameterizedTypeException, TypeNotPresentException, GenericSignatureFormatError
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int getModifiers()
See Also:
Modifier, Member
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String getName()
See Also:
Member
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Annotation[][] getParameterAnnotations()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Class<?>[] getParameterTypes()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Class<?> getReturnType()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public TypeVariable<Method>[] getTypeParameters()
See Also:
GenericSignatureFormatError, GenericDeclaration
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int hashCode()
See Also:
Hashtable, Object.equals(java.lang.Object)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Object invoke(Object obj,
                     Object... args)
              throws IllegalAccessException,
                     IllegalArgumentException,
                     InvocationTargetException
See Also:
ExceptionInInitializerError, NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Object invoke(Object obj,
                     Object[] args)
              throws IllegalAccessException,
                     IllegalArgumentException,
                     InvocationTargetException
See Also:
ExceptionInInitializerError, NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[119]Invoke zero-argument method through reflection
By Anonymous on 2002/11/04 17:51:29  Rate
/** dispatch to a zero-argument method that matches the name of the  
   * action command in the ActionEvent object.  
 **/
 
  
  
 public void actionPerformed ( ActionEvent ev )   {  
   try  {  
          Method m = getClass (  ) .getMethod ( getActionCommand (  ) , new Class [ 0 ]  ) ; 
          m.invoke ( this,new Object [ 0 ]  ) ; 
    }  
   catch  ( Exception ex )   {  
    
    }  
  }  
 


[928]Demonstrate static methods through reflection
By Anonymous on 2004/09/25 22:22:55  Rate
import java.lang.reflect.Method; 
  
  
 /** 
  * Demonstrate static methods. 
  */
 
 public class Testsomething 
  {  
  
  
   public static void main (  String [  ]  args  )  throws Exception 
    {  
     TestMethod test = new TestSubMethod (  ) ; 
     test.doSomething (  ) ; //OUTPUT: TestMethod.doSomething 
      
     TestSubMethod sub = new TestSubMethod (  ) ; 
     sub.doSomething (  ) ; //OUTPUT: TestSubMethod.doSomething 
      
      (  ( TestMethod ) sub ) .doSomething (  ) ; //OUTPUT: TestMethod.doSomething 
  
  
     Method method = TestMethod.class.getDeclaredMethod ( "doSomething", null ) ; 
     method.invoke ( sub, null ) ; //OUTPUT: TestMethod.doSomething 
     method.invoke ( test, null ) ; //OUTPUT: TestMethod.doSomething 
     method.invoke ( null, null ) ; //OUTPUT: TestMethod.doSomething 
  
  
     Method subMethod = TestSubMethod.class.getDeclaredMethod ( "doSomething", null ) ; 
     subMethod.invoke ( sub, null ) ; //OUTPUT: TestSubMethod.doSomething 
     subMethod.invoke ( test, null ) ; //OUTPUT: TestSubMethod.doSomething 
     subMethod.invoke ( null, null ) ; //OUTPUT: TestSubMethod.doSomething 
    }  
  }  
  
  
 class TestMethod 
  {  
   static void doSomething (  )  
    {  
     System.out.println ( "TestMethod.doSomething (  ) " ) ; 
    }  
  }  
  
  
 class TestSubMethod extends TestMethod 
  {  
   static void doSomething (  )  
    {  
     System.out.println ( "TestSubMethod.doSomething" ) ; 
    }  
  }  
 


[1714]The use of reflection on a method with array type parameters
By Edson Wunderlich on 2006/02/17 16:12:54  Rate
import java.lang.reflect.Method; 
  
  
 /* 
  * This class demonstrates de use of reflection to invoke a method with array type parameters 
  * along with non-array type parameters 
  *  
  * Please pay attention to the 'hints'. They are the information that were not found in previous examples 
  * 
  */
 
  
  
 public class TestMethodReflection 
  {  
   public static void main ( String [  ]  args )  throws Exception 
    {  
     TestMethodReflection thisClass = new TestMethodReflection (  ) ; 
      
     String methodName; 
     Class [  ]   methodParametersList; 
     Object [  ]  methodParametersValues; 
     Method method; 
      
     /* hint */ 
     // Invoking a non-static method using reflection 
     // 
     methodName = "nonStaticMethod"; 
      
     /* hint: how to specify the parameters type */ 
     methodParametersList = new Class [  ]   { String [  ] .class, long.class } ;  
      
     System.out.println ( "--- Invoking a non-static method using reflection ---\n" ) ; 
      
     method = TestMethodReflection.class.getMethod ( methodName,methodParametersList ) ; 
      
     /* hint: how to pass the parameters values to the invoke method */ 
     methodParametersValues = new Object [  ]   { new String [  ]  { "Non-static method called using reflection", "2nd String of 3", "3rd String of 3" } , 2006 } ; 
      
     /* hint: as the 1st parameter, one has to pass an instance of 
      * the class whose non-static method is being called using reflection. 
      * 'thisClass' is this instance. 
      * So, something like TestMethodReflection.class wouldn't work. 
      * When calling a static method using reflection, this is not necessary. 
      * In this second case, virtually 'anything' may be passed as the 1st parameter for 'invoke'. 
      */
 
     method.invoke ( thisClass,methodParametersValues ) ; 
      
     // Invoking a static method using reflection 
     // 
     methodName = "staticMethod"; 
     methodParametersList = new Class [  ]   { String [  ] .class, long.class } ; 
     methodParametersValues = new Object [  ]   { new String [  ]  { "Static method called using reflection", "2nd String of 3", "3rd String of 3" } , 2006 } ; 
      
     method = TestMethodReflection.class.getMethod ( methodName,methodParametersList ) ; 
  
  
     System.out.println ( "\n--- Invoking a static method using reflection ---\n" ) ; 
      
     method.invoke   ( TestMethodReflection.class, methodParametersValues ) ; 
     //method.invoke ( new Object (  ) , methodParametersValues ) ; //works fine 
     //method.invoke ( 1, methodParametersValues ) ; //works perfectly 
     //method.invoke ( null, methodParametersValues ) ; //works just as fine 
        
        /* This is the expected output: 
         *  
         * --- Invoking a non-static method using reflection --- 
         *  
         * Method..........: "nonStaticMethod"  ( Non-static method called using reflection )  
         * Parameters types: strings [ 0..2 ]   ( Quantity=3 )  
         * Parameters types: long 
         * Parameter values: 1. Parameter string [ 0 ]  = "Non-static method called using reflection" 
         * Parameter values: 2. Parameter string [ 1 ]  = "2nd String of 3" 
         * Parameter values: 3. Parameter string [ 2 ]  = "3rd String of 3" 
         * Parameter values: 4. Parameter long..... = 2006 
         *  
         * --- Invoking a static method using reflection --- 
         *  
         * Method..........: "staticMethod"  ( Static method called using reflection )  
         * Parameters types: strings [ 0..2 ]   ( Quantity=3 )  
         * Parameters types: long 
         * Parameter values: 1. Parameter string [ 0 ]  = "Static method called using reflection" 
         * Parameter values: 2. Parameter string [ 1 ]  = "2nd String of 3" 
         * Parameter values: 3. Parameter string [ 2 ]  = "3rd String of 3" 
         * Parameter values: 4. Parameter long..... = 2006 
         * 
         */
 
     }  
    
   private static void processMethodParameters ( String callingMethod, String [  ]  strings, long longValue )  
    {  
     String ident = "Parameter values: "; 
      
     System.out.println ( "Method..........: \"" + callingMethod + "\"" +  (  ( strings.length  >  0 )  ? "  ( " + strings [ 0 ]  + " ) " : "" )   ) ; 
      
      
     System.out.println ( "Parameters types: strings [ 0.."+ ( strings.length-1 ) +" ]   ( Quantity=" + strings.length + " ) " ) ; 
     System.out.println ( "Parameters types: long" ) ; 
     for  ( int i = 0; i  <  strings.length; i++ )  
      {  
       System.out.println ( ident +  ( i + 1 )  + ". Parameter string [ " + i + " ]  = \"" + strings [ i ]  + "\"" ) ; 
      }  
     System.out.println ( ident +  ( strings.length + 1 )  + ". Parameter long..... = " + longValue ) ;     
    }  
    
   public static void staticMethod ( String [  ]  strings, long longValue )  
    {  
     processMethodParameters ( "staticMethod", strings, longValue ) ; 
    }  
    
   //non-static methos must be public if they are to be called using reflection 
   // 
   public void nonStaticMethod ( String [  ]  strings, long longValue )  
    {  
     processMethodParameters ( "nonStaticMethod", strings, longValue ) ; 
    }  
  }  
 


public boolean isBridge()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean isSynthetic()
See Also:
Member
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean isVarArgs()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String toGenericString()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String toString()
See Also:
Object
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags