Search in sources :

Example 1 with ISourceRange

use of org.eclipse.jdt.core.ISourceRange in project che by eclipse.

the class OrganizeImportsOperation method createTextEdit.

public TextEdit createTextEdit(IProgressMonitor monitor) throws CoreException, OperationCanceledException {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    try {
        fNumberOfImportsAdded = 0;
        fNumberOfImportsRemoved = 0;
        monitor.beginTask(Messages.format(CodeGenerationMessages.OrganizeImportsOperation_description, BasicElementLabels.getFileName(fCompilationUnit)), 9);
        CompilationUnit astRoot = fASTRoot;
        if (astRoot == null) {
            astRoot = SharedASTProvider.getAST(fCompilationUnit, SharedASTProvider.WAIT_YES, new SubProgressMonitor(monitor, 2));
            if (monitor.isCanceled())
                throw new OperationCanceledException();
        } else {
            monitor.worked(2);
        }
        ImportRewrite importsRewrite = StubUtility.createImportRewrite(astRoot, false);
        Set<String> oldSingleImports = new HashSet<>();
        Set<String> oldDemandImports = new HashSet<>();
        List<SimpleName> typeReferences = new ArrayList<>();
        List<SimpleName> staticReferences = new ArrayList<>();
        if (!collectReferences(astRoot, typeReferences, staticReferences, oldSingleImports, oldDemandImports))
            return null;
        monitor.worked(1);
        processor = new TypeReferenceProcessor(oldSingleImports, oldDemandImports, astRoot, importsRewrite, fIgnoreLowerCaseNames);
        Iterator<SimpleName> refIterator = typeReferences.iterator();
        while (refIterator.hasNext()) {
            SimpleName typeRef = refIterator.next();
            processor.add(typeRef);
        }
        hasOpenChoices = processor.process(new SubProgressMonitor(monitor, 3));
        addStaticImports(staticReferences, importsRewrite);
        if (hasOpenChoices) {
            choices = processor.getChoices();
            ISourceRange[] ranges = processor.getChoicesSourceRanges();
            if (fChooseImportQuery != null) {
                TypeNameMatch[] chosen = fChooseImportQuery.chooseImports(choices, ranges);
                for (int i = 0; i < chosen.length; i++) {
                    TypeNameMatch typeInfo = chosen[i];
                    importsRewrite.addImport(typeInfo.getFullyQualifiedName());
                }
            } else if (chosenFQN != null) {
                chosenFQN.forEach(importsRewrite::addImport);
            }
        }
        TextEdit result = importsRewrite.rewriteImports(new SubProgressMonitor(monitor, 3));
        determineImportDifferences(importsRewrite, oldSingleImports, oldDemandImports);
        return result;
    } finally {
        monitor.done();
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch) TextEdit(org.eclipse.text.edits.TextEdit) HashSet(java.util.HashSet) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 2 with ISourceRange

use of org.eclipse.jdt.core.ISourceRange in project che by eclipse.

the class RenameTypeProcessor method analyseEnclosedTypes.

private RefactoringStatus analyseEnclosedTypes() throws CoreException {
    final ISourceRange typeRange = fType.getSourceRange();
    final RefactoringStatus result = new RefactoringStatus();
    CompilationUnit cuNode = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(fType.getCompilationUnit(), false);
    cuNode.accept(new ASTVisitor() {

        @Override
        public boolean visit(TypeDeclaration node) {
            // enums and annotations can't be local
            if (node.getStartPosition() <= typeRange.getOffset())
                return true;
            if (node.getStartPosition() > typeRange.getOffset() + typeRange.getLength())
                return true;
            if (getNewElementName().equals(node.getName().getIdentifier())) {
                RefactoringStatusContext context = JavaStatusContext.create(fType.getCompilationUnit(), node);
                String msg = null;
                if (node.isLocalTypeDeclaration()) {
                    msg = Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_local_type, new String[] { JavaElementUtil.createSignature(fType), getNewElementLabel() });
                } else if (node.isMemberTypeDeclaration()) {
                    msg = Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_member_type, new String[] { JavaElementUtil.createSignature(fType), getNewElementLabel() });
                }
                if (msg != null)
                    result.addError(msg, context);
            }
            MethodDeclaration[] methods = node.getMethods();
            for (int i = 0; i < methods.length; i++) {
                if (Modifier.isNative(methods[i].getModifiers())) {
                    RefactoringStatusContext context = JavaStatusContext.create(fType.getCompilationUnit(), methods[i]);
                    String msg = Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_enclosed_type_native, BasicElementLabels.getJavaElementName(node.getName().getIdentifier()));
                    result.addWarning(msg, context);
                }
            }
            return true;
        }
    });
    return result;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) RefactoringStatusContext(org.eclipse.ltk.core.refactoring.RefactoringStatusContext) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) ISourceRange(org.eclipse.jdt.core.ISourceRange) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 3 with ISourceRange

use of org.eclipse.jdt.core.ISourceRange in project che by eclipse.

