ICompilationUnit createdWorkingCopy = null;
try {
// Step 1: Process package fragment.
IPackageFragmentRoot root = getPackageFragmentRoot();
IPackageFragment pack = getPackageFragment();
if (pack == null) {
pack = root.getPackageFragment(""); //$NON-NLS-1$
}
if (!pack.exists()) {
String packName = pack.getElementName();
pack = root.createPackageFragment(packName, true, null);
}
monitor.worked(1);
// Step 2: Create aspect if not already existent.
boolean needsCommit = false;
String lineDelimiter = null;
String aspectName = getAspectName();
IType aspect = getAspect();
if (aspect == null) {
lineDelimiter = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
ICompilationUnit parentCU = pack.createCompilationUnit(
aspectName + ".java", "", false, new SubProgressMonitor(monitor, 2)); //$NON-NLS-1$ //$NON-NLS-2$
// create a working copy with a new owner
createdWorkingCopy = parentCU.getWorkingCopy(null);
// use the compiler template a first time to read the imports
String content = CodeGeneration.getCompilationUnitContent(createdWorkingCopy, null, "", lineDelimiter); //$NON-NLS-1$
if (content != null) {
createdWorkingCopy.getBuffer().setContents(content);
}
ImportsManager imports = new ImportsManager(createdWorkingCopy);
// add an import that will be removed again. Having this import solves 14661
imports.addImport(JavaModelUtil.concatenateName(pack.getElementName(), aspectName));
String typeContent = writeBasicAspect(imports, lineDelimiter);
String cuContent = constructCUContent(parentCU, typeContent, lineDelimiter);
createdWorkingCopy.getBuffer().setContents(cuContent);
if (monitor.isCanceled()) {