Search in sources :

Example 71 with AbstractTypeDeclaration

use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.

the class ChangeSignatureProcessor method createChangeManager.

private TextChangeManager createChangeManager(IProgressMonitor pm, RefactoringStatus result) throws CoreException {
    pm.beginTask(RefactoringCoreMessages.ChangeSignatureRefactoring_preview, 2);
    fChangeManager = new TextChangeManager();
    boolean isNoArgConstructor = isNoArgConstructor();
    Map<ICompilationUnit, Set<IType>> namedSubclassMapping = null;
    if (isNoArgConstructor) {
        //create only when needed;
        namedSubclassMapping = createNamedSubclassMapping(new SubProgressMonitor(pm, 1));
    } else {
        pm.worked(1);
    }
    for (int i = 0; i < fOccurrences.length; i++) {
        if (pm.isCanceled())
            throw new OperationCanceledException();
        SearchResultGroup group = fOccurrences[i];
        ICompilationUnit cu = group.getCompilationUnit();
        if (cu == null)
            continue;
        CompilationUnitRewrite cuRewrite;
        if (cu.equals(getCu())) {
            cuRewrite = fBaseCuRewrite;
        } else {
            cuRewrite = new CompilationUnitRewrite(cu);
            cuRewrite.getASTRewrite().setTargetSourceRangeComputer(new TightSourceRangeComputer());
        }
        ASTNode[] nodes = ASTNodeSearchUtil.findNodes(group.getSearchResults(), cuRewrite.getRoot());
        //IntroduceParameterObjectRefactoring needs to update declarations first:
        List<OccurrenceUpdate<? extends ASTNode>> deferredUpdates = new ArrayList<OccurrenceUpdate<? extends ASTNode>>();
        for (int j = 0; j < nodes.length; j++) {
            OccurrenceUpdate<? extends ASTNode> update = createOccurrenceUpdate(nodes[j], cuRewrite, result);
            if (update instanceof DeclarationUpdate) {
                update.updateNode();
            } else {
                deferredUpdates.add(update);
            }
        }
        for (Iterator<OccurrenceUpdate<? extends ASTNode>> iter = deferredUpdates.iterator(); iter.hasNext(); ) {
            iter.next().updateNode();
        }
        if (isNoArgConstructor && namedSubclassMapping.containsKey(cu)) {
            //only non-anonymous subclasses may have noArgConstructors to modify - see bug 43444
            Set<IType> subtypes = namedSubclassMapping.get(cu);
            for (Iterator<IType> iter = subtypes.iterator(); iter.hasNext(); ) {
                IType subtype = iter.next();
                AbstractTypeDeclaration subtypeNode = ASTNodeSearchUtil.getAbstractTypeDeclarationNode(subtype, cuRewrite.getRoot());
                if (subtypeNode != null)
                    modifyImplicitCallsToNoArgConstructor(subtypeNode, cuRewrite);
            }
        }
        TextChange change = cuRewrite.createChange(true);
        if (change != null)
            fChangeManager.manage(cu, change);
    }
    pm.done();
    return fChangeManager;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) Set(java.util.Set) HashSet(java.util.HashSet) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) TextChange(org.eclipse.ltk.core.refactoring.TextChange) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IType(org.eclipse.jdt.core.IType) TightSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer) ASTNode(org.eclipse.jdt.core.dom.ASTNode) TextChangeManager(org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 72 with AbstractTypeDeclaration

use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project buck by facebook.

the class JavaFileParser method getFullyQualifiedTypeName.

@Nullable
private String getFullyQualifiedTypeName(AbstractTypeDeclaration node) {
    LinkedList<String> nameParts = Lists.newLinkedList();
    nameParts.add(node.getName().toString());
    ASTNode parent = node.getParent();
    while (!(parent instanceof CompilationUnit)) {
        if (parent instanceof AbstractTypeDeclaration) {
            nameParts.addFirst(((AbstractTypeDeclaration) parent).getName().toString());
            parent = parent.getParent();
        } else if (parent instanceof AnonymousClassDeclaration) {
            // name.
            return null;
        } else {
            throw new RuntimeException("Unexpected parent " + parent + " for " + node);
        }
    }
    // A Java file might not have a package. Hopefully all of ours do though...
    PackageDeclaration packageDecl = ((CompilationUnit) parent).getPackage();
    if (packageDecl != null) {
        nameParts.addFirst(packageDecl.getName().toString());
    }
    return Joiner.on(".").join(nameParts);
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) PackageDeclaration(org.eclipse.jdt.core.dom.PackageDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) Nullable(javax.annotation.Nullable)

Example 73 with AbstractTypeDeclaration

use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.

the class ConvertAnonymousToNestedRefactoring method addNestedClass.

private void addNestedClass(CompilationUnitRewrite rewrite, ITypeBinding[] typeParameters) throws CoreException {
    final AbstractTypeDeclaration declarations = (AbstractTypeDeclaration) ASTNodes.getParent(fAnonymousInnerClassNode, AbstractTypeDeclaration.class);
    int index = findIndexOfFistNestedClass(declarations.bodyDeclarations());
    if (index == -1)
        index = 0;
    rewrite.getASTRewrite().getListRewrite(declarations, declarations.getBodyDeclarationsProperty()).insertAt(createNewNestedClass(rewrite, typeParameters), index, null);
}
Also used : AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 74 with AbstractTypeDeclaration

use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.

the class ConvertAnonymousToNestedRefactoring method createNewNestedClass.

private AbstractTypeDeclaration createNewNestedClass(CompilationUnitRewrite rewrite, ITypeBinding[] typeParameters) throws CoreException {
    final AST ast = fAnonymousInnerClassNode.getAST();
    final TypeDeclaration newDeclaration = ast.newTypeDeclaration();
    newDeclaration.setInterface(false);
    newDeclaration.setJavadoc(null);
    newDeclaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiersForNestedClass()));
    newDeclaration.setName(ast.newSimpleName(fClassName));
    TypeParameter parameter = null;
    for (int index = 0; index < typeParameters.length; index++) {
        parameter = ast.newTypeParameter();
        parameter.setName(ast.newSimpleName(typeParameters[index].getName()));
        newDeclaration.typeParameters().add(parameter);
    }
    setSuperType(newDeclaration);
    IJavaProject project = fCu.getJavaProject();
    IVariableBinding[] bindings = getUsedLocalVariables();
    ArrayList<String> fieldNames = new ArrayList<String>();
    for (int i = 0; i < bindings.length; i++) {
        String name = StubUtility.getBaseName(bindings[i], project);
        String[] fieldNameProposals = StubUtility.getVariableNameSuggestions(NamingConventions.VK_INSTANCE_FIELD, project, name, 0, fieldNames, true);
        fieldNames.add(fieldNameProposals[0]);
        if (fLinkedProposalModel != null) {
            LinkedProposalPositionGroup positionGroup = fLinkedProposalModel.getPositionGroup(KEY_FIELD_NAME_EXT + i, true);
            for (int k = 0; k < fieldNameProposals.length; k++) {
                positionGroup.addProposal(fieldNameProposals[k], null, fieldNameProposals.length - k);
            }
        }
    }
    String[] allFieldNames = fieldNames.toArray(new String[fieldNames.size()]);
    List<BodyDeclaration> newBodyDeclarations = newDeclaration.bodyDeclarations();
    createFieldsForAccessedLocals(rewrite, bindings, allFieldNames, newBodyDeclarations);
    MethodDeclaration newConstructorDecl = createNewConstructor(rewrite, bindings, allFieldNames);
    if (newConstructorDecl != null) {
        newBodyDeclarations.add(newConstructorDecl);
    }
    updateAndMoveBodyDeclarations(rewrite, bindings, allFieldNames, newBodyDeclarations, newConstructorDecl);
    if (doAddComments()) {
        String[] parameterNames = new String[typeParameters.length];
        for (int index = 0; index < parameterNames.length; index++) {
            parameterNames[index] = typeParameters[index].getName();
        }
        String string = CodeGeneration.getTypeComment(rewrite.getCu(), fClassName, parameterNames, StubUtility.getLineDelimiterUsed(fCu));
        if (string != null) {
            Javadoc javadoc = (Javadoc) rewrite.getASTRewrite().createStringPlaceholder(string, ASTNode.JAVADOC);
            newDeclaration.setJavadoc(javadoc);
        }
    }
    if (fLinkedProposalModel != null) {
        addLinkedPosition(KEY_TYPE_NAME, newDeclaration.getName(), rewrite.getASTRewrite(), false);
        ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(fLinkedProposalModel, rewrite.getASTRewrite(), newDeclaration.modifiers(), false);
    }
    return newDeclaration;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ArrayList(java.util.ArrayList) Javadoc(org.eclipse.jdt.core.dom.Javadoc) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) IJavaProject(org.eclipse.jdt.core.IJavaProject) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) LinkedProposalPositionGroup(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup)

