Search in sources :

Example 21 with SingleVariableDeclaration

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

the class ConvertAnonymousToNestedRefactoring method createNewConstructor.

private MethodDeclaration createNewConstructor(CompilationUnitRewrite rewrite, IVariableBinding[] bindings, String[] fieldNames) throws JavaModelException {
    ClassInstanceCreation instanceCreation = (ClassInstanceCreation) fAnonymousInnerClassNode.getParent();
    if (instanceCreation.arguments().isEmpty() && bindings.length == 0)
        return null;
    IJavaProject project = fCu.getJavaProject();
    AST ast = rewrite.getAST();
    ImportRewrite importRewrite = rewrite.getImportRewrite();
    ASTRewrite astRewrite = rewrite.getASTRewrite();
    MethodDeclaration newConstructor = ast.newMethodDeclaration();
    newConstructor.setConstructor(true);
    newConstructor.setJavadoc(null);
    newConstructor.modifiers().addAll(ASTNodeFactory.newModifiers(ast, fVisibility));
    newConstructor.setName(ast.newSimpleName(fClassName));
    addLinkedPosition(KEY_TYPE_NAME, newConstructor.getName(), astRewrite, false);
    newConstructor.setBody(ast.newBlock());
    List<Statement> newStatements = newConstructor.getBody().statements();
    List<SingleVariableDeclaration> newParameters = newConstructor.parameters();
    List<String> newParameterNames = new ArrayList<String>();
    // add parameters for elements passed with the instance creation
    if (!instanceCreation.arguments().isEmpty()) {
        IMethodBinding constructorBinding = getSuperConstructorBinding();
        if (constructorBinding != null) {
            SuperConstructorInvocation superConstructorInvocation = ast.newSuperConstructorInvocation();
            ITypeBinding[] parameterTypes = constructorBinding.getParameterTypes();
            String[][] parameterNames = StubUtility.suggestArgumentNamesWithProposals(project, constructorBinding);
            for (int i = 0; i < parameterNames.length; i++) {
                String[] nameProposals = parameterNames[i];
                String paramName = nameProposals[0];
                SingleVariableDeclaration param = newParameterDeclaration(ast, importRewrite, paramName, parameterTypes[i]);
                newParameters.add(param);
                newParameterNames.add(paramName);
                SimpleName newSIArgument = ast.newSimpleName(paramName);
                superConstructorInvocation.arguments().add(newSIArgument);
                if (fLinkedProposalModel != null) {
                    LinkedProposalPositionGroup positionGroup = fLinkedProposalModel.getPositionGroup(KEY_PARAM_NAME_CONST + String.valueOf(i), true);
                    positionGroup.addPosition(astRewrite.track(param.getName()), false);
                    positionGroup.addPosition(astRewrite.track(newSIArgument), false);
                    for (int k = 0; k < nameProposals.length; k++) {
                        positionGroup.addProposal(nameProposals[k], null, nameProposals.length - k);
                    }
                }
            }
            newStatements.add(superConstructorInvocation);
        }
    }
    // add parameters for all outer variables used
    boolean useThisAccess = useThisForFieldAccess();
    for (int i = 0; i < bindings.length; i++) {
        String baseName = StubUtility.getBaseName(bindings[i], project);
        String[] paramNameProposals = StubUtility.getVariableNameSuggestions(NamingConventions.VK_PARAMETER, project, baseName, 0, newParameterNames, true);
        String paramName = paramNameProposals[0];
        SingleVariableDeclaration param = newParameterDeclaration(ast, importRewrite, paramName, bindings[i].getType());
        newParameters.add(param);
        newParameterNames.add(paramName);
        String fieldName = fieldNames[i];
        SimpleName fieldNameNode = ast.newSimpleName(fieldName);
        SimpleName paramNameNode = ast.newSimpleName(paramName);
        newStatements.add(newFieldAssignment(ast, fieldNameNode, paramNameNode, useThisAccess || newParameterNames.contains(fieldName)));
        if (fLinkedProposalModel != null) {
            LinkedProposalPositionGroup positionGroup = fLinkedProposalModel.getPositionGroup(KEY_PARAM_NAME_EXT + String.valueOf(i), true);
            positionGroup.addPosition(astRewrite.track(param.getName()), false);
            positionGroup.addPosition(astRewrite.track(paramNameNode), false);
            for (int k = 0; k < paramNameProposals.length; k++) {
                positionGroup.addProposal(paramNameProposals[k], null, paramNameProposals.length - k);
            }
            fLinkedProposalModel.getPositionGroup(KEY_FIELD_NAME_EXT + i, true).addPosition(astRewrite.track(fieldNameNode), false);
        }
    }
    addExceptionsToNewConstructor(newConstructor, importRewrite);
    if (doAddComments()) {
        try {
            String[] allParamNames = newParameterNames.toArray(new String[newParameterNames.size()]);
            String string = CodeGeneration.getMethodComment(fCu, fClassName, fClassName, allParamNames, new String[0], null, new String[0], null, StubUtility.getLineDelimiterUsed(fCu));
            if (string != null) {
                Javadoc javadoc = (Javadoc) astRewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
                newConstructor.setJavadoc(javadoc);
            }
        } catch (CoreException exception) {
            throw new JavaModelException(exception);
        }
    }
    return newConstructor;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) JavaModelException(org.eclipse.jdt.core.JavaModelException) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Statement(org.eclipse.jdt.core.dom.Statement) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) IJavaProject(org.eclipse.jdt.core.IJavaProject) CoreException(org.eclipse.core.runtime.CoreException) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) LinkedProposalPositionGroup(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup)

