Search in sources :

Example 11 with SingleVariableDeclaration

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

the class ConstructorFromSuperclassProposal method addLinkedRanges.

private void addLinkedRanges(ASTRewrite rewrite, MethodDeclaration newStub) {
    List<SingleVariableDeclaration> parameters = newStub.parameters();
    for (int i = 0; i < parameters.size(); i++) {
        SingleVariableDeclaration curr = parameters.get(i);
        String name = curr.getName().getIdentifier();
        //$NON-NLS-1$
        addLinkedPosition(rewrite.track(curr.getType()), false, "arg_type_" + name);
        //$NON-NLS-1$
        addLinkedPosition(rewrite.track(curr.getName()), false, "arg_name_" + name);
    }
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration)

Example 12 with SingleVariableDeclaration

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

the class ConstructorFromSuperclassProposal method addEnclosingInstanceAccess.

private SuperConstructorInvocation addEnclosingInstanceAccess(ASTRewrite rewrite, ImportRewriteContext importRewriteContext, List<SingleVariableDeclaration> parameters, String[] paramNames, ITypeBinding enclosingInstance) {
    AST ast = rewrite.getAST();
    SuperConstructorInvocation invocation = ast.newSuperConstructorInvocation();
    SingleVariableDeclaration var = ast.newSingleVariableDeclaration();
    var.setType(getImportRewrite().addImport(enclosingInstance, ast, importRewriteContext));
    String[] enclosingArgNames = StubUtility.getArgumentNameSuggestions(getCompilationUnit().getJavaProject(), enclosingInstance.getTypeDeclaration().getName(), 0, paramNames);
    String firstName = enclosingArgNames[0];
    var.setName(ast.newSimpleName(firstName));
    parameters.add(var);
    Name enclosing = ast.newSimpleName(firstName);
    invocation.setExpression(enclosing);
    //$NON-NLS-1$
    String key = "arg_name_" + firstName;
    addLinkedPosition(rewrite.track(enclosing), false, key);
    for (int i = 0; i < enclosingArgNames.length; i++) {
        // alternative names
        addLinkedPositionProposal(key, enclosingArgNames[i], null);
    }
    return invocation;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) Name(org.eclipse.jdt.core.dom.Name)

Example 13 with SingleVariableDeclaration

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

the class ConstructorFromSuperclassProposal method createNewMethodDeclaration.

private MethodDeclaration createNewMethodDeclaration(AST ast, IMethodBinding binding, ASTRewrite rewrite, ImportRewriteContext importRewriteContext, CodeGenerationSettings commentSettings) throws CoreException {
    String name = fTypeNode.getName().getIdentifier();
    MethodDeclaration decl = ast.newMethodDeclaration();
    decl.setConstructor(true);
    decl.setName(ast.newSimpleName(name));
    Block body = ast.newBlock();
    decl.setBody(body);
    SuperConstructorInvocation invocation = null;
    List<SingleVariableDeclaration> parameters = decl.parameters();
    String[] paramNames = getArgumentNames(binding);
    ITypeBinding enclosingInstance = getEnclosingInstance();
    if (enclosingInstance != null) {
        invocation = addEnclosingInstanceAccess(rewrite, importRewriteContext, parameters, paramNames, enclosingInstance);
    }
    if (binding == null) {
        decl.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
    } else {
        decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, binding.getModifiers()));
        ITypeBinding[] params = binding.getParameterTypes();
        for (int i = 0; i < params.length; i++) {
            SingleVariableDeclaration var = ast.newSingleVariableDeclaration();
            var.setType(getImportRewrite().addImport(params[i], ast, importRewriteContext));
            var.setName(ast.newSimpleName(paramNames[i]));
            parameters.add(var);
        }
        List<Type> thrownExceptions = decl.thrownExceptionTypes();
        ITypeBinding[] excTypes = binding.getExceptionTypes();
        for (int i = 0; i < excTypes.length; i++) {
            Type excType = getImportRewrite().addImport(excTypes[i], ast, importRewriteContext);
            thrownExceptions.add(excType);
        }
        if (invocation == null) {
            invocation = ast.newSuperConstructorInvocation();
        }
        List<Expression> arguments = invocation.arguments();
        for (int i = 0; i < paramNames.length; i++) {
            Name argument = ast.newSimpleName(paramNames[i]);
            arguments.add(argument);
            //$NON-NLS-1$
            addLinkedPosition(rewrite.track(argument), false, "arg_name_" + paramNames[i]);
        }
    }
    String bodyStatement = (invocation == null) ? "" : ASTNodes.asFormattedString(invocation, 0, String.valueOf('\n'), getCompilationUnit().getJavaProject().getOptions(true));
    //$NON-NLS-1$
    String placeHolder = CodeGeneration.getMethodBodyContent(getCompilationUnit(), name, name, true, bodyStatement, String.valueOf('\n'));
    if (placeHolder != null) {
        ASTNode todoNode = rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
        body.statements().add(todoNode);
    }
    if (commentSettings != null) {
        String string = CodeGeneration.getMethodComment(getCompilationUnit(), name, decl, null, String.valueOf('\n'));
        if (string != null) {
            Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
            decl.setJavadoc(javadoc);
        }
    }
    return decl;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) Javadoc(org.eclipse.jdt.core.dom.Javadoc) Name(org.eclipse.jdt.core.dom.Name) Type(org.eclipse.jdt.core.dom.Type) Expression(org.eclipse.jdt.core.dom.Expression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation)