Example 75 with AbstractTypeDeclaration

use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.

the class ExtractConstantRefactoring method createConstantDeclaration.

private void createConstantDeclaration() throws CoreException {
    Type type = getConstantType();
    IExpressionFragment fragment = getSelectedExpression();
    Expression initializer = getSelectedExpression().createCopyTarget(fCuRewrite.getASTRewrite(), true);
    AST ast = fCuRewrite.getAST();
    VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
    variableDeclarationFragment.setName(ast.newSimpleName(fConstantName));
    variableDeclarationFragment.setInitializer(initializer);
    FieldDeclaration fieldDeclaration = ast.newFieldDeclaration(variableDeclarationFragment);
    fieldDeclaration.setType(type);
    Modifier.ModifierKeyword accessModifier = Modifier.ModifierKeyword.toKeyword(fVisibility);
    if (accessModifier != null)
        fieldDeclaration.modifiers().add(ast.newModifier(accessModifier));
    fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));
    fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD));
    boolean createComments = JavaPreferencesSettings.getCodeGenerationSettings(fCu.getJavaProject()).createComments;
    if (createComments) {
        String comment = CodeGeneration.getFieldComment(fCu, getConstantTypeName(), fConstantName, StubUtility.getLineDelimiterUsed(fCu));
        if (comment != null && comment.length() > 0) {
            Javadoc doc = (Javadoc) fCuRewrite.getASTRewrite().createStringPlaceholder(comment, ASTNode.JAVADOC);
            fieldDeclaration.setJavadoc(doc);
        }
    }
    AbstractTypeDeclaration parent = getContainingTypeDeclarationNode();
    ListRewrite listRewrite = fCuRewrite.getASTRewrite().getListRewrite(parent, parent.getBodyDeclarationsProperty());
    TextEditGroup msg = fCuRewrite.createGroupDescription(RefactoringCoreMessages.ExtractConstantRefactoring_declare_constant);
    if (insertFirst()) {
        listRewrite.insertFirst(fieldDeclaration, msg);
    } else {
        listRewrite.insertAfter(fieldDeclaration, getNodeToInsertConstantDeclarationAfter(), msg);
    }
    if (fLinkedProposalModel != null) {
        ASTRewrite rewrite = fCuRewrite.getASTRewrite();
        LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
        nameGroup.addPosition(rewrite.track(variableDeclarationFragment.getName()), true);
        String[] nameSuggestions = guessConstantNames();
        if (nameSuggestions.length > 0 && !nameSuggestions[0].equals(fConstantName)) {
            nameGroup.addProposal(fConstantName, null, nameSuggestions.length + 1);
        }
        for (int i = 0; i < nameSuggestions.length; i++) {
            nameGroup.addProposal(nameSuggestions[i], null, nameSuggestions.length - i);
        }
        LinkedProposalPositionGroup typeGroup = fLinkedProposalModel.getPositionGroup(KEY_TYPE, true);
        typeGroup.addPosition(rewrite.track(type), true);
        ITypeBinding typeBinding = guessBindingForReference(fragment.getAssociatedExpression());
        if (typeBinding != null) {
            ITypeBinding[] relaxingTypes = ASTResolving.getNarrowingTypes(ast, typeBinding);
            for (int i = 0; i < relaxingTypes.length; i++) {
                typeGroup.addProposal(relaxingTypes[i], fCuRewrite.getCu(), relaxingTypes.length - i);
            }
        }
        boolean isInterface = parent.resolveBinding() != null && parent.resolveBinding().isInterface();
        ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(fLinkedProposalModel, rewrite, fieldDeclaration.modifiers(), isInterface);
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) Type(org.eclipse.jdt.core.dom.Type) IExpressionFragment(org.eclipse.jdt.internal.corext.dom.fragments.IExpressionFragment) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier) TextEditGroup(org.eclipse.text.edits.TextEditGroup) LinkedProposalPositionGroup(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)233 ASTNode (org.eclipse.jdt.core.dom.ASTNode)116 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)88 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)85 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)81 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)72 AnonymousClassDeclaration (org.eclipse.jdt.core.dom.AnonymousClassDeclaration)52 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)49 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)49 AST (org.eclipse.jdt.core.dom.AST)44 ArrayList (java.util.ArrayList)43 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)41 IType (org.eclipse.jdt.core.IType)40 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)38 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)38 Type (org.eclipse.jdt.core.dom.Type)37 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)36 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)34 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)25 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)24