Search in sources :

Example 6 with RefactoringASTParser

use of org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser in project che by eclipse.

the class SelfEncapsulateFieldRefactoring method checkInitialConditions.

//----activation checking ----------------------------------------------------------
@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
    if (fVisibility < 0)
        fVisibility = (fField.getFlags() & (Flags.AccPublic | Flags.AccProtected | Flags.AccPrivate));
    RefactoringStatus result = new RefactoringStatus();
    result.merge(Checks.checkAvailability(fField));
    if (result.hasFatalError())
        return result;
    fRoot = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(fField.getCompilationUnit(), true, pm);
    ISourceRange sourceRange = fField.getNameRange();
    ASTNode node = NodeFinder.perform(fRoot, sourceRange.getOffset(), sourceRange.getLength());
    if (node == null) {
        return mappingErrorFound(result, node);
    }
    fFieldDeclaration = (VariableDeclarationFragment) ASTNodes.getParent(node, VariableDeclarationFragment.class);
    if (fFieldDeclaration == null) {
        return mappingErrorFound(result, node);
    }
    if (fFieldDeclaration.resolveBinding() == null) {
        if (!processCompilerError(result, node))
            result.addFatalError(RefactoringCoreMessages.SelfEncapsulateField_type_not_resolveable);
        return result;
    }
    computeUsedNames();
    return result;
}
Also used : RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) ASTNode(org.eclipse.jdt.core.dom.ASTNode) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 7 with RefactoringASTParser

use of org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser in project che by eclipse.

the class TypeContextChecker method resolveSuperClass.

public static ITypeBinding resolveSuperClass(String superclass, IType typeHandle, StubTypeContext superClassContext) {
    StringBuffer cuString = new StringBuffer();
    cuString.append(superClassContext.getBeforeString());
    cuString.append(superclass);
    cuString.append(superClassContext.getAfterString());
    try {
        ICompilationUnit wc = typeHandle.getCompilationUnit().getWorkingCopy(new WorkingCopyOwner() {
        }, new NullProgressMonitor());
        try {
            wc.getBuffer().setContents(cuString.toString());
            CompilationUnit compilationUnit = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(wc, true);
            ASTNode type = NodeFinder.perform(compilationUnit, superClassContext.getBeforeString().length(), superclass.length());
            if (type instanceof Type) {
                return handleBug84585(((Type) type).resolveBinding());
            } else if (type instanceof Name) {
                ASTNode parent = type.getParent();
                if (parent instanceof Type)
                    return handleBug84585(((Type) parent).resolveBinding());
            }
            throw new IllegalStateException();
        } finally {
            wc.discardWorkingCopy();
        }
    } catch (JavaModelException e) {
        return null;
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) IType(org.eclipse.jdt.core.IType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) JavaModelException(org.eclipse.jdt.core.JavaModelException) WorkingCopyOwner(org.eclipse.jdt.core.WorkingCopyOwner) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name)

Example 8 with RefactoringASTParser

use of org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser in project che by eclipse.

the class InlineMethodRefactoring method resolveSourceProvider.

private static SourceProvider resolveSourceProvider(RefactoringStatus status, ITypeRoot typeRoot, ASTNode invocation) {
    CompilationUnit root = (CompilationUnit) invocation.getRoot();
    IMethodBinding methodBinding = Invocations.resolveBinding(invocation);
    if (methodBinding == null) {
        status.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_error_noMethodDeclaration);
        return null;
    }
    MethodDeclaration declaration = (MethodDeclaration) root.findDeclaringNode(methodBinding);
    if (declaration != null) {
        return new SourceProvider(typeRoot, declaration);
    }
    IMethod method = (IMethod) methodBinding.getJavaElement();
    if (method != null) {
        CompilationUnit methodDeclarationAstRoot;
        ICompilationUnit methodCu = method.getCompilationUnit();
        if (methodCu != null) {
            methodDeclarationAstRoot = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(methodCu, true);
        } else {
            IClassFile classFile = method.getClassFile();
            if (!JavaElementUtil.isSourceAvailable(classFile)) {
                String methodLabel = JavaElementLabels.getTextLabel(method, JavaElementLabels.M_FULLY_QUALIFIED | JavaElementLabels.M_PARAMETER_TYPES);
                status.addFatalError(Messages.format(RefactoringCoreMessages.InlineMethodRefactoring_error_classFile, methodLabel));
                return null;
            }
            methodDeclarationAstRoot = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(classFile, true);
        }
        ASTNode node = methodDeclarationAstRoot.findDeclaringNode(methodBinding.getMethodDeclaration().getKey());
        if (node instanceof MethodDeclaration) {
            return new SourceProvider(methodDeclarationAstRoot.getTypeRoot(), (MethodDeclaration) node);
        }
    }
    status.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_error_noMethodDeclaration);
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) IClassFile(org.eclipse.jdt.core.IClassFile) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IMethod(org.eclipse.jdt.core.IMethod)

