Skip to main content
summaryrefslogtreecommitdiffstats
blob: c6709390f35ce4e6aa11c5f9cfa9b779cb223225 (plain) (blame)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*******************************************************************************
 * Copyright (c) 2015, 2016 Google, Inc and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Stefan Xenos (Google) - Initial implementation
 *******************************************************************************/
package org.eclipse.jdt.internal.core.nd.java;

import java.util.Arrays;
import java.util.List;

import org.eclipse.jdt.internal.core.nd.Nd;
import org.eclipse.jdt.internal.core.nd.db.IString;
import org.eclipse.jdt.internal.core.nd.field.FieldByte;
import org.eclipse.jdt.internal.core.nd.field.FieldList;
import org.eclipse.jdt.internal.core.nd.field.FieldLong;
import org.eclipse.jdt.internal.core.nd.field.FieldManyToOne;
import org.eclipse.jdt.internal.core.nd.field.FieldOneToMany;
import org.eclipse.jdt.internal.core.nd.field.FieldString;
import org.eclipse.jdt.internal.core.nd.field.StructDef;
import org.eclipse.jdt.internal.core.nd.util.CharArrayUtils;

public class NdType extends NdBinding {
	public static final FieldManyToOne<NdResourceFile> FILE;
	public static final FieldManyToOne<NdTypeId> TYPENAME;
	public static final FieldManyToOne<NdTypeSignature> SUPERCLASS;
	public static final FieldOneToMany<NdTypeInterface> INTERFACES;
	public static final FieldManyToOne<NdTypeId> DECLARING_TYPE;
	public static final FieldList<NdMethod> METHODS;
	public static final FieldList<NdTypeAnnotation> TYPE_ANNOTATIONS;
	public static final FieldList<NdAnnotation> ANNOTATIONS;
	public static final FieldList<NdVariable> VARIABLES;
	public static final FieldString MISSING_TYPE_NAMES;
	public static final FieldString SOURCE_FILE_NAME;
	public static final FieldString INNER_CLASS_SOURCE_NAME;
	public static final FieldByte FLAGS;
	public static final FieldLong TAG_BITS;
	public static final FieldString ENCLOSING_METHOD;
	/**
	 * Binary name that was recorded in the .class file if different from the binary
	 * name that was determined by the .class's name and location. This is only set for
	 * .class files that have been moved to the wrong folder.
	 */
	public static final FieldString FIELD_DESCRIPTOR_FROM_CLASS;

	@SuppressWarnings("hiding")
	public static final StructDef<NdType> type;

	static {
		type = StructDef.create(NdType.class, NdBinding.type);
		FILE = FieldManyToOne.createOwner(type, NdResourceFile.TYPES);
		TYPENAME = FieldManyToOne.create(type, NdTypeId.TYPES);
		DECLARING_TYPE = FieldManyToOne.create(type, NdTypeId.DECLARED_TYPES);
		INTERFACES = FieldOneToMany.create(type, NdTypeInterface.APPLIES_TO);
		SUPERCLASS = FieldManyToOne.create(type, NdTypeSignature.SUBCLASSES);
		METHODS = FieldList.create(type, NdMethod.type);
		TYPE_ANNOTATIONS = FieldList.create(type, NdTypeAnnotation.type);
		ANNOTATIONS = FieldList.create(type, NdAnnotation.type);
		VARIABLES = FieldList.create(type, NdVariable.type);
		MISSING_TYPE_NAMES = type.addString();
		SOURCE_FILE_NAME = type.addString();
		INNER_CLASS_SOURCE_NAME = type.addString();
		FLAGS = type.addByte();
		TAG_BITS = type.addLong();
		FIELD_DESCRIPTOR_FROM_CLASS = type.addString();
		ENCLOSING_METHOD = type.addString();
		type.done();
	}

	public static final byte FLG_TYPE_ANONYMOUS 	= 0x0001;
	public static final byte FLG_TYPE_LOCAL 		= 0x0002;
	public static final byte FLG_TYPE_MEMBER 		= 0x0004;
	public static final byte FLG_GENERIC_SIGNATURE_PRESENT = 0x0008;

	public NdType(Nd nd, long address) {
		super(nd, address);
	}

