Search in sources :

Example 61 with CoreException

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);
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) InsertEdit(org.eclipse.text.edits.InsertEdit) Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) CoreException(org.eclipse.core.runtime.CoreException) TextEdit(org.eclipse.text.edits.TextEdit) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) IDocument(org.eclipse.jface.text.IDocument)

Example 62 with CoreException

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);
            }
        }
    }
}
Also used : Path(org.eclipse.core.runtime.Path) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) ResourceChangedEvent(org.eclipse.che.jdt.core.resources.ResourceChangedEvent)

Example 63 with CoreException

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;
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) CoreException(org.eclipse.core.runtime.CoreException) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 64 with CoreException

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
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) IResource(org.eclipse.core.resources.IResource)

Example 65 with CoreException

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;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) IType(org.eclipse.jdt.core.IType) IRefactoringSearchRequestor(org.eclipse.jdt.internal.corext.refactoring.IRefactoringSearchRequestor) CoreException(org.eclipse.core.runtime.CoreException) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) TypeOccurrenceCollector(org.eclipse.jdt.internal.corext.refactoring.rename.TypeOccurrenceCollector) RefactoringSearchEngine2(org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine2)

Aggregations

CoreException (org.eclipse.core.runtime.CoreException)987 IStatus (org.eclipse.core.runtime.IStatus)264 Status (org.eclipse.core.runtime.Status)240 IFile (org.eclipse.core.resources.IFile)176 IOException (java.io.IOException)174 IProject (org.eclipse.core.resources.IProject)133 IResource (org.eclipse.core.resources.IResource)132 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)128 IPath (org.eclipse.core.runtime.IPath)126 ArrayList (java.util.ArrayList)125 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)100 File (java.io.File)95 InvocationTargetException (java.lang.reflect.InvocationTargetException)95 InputStream (java.io.InputStream)76 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)67 Path (org.eclipse.core.runtime.Path)61 ByteArrayInputStream (java.io.ByteArrayInputStream)54 IWorkspace (org.eclipse.core.resources.IWorkspace)53 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)52 IFileStore (org.eclipse.core.filesystem.IFileStore)51