use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.
the class NewCUUsingWizardProposal method getAdditionalProposalInfo.
/*
* @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension5#getAdditionalProposalInfo(org.eclipse.core.runtime.IProgressMonitor)
* @since 3.5
*/
@Override
public Object getAdditionalProposalInfo(IProgressMonitor monitor) {
StringBuffer buf = new StringBuffer();
switch(fTypeKind) {
case K_CLASS:
buf.append(CorrectionMessages.NewCUCompletionUsingWizardProposal_createclass_info);
break;
case K_INTERFACE:
buf.append(CorrectionMessages.NewCUCompletionUsingWizardProposal_createinterface_info);
break;
case K_ENUM:
buf.append(CorrectionMessages.NewCUCompletionUsingWizardProposal_createenum_info);
break;
case K_ANNOTATION:
buf.append(CorrectionMessages.NewCUCompletionUsingWizardProposal_createannotation_info);
break;
}
//$NON-NLS-1$
buf.append("<br>");
//$NON-NLS-1$
buf.append("<br>");
if (fTypeContainer instanceof IType) {
buf.append(CorrectionMessages.NewCUCompletionUsingWizardProposal_tooltip_enclosingtype);
} else {
buf.append(CorrectionMessages.NewCUCompletionUsingWizardProposal_tooltip_package);
}
//$NON-NLS-1$
buf.append(" <b>");
buf.append(JavaElementLabels.getElementLabel(fTypeContainer, JavaElementLabels.T_FULLY_QUALIFIED));
//$NON-NLS-1$
buf.append("</b><br>");
//$NON-NLS-1$
buf.append("public ");
switch(fTypeKind) {
case K_CLASS:
//$NON-NLS-1$
buf.append("class <b>");
break;
case K_INTERFACE:
//$NON-NLS-1$
buf.append("interface <b>");
break;
case K_ENUM:
//$NON-NLS-1$
buf.append("enum <b>");
break;
case K_ANNOTATION:
//$NON-NLS-1$
buf.append("@interface <b>");
break;
}
nameToHTML(fTypeNameWithParameters, buf);
ITypeBinding superclass = getPossibleSuperTypeBinding(fNode);
if (superclass != null) {
if (superclass.isClass()) {
if (fTypeKind == K_CLASS) {
//$NON-NLS-1$
buf.append("</b> extends <b>");
nameToHTML(BindingLabelProvider.getBindingLabel(superclass, BindingLabelProvider.DEFAULT_TEXTFLAGS), buf);
}
} else {
if (fTypeKind == K_INTERFACE) {
//$NON-NLS-1$
buf.append("</b> extends <b>");
} else {
//$NON-NLS-1$
buf.append("</b> implements <b>");
}
nameToHTML(BindingLabelProvider.getBindingLabel(superclass, BindingLabelProvider.DEFAULT_TEXTFLAGS), buf);
}
}
//$NON-NLS-1$
buf.append("</b> {<br>}<br>");
return buf.toString();
}
use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.
the class NewDefiningMethodProposal 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();
ImportRewrite importRewrite = getImportRewrite();
ITypeBinding[] bindings = fMethod.getParameterTypes();
IJavaProject project = getCompilationUnit().getJavaProject();
String[][] paramNames = StubUtility.suggestArgumentNamesWithProposals(project, fParamNames);
for (int i = 0; i < bindings.length; i++) {
ITypeBinding curr = bindings[i];
String[] proposedNames = paramNames[i];
SingleVariableDeclaration newParam = ast.newSingleVariableDeclaration();
newParam.setType(importRewrite.addImport(curr, ast));
newParam.setName(ast.newSimpleName(proposedNames[0]));
params.add(newParam);
//$NON-NLS-1$
String groupId = "arg_name_" + i;
addLinkedPosition(rewrite.track(newParam.getName()), false, groupId);
for (int k = 0; k < proposedNames.length; k++) {
addLinkedPositionProposal(groupId, proposedNames[k], null);
}
}
}
use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.
the class NewDefiningMethodProposal method addNewExceptions.
/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.proposals.AbstractMethodCorrectionProposal#addNewExceptions(org.eclipse.jdt.core.dom.rewrite.ASTRewrite, java.util.List)
*/
@Override
protected void addNewExceptions(ASTRewrite rewrite, List<Type> exceptions) throws CoreException {
AST ast = rewrite.getAST();
ImportRewrite importRewrite = getImportRewrite();
ITypeBinding[] bindings = fMethod.getExceptionTypes();
for (int i = 0; i < bindings.length; i++) {
Type newType = importRewrite.addImport(bindings[i], ast);
exceptions.add(newType);
//$NON-NLS-1$
addLinkedPosition(rewrite.track(newType), false, "exc_type_" + i);
}
}
use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.
the class NewMethodCorrectionProposal method getNewMethodType.
/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.proposals.AbstractMethodCorrectionProposal#getNewMethodType(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)
*/
@Override
protected Type getNewMethodType(ASTRewrite rewrite) throws CoreException {
ASTNode node = getInvocationNode();
AST ast = rewrite.getAST();
Type newTypeNode = null;
ITypeBinding[] otherProposals = null;
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, getImportRewrite());
if (node.getParent() instanceof MethodInvocation) {
MethodInvocation parent = (MethodInvocation) node.getParent();
if (parent.getExpression() == node) {
ITypeBinding[] bindings = ASTResolving.getQualifierGuess(node.getRoot(), parent.getName().getIdentifier(), parent.arguments(), getSenderBinding());
if (bindings.length > 0) {
newTypeNode = getImportRewrite().addImport(bindings[0], ast, importRewriteContext);
otherProposals = bindings;
}
}
}
if (newTypeNode == null) {
ITypeBinding binding = ASTResolving.guessBindingForReference(node);
if (binding != null && binding.isWildcardType()) {
binding = ASTResolving.normalizeWildcardType(binding, false, ast);
}
if (binding != null) {
newTypeNode = getImportRewrite().addImport(binding, ast, importRewriteContext);
} else {
ASTNode parent = node.getParent();
if (parent instanceof ExpressionStatement) {
newTypeNode = ast.newPrimitiveType(PrimitiveType.VOID);
} else {
newTypeNode = ASTResolving.guessTypeForReference(ast, node);
if (newTypeNode == null) {
//$NON-NLS-1$
newTypeNode = ast.newSimpleType(ast.newSimpleName("Object"));
}
}
}
}
addLinkedPosition(rewrite.track(newTypeNode), false, KEY_TYPE);
if (otherProposals != null) {
for (int i = 0; i < otherProposals.length; i++) {
addLinkedPositionProposal(KEY_TYPE, otherProposals[i]);
}
}
return newTypeNode;
}
use of org.eclipse.jdt.core.dom.ITypeBinding 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;
}
Aggregations