Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: javax/ide/util/MetaClass.java


1   package javax.ide.util;
2   
3   
4   /**
5    * A <code>MetaClass</code> describes a real {@link Class} with the purpose
6    * of providing to an IDE class level information, and delaying the loading of 
7    * that class to the last possible moment: when an instance of the class is
8    * required.<p>
9    *
10   * A <code>MetaClass</code> binds the {@link Class} object from its class name 
11   * using the appropriate class loader. Once the class is bound, new instances
12   * can be created using the {@link #newInstance} method.
13   */
14  public final class MetaClass 
15  {
16    private String _className;
17    private ClassLoader _classLoader;
18    
19    /**
20     * Construct a meta class for the specified class name.
21     * 
22     * @param classLoader the class loader to load the class from. Must not be 
23     *    null.
24     * @param className the fully qualified name of the class. Must not be null.
25     */
26    public MetaClass( ClassLoader classLoader, String className )
27    {
28      if ( classLoader == null )
29      {
30        throw new NullPointerException( "null classLoader" );
31      }
32      if ( className == null )
33      {
34        throw new NullPointerException( "null className" );
35      }
36      _classLoader = classLoader;
37      _className = className;    
38    }
39  
40    /**
41     * Get the fully qualified class name. This name is used to create the
42     * real {@link Class} instance.
43     * 
44     * @return The fully qualified class name.
45     */
46    public String getClassName()
47    {
48      return _className;
49    }
50    
51    /**
52     * Get the classloader for this meta class.
53     * 
54     * @return the class loader in which this meta class must be loaded.
55     */
56    ClassLoader getClassLoader()
57    {
58      return _classLoader;
59    }
60  
61    /**
62     * Build the {@link Class} object from the class name. Uses the right
63     * class loader in doing so.
64     * 
65     * @return The real {@link Class} object.
66     * @throws ClassNotFoundException if the class was not found.
67     */
68    public Class toClass() throws ClassNotFoundException
69    {
70      return Class.forName( _className, true, _classLoader );
71    }
72  
73    /**
74     * Creates a new instance of the class represented by this 
75     * <code>MetaClass</code> object.
76     *
77     * @return A newly created instance.
78     *
79     * @exception  IllegalAccessException  if the {@link Class} or its
80     * initializer is not accessible.
81     *
82     * @exception  InstantiationException  if the {@link Class} is an
83     * abstract class, an interface, an array class, a primitive type, or
84     * void; or if the instantiation fails for some other reason.
85     *
86     * @exception ClassNotFoundException if the {@link Class} is not found
87     * using the <code>MetaClass</code> name.
88     */
89    public Object newInstance() throws InstantiationException, 
90                                       IllegalAccessException,
91                                       ClassNotFoundException
92    {
93      return toClass().newInstance();
94    }
95  
96    /*-
97     * Object method.
98     */
99    public String toString()
100   {
101     return "MetaClass[ classLoader="+_classLoader+", className="+_className+"]";
102   }
103 
104   /*-
105    * Object method.
106    */
107   public int hashCode()
108   {
109     int hash = 42;
110     hash = 37 * hash + _className.hashCode();
111     hash = 37 * hash + _classLoader.hashCode();
112     
113     return hash;
114   }
115 
116   /*-
117    * Object method.
118    */
119   public boolean equals( Object object )
120   {
121     if ( object == this )
122     {
123       return true;
124     }
125     if ( !(object instanceof MetaClass) )
126     {
127       return false;
128     }
129     MetaClass mc = (MetaClass) object;
130     
131     return mc._classLoader.equals( this._classLoader ) &&
132            mc._className.equals( this._className );
133   }
134 }