KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > services > loader > ClassInfo


1 /*
2
3    Derby - Class org.apache.derby.iapi.services.loader.ClassInfo
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.services.loader;
23
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26
27 public class ClassInfo implements InstanceGetter {
28
29     private static final Class JavaDoc[] noParameters = new Class JavaDoc[0];
30     private static final Object JavaDoc[] noArguments = new Object JavaDoc[0];
31
32     private final Class JavaDoc clazz;
33     private boolean useConstructor = true;
34     private Constructor JavaDoc noArgConstructor;
35
36     public ClassInfo(Class JavaDoc clazz) {
37         this.clazz = clazz;
38     }
39
40     /**
41         Return the name of this class.
42     */

43     public final String JavaDoc getClassName() {
44         return clazz.getName();
45     }
46
47     /**
48         Return the class object for this class.
49
50     */

51     public final Class JavaDoc getClassObject() {
52
53         return clazz;
54     }
55
56     /**
57         Create an instance of this class. Assumes that clazz has already been
58         initialized. Optimizes Class.newInstance() by caching and using the
59         no-arg Constructor directly. Class.newInstance() looks up the constructor
60         each time.
61
62         @exception InstantiationException Zero arg constructor can not be executed
63         @exception IllegalAccessException Class or zero arg constructor is not public.
64         @exception InvocationTargetException Exception throw in zero-arg constructor.
65
66     */

67     public Object JavaDoc getNewInstance()
68         throws InstantiationException JavaDoc, IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
69
70         if (!useConstructor) {
71
72             return clazz.newInstance();
73         }
74
75         if (noArgConstructor == null) {
76
77             try {
78                 noArgConstructor = clazz.getConstructor(noParameters);
79
80             } catch (NoSuchMethodException JavaDoc nsme) {
81                 // let Class.newInstace() generate the exception
82
useConstructor = false;
83                 return getNewInstance();
84
85             } catch (SecurityException JavaDoc se) {
86                 // not allowed to to get a handle on the constructor
87
// just use the standard mechanism.
88
useConstructor = false;
89                 return getNewInstance();
90             }
91         }
92
93         try {
94             return noArgConstructor.newInstance(noArguments);
95         } catch (IllegalArgumentException JavaDoc iae) {
96             // can't happen since no arguments are passed.
97
return null;
98         }
99     }
100 }
101
Popular Tags