Skip to content

Commit

Permalink
Bug 413583: Setup of null annotations in plug-in project does not wor…
Browse files Browse the repository at this point in the history
…k with headless PDE/Build
  • Loading branch information
mkeller committed Jun 4, 2014
1 parent dc0070e commit d255e0b
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,6 @@ private CorrectionMessages() {
public static String UnimplementedCodeFix_TextEditGroup_label;

public static String UnresolvedElementsSubProcessor_swaparguments_description;
public static String UnresolvedElementsSubProcessor_add_annotation_bundle_description;
public static String UnresolvedElementsSubProcessor_add_annotation_bundle_info;
public static String UnresolvedElementsSubProcessor_add_static_import_description;
public static String UnresolvedElementsSubProcessor_addargumentcast_description;
public static String UnresolvedElementsSubProcessor_changemethod_description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,6 @@ MissingAnnotationAttributesProposal_add_missing_attributes_label=Add missing att
MissingReturnTypeCorrectionProposal_changereturnstatement_description=Change return statement

UnresolvedElementsSubProcessor_swaparguments_description=Swap arguments {0} and {1}
UnresolvedElementsSubProcessor_add_annotation_bundle_description=Add library with default null annotations to build path
UnresolvedElementsSubProcessor_add_annotation_bundle_info=Adds the library with default null annotations to the build path so that it is visible at compile time, but avoids a dependency on ''org.eclipse.jdt.annotation'' at run time.<br><br>Adds this entry to ''build.properties'':<br><code>{0}</code>
UnresolvedElementsSubProcessor_add_static_import_description=Add static import for ''{0}''
UnresolvedElementsSubProcessor_addargumentcast_description=Cast argument {0} to ''{1}''
UnresolvedElementsSubProcessor_changemethod_description=Change to ''{0}(..)''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ public interface IProposalRelevance {

public static final int CONVERT_TO_MESSAGE_FORMAT= 0;
public static final int COPY_ANNOTATION_JAR= 0;
public static final int ADD_ANNOTATION_BUNDLE= 0;
public static final int NO_SUGGESSTIONS_AVAILABLE= 0;
public static final int ADD_QUOTE= 0;
public static final int NEW_TYPE= 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,12 @@
import org.eclipse.core.runtime.Status;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;

import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.core.filebuffers.ITextFileBufferManager;
import org.eclipse.core.filebuffers.LocationKind;

import org.eclipse.text.edits.InsertEdit;
import org.eclipse.text.edits.MultiTextEdit;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.FindReplaceDocumentAdapter;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.TextUtilities;

import org.eclipse.ui.ISharedImages;

import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.CompositeChange;
import org.eclipse.ltk.core.refactoring.NullChange;
import org.eclipse.ltk.core.refactoring.TextFileChange;
import org.eclipse.ltk.core.refactoring.resource.DeleteResourceChange;
import org.eclipse.ltk.core.refactoring.resource.ResourceChange;

Expand Down Expand Up @@ -126,7 +109,6 @@
import org.eclipse.jdt.internal.corext.dom.Bindings;
import org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer;
import org.eclipse.jdt.internal.corext.refactoring.changes.ClasspathChange;
import org.eclipse.jdt.internal.corext.refactoring.nls.changes.CreateFileChange;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.internal.corext.util.QualifiedTypeNameHistory;
Expand Down Expand Up @@ -703,72 +685,10 @@ private static void addNullityAnnotationTypesProposals(ICompilationUnit cu, Name
if (annotationsBundles == null)
return;

if (! addAddToBuildPropertiesProposal(cu, node, nullityAnnotation, proposals))
if (! cu.getJavaProject().getProject().hasNature("org.eclipse.pde.PluginNature")) //$NON-NLS-1$
addCopyAnnotationsJarProposal(cu, node, nullityAnnotation, annotationsBundles[0], proposals);
}

private static boolean addAddToBuildPropertiesProposal(final ICompilationUnit cu, final Name name, final String fullyQualifiedName, Collection<ICommandAccess> proposals) throws CoreException {
IProject project= cu.getJavaProject().getProject();
final IFile buildProperties= project.getFile("build.properties"); //$NON-NLS-1$
boolean isBundle= project.hasNature("org.eclipse.pde.PluginNature"); //$NON-NLS-1$
if (!isBundle)
return false;

final String changeName= CorrectionMessages.UnresolvedElementsSubProcessor_add_annotation_bundle_description;
final String buildPropertiesEntry= "additional.bundles = org.eclipse.jdt.annotation"; //$NON-NLS-1$

ChangeCorrectionProposal proposal= new ChangeCorrectionProposal(changeName, null, IProposalRelevance.ADD_ANNOTATION_BUNDLE) {
@Override
protected Change createChange() throws CoreException {
if (!buildProperties.exists()) {
return new CreateFileChange(buildProperties.getFullPath(), buildPropertiesEntry, null);

} else {
TextFileChange change= new TextFileChange(changeName, buildProperties);
change.setEdit(new MultiTextEdit());

ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
manager.connect(buildProperties.getFullPath(), LocationKind.IFILE, null);
try {
ITextFileBuffer textFileBuffer= manager.getTextFileBuffer(buildProperties.getFullPath(), LocationKind.IFILE);
IDocument document= textFileBuffer.getDocument();
String lineDelim= TextUtilities.getDefaultLineDelimiter(document);

IRegion match= new FindReplaceDocumentAdapter(document).find(0, "additional\\.bundles\\s*=\\s*", true, false, false, true); //$NON-NLS-1$
if (match != null) {
StringBuilder buf= new StringBuilder("org.eclipse.jdt.annotation,\\").append(lineDelim); //$NON-NLS-1$
int spaces= match.getOffset() + match.getLength() - document.getLineOffset(document.getLineOfOffset(match.getOffset()));
while (spaces-- > 0)
buf.append(' ');
change.addEdit(new InsertEdit(match.getOffset() + match.getLength(), buf.toString()));
} else {
String entry= buildPropertiesEntry + lineDelim;
int len= document.getLength();
if (len > 0 && document.getLineInformation(document.getNumberOfLines() - 1).getLength() != 0) {
entry= lineDelim + entry;
}
change.addEdit(new InsertEdit(len, entry));
}
CompilationUnitChange addImportChange= createAddImportChange(cu, name, fullyQualifiedName);
return new CompositeChange(changeName, new Change[] { change, addImportChange});
} catch (BadLocationException e) {
JavaPlugin.log(e);
return new NullChange();
} finally {
manager.disconnect(buildProperties.getFullPath(), LocationKind.IFILE, null);
}
}
}
@Override
public Object getAdditionalProposalInfo(IProgressMonitor monitor) {
return Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_add_annotation_bundle_info, buildPropertiesEntry);
}
};
proposals.add(proposal);
return true;
}


private static void addCopyAnnotationsJarProposal(final ICompilationUnit cu, final Name name, final String fullyQualifiedName, Bundle annotationsBundle, Collection<ICommandAccess> proposals) {
final IJavaProject javaProject= cu.getJavaProject();
final File bundleFile;
Expand Down

0 comments on commit d255e0b

Please sign in to comment.