01: // Copyright (c) 2006 Per M.A. Bothner.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.kawa.reflect;
05:
06: import gnu.bytecode.*;
07: import gnu.mapping.*;
08: import gnu.expr.*;
09:
10: public class SingletonType extends ObjectType // implements TypeValue
11: {
12: static final SingletonType instance = new SingletonType("singleton");
13:
14: public SingletonType(String name) {
15: super (name);
16: }
17:
18: public static final SingletonType getInstance() {
19: return instance;
20: }
21:
22: public java.lang.Class getReflectClass() {
23: return getImplementationType().getReflectClass();
24: }
25:
26: public Type getImplementationType() {
27: return Type.pointer_type;
28: }
29:
30: public int compare(Type other) {
31: int otherRange = OccurrenceType.itemCountRange(other);
32: int otherMin = otherRange & 0xfff;
33: int otherMax = otherRange >> 12;
34: if (otherMax == 0 || otherMin > 1)
35: return -3;
36: if (otherMin == 1 && otherMax == 1)
37: return Type.pointer_type.compare(other);
38: int cmp = Type.pointer_type.compare(other);
39: if (cmp == 0 || cmp == -1)
40: return -1;
41: return -2;
42: }
43:
44: public static Object coerceToSingleton(Object obj) {
45: if (obj instanceof Values)
46: obj = ((Values) obj).canonicalize();
47: if (obj == null || obj instanceof Values)
48: throw new ClassCastException("value is not a singleton");
49: return obj;
50: }
51:
52: public Object coerceFromObject(Object obj) {
53: return coerceToSingleton(obj);
54: }
55:
56: public void emitCoerceFromObject(CodeAttr code) {
57: code.emitInvokeStatic(ClassType.make(
58: "gnu.kawa.reflect.SingletonType").getDeclaredMethod(
59: "coerceToSingleton", 1));
60: }
61:
62: public boolean isInstance(Object obj) {
63: return obj != null && !(obj instanceof Values);
64: }
65: }
|