KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > kawa > reflect > SingletonType


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

4 package gnu.kawa.reflect;
5 import gnu.bytecode.*;
6 import gnu.mapping.*;
7 import gnu.expr.*;
8
9 public class SingletonType extends ObjectType // implements TypeValue
10
{
11   static final SingletonType instance = new SingletonType("singleton");
12
13   public SingletonType (String JavaDoc name)
14   {
15     super(name);
16   }
17
18   public static final SingletonType getInstance () { return instance; }
19
20   public java.lang.Class JavaDoc getReflectClass()
21   {
22     return getImplementationType().getReflectClass();
23   }
24
25   public Type getImplementationType ()
26   {
27     return Type.pointer_type;
28   }
29
30   public int compare(Type other)
31   {
32     int otherRange = OccurrenceType.itemCountRange(other);
33     int otherMin = otherRange & 0xfff;
34     int otherMax = otherRange >> 12;
35     if (otherMax == 0 || otherMin > 1)
36       return -3;
37     if (otherMin == 1 && otherMax == 1)
38       return Type.pointer_type.compare(other);
39     int cmp = Type.pointer_type.compare(other);
40     if (cmp == 0 || cmp == -1)
41       return -1;
42     return -2;
43   }
44
45   public static Object JavaDoc coerceToSingleton (Object JavaDoc obj)
46   {
47     if (obj instanceof Values)
48       obj = ((Values) obj).canonicalize();
49     if (obj == null || obj instanceof Values)
50       throw new ClassCastException JavaDoc("value if not a singleton");
51     return obj;
52   }
53
54   public Object JavaDoc coerceFromObject (Object JavaDoc obj)
55   {
56     return coerceToSingleton(obj);
57   }
58
59   public void emitCoerceFromObject (CodeAttr code)
60   {
61     code.emitInvokeStatic(ClassType.make("gnu.kawa.reflect.SingletonType")
62                           .getDeclaredMethod("coerceToSingleton", 1));
63   }
64
65   public boolean isInstance (Object JavaDoc obj)
66   {
67     return obj != null && ! (obj instanceof Values);
68   }
69 }
70
Popular Tags