Search in sources :

Example 21 with VariableDeclaration

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

the class ExtractMethodRefactoring method createNewMethodDeclaration.

private MethodDeclaration createNewMethodDeclaration() {
    MethodDeclaration result = fAST.newMethodDeclaration();
    int modifiers = fVisibility;
    BodyDeclaration enclosingBodyDeclaration = fAnalyzer.getEnclosingBodyDeclaration();
    boolean isDestinationInterface = isDestinationInterface();
    if (isDestinationInterface && !(enclosingBodyDeclaration instanceof MethodDeclaration && enclosingBodyDeclaration.getParent() == fDestination && Modifier.isPublic(enclosingBodyDeclaration.getModifiers()))) {
        modifiers = Modifier.NONE;
    }
    boolean shouldBeStatic = false;
    ASTNode currentParent = enclosingBodyDeclaration;
    do {
        if (currentParent instanceof BodyDeclaration) {
            shouldBeStatic = shouldBeStatic || JdtFlags.isStatic((BodyDeclaration) currentParent);
        }
        currentParent = currentParent.getParent();
    } while (!shouldBeStatic && currentParent != null && currentParent != fDestination);
    if (shouldBeStatic || fAnalyzer.getForceStatic() || forceStatic()) {
        modifiers |= Modifier.STATIC;
    } else if (isDestinationInterface) {
        modifiers |= Modifier.DEFAULT;
    }
    ITypeBinding[] typeVariables = computeLocalTypeVariables(modifiers);
    List<TypeParameter> typeParameters = result.typeParameters();
    for (int i = 0; i < typeVariables.length; i++) {
        TypeParameter parameter = fAST.newTypeParameter();
        parameter.setName(fAST.newSimpleName(typeVariables[i].getName()));
        ITypeBinding[] bounds = typeVariables[i].getTypeBounds();
        for (int j = 0; j < bounds.length; j++) if (//$NON-NLS-1$
        !"java.lang.Object".equals(bounds[j].getQualifiedName()))
            parameter.typeBounds().add(fImportRewriter.addImport(bounds[j], fAST));
        typeParameters.add(parameter);
    }
    result.modifiers().addAll(ASTNodeFactory.newModifiers(fAST, modifiers));
    result.setReturnType2((Type) ASTNode.copySubtree(fAST, fAnalyzer.getReturnType()));
    result.setName(fAST.newSimpleName(fMethodName));
    ImportRewriteContext context = new ContextSensitiveImportRewriteContext(enclosingBodyDeclaration, fImportRewriter);
    List<SingleVariableDeclaration> parameters = result.parameters();
    for (int i = 0; i < fParameterInfos.size(); i++) {
        ParameterInfo info = fParameterInfos.get(i);
        VariableDeclaration infoDecl = getVariableDeclaration(info);
        SingleVariableDeclaration parameter = fAST.newSingleVariableDeclaration();
        parameter.modifiers().addAll(ASTNodeFactory.newModifiers(fAST, ASTNodes.getModifiers(infoDecl)));
        parameter.setType(ASTNodeFactory.newType(fAST, infoDecl, fImportRewriter, context));
        parameter.setName(fAST.newSimpleName(info.getNewName()));
        parameter.setVarargs(info.isNewVarargs());
        parameters.add(parameter);
    }
    List<Type> exceptions = result.thrownExceptionTypes();
    ITypeBinding[] exceptionTypes = fAnalyzer.getExceptions(fThrowRuntimeExceptions);
    for (int i = 0; i < exceptionTypes.length; i++) {
        ITypeBinding exceptionType = exceptionTypes[i];
        exceptions.add(fImportRewriter.addImport(exceptionType, fAST, context));
    }
    return result;
}
Also used : TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration)

Example 22 with VariableDeclaration

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

the class ExtractMethodRefactoring method initializeParameterInfos.

