001    /*
002     * LAPIS lightweight structured text processing system
003     *
004     * Copyright (C) 1998-2002 Carnegie Mellon University,
005     * Copyright (C) 2003 Massachusetts Institute of Technology.
006     * All rights reserved.
007     *
008     * This library is free software; you can redistribute it
009     * and/or modify it under the terms of the GNU General
010     * Public License as published by the Free Software
011     * Foundation, version 2.
012     *
013     * LAPIS homepage: http://graphics.lcs.mit.edu/lapis/
014     */
015    
016    
017    package lapisx.util;
018    
019    import java.lang.reflect.*;
020    
021    /**
022     * Handy reflection routines.
023     */
024    public abstract class Reflect {
025        /**
026         * Create a new instance of a class by calling a constructor with 
027         * arguments.
028         */
029        public static Object newInstance (String className, 
030                                          Class[] signature,
031                                          Object[] args) 
032            throws Exception {
033            
034            Class cls = Class.forName (className);
035            Constructor constructor = cls.getConstructor (signature);
036            return constructor.newInstance (args);
037        }
038    
039        /**
040         * Call a method of an object.
041         */
042        public static Object callMethod (Object obj, 
043                                         String methodName,
044                                         Class[] signature,
045                                         Object[] args) 
046            throws Exception {
047    
048            Class cls = obj.getClass ();
049            Method method = cls.getMethod (methodName, signature);
050            return method.invoke (obj, args);
051        }
052    
053        /**
054         * Call a static method of a class.
055         */
056        public static Object callStaticMethod (String className, 
057                                               String methodName,
058                                               Class[] signature,
059                                               Object[] args) 
060            throws Exception {
061            
062            Class cls = Class.forName (className);
063            Method method = cls.getMethod (methodName, signature);
064            return method.invoke (cls, args);
065        }
066    
067        /**
068         * Fetch a static field from a class.
069         */
070        public static Object getStaticField (String className, 
071                                             String fieldName) 
072            throws Exception {
073            
074            Class cls = Class.forName (className);
075            Field fld = cls.getField (fieldName);
076    
077            if (!Modifier.isStatic (fld.getModifiers ()))
078                throw new NoSuchFieldException ();
079    
080            return fld.get (null);
081        }
082    }