KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > bytecode > ObjectType


1 // Copyright (c) 1997, 2000 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.bytecode;
5
6 /**
7   * Semi-abstract class object reference types.
8   * <p>
9   * Extended by ClassType and ArrayType. */

10
11 public class ObjectType extends Type
12 {
13   protected ObjectType ()
14   {
15     size = 4;
16   }
17
18   public ObjectType (String JavaDoc name)
19   {
20     this_name = name;
21     size = 4;
22   }
23
24   // Miscellaneous bits:
25
final static int ADD_FIELDS_DONE = 1;
26   final static int ADD_METHODS_DONE = 2;
27   // A ClassType that we can expect to have a corresponding reflectClass.
28
final static int EXISTING_CLASS = 4;
29
30   final static int HAS_OUTER_LINK = 8;
31
32   /* */ public int flags;
33
34   public final boolean isExisting()
35   {
36     return (flags & EXISTING_CLASS) != 0;
37   }
38
39   public final void setExisting(boolean existing)
40   {
41     if (existing) flags |= EXISTING_CLASS;
42     else flags &= ~ EXISTING_CLASS;
43   }
44
45   /** Returns class name if a class type, signature if an array type.
46    * In both cases, uses '/' rather than '.' after packages prefixes.
47    * Seems rather arbitrary - but that is how classes are represented
48    * in the constant pool (CONSTANT_Class constants).
49    * Also, Class.forName is the same, except using '.'.
50    */

51   public String JavaDoc getInternalName()
52   {
53     return getName().replace('.', '/');
54   }
55
56   /** Get the java.lang.Class object for the representation type. */
57   public Class JavaDoc getReflectClass()
58   {
59     try
60       {
61     if (reflectClass == null)
62       {
63             String JavaDoc cname = getInternalName().replace('/', '.');
64         /* #ifdef JAVA2 */
65         /* Specifies optional 'initialize' argument. */
66         reflectClass = Class.forName(cname,
67                                          false, getClass().getClassLoader());
68         /* #else */
69         // reflectClass = Class.forName(cname);
70
/* #endif */
71       }
72         flags |= EXISTING_CLASS;
73       }
74     catch (java.lang.ClassNotFoundException JavaDoc ex)
75       {
76         if ((flags & EXISTING_CLASS) != 0)
77           {
78         RuntimeException JavaDoc rex
79               = new RuntimeException JavaDoc("no such class: "+getName());
80             /* #ifdef use:java.lang.Throwable.getCause */
81             rex.initCause(ex);
82             /* #endif */
83             throw rex;
84           }
85       }
86     return reflectClass;
87   }
88
89   public Type getImplementationType()
90   {
91     return this == nullType ? pointer_type
92       : this == tostring_type ? string_type : this;
93   }
94
95   public Type promote ()
96   {
97     return this == nullType ? pointer_type : this;
98   }
99
100   public boolean isInstance (Object JavaDoc obj)
101   {
102     if (this == nullType)
103       return obj == null;
104     return super.isInstance(obj);
105   }
106
107   public int compare(Type other)
108   {
109     // Assume this == nullType.
110
return other == nullType ? 0 : -1;
111   }
112
113   /* #ifdef JAVA5 */
114   // @SuppressWarnings("unchecked")
115
/* #endif */
116   /** Convert an object to a value of this Type.
117    * Throw a ClassCastException when this is not possible. */

118   public Object JavaDoc coerceFromObject (Object JavaDoc obj)
119   {
120     if (obj != null)
121       {
122     if (this == Type.tostring_type)
123       return obj.toString();
124         Class JavaDoc clas = getReflectClass();
125         Class JavaDoc objClass = obj.getClass();
126         if (! clas.isAssignableFrom(objClass))
127           throw new ClassCastException JavaDoc("don't know how to coerce "
128                                        + objClass.getName() + " to "
129                                        + getName());
130       }
131     return obj;
132   }
133
134   /** Compile (in given method) cast from Object to this Type. */
135   public void emitCoerceFromObject (CodeAttr code)
136   {
137     if (this == Type.tostring_type)
138       {
139     // This would be nice but it doesn't verify, alas!
140
// code.reserve(4);
141
// code.emitDup();
142
// code.put1(198); // ifnull
143
// code.put2(6); // skip after emitInvokeVirtual.
144
// code.emitInvokeVirtual(Type.toString_method);
145
code.emitDup();
146     code.emitIfNull();
147     code.emitPop(1);
148     code.emitPushNull();
149     code.emitElse();
150     code.emitInvokeVirtual(Type.toString_method);
151     code.emitFi();
152       }
153     else if (this != Type.pointer_type)
154       code.emitCheckcast(this);
155   }
156 }
157
Popular Tags