Example 22 with SingleVariableDeclaration

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

the class FullConstraintCreator method create.

/* (non-Javadoc)
	 * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.MethodDeclaration)
	 */
@Override
public ITypeConstraint[] create(MethodDeclaration declaration) {
    List<ITypeConstraint> result = new ArrayList<ITypeConstraint>();
    IMethodBinding methodBinding = declaration.resolveBinding();
    if (methodBinding == null)
        return new ITypeConstraint[0];
    ITypeConstraint[] constraints = fTypeConstraintFactory.createDefinesConstraint(fConstraintVariableFactory.makeDeclaringTypeVariable(methodBinding), fConstraintVariableFactory.makeRawBindingVariable(methodBinding.getDeclaringClass()));
    result.addAll(Arrays.asList(constraints));
    if (!methodBinding.isConstructor() && !methodBinding.getReturnType().isPrimitive()) {
        ConstraintVariable returnTypeBindingVariable = fConstraintVariableFactory.makeReturnTypeVariable(methodBinding);
        ConstraintVariable returnTypeVariable = fConstraintVariableFactory.makeTypeVariable(declaration.getReturnType2());
        ITypeConstraint[] defines = fTypeConstraintFactory.createDefinesConstraint(returnTypeBindingVariable, returnTypeVariable);
        result.addAll(Arrays.asList(defines));
    }
    for (int i = 0, n = declaration.parameters().size(); i < n; i++) {
        SingleVariableDeclaration paramDecl = (SingleVariableDeclaration) declaration.parameters().get(i);
        ConstraintVariable parameterTypeVariable = fConstraintVariableFactory.makeParameterTypeVariable(methodBinding, i);
        ConstraintVariable parameterNameVariable = fConstraintVariableFactory.makeExpressionOrTypeVariable(paramDecl.getName(), getContext());
        ITypeConstraint[] constraint = fTypeConstraintFactory.createDefinesConstraint(parameterTypeVariable, parameterNameVariable);
        result.addAll(Arrays.asList(constraint));
    }
    if (MethodChecks.isVirtual(methodBinding)) {
        Collection<ITypeConstraint> constraintsForOverriding = getConstraintsForOverriding(methodBinding);
        result.addAll(constraintsForOverriding);
    }
    return result.toArray(new ITypeConstraint[result.size()]);
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ArrayList(java.util.ArrayList)

Example 23 with SingleVariableDeclaration

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

the class FullConstraintCreator method create.

@Override
public ITypeConstraint[] create(CatchClause node) {
    SingleVariableDeclaration exception = node.getException();
    ConstraintVariable nameVariable = fConstraintVariableFactory.makeExpressionOrTypeVariable(exception.getName(), getContext());
    ITypeConstraint[] defines = fTypeConstraintFactory.createDefinesConstraint(nameVariable, fConstraintVariableFactory.makeTypeVariable(exception.getType()));
    //$NON-NLS-1$
    ITypeBinding throwable = node.getAST().resolveWellKnownType("java.lang.Throwable");
    ITypeConstraint[] catchBound = fTypeConstraintFactory.createSubtypeConstraint(nameVariable, fConstraintVariableFactory.makeRawBindingVariable(throwable));
    ArrayList<ITypeConstraint> result = new ArrayList<ITypeConstraint>();
    result.addAll(Arrays.asList(defines));
    result.addAll(Arrays.asList(catchBound));
    return result.toArray(new ITypeConstraint[result.size()]);
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ArrayList(java.util.ArrayList)

Example 24 with SingleVariableDeclaration

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

the class JavadocUtil method addParamJavadoc.

/**
	 * Adds a "param" javadoc tag for a new last parameter if necessary.
	 * @param parameterName
	 * @param methodDeclaration
	 * @param astRewrite
	 * @param javaProject
	 * @param groupDescription
	 */
public static void addParamJavadoc(String parameterName, MethodDeclaration methodDeclaration, ASTRewrite astRewrite, IJavaProject javaProject, TextEditGroup groupDescription) {
    if (!shouldAddParamJavadoc(methodDeclaration))
        return;
    ListRewrite tagsRewrite = astRewrite.getListRewrite(methodDeclaration.getJavadoc(), Javadoc.TAGS_PROPERTY);
    HashSet<String> leadingNames = new HashSet<String>();
    for (Iterator<SingleVariableDeclaration> iter = methodDeclaration.parameters().iterator(); iter.hasNext(); ) {
        SingleVariableDeclaration curr = iter.next();
        leadingNames.add(curr.getName().getIdentifier());
    }
    TagElement parameterTag = createParamTag(parameterName, astRewrite.getAST(), javaProject);
    JavadocTagsSubProcessor.insertTag(tagsRewrite, parameterTag, leadingNames, groupDescription);
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) TagElement(org.eclipse.jdt.core.dom.TagElement) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) HashSet(java.util.HashSet)

Example 25 with SingleVariableDeclaration

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

the class ReferenceFinderUtil method getTypesUsedInDeclaration.

//set of ITypeBindings
public static Set<ITypeBinding> getTypesUsedInDeclaration(MethodDeclaration methodDeclaration) {
    if (methodDeclaration == null)
        return new HashSet<ITypeBinding>(0);
    Set<ITypeBinding> result = new HashSet<ITypeBinding>();
    ITypeBinding binding = null;
    Type returnType = methodDeclaration.getReturnType2();
    if (returnType != null) {
        binding = returnType.resolveBinding();
        if (binding != null)
            result.add(binding);
    }
    for (Iterator<SingleVariableDeclaration> iter = methodDeclaration.parameters().iterator(); iter.hasNext(); ) {
        binding = iter.next().getType().resolveBinding();
        if (binding != null)
            result.add(binding);
    }
    for (Iterator<Type> iter = methodDeclaration.thrownExceptionTypes().iterator(); iter.hasNext(); ) {
        binding = iter.next().resolveBinding();
        if (binding != null)
            result.add(binding);
    }
    return result;
}
Also used : IType(org.eclipse.jdt.core.IType) Type(org.eclipse.jdt.core.dom.Type) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) HashSet(java.util.HashSet)

Aggregations

SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)121 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)41 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)37 Type (org.eclipse.jdt.core.dom.Type)37 ASTNode (org.eclipse.jdt.core.dom.ASTNode)33 Block (org.eclipse.jdt.core.dom.Block)33 AST (org.eclipse.jdt.core.dom.AST)32 SimpleName (org.eclipse.jdt.core.dom.SimpleName)27 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)27 ArrayList (java.util.ArrayList)26 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)25 List (java.util.List)24 Expression (org.eclipse.jdt.core.dom.Expression)22 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)18 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)18 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)17 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)17 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)16 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)16 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)16