blob: 218a31ccecc9e33019367968af1621b66f06768b [file] [log] [blame]
Stuart McCulloch26e7a5a2011-10-17 10:31:43 +00001package aQute.bnd.compatibility;
2
3import java.io.*;
4import java.util.*;
5
6public class Scope {
7 final Map<String, Scope> children = new LinkedHashMap<String, Scope>();
8
9 // class: slashed name
10 // field: name ":" typed
11 // constructor: ":(" typed* ")" typed
12 // method: name ":(" typed* ")" typed
13 final String name;
14
15 Access access;
16 Kind kind;
17 Scope enclosing;
18 Scope declaring;
19 GenericParameter typeVars[];
20 Map<String, String[]> name2bounds;
21
22 // class: super
23 // field: type
24 // constructor: void
25 // method: return
26 GenericType base;
27
28 // class: interfaces
29 // constructor: args
30 // method: args
31 GenericType[] parameters;
32
33 // constructor: exceptions
34 // method: exceptions
35 GenericType[] exceptions;
36
37 // class: super interfaces*
38 // field: type
39 // constructor: void arguments*
40 // method: return arguments*
41
42 public Scope(Access access, Kind kind, String name) {
43 this.access = access;
44 this.kind = kind;
45 this.name = name;
46 }
47
48 Scope getScope(String name) {
49 Scope s = children.get(name);
50 if (s != null)
51 return s;
52
53 s = new Scope(Access.UNKNOWN, Kind.UNKNOWN, name);
54 children.put(name, s);
55 s.declaring = this;
56 return s;
57 }
58
59 public void setParameterTypes(GenericType[] convert) {
60 this.parameters = convert;
61 }
62
63 public void setExceptionTypes(GenericType[] convert) {
64 this.exceptions = convert;
65 }
66
67 public void setBase(GenericType typeSignature) {
68 base = typeSignature;
69 }
70
71 public String toString() {
72 StringBuilder sb = new StringBuilder();
73
74 if ( typeVars != null && typeVars.length !=0) {
75 sb.append("<");
76 for ( GenericParameter v : typeVars) {
77 sb.append(v);
78 }
79 sb.append(">");
80 }
81 sb.append(access.toString());
82 sb.append(" ");
83 sb.append(kind.toString());
84 sb.append( " ");
85 sb.append( name );
86 return sb.toString();
87 }
88
89 public void report(Appendable a, int indent) throws IOException {
90 for (int i = 0; i < indent; i++)
91 a.append(" ");
92 a.append(toString());
93 a.append("\n");
94 for (Scope s : children.values())
95 s.report(a, indent + 1);
96 }
97
98 public void add(Scope m) {
99 children.put(m.name, m);
100
101 }
102
103 public void setDeclaring(Scope declaring) {
104 this.declaring = declaring;
105 }
106
107 public void setAccess(Access access) {
108 this.access = access;
109 }
110
111 public void setEnclosing(Scope enclosing) {
112 this.enclosing = enclosing;
113 if (this.enclosing != null) {
114 this.enclosing.add(this);
115 }
116 }
117
118 public boolean isTop() {
119 return enclosing == null;
120 }
121
122 public void setKind(Kind kind) {
123 this.kind = kind;
124 }
125
126 static public String classIdentity(String name) {
127 return name.replace('.', '/');
128 }
129
130 static public String methodIdentity(String name, String descriptor) {
131 return name + ":" + descriptor;
132 }
133
134 static public String constructorIdentity(String descriptor) {
135 return ":" + descriptor;
136 }
137
138 static public String fieldIdentity(String name, String descriptor) {
139 return name + ":" + descriptor;
140 }
141
142 public void cleanRoot() {
143 Iterator<Map.Entry<String, Scope>> i = children.entrySet().iterator();
144 while (i.hasNext()) {
145 Map.Entry<String, Scope> entry = i.next();
146 if (!entry.getValue().isTop())
147 i.remove();
148 }
149 }
150
151 public void prune(EnumSet<Access> level) {
152 Iterator<Map.Entry<String, Scope>> i = children.entrySet().iterator();
153 while (i.hasNext()) {
154 Map.Entry<String, Scope> entry = i.next();
155 if (!level.contains(entry.getValue().access))
156 i.remove();
157 else
158 entry.getValue().prune(level);
159 }
160 }
161
162 public void setGenericParameter(GenericParameter[] typeVars) {
163 this.typeVars = typeVars;
164 }
165
166}