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;
}
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;
}
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;
}
}
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;
}
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;
}
}
Aggregations