Search in sources :

Example 1 with ThrowStatement

use of org.eclipse.jdt.core.dom.ThrowStatement in project AutoRefactor by JnRouvignac.

the class ASTBuilder method throw0.

/**
 * Builds a new {@link ThrowStatement} instance.
 *
 * @param expression the expression to throw
 * @return a new throw statement
 */
public ThrowStatement throw0(final Expression expression) {
    final ThrowStatement throwS = ast.newThrowStatement();
    throwS.setExpression(expression);
    return throwS;
}
Also used : ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement)

Example 2 with ThrowStatement

use of org.eclipse.jdt.core.dom.ThrowStatement in project eclipse.jdt.ls by eclipse.

the class UnresolvedElementsSubProcessor method getMethodProposals.

public static void getMethodProposals(IInvocationContext context, IProblemLocation problem, boolean isOnlyParameterMismatch, Collection<CUCorrectionProposal> proposals) throws CoreException {
    ICompilationUnit cu = context.getCompilationUnit();
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    if (!(selectedNode instanceof SimpleName)) {
        return;
    }
    SimpleName nameNode = (SimpleName) selectedNode;
    List<Expression> arguments;
    Expression sender;
    boolean isSuperInvocation;
    ASTNode invocationNode = nameNode.getParent();
    if (invocationNode instanceof MethodInvocation) {
        MethodInvocation methodImpl = (MethodInvocation) invocationNode;
        arguments = methodImpl.arguments();
        sender = methodImpl.getExpression();
        isSuperInvocation = false;
    } else if (invocationNode instanceof SuperMethodInvocation) {
        SuperMethodInvocation methodImpl = (SuperMethodInvocation) invocationNode;
        arguments = methodImpl.arguments();
        sender = methodImpl.getQualifier();
        isSuperInvocation = true;
    } else {
        return;
    }
    String methodName = nameNode.getIdentifier();
    int nArguments = arguments.size();
    // corrections
    IBinding[] bindings = (new ScopeAnalyzer(astRoot)).getDeclarationsInScope(nameNode, ScopeAnalyzer.METHODS);
    HashSet<String> suggestedRenames = new HashSet<>();
    for (int i = 0; i < bindings.length; i++) {
        IMethodBinding binding = (IMethodBinding) bindings[i];
        String curr = binding.getName();
        if (!curr.equals(methodName) && binding.getParameterTypes().length == nArguments && NameMatcher.isSimilarName(methodName, curr) && suggestedRenames.add(curr)) {
            String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changemethod_description, BasicElementLabels.getJavaElementName(curr));
            proposals.add(new RenameNodeCorrectionProposal(label, context.getCompilationUnit(), problem.getOffset(), problem.getLength(), curr, IProposalRelevance.CHANGE_METHOD));
        }
    }
    suggestedRenames = null;
    if (isOnlyParameterMismatch) {
        ArrayList<IMethodBinding> parameterMismatchs = new ArrayList<>();
        for (int i = 0; i < bindings.length; i++) {
            IMethodBinding binding = (IMethodBinding) bindings[i];
            if (binding.getName().equals(methodName)) {
                parameterMismatchs.add(binding);
            }
        }
        addParameterMissmatchProposals(context, problem, parameterMismatchs, invocationNode, arguments, proposals);
    }
    if (sender == null) {
        addStaticImportFavoriteProposals(context, nameNode, true, proposals);
    }
    // new method
    addNewMethodProposals(cu, astRoot, sender, arguments, isSuperInvocation, invocationNode, methodName, proposals);
    if (!isOnlyParameterMismatch && !isSuperInvocation && sender != null) {
        addMissingCastParentsProposal(cu, (MethodInvocation) invocationNode, proposals);
    }
    if (!isSuperInvocation && sender == null && invocationNode.getParent() instanceof ThrowStatement) {
        // $NON-NLS-1$ // do it the manual way, copting all the arguments is nasty
        String str = "new ";
        String label = CorrectionMessages.UnresolvedElementsSubProcessor_addnewkeyword_description;
        int relevance = Character.isUpperCase(methodName.charAt(0)) ? IProposalRelevance.ADD_NEW_KEYWORD_UPPERCASE : IProposalRelevance.ADD_NEW_KEYWORD;
        ReplaceCorrectionProposal proposal = new ReplaceCorrectionProposal(label, cu, invocationNode.getStartPosition(), 0, str, relevance);
        proposals.add(proposal);
    }
}
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) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ScopeAnalyzer(org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) HashSet(java.util.HashSet)

Example 3 with ThrowStatement

use of org.eclipse.jdt.core.dom.ThrowStatement in project whole by wholeplatform.

the class CompilationUnitBuilder method newThrowStatement.

public ThrowStatement newThrowStatement(Expression exp) {
    ThrowStatement stm = ast.newThrowStatement();
    stm.setExpression(exp);
    return stm;
}
Also used : ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement)

Example 4 with ThrowStatement

use of org.eclipse.jdt.core.dom.ThrowStatement in project AutoRefactor by JnRouvignac.

the class ASTNodeFactory method newThrowStatement.

