use of org.eclipse.jdt.core.dom.Expression in project che by eclipse.
the class NewMethodCorrectionProposal method evaluateModifiers.
private int evaluateModifiers(ASTNode targetTypeDecl) {
if (getSenderBinding().isAnnotation()) {
return 0;
}
if (getSenderBinding().isInterface()) {
// for interface and annotation members copy the modifiers from an existing field
MethodDeclaration[] methodDecls = ((TypeDeclaration) targetTypeDecl).getMethods();
if (methodDecls.length > 0) {
return methodDecls[0].getModifiers();
}
return 0;
}
ASTNode invocationNode = getInvocationNode();
if (invocationNode instanceof MethodInvocation) {
int modifiers = 0;
Expression expression = ((MethodInvocation) invocationNode).getExpression();
if (expression != null) {
if (expression instanceof Name && ((Name) expression).resolveBinding().getKind() == IBinding.TYPE) {
modifiers |= Modifier.STATIC;
}
} else if (ASTResolving.isInStaticContext(invocationNode)) {
modifiers |= Modifier.STATIC;
}
ASTNode node = ASTResolving.findParentType(invocationNode);
if (targetTypeDecl.equals(node)) {
modifiers |= Modifier.PRIVATE;
} else if (node instanceof AnonymousClassDeclaration && ASTNodes.isParent(node, targetTypeDecl)) {
modifiers |= Modifier.PROTECTED;
if (ASTResolving.isInStaticContext(node) && expression == null) {
modifiers |= Modifier.STATIC;
}
} else {
modifiers |= Modifier.PUBLIC;
}
return modifiers;
}
return Modifier.PUBLIC;
}
use of org.eclipse.jdt.core.dom.Expression in project che by eclipse.
the class NewMethodCorrectionProposal method addNewParameters.
/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.proposals.AbstractMethodCorrectionProposal#addNewParameters(org.eclipse.jdt.core.dom.rewrite.ASTRewrite, java.util.List, java.util.List)
*/
@Override
protected void addNewParameters(ASTRewrite rewrite, List<String> takenNames, List<SingleVariableDeclaration> params) throws CoreException {
AST ast = rewrite.getAST();
List<Expression> arguments = fArguments;
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(ASTResolving.findParentBodyDeclaration(getInvocationNode()), getImportRewrite());
for (int i = 0; i < arguments.size(); i++) {
Expression elem = arguments.get(i);
SingleVariableDeclaration param = ast.newSingleVariableDeclaration();
// argument type
//$NON-NLS-1$
String argTypeKey = "arg_type_" + i;
Type type = evaluateParameterType(ast, elem, argTypeKey, context);
param.setType(type);
// argument name
//$NON-NLS-1$
String argNameKey = "arg_name_" + i;
String name = evaluateParameterName(takenNames, elem, type, argNameKey);
param.setName(ast.newSimpleName(name));
params.add(param);
addLinkedPosition(rewrite.track(param.getType()), false, argTypeKey);
addLinkedPosition(rewrite.track(param.getName()), false, argNameKey);
}
}
use of org.eclipse.jdt.core.dom.Expression in project che by eclipse.
the class NewVariableCorrectionProposal method doAddLocal.
private ASTRewrite doAddLocal(CompilationUnit cu) {
AST ast = cu.getAST();
Block body;
BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(fOriginalNode);
IBinding targetContext = null;
if (decl instanceof MethodDeclaration) {
body = (((MethodDeclaration) decl).getBody());
targetContext = ((MethodDeclaration) decl).resolveBinding();
} else if (decl instanceof Initializer) {
body = (((Initializer) decl).getBody());
targetContext = Bindings.getBindingOfParentType(decl);
} else {
return null;
}
ASTRewrite rewrite = ASTRewrite.create(ast);
ImportRewrite imports = createImportRewrite((CompilationUnit) decl.getRoot());
SimpleName[] names = getAllReferences(body);
ASTNode dominant = getDominantNode(names);
Statement dominantStatement = ASTResolving.findParentStatement(dominant);
if (ASTNodes.isControlStatementBody(dominantStatement.getLocationInParent())) {
dominantStatement = (Statement) dominantStatement.getParent();
}
SimpleName node = names[0];
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, imports);
if (isAssigned(dominantStatement, node)) {
// x = 1; -> int x = 1;
Assignment assignment = (Assignment) node.getParent();
// trick to avoid comment removal around the statement: keep the expression statement
// and replace the assignment with an VariableDeclarationExpression
VariableDeclarationFragment newDeclFrag = ast.newVariableDeclarationFragment();
VariableDeclarationExpression newDecl = ast.newVariableDeclarationExpression(newDeclFrag);
newDecl.setType(evaluateVariableType(ast, imports, importRewriteContext, targetContext));
Expression placeholder = (Expression) rewrite.createCopyTarget(assignment.getRightHandSide());
newDeclFrag.setInitializer(placeholder);
newDeclFrag.setName(ast.newSimpleName(node.getIdentifier()));
rewrite.replace(assignment, newDecl, null);
addLinkedPosition(rewrite.track(newDecl.getType()), false, KEY_TYPE);
addLinkedPosition(rewrite.track(newDeclFrag.getName()), true, KEY_NAME);
setEndPosition(rewrite.track(assignment.getParent()));
return rewrite;
} else if ((dominant != dominantStatement) && isForStatementInit(dominantStatement, node)) {
// for (x = 1;;) ->for (int x = 1;;)
Assignment assignment = (Assignment) node.getParent();
VariableDeclarationFragment frag = ast.newVariableDeclarationFragment();
VariableDeclarationExpression expression = ast.newVariableDeclarationExpression(frag);
frag.setName(ast.newSimpleName(node.getIdentifier()));
Expression placeholder = (Expression) rewrite.createCopyTarget(assignment.getRightHandSide());
frag.setInitializer(placeholder);
expression.setType(evaluateVariableType(ast, imports, importRewriteContext, targetContext));
rewrite.replace(assignment, expression, null);
addLinkedPosition(rewrite.track(expression.getType()), false, KEY_TYPE);
addLinkedPosition(rewrite.track(frag.getName()), true, KEY_NAME);
setEndPosition(rewrite.track(expression));
return rewrite;
} else if ((dominant != dominantStatement) && isEnhancedForStatementVariable(dominantStatement, node)) {
// for (x: collectionOfT) -> for (T x: collectionOfT)
EnhancedForStatement enhancedForStatement = (EnhancedForStatement) dominantStatement;
SingleVariableDeclaration parameter = enhancedForStatement.getParameter();
Expression expression = enhancedForStatement.getExpression();
SimpleName newName = (SimpleName) rewrite.createMoveTarget(node);
rewrite.set(parameter, SingleVariableDeclaration.NAME_PROPERTY, newName, null);
ITypeBinding elementBinding = null;
ITypeBinding typeBinding = expression.resolveTypeBinding();
if (typeBinding != null) {
if (typeBinding.isArray()) {
elementBinding = typeBinding.getElementType();
} else {
//$NON-NLS-1$
ITypeBinding iterable = Bindings.findTypeInHierarchy(typeBinding, "java.lang.Iterable");
if (iterable != null) {
ITypeBinding[] typeArguments = iterable.getTypeArguments();
if (typeArguments.length == 1) {
elementBinding = typeArguments[0];
elementBinding = Bindings.normalizeForDeclarationUse(elementBinding, ast);
}
}
}
}
Type type;
if (elementBinding != null) {
type = imports.addImport(elementBinding, ast, importRewriteContext);
} else {
//$NON-NLS-1$
type = ast.newSimpleType(ast.newSimpleName("Object"));
}
rewrite.set(parameter, SingleVariableDeclaration.TYPE_PROPERTY, type, null);
addLinkedPosition(rewrite.track(type), false, KEY_TYPE);
addLinkedPosition(rewrite.track(newName), true, KEY_NAME);
setEndPosition(rewrite.track(expression));
return rewrite;
}
// foo(x) -> int x; foo(x)
VariableDeclarationFragment newDeclFrag = ast.newVariableDeclarationFragment();
VariableDeclarationStatement newDecl = ast.newVariableDeclarationStatement(newDeclFrag);
newDeclFrag.setName(ast.newSimpleName(node.getIdentifier()));
newDecl.setType(evaluateVariableType(ast, imports, importRewriteContext, targetContext));
// newDeclFrag.setInitializer(ASTNodeFactory.newDefaultExpression(ast, newDecl.getType(), 0));
addLinkedPosition(rewrite.track(newDecl.getType()), false, KEY_TYPE);
addLinkedPosition(rewrite.track(node), true, KEY_NAME);
addLinkedPosition(rewrite.track(newDeclFrag.getName()), false, KEY_NAME);
Statement statement = dominantStatement;
List<? extends ASTNode> list = ASTNodes.getContainingList(statement);
while (list == null && statement.getParent() instanceof Statement) {
// parent must be if, for or while
statement = (Statement) statement.getParent();
list = ASTNodes.getContainingList(statement);
}
if (list != null) {
ASTNode parent = statement.getParent();
StructuralPropertyDescriptor childProperty = statement.getLocationInParent();
if (childProperty.isChildListProperty()) {
rewrite.getListRewrite(parent, (ChildListPropertyDescriptor) childProperty).insertBefore(newDecl, statement, null);
return rewrite;
} else {
return null;
}
}
return rewrite;
}
use of org.eclipse.jdt.core.dom.Expression 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;
}
use of org.eclipse.jdt.core.dom.Expression in project che by eclipse.
the class UnresolvedElementsSubProcessor method getArgumentTypes.
private static ITypeBinding[] getArgumentTypes(List<Expression> arguments) {
ITypeBinding[] res = new ITypeBinding[arguments.size()];
for (int i = 0; i < res.length; i++) {
Expression expression = arguments.get(i);
ITypeBinding curr = expression.resolveTypeBinding();
if (curr == null) {
return null;
}
if (!curr.isNullType()) {
// don't normalize null type
curr = Bindings.normalizeTypeBinding(curr);
if (curr == null) {
//$NON-NLS-1$
curr = expression.getAST().resolveWellKnownType("java.lang.Object");
}
}
res[i] = curr;
}
return res;
}
Aggregations