	public NdType(Nd nd, NdResourceFile resource) {
		super(nd);

		FILE.put(nd, this.address, resource);
	}

	public NdTypeId getTypeId() {
		return TYPENAME.get(getNd(), this.address);
	}

	public void setTypeId(NdTypeId typeId) {
		TYPENAME.put(getNd(), this.address, typeId);
	}

	public void setFile(NdResourceFile file) {
		FILE.put(getNd(), this.address, file);
	}

	public NdResourceFile getFile() {
		return FILE.get(getNd(), this.address);
	}

	/**
	 * Sets the source name for this type.
	 */
	public void setSourceNameOverride(char[] sourceName) {
		char[] oldSourceName = getSourceName();
		if (!CharArrayUtils.equals(oldSourceName, sourceName)) {
			INNER_CLASS_SOURCE_NAME.put(getNd(), this.address, sourceName);
		}
	}

	public IString getSourceNameOverride() {
		return INNER_CLASS_SOURCE_NAME.get(getNd(), this.address);
	}

	public long getResourceAddress() {
		return FILE.getAddress(getNd(), this.address);
	}

	public void setSuperclass(NdTypeSignature superclassTypeName) {
		SUPERCLASS.put(getNd(), this.address, superclassTypeName);
	}

	public NdTypeSignature getSuperclass() {
		return SUPERCLASS.get(getNd(), this.address);
	}

	public List<NdTypeInterface> getInterfaces() {
		return INTERFACES.asList(getNd(), this.address);
	}

	public NdResourceFile getResourceFile() {
		return FILE.get(getNd(), this.address);
	}

	/**
	 * @param createTypeIdFromBinaryName
	 */
	public void setDeclaringType(NdTypeId createTypeIdFromBinaryName) {
		DECLARING_TYPE.put(getNd(), this.address, createTypeIdFromBinaryName);
	}

	public NdTypeId getDeclaringType() {
		return DECLARING_TYPE.get(getNd(), this.address);
	}

	/**
	 * Sets the missing type names (if any) for this class. The names are encoded in a comma-separated list.
	 */
	public void setMissingTypeNames(char[] contents) {
		MISSING_TYPE_NAMES.put(getNd(), this.address, contents);
	}

	/**
	 * Returns the missing type names as a comma-separated list
	 */
	public IString getMissingTypeNames() {
		return MISSING_TYPE_NAMES.get(getNd(), this.address);
	}

	public void setSourceFileName(char[] sourceFileName) {
		SOURCE_FILE_NAME.put(getNd(), this.address, sourceFileName);
	}

	public IString getSourceFileName() {
		return SOURCE_FILE_NAME.get(getNd(), this.address);
	}

	public void setAnonymous(boolean anonymous) {
		setFlag(FLG_TYPE_ANONYMOUS, anonymous);
	}

	public void setIsLocal(boolean local) {
		setFlag(FLG_TYPE_LOCAL, local);
	}

	public void setIsMember(boolean member) {
		setFlag(FLG_TYPE_MEMBER, member);
	}

	public boolean isAnonymous() {
		return getFlag(FLG_TYPE_ANONYMOUS);
	}

	public boolean isLocal() {
		return getFlag(FLG_TYPE_LOCAL);
	}

	public boolean isMember() {
		return getFlag(FLG_TYPE_MEMBER);
	}

	public void setFlag(byte flagConstant, boolean value) {
		int oldFlags = FLAGS.get(getNd(), this.address);
		int newFlags =  ((oldFlags & ~flagConstant) | (value ? flagConstant : 0));
		FLAGS.put(getNd(), this.address, (byte)newFlags);
	}

	public boolean getFlag(byte flagConstant) {
		return (FLAGS.get(getNd(), this.address) & flagConstant) != 0;
	}

	public char[] getSourceName() {
		IString sourceName = getSourceNameOverride();
		if (sourceName.length() != 0) {
			return sourceName.getChars();
		}
		char[] simpleName = getTypeId().getSimpleNameCharArray();
		return JavaNames.simpleNameToSourceName(simpleName);
	}

	public List<NdVariable> getVariables() {
		return VARIABLES.asList(getNd(), this.address);
	}

	@Override
	public List<NdTypeParameter> getTypeParameters() {
		return TYPE_PARAMETERS.asList(getNd(), this.address);
	}

