Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 29b31b1349cd0d6e64c56b1b4c78c7da3abcb5c5 (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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/*******************************************************************************
 * Copyright (c) 2000, 2020 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *     Stephan Herrmann <stephan@cs.tu-berlin.de> - Contributions for
 *     								Bug 320841 - TypeConverters don't set enclosingType
 *     								Bug 353474 - type converters should include more annotations
 *******************************************************************************/
package org.eclipse.jdt.internal.compiler.parser;

/**
 * Converter from source element type to parsed compilation unit.
 *
 * Limitation:
 * | The source element field does not carry any information for its constant part, thus
 * | the converted parse tree will not include any field initializations.
 * | Therefore, any binary produced by compiling against converted source elements will
 * | not take advantage of remote field constant inlining.
 * | Given the intended purpose of the conversion is to resolve references, this is not
 * | a problem.
 *
 */

import org.eclipse.jdt.core.IAnnotatable;
import org.eclipse.jdt.core.IAnnotation;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IImportDeclaration;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.ILocalVariable;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.Signature;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.CompilationResult;
import org.eclipse.jdt.internal.compiler.ast.*;
import org.eclipse.jdt.internal.compiler.ast.Annotation;
import org.eclipse.jdt.internal.compiler.ast.Initializer;
import org.eclipse.jdt.internal.compiler.ast.TypeParameter;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.env.*;
import org.eclipse.jdt.internal.compiler.lookup.ExtraCompilerModifiers;
import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
import org.eclipse.jdt.internal.core.*;
import org.eclipse.jdt.internal.core.util.Util;

public class SourceTypeConverter extends TypeConverter {

	/*
	 * Exception thrown while converting an anonymous type of a member type
	 * in this case, we must parse the source as the enclosing instance cannot be recreated
	 * from the model
	 */
	static class AnonymousMemberFound extends RuntimeException {
		private static final long serialVersionUID = 1L;
	}

	public static final int FIELD = 0x01;
	public static final int CONSTRUCTOR = 0x02;
	public static final int METHOD = 0x04;
	public static final int MEMBER_TYPE = 0x08;
	public static final int FIELD_INITIALIZATION = 0x10;
	public static final int FIELD_AND_METHOD = FIELD | CONSTRUCTOR | METHOD;
	public static final int LOCAL_TYPE = 0x20;
	public static final int NONE = 0;

	private int flags;
	private CompilationUnitDeclaration unit;
	private Parser parser;
	private ICompilationUnit cu;
	private char[] source;

	private SourceTypeConverter(int flags, ProblemReporter problemReporter) {
		super(problemReporter, Signature.C_DOT);
		this.flags = flags;
	}

	/*
	 * Convert a set of source element types into a parsed compilation unit declaration
	 * The argument types are then all grouped in the same unit. The argument types must
	 * at least contain one type.
	 * Can optionally ignore fields & methods or member types or field initialization
	 */
	public static CompilationUnitDeclaration buildCompilationUnit(
		ISourceType[] sourceTypes,
		int flags,
		ProblemReporter problemReporter,
		CompilationResult compilationResult) {

//		long start = System.currentTimeMillis();
		SourceTypeConverter converter = new SourceTypeConverter(flags, problemReporter);
		try {
			return converter.convert(sourceTypes, compilationResult);
		} catch (JavaModelException e) {
			return null;
/*		} finally {
			System.out.println("Spent " + (System.currentTimeMillis() - start) + "ms to convert " + ((JavaElement) converter.cu).toStringWithAncestors());
*/		}
	}

	public static CompilationUnitDeclaration buildModularCompilationUnit(
		IModule module,
		ProblemReporter problemReporter,
		CompilationResult compilationResult) {

		SourceTypeConverter converter = new SourceTypeConverter(0, problemReporter);
		try {
			return converter.convert(module, compilationResult);
		} catch (JavaModelException e) {
			return null;
		}
	}

	/*
	 * Convert a set of source element types into a parsed compilation unit declaration
	 * The argument types are then all grouped in the same unit. The argument types must
	 * at least contain one type.
	 */
	private CompilationUnitDeclaration convert(ISourceType[] sourceTypes, CompilationResult compilationResult) throws JavaModelException {
		this.unit = new CompilationUnitDeclaration(this.problemReporter, compilationResult, 0);
		// not filled at this point

		if (sourceTypes.length == 0) return this.unit;
		SourceTypeElementInfo topLevelTypeInfo = (SourceTypeElementInfo) sourceTypes[0];
		org.eclipse.jdt.core.ICompilationUnit cuHandle = topLevelTypeInfo.getHandle().getCompilationUnit();
		this.cu = (ICompilationUnit) cuHandle;
		final CompilationUnitElementInfo compilationUnitElementInfo = (CompilationUnitElementInfo) ((JavaElement) this.cu).getElementInfo();
		if (this.has1_5Compliance &&
				(compilationUnitElementInfo.annotationNumber >= CompilationUnitElementInfo.ANNOTATION_THRESHOLD_FOR_DIET_PARSE ||
				(compilationUnitElementInfo.hasFunctionalTypes && (this.flags & LOCAL_TYPE) != 0))) {
			// If more than 10 annotations, diet parse as this is faster, but not if
			// the client wants local and anonymous types to be converted (https://bugs.eclipse.org/bugs/show_bug.cgi?id=254738)
			// Also see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=405843
			if ((this.flags & LOCAL_TYPE) == 0) {
				return new Parser(this.problemReporter, true).dietParse(this.cu, compilationResult);
			} else {
				return new Parser(this.problemReporter, true).parse(this.cu, compilationResult);
			}
		}

		/* only positions available */
		int start = topLevelTypeInfo.getNameSourceStart();
		int end = topLevelTypeInfo.getNameSourceEnd();

		/* convert package and imports */
		String[] packageName = ((PackageFragment) cuHandle.getParent()).names;
		if (packageName.length > 0)
			// if its null then it is defined in the default package
			this.unit.currentPackage =
				createImportReference(packageName, start, end, false, ClassFileConstants.AccDefault);
		IImportDeclaration[] importDeclarations = topLevelTypeInfo.getHandle().getCompilationUnit().getImports();
		int importCount = importDeclarations.length;
		this.unit.imports = new ImportReference[importCount];
		for (int i = 0; i < importCount; i++) {
			ImportDeclaration importDeclaration = (ImportDeclaration) importDeclarations[i];
			ISourceImport sourceImport = (ISourceImport) importDeclaration.getElementInfo();
			String nameWithoutStar = importDeclaration.getNameWithoutStar();
			this.unit.imports[i] = createImportReference(
				Util.splitOn('.', nameWithoutStar, 0, nameWithoutStar.length()),
				sourceImport.getDeclarationSourceStart(),
				sourceImport.getDeclarationSourceEnd(),
				importDeclaration.isOnDemand(),
				sourceImport.getModifiers());
		}
		/* convert type(s) */
		try {
			int typeCount = sourceTypes.length;
			final TypeDeclaration[] types = new TypeDeclaration[typeCount];
			/*
			 * We used a temporary types collection to prevent this.unit.types from being null during a call to
			 * convert(...) when the source is syntactically incorrect and the parser is flushing the unit's types.
			 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=97466
			 */
			for (int i = 0; i < typeCount; i++) {
				SourceTypeElementInfo typeInfo = (SourceTypeElementInfo) sourceTypes[i];
				types[i] = convert((SourceType) typeInfo.getHandle(), compilationResult);
			}
			this.unit.types = types;
			return this.unit;
		} catch (AnonymousMemberFound e) {
			return new Parser(this.problemReporter, true).parse(this.cu, compilationResult);
		}
	}

	private CompilationUnitDeclaration convert(IModule module, CompilationResult compilationResult) throws JavaModelException {
		this.unit = new CompilationUnitDeclaration(this.problemReporter, compilationResult, 0);
		// not filled at this point

		ModuleDescriptionInfo moduleInfo = (ModuleDescriptionInfo) module;
		org.eclipse.jdt.core.ICompilationUnit cuHandle = moduleInfo.getHandle().getCompilationUnit();
		this.cu = (ICompilationUnit) cuHandle;
		// always parse, because (a) dietParse is always sufficient, (b) we don't yet have the necessary conversion methods for module directives
		return new Parser(this.problemReporter, true).dietParse(this.cu, compilationResult);
	}

	/*
	 * Convert an initializerinfo into a parsed initializer declaration
	 */
	private Initializer convert(InitializerElementInfo initializerInfo, CompilationResult compilationResult) throws JavaModelException {

		Block block = new Block(0);
		Initializer initializer = new Initializer(block, ClassFileConstants.AccDefault);

		int start = initializerInfo.getDeclarationSourceStart();
		int end = initializerInfo.getDeclarationSourceEnd();

		initializer.sourceStart = initializer.declarationSourceStart = start;
		initializer.sourceEnd = initializer.declarationSourceEnd = end;
		initializer.modifiers = initializerInfo.getModifiers();

		/* convert local and anonymous types */
		IJavaElement[] children = initializerInfo.getChildren();
		int typesLength = children.length;
		if (typesLength > 0) {
			Statement[] statements = new Statement[typesLength];
			for (int i = 0; i < typesLength; i++) {
				SourceType type = (SourceType) children[i];
				TypeDeclaration localType = convert(type, compilationResult);
				if ((localType.bits & ASTNode.IsAnonymousType) != 0) {
					QualifiedAllocationExpression expression = new QualifiedAllocationExpression(localType);
					expression.type = localType.superclass;
					localType.superclass = null;
					localType.superInterfaces = null;
					localType.allocation = expression;
					statements[i] = expression;
				} else {
					statements[i] = localType;
				}
			}
			block.statements = statements;
		}

		return initializer;
	}

	/*
	 * Convert a field source element into a parsed field declaration
	 */
	private RecordComponent convertRecordComponents(SourceField component,
										TypeDeclaration type,
										CompilationResult compilationResult) throws JavaModelException {

		SourceFieldElementInfo compInfo = (SourceFieldElementInfo) component.getElementInfo();
		RecordComponent comp = new RecordComponent(null, -1, -1);

		int start = compInfo.getNameSourceStart();
		int end = compInfo.getNameSourceEnd();

		comp.name = component.getElementName().toCharArray();
		comp.sourceStart = start;
		comp.sourceEnd = end;
		comp.declarationSourceStart = compInfo.getDeclarationSourceStart();
		comp.declarationSourceEnd = compInfo.getDeclarationSourceEnd();
		comp.type = createTypeReference(compInfo.getTypeName(), start, end);

		/* convert annotations */
		comp.annotations = convertAnnotations(component);
		return comp;
	}
	/*
	 * Convert a record component source element into a parsed record component declaration
	 */
	private FieldDeclaration convert(SourceField fieldHandle, TypeDeclaration type, CompilationResult compilationResult) throws JavaModelException {

		SourceFieldElementInfo fieldInfo = (SourceFieldElementInfo) fieldHandle.getElementInfo();
		FieldDeclaration field = new FieldDeclaration();

		int start = fieldInfo.getNameSourceStart();
		int end = fieldInfo.getNameSourceEnd();

		field.name = fieldHandle.getElementName().toCharArray();
		field.sourceStart = start;
		field.sourceEnd = end;
		field.declarationSourceStart = fieldInfo.getDeclarationSourceStart();
		field.declarationSourceEnd = fieldInfo.getDeclarationSourceEnd();
		int modifiers = fieldInfo.getModifiers();
		boolean isEnumConstant = (modifiers & ClassFileConstants.AccEnum) != 0;
		if (isEnumConstant) {
			field.modifiers = modifiers & ~ClassFileConstants.AccEnum; // clear AccEnum bit onto AST (binding will add it)
		} else {
			field.modifiers = modifiers;
			field.type = createTypeReference(fieldInfo.getTypeName(), start, end);
		}

		// convert 1.5 specific constructs only if compliance is 1.5 or above
		if (this.has1_5Compliance) {
			/* convert annotations */
			field.annotations = convertAnnotations(fieldHandle);
		}

		/* conversion of field constant */
		if ((this.flags & FIELD_INITIALIZATION) != 0) {
			char[] initializationSource = fieldInfo.getInitializationSource();
			if (initializationSource != null) {
				if (this.parser == null) {
					this.parser = new Parser(this.problemReporter, true);
				}
				this.parser.parse(field, type, this.unit, initializationSource);
			}
		}

		/* conversion of local and anonymous types */
		if ((this.flags & LOCAL_TYPE) != 0) {
			IJavaElement[] children = fieldInfo.getChildren();
			int childrenLength = children.length;
			if (childrenLength == 1) {
				field.initialization = convert(children[0], isEnumConstant ? field : null, compilationResult);
			} else if (childrenLength > 1) {
				ArrayInitializer initializer = new ArrayInitializer();
				field.initialization = initializer;
				Expression[] expressions = new Expression[childrenLength];
				initializer.expressions = expressions;
				for (int i = 0; i < childrenLength; i++) {
					expressions[i] = convert(children[i], isEnumConstant ? field : null, compilationResult);
				}
			}
		}
		return field;
	}

	private QualifiedAllocationExpression convert(IJavaElement localType, FieldDeclaration enumConstant, CompilationResult compilationResult) throws JavaModelException {
		TypeDeclaration anonymousLocalTypeDeclaration = convert((SourceType) localType, compilationResult);
		QualifiedAllocationExpression expression = new QualifiedAllocationExpression(anonymousLocalTypeDeclaration);
		expression.type = anonymousLocalTypeDeclaration.superclass;
		anonymousLocalTypeDeclaration.superclass = null;
		anonymousLocalTypeDeclaration.superInterfaces = null;
		anonymousLocalTypeDeclaration.allocation = expression;
		if (enumConstant != null) {
			anonymousLocalTypeDeclaration.modifiers &= ~ClassFileConstants.AccEnum;
			expression.enumConstant = enumConstant;
			expression.type = null;
		}
		return expression;
	}

	/*
	 * Convert a method source element into a parsed method/constructor declaration
	 */
	private AbstractMethodDeclaration convert(SourceMethod methodHandle, SourceMethodElementInfo methodInfo, CompilationResult compilationResult) throws JavaModelException {
		AbstractMethodDeclaration method;

		/* only source positions available */
		int start = methodInfo.getNameSourceStart();
		int end = methodInfo.getNameSourceEnd();

		/* https://bugs.eclipse.org/bugs/show_bug.cgi?id=324850, Even when this type is being constructed
		   on behalf of a 1.4 project we must internalize type variables properly in order to be able to
		   recognize usages of them in the method signature, to apply substitutions and thus to be able to
		   detect overriding in the presence of generics. If we simply drop them, when the method signature
		   refers to the type parameter, we won't know it should be bound to the type parameter and perform
		   incorrect lookup and may mistakenly end up with missing types
		 */
		TypeParameter[] typeParams = null;
		char[][] typeParameterNames = methodInfo.getTypeParameterNames();
		if (typeParameterNames != null) {
			int parameterCount = typeParameterNames.length;
			if (parameterCount > 0) { // method's type parameters must be null if no type parameter
				char[][][] typeParameterBounds = methodInfo.getTypeParameterBounds();
				typeParams = new TypeParameter[parameterCount];
				for (int i = 0; i < parameterCount; i++) {
					typeParams[i] = createTypeParameter(typeParameterNames[i], typeParameterBounds[i], start, end);
				}
			}
		}

		int modifiers = methodInfo.getModifiers();
		if (methodInfo.isConstructor()) {
			ConstructorDeclaration decl = new ConstructorDeclaration(compilationResult);
			decl.bits &= ~ASTNode.IsDefaultConstructor;
			method = decl;
			decl.typeParameters = typeParams;
		} else {
			MethodDeclaration decl;
			if (methodInfo.isAnnotationMethod()) {
				AnnotationMethodDeclaration annotationMethodDeclaration = new AnnotationMethodDeclaration(compilationResult);

				/* conversion of default value */
				SourceAnnotationMethodInfo annotationMethodInfo = (SourceAnnotationMethodInfo) methodInfo;
				boolean hasDefaultValue = annotationMethodInfo.defaultValueStart != -1 || annotationMethodInfo.defaultValueEnd != -1;
				if ((this.flags & FIELD_INITIALIZATION) != 0) {
					if (hasDefaultValue) {
						char[] defaultValueSource = CharOperation.subarray(getSource(), annotationMethodInfo.defaultValueStart, annotationMethodInfo.defaultValueEnd+1);
						if (defaultValueSource != null) {
    						Expression expression =  parseMemberValue(defaultValueSource);
    						if (expression != null) {
    							annotationMethodDeclaration.defaultValue = expression;
    						}
						} else {
							// could not retrieve the default value
							hasDefaultValue = false;
						}
					}
				}
				if (hasDefaultValue)
					modifiers |= ClassFileConstants.AccAnnotationDefault;
				decl = annotationMethodDeclaration;
			} else {
				decl = new MethodDeclaration(compilationResult);
			}

			// convert return type
			decl.returnType = createTypeReference(methodInfo.getReturnTypeName(), start, end);

			// type parameters
			decl.typeParameters = typeParams;

			method = decl;
		}
		method.selector = methodHandle.getElementName().toCharArray();
		boolean isVarargs = (modifiers & ClassFileConstants.AccVarargs) != 0;
		method.modifiers = modifiers & ~ClassFileConstants.AccVarargs;
		method.sourceStart = start;
		method.sourceEnd = end;
		method.declarationSourceStart = methodInfo.getDeclarationSourceStart();
		method.declarationSourceEnd = methodInfo.getDeclarationSourceEnd();

		// convert 1.5 specific constructs only if compliance is 1.5 or above
		if (this.has1_5Compliance) {
			/* convert annotations */
			method.annotations = convertAnnotations(methodHandle);
		}

		/* convert arguments */
		String[] argumentTypeSignatures = methodHandle.getParameterTypes();
		char[][] argumentNames = methodInfo.getArgumentNames();
		int argumentCount = argumentTypeSignatures == null ? 0 : argumentTypeSignatures.length;
		if (argumentCount > 0) {
			ILocalVariable[] parameters = methodHandle.getParameters();
			long position = ((long) start << 32) + end;
			method.arguments = new Argument[argumentCount];
			for (int i = 0; i < argumentCount; i++) {
				TypeReference typeReference = createTypeReference(argumentTypeSignatures[i], start, end);
				if (isVarargs && i == argumentCount-1) {
					typeReference.bits |= ASTNode.IsVarArgs;
				}
				method.arguments[i] =
					new Argument(
						argumentNames[i],
						position,
						typeReference,
						ClassFileConstants.AccDefault);
				// do not care whether was final or not
				// convert 1.5 specific constructs only if compliance is 1.5 or above
				if (this.has1_5Compliance) {
					/* convert annotations */
					method.arguments[i].annotations = convertAnnotations(parameters[i]);
				}
			}
		}

		/* convert thrown exceptions */
		char[][] exceptionTypeNames = methodInfo.getExceptionTypeNames();
		int exceptionCount = exceptionTypeNames == null ? 0 : exceptionTypeNames.length;
		if (exceptionCount > 0) {
			method.thrownExceptions = new TypeReference[exceptionCount];
			for (int i = 0; i < exceptionCount; i++) {
				method.thrownExceptions[i] =
					createTypeReference(exceptionTypeNames[i], start, end);
			}
		}

		/* convert local and anonymous types */
		if ((this.flags & LOCAL_TYPE) != 0) {
			IJavaElement[] children = methodInfo.getChildren();
			int typesLength = children.length;
			if (typesLength != 0) {
				Statement[] statements = new Statement[typesLength];
				for (int i = 0; i < typesLength; i++) {
					SourceType type = (SourceType) children[i];
					TypeDeclaration localType = convert(type, compilationResult);
					if ((localType.bits & ASTNode.IsAnonymousType) != 0) {
						QualifiedAllocationExpression expression = new QualifiedAllocationExpression(localType);
						expression.type = localType.superclass;
						localType.superclass = null;
						localType.superInterfaces = null;
						localType.allocation = expression;
						statements[i] = expression;
					} else {
						statements[i] = localType;
					}
				}
				method.statements = statements;
			}
		}

		return method;
	}

	/*
	 * Convert a source element type into a parsed type declaration
	 */
	private TypeDeclaration convert(SourceType typeHandle, CompilationResult compilationResult) throws JavaModelException {
		SourceTypeElementInfo typeInfo = (SourceTypeElementInfo) typeHandle.getElementInfo();
		if (typeInfo.isAnonymousMember())
			throw new AnonymousMemberFound();
		/* create type/record declaration - can be member type */
		TypeDeclaration type = new TypeDeclaration(compilationResult);
		if ((TypeDeclaration.kind(typeInfo.getModifiers()) == TypeDeclaration.RECORD_DECL)) {
			// The first choice constructor that takes CompilationResult as arg is not setting all the fields
			// Hence, use the one that does
			type.modifiers |= ExtraCompilerModifiers.AccRecord;
			IField[] recordComponents = typeHandle.getRecordComponents();
			type.recordComponents = new RecordComponent[recordComponents.length];
			for(int i = 0; i < recordComponents.length; i++) {
				type.recordComponents[i] = convertRecordComponents((SourceField)recordComponents[i], type, compilationResult);
			}
		}
		if (typeInfo.getEnclosingType() == null) {
			if (typeHandle.isAnonymous()) {
				type.name = CharOperation.NO_CHAR;
				type.bits |= (ASTNode.IsAnonymousType|ASTNode.IsLocalType);
			} else {
				if (typeHandle.isLocal()) {
					type.bits |= ASTNode.IsLocalType;
				}
			}
		}  else {
			type.bits |= ASTNode.IsMemberType;
		}
		if ((type.bits & ASTNode.IsAnonymousType) == 0) {
			type.name = typeInfo.getName();
		}
		type.name = typeInfo.getName();
		int start, end; // only positions available
		type.sourceStart = start = typeInfo.getNameSourceStart();
		type.sourceEnd = end = typeInfo.getNameSourceEnd();
		type.modifiers = typeInfo.getModifiers();
		type.declarationSourceStart = typeInfo.getDeclarationSourceStart();
		type.declarationSourceEnd = typeInfo.getDeclarationSourceEnd();
		type.bodyEnd = type.declarationSourceEnd;

		// convert 1.5 specific constructs only if compliance is 1.5 or above
		if (this.has1_5Compliance) {
			/* convert annotations */
			type.annotations = convertAnnotations(typeHandle);
		}
		/* https://bugs.eclipse.org/bugs/show_bug.cgi?id=324850, even in a 1.4 project, we
		   must internalize type variables and observe any parameterization of super class
		   and/or super interfaces in order to be able to detect overriding in the presence
		   of generics.
		 */
		char[][] typeParameterNames = typeInfo.getTypeParameterNames();
		if (typeParameterNames.length > 0) {
			int parameterCount = typeParameterNames.length;
			char[][][] typeParameterBounds = typeInfo.getTypeParameterBounds();
			type.typeParameters = new TypeParameter[parameterCount];
			for (int i = 0; i < parameterCount; i++) {
				type.typeParameters[i] = createTypeParameter(typeParameterNames[i], typeParameterBounds[i], start, end);
			}
		}

		/* set superclass and superinterfaces */
		if (typeInfo.getSuperclassName() != null) {
			type.superclass = createTypeReference(typeInfo.getSuperclassName(), start, end, true /* include generics */);
			type.superclass.bits |= ASTNode.IsSuperType;
		}
		char[][] interfaceNames = typeInfo.getInterfaceNames();
		int interfaceCount = interfaceNames == null ? 0 : interfaceNames.length;
		if (interfaceCount > 0) {
			type.superInterfaces = new TypeReference[interfaceCount];
			for (int i = 0; i < interfaceCount; i++) {
				type.superInterfaces[i] = createTypeReference(interfaceNames[i], start, end, true /* include generics */);
				type.superInterfaces[i].bits |= ASTNode.IsSuperType;
			}
		}
		/* convert member types */
		if ((this.flags & MEMBER_TYPE) != 0) {
			SourceType[] sourceMemberTypes = typeInfo.getMemberTypeHandles();
			int sourceMemberTypeCount = sourceMemberTypes.length;
			type.memberTypes = new TypeDeclaration[sourceMemberTypeCount];
			for (int i = 0; i < sourceMemberTypeCount; i++) {
				type.memberTypes[i] = convert(sourceMemberTypes[i], compilationResult);
				type.memberTypes[i].enclosingType = type;
			}
		}

		/* convert intializers and fields*/
		InitializerElementInfo[] initializers = null;
		int initializerCount = 0;
		if ((this.flags & LOCAL_TYPE) != 0) {
			initializers = typeInfo.getInitializers();
			initializerCount = initializers.length;
		}
		SourceField[] sourceFields = null;
		int sourceFieldCount = 0;
		if ((this.flags & FIELD) != 0) {
			sourceFields = typeInfo.getFieldHandles();
			sourceFieldCount = sourceFields.length;
		}
		int length = initializerCount + sourceFieldCount;
		if (length > 0) {
			type.fields = new FieldDeclaration[length];
			for (int i = 0; i < initializerCount; i++) {
				type.fields[i] = convert(initializers[i], compilationResult);
			}
			int index = 0;
			for (int i = initializerCount; i < length; i++) {
				type.fields[i] = convert(sourceFields[index++], type, compilationResult);
			}
		}

		/* convert methods - need to add default constructor if necessary */
		boolean needConstructor = (this.flags & CONSTRUCTOR) != 0;
		boolean needMethod = (this.flags & METHOD) != 0;
		if (needConstructor || needMethod) {

			SourceMethod[] sourceMethods = typeInfo.getMethodHandles();
			int sourceMethodCount = sourceMethods.length;

			/* source type has a constructor ?           */
			/* by default, we assume that one is needed. */
			int extraConstructor = 0;
			int methodCount = 0;
			int kind = TypeDeclaration.kind(type.modifiers);
			boolean isAbstract = kind == TypeDeclaration.INTERFACE_DECL || kind == TypeDeclaration.ANNOTATION_TYPE_DECL;
			if (!isAbstract) {
				extraConstructor = needConstructor ? 1 : 0;
				for (int i = 0; i < sourceMethodCount; i++) {
					if (sourceMethods[i].isConstructor()) {
						if (needConstructor) {
							extraConstructor = 0; // Does not need the extra constructor since one constructor already exists.
							methodCount++;
						}
					} else if (needMethod) {
						methodCount++;
					}
				}
			} else {
				methodCount = needMethod ? sourceMethodCount : 0;
			}
			type.methods = new AbstractMethodDeclaration[methodCount + extraConstructor];
			if (extraConstructor != 0) { // add default constructor in first position
				type.methods[0] = type.createDefaultConstructor(false, false);
			}
			int index = 0;
			boolean hasAbstractMethods = false;
			for (int i = 0; i < sourceMethodCount; i++) {
				SourceMethod sourceMethod = sourceMethods[i];
				SourceMethodElementInfo methodInfo = (SourceMethodElementInfo)sourceMethod.getElementInfo();
				boolean isConstructor = methodInfo.isConstructor();
				if ((methodInfo.getModifiers() & ClassFileConstants.AccAbstract) != 0) {
					hasAbstractMethods = true;
				}
				if ((isConstructor && needConstructor) || (!isConstructor && needMethod)) {
					AbstractMethodDeclaration method = convert(sourceMethod, methodInfo, compilationResult);
					if (isAbstract || method.isAbstract()) { // fix-up flag
						method.modifiers |= ExtraCompilerModifiers.AccSemicolonBody;
					}
					type.methods[extraConstructor + index++] = method;
				}
			}
			if (hasAbstractMethods) type.bits |= ASTNode.HasAbstractMethods;
		}

		return type;
	}

	private Annotation[] convertAnnotations(IAnnotatable element) throws JavaModelException {
		IAnnotation[] annotations = element.getAnnotations();
		int length = annotations.length;
		Annotation[] astAnnotations = new Annotation[length];
		if (length > 0) {
			char[] cuSource = getSource();
			int recordedAnnotations = 0;
			for (int i = 0; i < length; i++) {
				ISourceRange positions = annotations[i].getSourceRange();
				int start = positions.getOffset();
				int end = start + positions.getLength();
				char[] annotationSource = CharOperation.subarray(cuSource, start, end);
				if (annotationSource != null) {
	    			Expression expression = parseMemberValue(annotationSource);
	    			/*
	    			 * expression can be null or not an annotation if the source has changed between
	    			 * the moment where the annotation source positions have been retrieved and the moment were
	    			 * this parsing occurred.
	    			 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=90916
	    			 */
	    			if (expression instanceof Annotation) {
	    				astAnnotations[recordedAnnotations++] = (Annotation) expression;
	    			}
				}
			}
			if (length != recordedAnnotations) {
				// resize to remove null annotations
				System.arraycopy(astAnnotations, 0, (astAnnotations = new Annotation[recordedAnnotations]), 0, recordedAnnotations);
			}
		}
		return astAnnotations;
	}

	private char[] getSource() {
		if (this.source == null)
			this.source = this.cu.getContents();
		return this.source;
	}

	private Expression parseMemberValue(char[] memberValue) {
		// memberValue must not be null
		if (this.parser == null) {
			this.parser = new Parser(this.problemReporter, true);
		}
		return this.parser.parseMemberValue(memberValue, 0, memberValue.length, this.unit);
	}
}

Back to the top