KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rcm > util > Reflect


1 /*
2  * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
18  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
21  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  */

30
31 package rcm.util;
32
33 import java.lang.reflect.*;
34
35 /**
36  * Handy reflection routines.
37  */

38 public abstract class Reflect {
39     /**
40      * Create a new instance of a class by calling a constructor with
41      * arguments.
42      */

43     public static Object JavaDoc newInstance (String JavaDoc className,
44                                       Class JavaDoc[] signature,
45                                       Object JavaDoc[] args)
46         throws Exception JavaDoc {
47         
48         Class JavaDoc cls = Class.forName (className);
49         Constructor constructor = cls.getConstructor (signature);
50         return constructor.newInstance (args);
51     }
52
53     /**
54      * Call a method of an object.
55      */

56     public static Object JavaDoc callMethod (Object JavaDoc obj,
57                                      String JavaDoc methodName,
58                                      Class JavaDoc[] signature,
59                                      Object JavaDoc[] args)
60         throws Exception JavaDoc {
61
62         Class JavaDoc cls = obj.getClass ();
63         Method method = cls.getMethod (methodName, signature);
64         return method.invoke (obj, args);
65     }
66
67     /**
68      * Call a static method of a class.
69      */

70     public static Object JavaDoc callStaticMethod (String JavaDoc className,
71                                            String JavaDoc methodName,
72                                            Class JavaDoc[] signature,
73                                            Object JavaDoc[] args)
74         throws Exception JavaDoc {
75         
76         Class JavaDoc cls = Class.forName (className);
77         Method method = cls.getMethod (methodName, signature);
78         return method.invoke (cls, args);
79     }
80 }
81
82
Popular Tags