Search in sources :

Example 56 with Expression

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

the class CallInliner method computeRealArguments.

private void computeRealArguments() {
    List<Expression> arguments = Invocations.getArguments(fInvocation);
    Set<Expression> canNotInline = crossCheckArguments(arguments);
    boolean needsVarargBoxing = needsVarargBoxing(arguments);
    int varargIndex = fSourceProvider.getVarargIndex();
    AST ast = fInvocation.getAST();
    Expression[] realArguments = new Expression[needsVarargBoxing ? varargIndex + 1 : arguments.size()];
    for (int i = 0; i < (needsVarargBoxing ? varargIndex : arguments.size()); i++) {
        Expression expression = arguments.get(i);
        ParameterData parameter = fSourceProvider.getParameterData(i);
        if (canInline(expression, parameter) && !canNotInline.contains(expression)) {
            realArguments[i] = expression;
        } else {
            String name = fInvocationScope.createName(parameter.getName(), true);
            realArguments[i] = ast.newSimpleName(name);
            VariableDeclarationStatement local = createLocalDeclaration(parameter.getTypeBinding(), name, (Expression) fRewrite.createCopyTarget(expression));
            if (parameter.isFinal()) {
                local.modifiers().add(fInvocation.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD));
            }
            fLocals.add(local);
        }
    }
    if (needsVarargBoxing) {
        ParameterData parameter = fSourceProvider.getParameterData(varargIndex);
        String name = fInvocationScope.createName(parameter.getName(), true);
        realArguments[varargIndex] = ast.newSimpleName(name);
        Type type = fImportRewrite.addImport(parameter.getTypeBinding(), ast);
        VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
        fragment.setName(ast.newSimpleName(name));
        ArrayInitializer initializer = ast.newArrayInitializer();
        for (int i = varargIndex; i < arguments.size(); i++) {
            initializer.expressions().add(fRewrite.createCopyTarget(arguments.get(i)));
        }
        fragment.setInitializer(initializer);
        VariableDeclarationStatement decl = ast.newVariableDeclarationStatement(fragment);
        decl.setType(type);
        fLocals.add(decl);
    }
    fContext.compilationUnit = fCUnit;
    fContext.arguments = realArguments;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Type(org.eclipse.jdt.core.dom.Type) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) 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) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 57 with Expression

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

the class CallInliner method crossCheckArguments.

/**
     * Checks whether arguments are passed to the method which do some assignments
     * inside the expression. If so these arguments can't be inlined into the
     * calling method since the assignments might be reorder. An example is:
     * <code>
     *   add((field=args).length,field.hashCode());
     * </code>
     * Field might not be initialized when the arguments are reorder in the called
     * method.
     * @param arguments the arguments
     * @return all arguments that cannot be inlined
     */
private Set<Expression> crossCheckArguments(List<Expression> arguments) {
    final Set<IBinding> assigned = new HashSet<IBinding>();
    final Set<Expression> result = new HashSet<Expression>();
    for (Iterator<Expression> iter = arguments.iterator(); iter.hasNext(); ) {
        final Expression expression = iter.next();
        expression.accept(new ASTVisitor() {

            @Override
            public boolean visit(Assignment node) {
                Expression lhs = node.getLeftHandSide();
                if (lhs instanceof Name) {
                    IBinding binding = ((Name) lhs).resolveBinding();
                    if (binding instanceof IVariableBinding) {
                        assigned.add(binding);
                        result.add(expression);
                    }
                }
                return true;
            }
        });
    }
    for (Iterator<Expression> iter = arguments.iterator(); iter.hasNext(); ) {
        final Expression expression = iter.next();
        if (!result.contains(expression)) {
            expression.accept(new HierarchicalASTVisitor() {

                @Override
                public boolean visit(Name node) {
                    IBinding binding = node.resolveBinding();
                    if (binding != null && assigned.contains(binding))
                        result.add(expression);
                    return false;
                }
            });
        }
    }
    return result;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) 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) IBinding(org.eclipse.jdt.core.dom.IBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) HierarchicalASTVisitor(org.eclipse.jdt.internal.corext.dom.HierarchicalASTVisitor) HashSet(java.util.HashSet) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) HierarchicalASTVisitor(org.eclipse.jdt.internal.corext.dom.HierarchicalASTVisitor) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name)