the class RenameNonVirtualMethodProcessor method addDeclarationUpdate.

private void addDeclarationUpdate(TextChangeManager manager) throws CoreException {
    if (getDelegateUpdating()) {
        // create the delegate
        CompilationUnitRewrite rewrite = new CompilationUnitRewrite(getDeclaringCU());
        rewrite.setResolveBindings(true);
        MethodDeclaration methodDeclaration = ASTNodeSearchUtil.getMethodDeclarationNode(getMethod(), rewrite.getRoot());
        DelegateMethodCreator creator = new DelegateMethodCreator();
        creator.setDeclaration(methodDeclaration);
        creator.setDeclareDeprecated(getDeprecateDelegates());
        creator.setSourceRewrite(rewrite);
        creator.setCopy(true);
        creator.setNewElementName(getNewElementName());
        creator.prepareDelegate();
        creator.createEdit();
        CompilationUnitChange cuChange = rewrite.createChange(true);
        if (cuChange != null) {
            cuChange.setKeepPreviewEdits(true);
            manager.manage(getDeclaringCU(), cuChange);
        }
    }
    String editName = RefactoringCoreMessages.RenameMethodRefactoring_update_declaration;
    ISourceRange nameRange = getMethod().getNameRange();
    ReplaceEdit replaceEdit = new ReplaceEdit(nameRange.getOffset(), nameRange.getLength(), getNewElementName());
    addTextEdit(manager.get(getDeclaringCU()), editName, replaceEdit);
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) DelegateMethodCreator(org.eclipse.jdt.internal.corext.refactoring.delegates.DelegateMethodCreator) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 4 with ISourceRange

use of org.eclipse.jdt.core.ISourceRange in project che by eclipse.

the class RenameAnalyzeUtil method addReferenceShadowedError.

private static void addReferenceShadowedError(ICompilationUnit cu, SearchMatch newMatch, String newElementName, RefactoringStatus result) {
    //TODO: should not have to filter declarations:
    if (newMatch instanceof MethodDeclarationMatch || newMatch instanceof FieldDeclarationMatch)
        return;
    ISourceRange range = getOldSourceRange(newMatch);
    RefactoringStatusContext context = JavaStatusContext.create(cu, range);
    String message = Messages.format(RefactoringCoreMessages.RenameAnalyzeUtil_reference_shadowed, new String[] { BasicElementLabels.getFileName(cu), BasicElementLabels.getJavaElementName(newElementName) });
    result.addError(message, context);
}
Also used : RefactoringStatusContext(org.eclipse.ltk.core.refactoring.RefactoringStatusContext) MethodDeclarationMatch(org.eclipse.jdt.core.search.MethodDeclarationMatch) FieldDeclarationMatch(org.eclipse.jdt.core.search.FieldDeclarationMatch) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 5 with ISourceRange

use of org.eclipse.jdt.core.ISourceRange in project che by eclipse.

the class RenameAnalyzeUtil method addShadowsError.

private static void addShadowsError(ICompilationUnit cu, SearchMatch oldMatch, RefactoringStatus result) {
    //TODO: should not have to filter declarations:
    if (oldMatch instanceof MethodDeclarationMatch || oldMatch instanceof FieldDeclarationMatch)
        return;
    ISourceRange range = new SourceRange(oldMatch.getOffset(), oldMatch.getLength());
    RefactoringStatusContext context = JavaStatusContext.create(cu, range);
    String message = Messages.format(RefactoringCoreMessages.RenameAnalyzeUtil_shadows, BasicElementLabels.getFileName(cu));
    result.addError(message, context);
}
Also used : RefactoringStatusContext(org.eclipse.ltk.core.refactoring.RefactoringStatusContext) ISourceRange(org.eclipse.jdt.core.ISourceRange) SourceRange(org.eclipse.jdt.core.SourceRange) MethodDeclarationMatch(org.eclipse.jdt.core.search.MethodDeclarationMatch) FieldDeclarationMatch(org.eclipse.jdt.core.search.FieldDeclarationMatch) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Aggregations

ISourceRange (org.eclipse.jdt.core.ISourceRange)53 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)18 IJavaElement (org.eclipse.jdt.core.IJavaElement)14 JavaModelException (org.eclipse.jdt.core.JavaModelException)13 ASTNode (org.eclipse.jdt.core.dom.ASTNode)12 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)10 ISourceReference (org.eclipse.jdt.core.ISourceReference)9 IType (org.eclipse.jdt.core.IType)8 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)7 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)6 SourceRange (org.eclipse.jdt.core.SourceRange)6 IBuffer (org.eclipse.jdt.core.IBuffer)5 IMethod (org.eclipse.jdt.core.IMethod)5 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)5 ArrayList (java.util.ArrayList)4 IField (org.eclipse.jdt.core.IField)4 IMember (org.eclipse.jdt.core.IMember)4 CoreException (org.eclipse.core.runtime.CoreException)3 IClassFile (org.eclipse.jdt.core.IClassFile)3