View Javadoc

1   // Constructor.java, created Thu Jul  4  4:50:03 2002 by joewhaley
2   // Copyright (C) 2001-3 John Whaley <jwhaley@alum.mit.edu>
3   // Licensed under the terms of the GNU LGPL; see COPYING for details.
4   package joeq.ClassLib.Common.java.lang.reflect;
5   
6   import joeq.Class.jq_Class;
7   import joeq.Class.jq_Initializer;
8   import joeq.Class.jq_NameAndDesc;
9   import joeq.Class.jq_Type;
10  import joeq.ClassLib.ClassLibInterface;
11  import joeq.ClassLib.Common.ClassUtils;
12  import joeq.Main.jq;
13  import joeq.Runtime.Reflection;
14  import joeq.UTF.Utf8;
15  import jwutil.util.Assert;
16  
17  /***
18   * Constructor
19   *
20   * @author  John Whaley <jwhaley@alum.mit.edu>
21   * @version $Id: Constructor.java 1941 2004-09-30 03:37:06Z joewhaley $
22   */
23  public class Constructor extends AccessibleObject {
24  
25      // additional instance field.
26      public final jq_Initializer jq_init;
27      
28      private java.lang.Class clazz;
29      private java.lang.Class[] parameterTypes;
30      private java.lang.Class[] exceptionTypes;
31      private int modifiers;
32      private int slot;
33      
34      private Constructor() {
35          Assert.UNREACHABLE();
36          this.jq_init = null;
37      }
38      
39      private Constructor(jq_Initializer i) {
40          this.jq_init = i;
41      }
42      private Constructor(java.lang.Class clazz,
43                          java.lang.Class[] parameterTypes,
44                          java.lang.Class[] exceptionTypes,
45                          int modifiers,
46                          int slot) {
47          this.clazz = clazz;
48          this.parameterTypes = parameterTypes;
49          this.exceptionTypes = exceptionTypes;
50          this.modifiers = modifiers;
51          this.slot = slot;
52          jq_Class k = (jq_Class) ClassLibInterface.DEFAULT.getJQType(clazz);
53          
54          StringBuffer desc = new StringBuffer();
55          desc.append('(');
56          for (int i=0; i<parameterTypes.length; ++i) {
57              desc.append(Reflection.getJQType(parameterTypes[i]).getDesc().toString());
58          }
59          desc.append(")V");
60          jq_NameAndDesc nd = new jq_NameAndDesc(Utf8.get("<init>"), Utf8.get(desc.toString()));
61          nd = joeq.ClassLib.ClassLibInterface.convertClassLibNameAndDesc(k, nd);
62          jq_Initializer init = (jq_Initializer) k.getDeclaredMember(nd);
63          if (init == null) {
64              init = (jq_Initializer) k.getOrCreateInstanceMethod(nd);
65          }
66          this.jq_init = init;
67      }
68      
69      public java.lang.Object newInstance(java.lang.Object[] initargs)
70          throws java.lang.InstantiationException, java.lang.IllegalAccessException,
71                 java.lang.IllegalArgumentException, java.lang.reflect.InvocationTargetException
72      {
73          jq_Initializer jq_i = this.jq_init;
74          jq_Class k = jq_i.getDeclaringClass();
75          if (k.isAbstract()) throw new InstantiationException();
76          if (!this.isAccessible()) ClassUtils.checkCallerAccess(jq_i, 2);
77          jq_Type[] argtypes = jq_i.getParamTypes();
78          int nargs = initargs == null ? 0 : initargs.length;
79          if (nargs != argtypes.length-1)
80              throw new java.lang.IllegalArgumentException("Constructor takes "+(argtypes.length-1)+" arguments, but "+nargs+" arguments passed in");
81          Object o = k.newInstance();
82          Reflection.invoke(jq_i, o, initargs);
83          return o;
84      }
85      
86      // additional methods.
87      // ONLY TO BE CALLED BY jq_Member CONSTRUCTOR!!!
88      public static java.lang.reflect.Constructor createNewConstructor(jq_Initializer jq_init) {
89          Object o = new Constructor(jq_init);
90          return (java.lang.reflect.Constructor)o;
91      }
92      
93      public static void initNewConstructor(Constructor o, jq_Initializer jq_init) {
94          if (!jq.RunningNative) return;
95          Assert._assert(jq_init == o.jq_init);
96          java.lang.Class clazz = jq_init.getDeclaringClass().getJavaLangClassObject();
97          o.clazz = clazz;
98          jq_Type[] paramTypes = jq_init.getParamTypes();
99          java.lang.Class[] parameterTypes = new java.lang.Class[paramTypes.length-1];
100         for (int i=1; i<paramTypes.length; ++i) {
101             parameterTypes[i-1] = Reflection.getJDKType(paramTypes[i]);
102         }
103         o.parameterTypes = parameterTypes;
104         // TODO: exception types
105         java.lang.Class[] exceptionTypes = new java.lang.Class[0];
106         o.exceptionTypes = exceptionTypes;
107         int modifiers = jq_init.getAccessFlags();
108         o.modifiers = modifiers;
109     }
110 }