Example 9 with RefactoringASTParser

use of org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser in project eclipse.platform.runtime by eclipse.

the class MessageBundleRefactoring method processCompilationUnit.

private void processCompilationUnit(RefactoringStatus result, ICompilationUnit unit, IProgressMonitor monitor) throws CoreException {
    monitor.beginTask("", 2);
    CompilationUnit root = new RefactoringASTParser(AST.JLS3).parse(unit, true, new SubProgressMonitor(monitor, 1));
    ASTRewrite rewriter = ASTRewrite.create(root.getAST());
    processAST(result, root, rewriter, new SubProgressMonitor(monitor, 1));
    TextFileChange change = new TextFileChange(unit.getElementName(), (IFile) unit.getResource());
    try {
        ITextFileBuffer buffer = RefactoringFileBuffers.acquire(unit);
        IDocument document = buffer.getDocument();
        change.setEdit(rewriter.rewriteAST(document, null));
    } finally {
        RefactoringFileBuffers.release(unit);
    }
    fChange.add(change);
    monitor.done();
}
Also used : RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) IDocument(org.eclipse.jface.text.IDocument)

Example 10 with RefactoringASTParser

use of org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser in project che by eclipse.

the class ExtractTempRefactoring method checkNewSource.

private void checkNewSource(SubProgressMonitor monitor, RefactoringStatus result) throws CoreException {
    String newCuSource = fChange.getPreviewContent(new NullProgressMonitor());
    CompilationUnit newCUNode = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(newCuSource, fCu, true, true, monitor);
    IProblem[] newProblems = RefactoringAnalyzeUtil.getIntroducedCompileProblems(newCUNode, fCompilationUnitNode);
    for (int i = 0; i < newProblems.length; i++) {
        IProblem problem = newProblems[i];
        if (problem.isError())
            result.addEntry(new RefactoringStatusEntry((problem.isError() ? RefactoringStatus.ERROR : RefactoringStatus.WARNING), problem.getMessage(), new JavaStringStatusContext(newCuSource, SourceRangeFactory.create(problem))));
    }
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) RefactoringStatusEntry(org.eclipse.ltk.core.refactoring.RefactoringStatusEntry) JavaStringStatusContext(org.eclipse.jdt.internal.corext.refactoring.base.JavaStringStatusContext) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Aggregations

RefactoringASTParser (org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser)15 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)12 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)12 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)6 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)6 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)4 ArrayList (java.util.ArrayList)3 IMethod (org.eclipse.jdt.core.IMethod)3 IType (org.eclipse.jdt.core.IType)3 IProblem (org.eclipse.jdt.core.compiler.IProblem)3 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)3 Name (org.eclipse.jdt.core.dom.Name)3 SimpleName (org.eclipse.jdt.core.dom.SimpleName)3 JavaStringStatusContext (org.eclipse.jdt.internal.corext.refactoring.base.JavaStringStatusContext)3 IDocument (org.eclipse.jface.text.IDocument)3 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)2 IClassFile (org.eclipse.jdt.core.IClassFile)2 ISourceRange (org.eclipse.jdt.core.ISourceRange)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2