/**
 * Builds a new {@link ThrowStatement} instance.
 *
 * @param expression the expression to throw
 * @return a new throw statement
 */
public ThrowStatement newThrowStatement(final Expression expression) {
    ThrowStatement throwS = ast.newThrowStatement();
    throwS.setExpression(expression);
    return throwS;
}
Also used : ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement)

Example 5 with ThrowStatement

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

the class UnresolvedElementsSubProcessor method getMethodProposals.

public static void getMethodProposals(IInvocationContext context, IProblemLocation problem, boolean isOnlyParameterMismatch, Collection<ICommandAccess> proposals) throws CoreException {
    ICompilationUnit cu = context.getCompilationUnit();
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    if (!(selectedNode instanceof SimpleName)) {
        return;
    }
    SimpleName nameNode = (SimpleName) selectedNode;
    List<Expression> arguments;
    Expression sender;
    boolean isSuperInvocation;
    ASTNode invocationNode = nameNode.getParent();
    if (invocationNode instanceof MethodInvocation) {
        MethodInvocation methodImpl = (MethodInvocation) invocationNode;
        arguments = methodImpl.arguments();
        sender = methodImpl.getExpression();
        isSuperInvocation = false;
    } else if (invocationNode instanceof SuperMethodInvocation) {
        SuperMethodInvocation methodImpl = (SuperMethodInvocation) invocationNode;
        arguments = methodImpl.arguments();
        sender = methodImpl.getQualifier();
        isSuperInvocation = true;
    } else {
        return;
    }
    String methodName = nameNode.getIdentifier();
    int nArguments = arguments.size();
    // corrections
    IBinding[] bindings = (new ScopeAnalyzer(astRoot)).getDeclarationsInScope(nameNode, ScopeAnalyzer.METHODS);
    HashSet<String> suggestedRenames = new HashSet<String>();
    for (int i = 0; i < bindings.length; i++) {
        IMethodBinding binding = (IMethodBinding) bindings[i];
        String curr = binding.getName();
        if (!curr.equals(methodName) && binding.getParameterTypes().length == nArguments && NameMatcher.isSimilarName(methodName, curr) && suggestedRenames.add(curr)) {
            String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changemethod_description, BasicElementLabels.getJavaElementName(curr));
            proposals.add(new RenameNodeCorrectionProposal(label, context.getCompilationUnit(), problem.getOffset(), problem.getLength(), curr, IProposalRelevance.CHANGE_METHOD));
        }
    }
    suggestedRenames = null;
    if (isOnlyParameterMismatch) {
        ArrayList<IMethodBinding> parameterMismatchs = new ArrayList<IMethodBinding>();
        for (int i = 0; i < bindings.length; i++) {
            IMethodBinding binding = (IMethodBinding) bindings[i];
            if (binding.getName().equals(methodName)) {
                parameterMismatchs.add(binding);
            }
        }
        addParameterMissmatchProposals(context, problem, parameterMismatchs, invocationNode, arguments, proposals);
    }
    if (sender == null) {
        addStaticImportFavoriteProposals(context, nameNode, true, proposals);
    }
    // new method
    addNewMethodProposals(cu, astRoot, sender, arguments, isSuperInvocation, invocationNode, methodName, proposals);
    if (!isOnlyParameterMismatch && !isSuperInvocation && sender != null) {
        addMissingCastParentsProposal(cu, (MethodInvocation) invocationNode, proposals);
    }
    if (!isSuperInvocation && sender == null && invocationNode.getParent() instanceof ThrowStatement) {
        //$NON-NLS-1$ // do it the manual way, copting all the arguments is nasty
        String str = "new ";
        String label = CorrectionMessages.UnresolvedElementsSubProcessor_addnewkeyword_description;
        int relevance = Character.isUpperCase(methodName.charAt(0)) ? IProposalRelevance.ADD_NEW_KEYWORD_UPPERCASE : IProposalRelevance.ADD_NEW_KEYWORD;
        ReplaceCorrectionProposal proposal = new ReplaceCorrectionProposal(label, cu, invocationNode.getStartPosition(), 0, str, relevance);
        proposals.add(proposal);
    }
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) RenameNodeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.RenameNodeCorrectionProposal) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ScopeAnalyzer(org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) HashSet(java.util.HashSet) ReplaceCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ReplaceCorrectionProposal)

Aggregations

ThrowStatement (org.eclipse.jdt.core.dom.ThrowStatement)6 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 CastExpression (org.eclipse.jdt.core.dom.CastExpression)2 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)2 Expression (org.eclipse.jdt.core.dom.Expression)2 IBinding (org.eclipse.jdt.core.dom.IBinding)2 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)2 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)2 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)2 SimpleName (org.eclipse.jdt.core.dom.SimpleName)2 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)2 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)2 ScopeAnalyzer (org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer)2 RenameNodeCorrectionProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.RenameNodeCorrectionProposal)1 ReplaceCorrectionProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.ReplaceCorrectionProposal)1 ConstructorStatement (org.evosuite.testcase.ConstructorStatement)1 VariableReference (org.evosuite.testcase.VariableReference)1