Example 14 with SingleVariableDeclaration

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

the class SourceAnalyzer method checkActivation.

public RefactoringStatus checkActivation() throws JavaModelException {
    RefactoringStatus result = new RefactoringStatus();
    if (!fTypeRoot.isStructureKnown()) {
        result.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_SourceAnalyzer_syntax_errors, JavaStatusContext.create(fTypeRoot));
        return result;
    }
    IProblem[] problems = ASTNodes.getProblems(fDeclaration, ASTNodes.NODE_ONLY, ASTNodes.ERROR);
    if (problems.length > 0) {
        result.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_SourceAnalyzer_declaration_has_errors, JavaStatusContext.create(fTypeRoot, fDeclaration));
        return result;
    }
    final IMethodBinding declarationBinding = fDeclaration.resolveBinding();
    if (declarationBinding != null) {
        final int modifiers = declarationBinding.getModifiers();
        if (Modifier.isAbstract(modifiers)) {
            result.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_SourceAnalyzer_abstract_methods, JavaStatusContext.create(fTypeRoot, fDeclaration));
            return result;
        } else if (Modifier.isNative(modifiers)) {
            result.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_SourceAnalyzer_native_methods, JavaStatusContext.create(fTypeRoot, fDeclaration));
            return result;
        }
    } else {
        result.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_SourceAnalyzer_methoddeclaration_has_errors, JavaStatusContext.create(fTypeRoot));
        return result;
    }
    ActivationAnalyzer analyzer = new ActivationAnalyzer();
    fDeclaration.accept(analyzer);
    result.merge(analyzer.status);
    if (!result.hasFatalError()) {
        List<SingleVariableDeclaration> parameters = fDeclaration.parameters();
        fParameters = new HashMap<IVariableBinding, ParameterData>(parameters.size() * 2);
        for (Iterator<SingleVariableDeclaration> iter = parameters.iterator(); iter.hasNext(); ) {
            SingleVariableDeclaration element = iter.next();
            IVariableBinding binding = element.resolveBinding();
            if (binding == null) {
                result.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_SourceAnalyzer_declaration_has_errors, JavaStatusContext.create(fTypeRoot, fDeclaration));
                return result;
            }
            fParameters.put(binding, (ParameterData) element.getProperty(ParameterData.PROPERTY));
        }
        fNames = new HashMap<IBinding, NameData>();
        fImplicitReceivers = new ArrayList<Expression>(2);
        fTypeParameterReferences = new ArrayList<NameData>(0);
        fTypeParameterMapping = new HashMap<ITypeBinding, NameData>();
        ITypeBinding declaringType = declarationBinding.getDeclaringClass();
        if (declaringType == null) {
            result.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_SourceAnalyzer_typedeclaration_has_errors, JavaStatusContext.create(fTypeRoot));
            return result;
        }
        ITypeBinding[] typeParameters = declaringType.getTypeParameters();
        for (int i = 0; i < typeParameters.length; i++) {
            NameData data = new NameData(typeParameters[i].getName());
            fTypeParameterReferences.add(data);
            fTypeParameterMapping.put(typeParameters[i], data);
        }
        fMethodTypeParameterReferences = new ArrayList<NameData>(0);
        fMethodTypeParameterMapping = new HashMap<ITypeBinding, NameData>();
        IMethodBinding method = declarationBinding;
        typeParameters = method.getTypeParameters();
        for (int i = 0; i < typeParameters.length; i++) {
            NameData data = new NameData(typeParameters[i].getName());
            fMethodTypeParameterReferences.add(data);
            fMethodTypeParameterMapping.put(typeParameters[i], data);
        }
    }
    if (fDeclaration.isVarargs()) {
        List<SingleVariableDeclaration> parameters = fDeclaration.parameters();
        VarargAnalyzer vAnalyzer = new VarargAnalyzer(parameters.get(parameters.size() - 1).getName().resolveBinding());
        fDeclaration.getBody().accept(vAnalyzer);
    }
    return result;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) IBinding(org.eclipse.jdt.core.dom.IBinding) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) IProblem(org.eclipse.jdt.core.compiler.IProblem) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 15 with SingleVariableDeclaration

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

