if (monitor == null) {
monitor = new NullProgressMonitor();
}
monitor.beginTask("Creating aspect...", 10);
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()) {
throw new InterruptedException();
}
imports.create(false, new SubProgressMonitor(monitor, 1));
JavaModelUtil.reconcile(createdWorkingCopy);
aspect = createdWorkingCopy.getType(aspectName);
needsCommit = true;
}
if (monitor.isCanceled()) {
throw new InterruptedException();
}
ICompilationUnit cu = aspect.getCompilationUnit();
boolean needsSave = cu.isWorkingCopy();
ImportsManager imports = new ImportsManager(cu);
lineDelimiter = StubUtility.getLineDelimiterUsed(aspect);
// Write crosscut and format it
IField field = fCrosscutPage.writeCrosscut(aspect, fCrosscutTypeDialogField.getSelectionIndex(), imports,
new SubProgressMonitor(monitor, 2), lineDelimiter);
ISourceRange range = field.getSourceRange();
IBuffer buf = cu.getBuffer();
String originalContent = buf.getText(range.getOffset(), range.getLength());
String formattedContent = CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, originalContent, 1, null,
lineDelimiter, field.getJavaProject());
buf.replace(range.getOffset(), range.getLength(), formattedContent);
if (!cu.isWorkingCopy())
buf.save(null, false);
imports.create(needsSave, new SubProgressMonitor(monitor, 1));
removeUnusedImports(cu, needsSave);
if (needsCommit)
cu.commitWorkingCopy(false, new SubProgressMonitor(monitor, 1));
} finally {
if (createdWorkingCopy != null) {
createdWorkingCopy.discardWorkingCopy();
}
monitor.done();