use of org.eclipse.core.runtime.CoreException in project che by eclipse.
the class LocalCorrectionsSubProcessor method getInvalidOperatorProposals.
public static void getInvalidOperatorProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
CompilationUnit root = context.getASTRoot();
AST ast = root.getAST();
ASTNode selectedNode = problem.getCoveringNode(root);
while (selectedNode instanceof ParenthesizedExpression) {
selectedNode = ((ParenthesizedExpression) selectedNode).getExpression();
}
if (selectedNode instanceof PrefixExpression) {
// !x instanceof X -> !(x instanceof X)
PrefixExpression expression = (PrefixExpression) selectedNode;
if (expression.getOperator() == PrefixExpression.Operator.NOT) {
ASTNode parent = expression.getParent();
String label = null;
switch(parent.getNodeType()) {
case ASTNode.INSTANCEOF_EXPRESSION:
label = CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_instanceof_description;
break;
case ASTNode.INFIX_EXPRESSION:
InfixExpression infixExpression = (InfixExpression) parent;
label = Messages.format(CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_description, infixExpression.getOperator().toString());
break;
}
if (label != null) {
ASTRewrite rewrite = ASTRewrite.create(ast);
rewrite.replace(selectedNode, rewrite.createMoveTarget(expression.getOperand()), null);
ParenthesizedExpression newParentExpr = ast.newParenthesizedExpression();
newParentExpr.setExpression((Expression) rewrite.createMoveTarget(parent));
PrefixExpression newPrefixExpr = ast.newPrefixExpression();
newPrefixExpr.setOperand(newParentExpr);
newPrefixExpr.setOperator(PrefixExpression.Operator.NOT);
rewrite.replace(parent, newPrefixExpr, null);
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVALID_OPERATOR, image);
proposals.add(proposal);
}
}
} else if (selectedNode instanceof InfixExpression && isBitOperation((((InfixExpression) selectedNode).getOperator()))) {
// a & b == c -> (a & b) == c
final CompareInBitWiseOpFinder opFinder = new CompareInBitWiseOpFinder(selectedNode);
if (opFinder.getCompareExpression() != null) {
// compare operation inside bit operations: set parents
String label = CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_bitop_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST);
CUCorrectionProposal proposal = new CUCorrectionProposal(label, context.getCompilationUnit(), IProposalRelevance.INVALID_OPERATOR, image) {
@Override
protected void addEdits(IDocument document, TextEdit edit) throws CoreException {
InfixExpression compareExpression = opFinder.getCompareExpression();
InfixExpression expression = opFinder.getParentInfixExpression();
ASTNode left = compareExpression.getLeftOperand();
if (expression.getStartPosition() < left.getStartPosition()) {
edit.addChild(new InsertEdit(expression.getStartPosition(), String.valueOf('(')));
edit.addChild(new InsertEdit(ASTNodes.getExclusiveEnd(left), String.valueOf(')')));
}
ASTNode rigth = compareExpression.getRightOperand();
int selEnd = ASTNodes.getExclusiveEnd(expression);
if (selEnd > ASTNodes.getExclusiveEnd(rigth)) {
edit.addChild(new InsertEdit(rigth.getStartPosition(), String.valueOf('(')));
edit.addChild(new InsertEdit(selEnd, String.valueOf(')')));
}
}
};
proposals.add(proposal);
}
}
}
use of org.eclipse.core.runtime.CoreException in project che by eclipse.
the class ProjectListeners method handleEvent.
public void handleEvent(ProjectItemModifiedEvent event) {
final String eventPath = event.getPath();
if (!isJavaProject(event.getProject())) {
return;
}
try {
JavaModelManager.getJavaModelManager().deltaState.resourceChanged(new ResourceChangedEvent(workspace, event));
} catch (Throwable t) {
//catch all exceptions that may be happened
LOG.error("Can't update java model in " + eventPath, t);
}
if (event.getType() == ProjectItemModifiedEvent.EventType.UPDATED) {
ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
ITextFileBuffer fileBuffer = manager.getTextFileBuffer(new Path(eventPath), LocationKind.IFILE);
if (fileBuffer != null) {
try {
fileBuffer.revert(new NullProgressMonitor());
} catch (CoreException e) {
LOG.error("Can't read file content: " + eventPath, e);
}
}
}
}
use of org.eclipse.core.runtime.CoreException in project che by eclipse.
the class CompilationUnitRewriteOperationsFix method createChange.
// /**
// * {@inheritDoc}
// */
// @Override
// public LinkedProposalModel getLinkedPositions() {
// if (!fLinkedProposalModel.hasLinkedPositions())
// return null;
//
// return fLinkedProposalModel;
// }
/**
* {@inheritDoc}
*/
public CompilationUnitChange createChange(IProgressMonitor progressMonitor) throws CoreException {
CompilationUnitRewrite cuRewrite = new CompilationUnitRewrite((ICompilationUnit) fCompilationUnit.getJavaElement(), fCompilationUnit);
fLinkedProposalModel.clear();
for (int i = 0; i < fOperations.length; i++) {
CompilationUnitRewriteOperation operation = fOperations[i];
operation.rewriteAST(cuRewrite, fLinkedProposalModel);
}
CompilationUnitChange result = cuRewrite.createChange(getDisplayString(), true, null);
if (result == null)
throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.ID_PLUGIN, Messages.format(FixMessages.CompilationUnitRewriteOperationsFix_nullChangeError, getDisplayString())));
return result;
}
use of org.eclipse.core.runtime.CoreException in project che by eclipse.
the class CreateCopyOfCompilationUnitChange method setEncoding.
private void setEncoding(ICompilationUnit unit) {
IResource resource = unit.getResource();
// no file so the encoding is taken from the target
if (!(resource instanceof IFile))
return;
IFile file = (IFile) resource;
try {
String encoding = file.getCharset(false);
if (encoding != null) {
setEncoding(encoding, true);
} else {
encoding = file.getCharset(true);
if (encoding != null) {
setEncoding(encoding, false);
}
}
} catch (CoreException e) {
// Take encoding from target
}
}
use of org.eclipse.core.runtime.CoreException in project che by eclipse.
the class CreateCopyOfCompilationUnitChange method getReferences.
private static SearchResultGroup getReferences(final ICompilationUnit copy, IProgressMonitor monitor) throws JavaModelException {
final ICompilationUnit[] copies = new ICompilationUnit[] { copy };
IJavaSearchScope scope = SearchEngine.createJavaSearchScope(copies);
final IType type = copy.findPrimaryType();
if (type == null)
return null;
SearchPattern pattern = createSearchPattern(type);
final RefactoringSearchEngine2 engine = new RefactoringSearchEngine2(pattern);
engine.setScope(scope);
engine.setWorkingCopies(copies);
engine.setRequestor(new IRefactoringSearchRequestor() {
TypeOccurrenceCollector fTypeOccurrenceCollector = new TypeOccurrenceCollector(type);
public SearchMatch acceptSearchMatch(SearchMatch match) {
try {
return fTypeOccurrenceCollector.acceptSearchMatch2(copy, match);
} catch (CoreException e) {
JavaPlugin.log(e);
return null;
}
}
});
engine.searchPattern(monitor);
final Object[] results = engine.getResults();
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=106127)
for (int index = 0; index < results.length; index++) {
SearchResultGroup group = (SearchResultGroup) results[index];
if (group.getCompilationUnit().equals(copy))
return group;
}
return null;
}
Aggregations