	public List<NdTypeAnnotation> getTypeAnnotations() {
		return TYPE_ANNOTATIONS.asList(getNd(), this.address);
	}

	public List<NdAnnotation> getAnnotations() {
		return ANNOTATIONS.asList(getNd(), this.address);
	}

	public NdAnnotation createAnnotation() {
		return ANNOTATIONS.append(getNd(), getAddress());
	}

	public void allocateAnnotations(int length) {
		ANNOTATIONS.allocate(getNd(), getAddress(), length);
	}

	/**
	 * Returns the list of methods, sorted by ascending method name (selector + descriptor).
	 */
	public List<NdMethod> getMethods() {
		return METHODS.asList(getNd(), this.address);
	}

	/**
	 * Returns the list of methods, in declaration order.
	 */
	public List<NdMethod> getMethodsInDeclarationOrder() {
		List<NdMethod> unsorted = getMethods();
		NdMethod[] sorted = new NdMethod[unsorted.size()];
		for (NdMethod next : unsorted) {
			int pos = next.getDeclarationPosition();

			if (pos < 0 || pos >= sorted.length) {
				throw getNd().describeProblem()
					.addProblemAddress(NdMethod.DECLARATION_POSITION, next.getAddress())
					.build("Method " + next.getMethodName().getString() + " reports invalid position of " + pos); //$NON-NLS-1$//$NON-NLS-2$
			}

			NdMethod oldMethodAtThisPosition = sorted[pos];
			if (oldMethodAtThisPosition != null) {
				throw getNd().describeProblem()
					.addProblemAddress(NdMethod.DECLARATION_POSITION, next.getAddress())
					.addProblemAddress(NdMethod.DECLARATION_POSITION, oldMethodAtThisPosition.getAddress())
					.build("Method " + oldMethodAtThisPosition.getMethodName().getString()  //$NON-NLS-1$
							+ " and method " + next.getMethodName().getString() + " both claim to be at position "  //$NON-NLS-1$//$NON-NLS-2$
							+ pos);
			}
			sorted[pos] = next;
		}

		return Arrays.asList(sorted);
	}

	@Override
	public String toString() {
		try {
			return "class " + new String(getSourceName()); //$NON-NLS-1$
		} catch (RuntimeException e) {
			return super.toString();
		}
	}

	public void setTagBits(long tagBits) {
		TAG_BITS.put(getNd(), this.address, tagBits);
	}

	public long getTagBits() {
		return TAG_BITS.get(getNd(), this.address);
	}

	public void setFieldDescriptorFromClass(char[] fieldDescriptorFromClass) {
		FIELD_DESCRIPTOR_FROM_CLASS.put(getNd(), this.address, fieldDescriptorFromClass);
	}

	/**
	 * Returns the field descriptor for this type, based on the binary type information stored in the
	 * .class file itself. Note that this may differ from the field descriptor of this type's typeId in
	 * the event that the .class file has been moved.
	 */
	public IString getFieldDescriptor() {
		IString descriptorFromClass = FIELD_DESCRIPTOR_FROM_CLASS.get(getNd(), this.address);
		if (descriptorFromClass.length() == 0) {
			return getTypeId().getFieldDescriptor();
		}
		return descriptorFromClass;
	}

	public NdTypeAnnotation createTypeAnnotation() {
		return TYPE_ANNOTATIONS.append(getNd(), getAddress());
	}

	public void allocateTypeAnnotations(int length) {
		TYPE_ANNOTATIONS.allocate(getNd(), getAddress(), length);
	}

	public NdVariable createVariable() {
		return VARIABLES.append(getNd(), getAddress());
	}

	public void allocateVariables(int length) {
		VARIABLES.allocate(getNd(), getAddress(), length);
	}

	public void allocateMethods(int length) {
		METHODS.allocate(getNd(), getAddress(), length);
	}

	public NdMethod createMethod() {
		return METHODS.append(getNd(), getAddress());
	}

	public void setDeclaringMethod(char[] enclosingMethod) {
		ENCLOSING_METHOD.put(getNd(), getAddress(), enclosingMethod);
	}

	public IString getDeclaringMethod() {
		return ENCLOSING_METHOD.get(getNd(), getAddress());
	}
}

Back to the top