//---- Helper methods ------------------------------------------------------------------------
private void initializeParameterInfos() {
    IVariableBinding[] arguments = fAnalyzer.getArguments();
    fParameterInfos = new ArrayList<ParameterInfo>(arguments.length);
    ASTNode root = fAnalyzer.getEnclosingBodyDeclaration();
    ParameterInfo vararg = null;
    for (int i = 0; i < arguments.length; i++) {
        IVariableBinding argument = arguments[i];
        if (argument == null)
            continue;
        VariableDeclaration declaration = ASTNodes.findVariableDeclaration(argument, root);
        boolean isVarargs = declaration instanceof SingleVariableDeclaration ? ((SingleVariableDeclaration) declaration).isVarargs() : false;
        ParameterInfo info = new ParameterInfo(argument, getType(declaration, isVarargs), argument.getName(), i);
        if (isVarargs) {
            vararg = info;
        } else {
            fParameterInfos.add(info);
        }
    }
    if (vararg != null) {
        fParameterInfos.add(vararg);
    }
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 23 with VariableDeclaration

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

the class TempDeclarationFinder method findTempDeclaration.

/**
	 * @return <code>null</code> if the selection is invalid or does not cover a temp
	 * declaration or reference.
	 */
public static VariableDeclaration findTempDeclaration(CompilationUnit cu, int selectionOffset, int selectionLength) {
    TempSelectionAnalyzer analyzer = new TempSelectionAnalyzer(selectionOffset, selectionLength);
    cu.accept(analyzer);
    ASTNode[] selected = analyzer.getSelectedNodes();
    if (selected == null || selected.length != 1)
        return null;
    ASTNode selectedNode = selected[0];
    if (selectedNode instanceof VariableDeclaration)
        return (VariableDeclaration) selectedNode;
    if (selectedNode instanceof Name) {
        Name reference = (Name) selectedNode;
        IBinding binding = reference.resolveBinding();
        if (binding == null)
            return null;
        ASTNode declaringNode = cu.findDeclaringNode(binding);
        if (declaringNode instanceof VariableDeclaration)
            return (VariableDeclaration) declaringNode;
        else
            return null;
    } else if (selectedNode instanceof VariableDeclarationStatement) {
        VariableDeclarationStatement vds = (VariableDeclarationStatement) selectedNode;
        if (vds.fragments().size() != 1)
            return null;
        return (VariableDeclaration) vds.fragments().get(0);
    }
    return null;
}
Also used : IBinding(org.eclipse.jdt.core.dom.IBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name)

Example 24 with VariableDeclaration

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

the class JavadocTagsSubProcessor method getMissingJavadocCommentProposals.

public static void getMissingJavadocCommentProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
    ASTNode node = problem.getCoveringNode(context.getASTRoot());
    if (node == null) {
        return;
    }
    BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(node);
    if (declaration == null) {
        return;
    }
    ICompilationUnit cu = context.getCompilationUnit();
    ITypeBinding binding = Bindings.getBindingOfParentType(declaration);
    if (binding == null) {
        return;
    }
    if (declaration instanceof MethodDeclaration) {
        MethodDeclaration methodDecl = (MethodDeclaration) declaration;
        IMethodBinding methodBinding = methodDecl.resolveBinding();
        IMethodBinding overridden = null;
        if (methodBinding != null) {
            overridden = Bindings.findOverriddenMethod(methodBinding, true);
        }
        String string = CodeGeneration.getMethodComment(cu, binding.getName(), methodDecl, overridden, String.valueOf('\n'));
        if (string != null) {
            String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_method_description;
            proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_METHOD, declaration.getStartPosition(), string));
        }
    } else if (declaration instanceof AbstractTypeDeclaration) {
        String typeQualifiedName = Bindings.getTypeQualifiedName(binding);
        String[] typeParamNames;
        if (declaration instanceof TypeDeclaration) {
            List<TypeParameter> typeParams = ((TypeDeclaration) declaration).typeParameters();
            typeParamNames = new String[typeParams.size()];
            for (int i = 0; i < typeParamNames.length; i++) {
                typeParamNames[i] = (typeParams.get(i)).getName().getIdentifier();
            }
        } else {
            typeParamNames = new String[0];
        }
        String string = CodeGeneration.getTypeComment(cu, typeQualifiedName, typeParamNames, String.valueOf('\n'));
        if (string != null) {
            String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_type_description;
            proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_TYPE, declaration.getStartPosition(), string));
        }
    } else if (declaration instanceof FieldDeclaration) {
        //$NON-NLS-1$
        String comment = "/**\n *\n */\n";
        List<VariableDeclarationFragment> fragments = ((FieldDeclaration) declaration).fragments();
        if (fragments != null && fragments.size() > 0) {
            VariableDeclaration decl = fragments.get(0);
            String fieldName = decl.getName().getIdentifier();
            String typeName = binding.getName();
            comment = CodeGeneration.getFieldComment(cu, typeName, fieldName, String.valueOf('\n'));
        }
        if (comment != null) {
            String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_field_description;
            proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_FIELD, declaration.getStartPosition(), comment));
        }
    } else if (declaration instanceof EnumConstantDeclaration) {
        EnumConstantDeclaration enumDecl = (EnumConstantDeclaration) declaration;
        String id = enumDecl.getName().getIdentifier();
        String comment = CodeGeneration.getFieldComment(cu, binding.getName(), id, String.valueOf('\n'));
        String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_enumconst_description;
        proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_ENUM, declaration.getStartPosition(), comment));
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) EnumConstantDeclaration(org.eclipse.jdt.core.dom.EnumConstantDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) List(java.util.List) ArrayList(java.util.ArrayList) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 25 with VariableDeclaration

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

the class RenameLocalVariableProcessor method initAST.

private void initAST() {
    if (!fIsComposite)
        fCompilationUnitNode = RefactoringASTParser.parseWithASTProvider(fCu, true, null);
    ISourceRange sourceRange = fLocalVariable.getNameRange();
    ASTNode name = NodeFinder.perform(fCompilationUnitNode, sourceRange);
    if (name == null)
        return;
    if (name.getParent() instanceof VariableDeclaration)
        fTempDeclarationNode = (VariableDeclaration) name.getParent();
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Aggregations

VariableDeclaration (org.eclipse.jdt.core.dom.VariableDeclaration)40 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)22 ASTNode (org.eclipse.jdt.core.dom.ASTNode)19 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)15 ArrayList (java.util.ArrayList)12 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)12 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)10 SimpleName (org.eclipse.jdt.core.dom.SimpleName)9 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)8 Expression (org.eclipse.jdt.core.dom.Expression)8 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)8 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)8 Type (org.eclipse.jdt.core.dom.Type)7 AST (org.eclipse.jdt.core.dom.AST)6 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)6 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)6 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)6 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)6 ArrayCreation (org.eclipse.jdt.core.dom.ArrayCreation)5 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)5