01: /**************************************************************************/
02: /* N I C E */
03: /* A high-level object-oriented research language */
04: /* (c) Daniel Bonniot 2002 */
05: /* */
06: /* This program is free software; you can redistribute it and/or modify */
07: /* it under the terms of the GNU General Public License as published by */
08: /* the Free Software Foundation; either version 2 of the License, or */
09: /* (at your option) any later version. */
10: /* */
11: /**************************************************************************/package gnu.bytecode;
12:
13: import java.util.Stack;
14:
15: /**
16: A Generic Java / JDK 1.5 type variable.
17:
18: @version $Date: 2002/09/09 12:59:26 $
19: @author Daniel Bonniot (bonniot@users.sourceforge.net)
20: */
21:
22: public class TypeVariable extends ObjectType {
23: TypeVariable(String name, Type bound) {
24: super (name);
25: this .bound = bound;
26: }
27:
28: public Type bound;
29:
30: public String toString() {
31: return "Type variable " + getName();
32: }
33:
34: static TypeVariable[] none = new TypeVariable[0];
35:
36: static TypeVariable[] parse(String signature, int[] pos) {
37: // Skip the '<'
38: pos[0]++;
39:
40: Stack vars = new Stack();
41: do {
42: int colon = signature.indexOf(':', pos[0]);
43: String name = signature.substring(pos[0], colon);
44:
45: pos[0] = colon + 1;
46: Type bound = Type.fullSignatureToType(signature, pos);
47:
48: vars.push(new TypeVariable(name, bound));
49: } while (signature.charAt(pos[0]) != '>');
50: // Skip the '>'
51: pos[0]++;
52:
53: TypeVariable[] res = new TypeVariable[vars.size()];
54: for (int i = vars.size(); --i >= 0;)
55: res[i] = (TypeVariable) vars.pop();
56:
57: return res;
58: }
59:
60: /****************************************************************
61: * Scoping
62: ****************************************************************/
63:
64: /**
65: Type variables are lookup up from at most two scopes.
66: The first one is typically a method's type parameters.
67: The second one the the englobing class'.
68: */
69: static TypeVariable[] scope1;
70: static TypeVariable[] scope2;
71:
72: static TypeVariable lookup(String name) {
73: TypeVariable res = lookup(name, scope1);
74: if (res != null)
75: return res;
76: return lookup(name, scope2);
77: }
78:
79: static TypeVariable lookup(String name, TypeVariable[] scope) {
80: if (scope != null)
81: for (int i = 0; i < scope.length; i++)
82: if (scope[i].this_name.equals(name))
83: return scope[i];
84:
85: return null;
86: }
87: }
|