Example 58 with Expression

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

the class CallInliner method computeReceiver.

private void computeReceiver() throws BadLocationException {
    Expression receiver = Invocations.getExpression(fInvocation);
    if (receiver == null)
        return;
    final boolean isName = receiver instanceof Name;
    if (isName)
        fContext.receiverIsStatic = ((Name) receiver).resolveBinding() instanceof ITypeBinding;
    if (ASTNodes.isLiteral(receiver) || isName || receiver instanceof ThisExpression) {
        fContext.receiver = fBuffer.getDocument().get(receiver.getStartPosition(), receiver.getLength());
        return;
    }
    switch(fSourceProvider.getReceiversToBeUpdated()) {
        case 0:
            // Make sure we evaluate the current receiver. Best is to assign to
            // local.
            fLocals.add(createLocalDeclaration(receiver.resolveTypeBinding(), //$NON-NLS-1$
            fInvocationScope.createName("r", true), (Expression) fRewrite.createCopyTarget(receiver)));
            return;
        case 1:
            fContext.receiver = fBuffer.getDocument().get(receiver.getStartPosition(), receiver.getLength());
            return;
        default:
            //$NON-NLS-1$
            String local = fInvocationScope.createName("r", true);
            fLocals.add(createLocalDeclaration(receiver.resolveTypeBinding(), local, (Expression) fRewrite.createCopyTarget(receiver)));
            fContext.receiver = local;
            return;
    }
}
Also used : ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) 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) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name)

Example 59 with Expression

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

the class ExtractConstantRefactoring method guessConstantNames.

/**
	 * @return proposed variable names (may be empty, but not null).
	 * The first proposal should be used as "best guess" (if it exists).
	 */
public String[] guessConstantNames() {
    if (fGuessedConstNames == null) {
        try {
            Expression expression = getSelectedExpression().getAssociatedExpression();
            if (expression != null) {
                ITypeBinding binding = guessBindingForReference(expression);
                fGuessedConstNames = StubUtility.getVariableNameSuggestions(NamingConventions.VK_STATIC_FINAL_FIELD, fCu.getJavaProject(), binding, expression, Arrays.asList(getExcludedVariableNames()));
            }
        } catch (JavaModelException e) {
        }
        if (fGuessedConstNames == null)
            fGuessedConstNames = new String[0];
    }
    return fGuessedConstNames;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) Expression(org.eclipse.jdt.core.dom.Expression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 60 with Expression

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

the class ExtractConstantRefactoring method isLiteralNodeSelected.

//	 !!! -- same as in ExtractTempRefactoring
private boolean isLiteralNodeSelected() throws JavaModelException {
    IExpressionFragment fragment = getSelectedExpression();
    if (fragment == null)
        return false;
    Expression expression = fragment.getAssociatedExpression();
    if (expression == null)
        return false;
    switch(expression.getNodeType()) {
        case ASTNode.BOOLEAN_LITERAL:
        case ASTNode.CHARACTER_LITERAL:
        case ASTNode.NULL_LITERAL:
        case ASTNode.NUMBER_LITERAL:
            return true;
        default:
            return false;
    }
}
Also used : IExpressionFragment(org.eclipse.jdt.internal.corext.dom.fragments.IExpressionFragment) Expression(org.eclipse.jdt.core.dom.Expression)

Aggregations

Expression (org.eclipse.jdt.core.dom.Expression)552 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)263 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)213 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)187 CastExpression (org.eclipse.jdt.core.dom.CastExpression)185 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)135 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)126 ASTNode (org.eclipse.jdt.core.dom.ASTNode)125 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)122 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)121 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)112 AST (org.eclipse.jdt.core.dom.AST)101 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)95 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)88 SimpleName (org.eclipse.jdt.core.dom.SimpleName)83 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)76 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)73 Type (org.eclipse.jdt.core.dom.Type)70 ArrayList (java.util.ArrayList)69 Block (org.eclipse.jdt.core.dom.Block)63