String simpleTypeStub = constructSimpleTypeStub();
String cuContent = constructCUContent(parentCU, simpleTypeStub, lineDelimiter);
buffer.setContents(cuContent);
CompilationUnit astRoot = createASTForImports(parentCU);
existingImports = getExistingImports(astRoot);
imports = new ImportsManager(astRoot);
// add an import that will be removed again. Having this import solves 14661
imports.addImport(JavaModelUtil.concatenateName(pack.getElementName(), typeName));
String typeContent = constructTypeStub(parentCU, imports, lineDelimiter);
int index = cuContent.lastIndexOf(simpleTypeStub);
if (index == -1) {
AbstractTypeDeclaration typeNode = (AbstractTypeDeclaration) astRoot.types().get(0);
int start = ((ASTNode) typeNode.modifiers().get(0)).getStartPosition();
int end = typeNode.getStartPosition() + typeNode.getLength();
buffer.replace(start, end - start, typeContent);
} else {
buffer.replace(index, simpleTypeStub.length(), typeContent);
}
createdType = parentCU.getType(typeName);
} else {
IType enclosingType = getEnclosingType();
ICompilationUnit parentCU = enclosingType.getCompilationUnit();
needsSave = !parentCU.isWorkingCopy();
parentCU.becomeWorkingCopy(new SubProgressMonitor(monitor, 1)); // cu is now for sure (primary) a working copy
connectedCU = parentCU;
CompilationUnit astRoot = createASTForImports(parentCU);
imports = new ImportsManager(astRoot);
existingImports = getExistingImports(astRoot);
// add imports that will be removed again. Having the imports solves 14661
IType[] topLevelTypes = parentCU.getTypes();
for (int i = 0; i < topLevelTypes.length; i++) {
imports.addImport(topLevelTypes[i].getFullyQualifiedName('.'));
}
lineDelimiter = StubUtility.getLineDelimiterUsed(enclosingType);
StringBuffer content = new StringBuffer();
String comment = getTypeComment(parentCU, lineDelimiter);
if (comment != null) {
content.append(comment);
content.append(lineDelimiter);
}
content.append(constructTypeStub(parentCU, imports, lineDelimiter));
IJavaElement sibling = null;
if (enclosingType.isEnum()) {
IField[] fields = enclosingType.getFields();
if (fields.length > 0) {
for (int i = 0, max = fields.length; i < max; i++) {
if (!fields[i].isEnumConstant()) {
sibling = fields[i];
break;
}
}
}
} else {
IJavaElement[] elems = enclosingType.getChildren();
sibling = elems.length > 0 ? elems[0] : null;
}
createdType = enclosingType.createType(content.toString(), sibling, false, new SubProgressMonitor(monitor, 2));
indent = StubUtility.getIndentUsed(enclosingType) + 1;
}
if (monitor.isCanceled()) {
throw new InterruptedException();
}
// add imports for superclass/interfaces, so types can be resolved correctly
ICompilationUnit cu = createdType.getCompilationUnit();
imports.create(false, new SubProgressMonitor(monitor, 1));
JavaModelUtil.reconcile(cu);
if (monitor.isCanceled()) {
throw new InterruptedException();
}
// set up again
CompilationUnit astRoot = createASTForImports(imports.getCompilationUnit());
imports = new ImportsManager(astRoot);
createTypeMembers(createdType, imports, new SubProgressMonitor(monitor, 1));
// add imports