-
Notifications
You must be signed in to change notification settings - Fork 960
/
GeneratedTypeBinding.java
213 lines (181 loc) · 5.4 KB
/
GeneratedTypeBinding.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* Copyright 2011 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.devtools.j2objc.jdt;
import com.google.common.collect.Sets;
import com.google.devtools.j2objc.util.BindingUtil;
import java.util.Set;
import javax.lang.model.element.PackageElement;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.IPackageBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.Modifier;
/**
* Binding class for types created during translation.
*
* @author Keith Stanger
*/
class GeneratedTypeBinding extends AbstractTypeBinding {
protected final String name;
private int modifiers;
private final PackageElement packageElement;
private final ITypeBinding superClass;
private final boolean isInterface;
private final ITypeBinding componentType;
private final Set<IVariableBinding> fields = Sets.newHashSet();
private final Set<IMethodBinding> methods = Sets.newHashSet();
public GeneratedTypeBinding(
String name, PackageElement packageElement, ITypeBinding superClass, boolean isInterface,
ITypeBinding componentType) {
this.name = name;
this.packageElement = packageElement;
this.superClass = superClass;
this.isInterface = isInterface;
this.componentType = componentType;
}
/**
* Creates a new non-array type, extracting the package from the provided name.
*/
public static GeneratedTypeBinding newTypeBinding(
String name, ITypeBinding superClass, boolean isInterface) {
int idx = name.lastIndexOf('.');
String packageName = idx < 0 ? "" : name.substring(0, idx);
PackageElement packageElement = (PackageElement)
BindingConverter.getElement(new GeneratedPackageBinding(packageName));
name = name.substring(idx + 1);
return new GeneratedTypeBinding(name, packageElement, superClass, isInterface, null);
}
public static GeneratedTypeBinding newArrayType(ITypeBinding componentType) {
return new GeneratedTypeBinding(
componentType.getName() + "[]", (PackageElement) null, null, false, componentType);
}
@Override
public String getKey() {
return name;
}
@Override
public boolean isEqualTo(IBinding binding) {
if (binding == this) {
return true;
}
if (binding instanceof GeneratedTypeBinding) {
return name.equals(((GeneratedTypeBinding) binding).name);
}
return false;
}
@Override
public String getBinaryName() {
return getQualifiedName();
}
@Override
public String getName() {
return name;
}
@Override
public int getModifiers() {
return modifiers;
}
public void setModifiers(int newModifiers) {
modifiers = newModifiers;
}
public void addModifiers(int modifiersToAdd) {
modifiers |= modifiersToAdd;
}
@Override
public String getQualifiedName() {
if (packageElement != null) {
return packageElement.getQualifiedName() + "." + name;
}
return name;
}
@Override
public boolean isArray() {
return componentType != null;
}
@Override
public boolean isClass() {
return !isInterface;
}
@Override
public boolean isInterface() {
return isInterface;
}
@Override
public ITypeBinding getComponentType() {
return componentType;
}
@Override
public IVariableBinding[] getDeclaredFields() {
return fields.toArray(new IVariableBinding[0]);
}
public void addField(IVariableBinding field) {
fields.add(field);
}
@Override
public IMethodBinding[] getDeclaredMethods() {
return methods.toArray(new IMethodBinding[0]);
}
public void addMethod(IMethodBinding method) {
methods.add(method);
}
@Override
public int getDeclaredModifiers() {
return Modifier.PUBLIC;
}
@Override
public int getDimensions() {
if (componentType != null) {
return componentType.getDimensions() + 1;
}
return 0;
}
@Override
public ITypeBinding getElementType() {
ITypeBinding elementType = componentType;
while (elementType != null && elementType.isArray()) {
elementType = elementType.getComponentType();
}
return elementType;
}
@Override
public IPackageBinding getPackage() {
return (IPackageBinding) BindingConverter.unwrapElement(packageElement);
}
@Override
public ITypeBinding getSuperclass() {
return superClass;
}
@Override
public boolean isAssignmentCompatible(ITypeBinding variableType) {
if (componentType != null && variableType.isArray()) {
return componentType.isAssignmentCompatible(variableType.getComponentType());
}
return isEqualTo(variableType);
}
@Override
public boolean isCastCompatible(ITypeBinding type) {
return isEqualTo(type);
}
@Override
public String toString() {
return name;
}
@Override
public boolean isSynthetic() {
return BindingUtil.isSynthetic(this);
}
}