1 // Copyright (c) 2011  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 
7 /** The type of lazy values - i.e. ones that eventually produce values.
8  * This includes futures and promises.
9  */
10 
11 public class LazyType extends ObjectType
12 {
13     ClassType rawType;
14     Type valueType;
15 
getValueType()16     public Type getValueType() {
17         return valueType;
18     }
19 
getRawType()20     public Type getRawType() {
21         return rawType;
22     }
23 
LazyType(ClassType rawType, Type valueType)24     public LazyType(ClassType rawType, Type valueType) {
25         this.rawType = rawType;
26         this.valueType = valueType;
27     }
28 
29     ParameterizedType implementationType;
getImplementationType()30     public Type getImplementationType() {
31         if (implementationType == null) {
32             implementationType = new ParameterizedType(rawType, valueType);
33             implementationType.setTypeArgumentBound(0, '+');
34         }
35         return implementationType;
36     }
37 
getInstance(ClassType rawType, Type valueType)38     public static LazyType getInstance(ClassType rawType, Type valueType) {
39         return new LazyType(rawType, valueType);
40     }
41 
42     public static final ClassType lazyType = ClassType.make("gnu.mapping.Lazy");
43     public static final ClassType promiseType = ClassType.make("gnu.mapping.Promise");
44 
compare(Type other)45     public int compare(Type other) {
46 	return valueType.compare(other);
47     }
48 
getLazyType(Type valueType)49     public static LazyType getLazyType(Type valueType) {
50         return getInstance(lazyType, valueType);
51     }
52 
getPromiseType(Type valueType)53     public static LazyType getPromiseType(Type valueType) {
54         return getInstance(promiseType, valueType);
55     }
56 
toString()57     public String toString() {
58         return rawType.toString()+'['+valueType.toString()+']'; // FIXME
59     }
60 
61     /* MAYBE FUTURE use
62     public static Type maybeValueType(Type type) {
63         if (type instanceof LazyType)
64             return ((LazyType) type).getValueType();
65         Type itype = type.getImplementationType();
66         if (type != itype)
67             return maybeValueType(itype);
68         if (type instanceof ClassType) {
69             ClassType[] ifaces = ((ClassType) type).getInterfaces();
70             for (int i = ifaces.length; --i >= 0; ) {
71                 Type v = maybeValueType(ifaces[i]);
72                 if (v != null)
73                     return v;
74             }
75         }
76         return null;
77     }
78     */
79 
maybeLazy(Type type)80     public static boolean maybeLazy (Type type) {
81         type = type.getRawType();
82         if (type instanceof ClassType
83             && ((ClassType) type).implementsInterface(lazyType))
84             return true;
85         if (type == Type.objectType)
86             return true;
87         return false;
88     }
89 }
90