the class InferTypeArgumentsConstraintCreator method endVisit.

@Override
public void endVisit(MethodDeclaration node) {
    IMethodBinding methodBinding = node.resolveBinding();
    if (methodBinding == null)
        //TODO: emit error?
        return;
    int parameterCount = node.parameters().size();
    ConstraintVariable2[] parameterTypeCvs = new ConstraintVariable2[parameterCount];
    for (int i = 0; i < parameterCount; i++) {
        SingleVariableDeclaration paramDecl = (SingleVariableDeclaration) node.parameters().get(i);
        //parameterTypeVariable currently not used, but need to register in order to store source range
        ConstraintVariable2 parameterTypeCv = fTCModel.makeDeclaredParameterTypeVariable(methodBinding, i, fCU);
        parameterTypeCvs[i] = parameterTypeCv;
        if (parameterTypeCv == null)
            continue;
        //creating equals constraint between parameterTypeVariable's elements and the Type's elements
        ConstraintVariable2 typeCv = getConstraintVariable(paramDecl.getType());
        fTCModel.createElementEqualsConstraints(parameterTypeCv, typeCv);
        //TODO: should avoid having a VariableVariable as well as a ParameterVariable for a parameter
        ConstraintVariable2 nameCv = getConstraintVariable(paramDecl.getName());
        fTCModel.createElementEqualsConstraints(parameterTypeCv, nameCv);
    }
    ConstraintVariable2 returnTypeCv = null;
    if (!methodBinding.isConstructor()) {
        //TODO: should only create return type variable if type is generic?
        ConstraintVariable2 returnTypeBindingCv = fTCModel.makeDeclaredReturnTypeVariable(methodBinding, fCU);
        if (returnTypeBindingCv != null) {
            returnTypeCv = getConstraintVariable(node.getReturnType2());
            fTCModel.createElementEqualsConstraints(returnTypeBindingCv, returnTypeCv);
        }
    }
    if (MethodChecks.isVirtual(methodBinding)) {
        //TODO: RippleMethod constraints for corner cases: see testCuRippleMethods3, bug 41989
        addConstraintsForOverriding(methodBinding, returnTypeCv, parameterTypeCvs);
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2)

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