use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.
the class GenerateForLoopAssistProposal method generateIteratorBasedForRewrite.
/**
* Helper to generate an iterator based <code>for</code> loop to iterate over an
* {@link Iterable}.
*
* @param ast the {@link AST} instance to rewrite the loop to
* @return the complete {@link ASTRewrite} object
*/
private ASTRewrite generateIteratorBasedForRewrite(AST ast) {
ASTRewrite rewrite = ASTRewrite.create(ast);
ForStatement loopStatement = ast.newForStatement();
ITypeBinding loopOverType = extractElementType(ast);
//$NON-NLS-1$
SimpleName loopVariableName = resolveLinkedVariableNameWithProposals(rewrite, "iterator", null, true);
loopStatement.initializers().add(getIteratorBasedForInitializer(rewrite, loopVariableName));
MethodInvocation loopExpression = ast.newMethodInvocation();
//$NON-NLS-1$
loopExpression.setName(ast.newSimpleName("hasNext"));
SimpleName expressionName = ast.newSimpleName(loopVariableName.getIdentifier());
addLinkedPosition(rewrite.track(expressionName), LinkedPositionGroup.NO_STOP, expressionName.getIdentifier());
loopExpression.setExpression(expressionName);
loopStatement.setExpression(loopExpression);
Block forLoopBody = ast.newBlock();
Assignment assignResolvedVariable = getIteratorBasedForBodyAssignment(rewrite, loopOverType, loopVariableName);
forLoopBody.statements().add(ast.newExpressionStatement(assignResolvedVariable));
forLoopBody.statements().add(createBlankLineStatementWithCursorPosition(rewrite));
loopStatement.setBody(forLoopBody);
rewrite.replace(fCurrentNode, loopStatement, null);
return rewrite;
}
use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.
the class GenerateForLoopAssistProposal method extractElementType.
/**
* Extracts the type parameter of the variable contained in fCurrentExpression or the elements
* type to iterate over an array using <code>foreach</code>.
*
* @param ast the current {@link AST} instance
* @return the {@link ITypeBinding} of the elements to iterate over
*/
private ITypeBinding extractElementType(AST ast) {
if (fExpressionType.isArray()) {
return Bindings.normalizeForDeclarationUse(fExpressionType.getElementType(), ast);
}
// extract elements type directly out of the bindings
//$NON-NLS-1$
IMethodBinding iteratorMethodBinding = Bindings.findMethodInHierarchy(fExpressionType, "iterator", new ITypeBinding[] {});
IMethodBinding iteratorNextMethodBinding = Bindings.findMethodInHierarchy(iteratorMethodBinding.getReturnType(), "next", //$NON-NLS-1$
new ITypeBinding[] {});
ITypeBinding currentElementBinding = iteratorNextMethodBinding.getReturnType();
return Bindings.normalizeForDeclarationUse(currentElementBinding, ast);
}
use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.
the class GenerateForLoopAssistProposal method generateForEachRewrite.
/**
* Helper to generate a <code>foreach</code> loop to iterate over an {@link Iterable}.
*
* @param ast the {@link AST} instance to rewrite the loop to
* @return the complete {@link ASTRewrite} object
*/
private ASTRewrite generateForEachRewrite(AST ast) {
EnhancedForStatement loopStatement = ast.newEnhancedForStatement();
ASTRewrite rewrite = ASTRewrite.create(ast);
ITypeBinding loopOverType = extractElementType(ast);
// generate name proposals and add them to the variable declaration
SimpleName forDeclarationName = resolveLinkedVariableNameWithProposals(rewrite, loopOverType.getName(), null, true);
SingleVariableDeclaration forLoopInitializer = ast.newSingleVariableDeclaration();
forLoopInitializer.setType(getImportRewrite().addImport(loopOverType, ast, new ContextSensitiveImportRewriteContext(fCurrentNode, getImportRewrite())));
forLoopInitializer.setName(forDeclarationName);
loopStatement.setParameter(forLoopInitializer);
loopStatement.setExpression((Expression) rewrite.createCopyTarget(fCurrentExpression));
Block forLoopBody = ast.newBlock();
forLoopBody.statements().add(createBlankLineStatementWithCursorPosition(rewrite));
loopStatement.setBody(forLoopBody);
rewrite.replace(fCurrentNode, loopStatement, null);
return rewrite;
}
use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.
the class AssignToVariableAssistProposal method evaluateType.
private Type evaluateType(AST ast) {
ITypeBinding[] proposals = ASTResolving.getRelaxingTypes(ast, fTypeBinding);
for (int i = 0; i < proposals.length; i++) {
addLinkedPositionProposal(KEY_TYPE, proposals[i]);
}
ImportRewrite importRewrite = getImportRewrite();
CompilationUnit cuNode = (CompilationUnit) fNodeToAssign.getRoot();
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(cuNode, fNodeToAssign.getStartPosition(), importRewrite);
return importRewrite.addImport(fTypeBinding, ast, context);
}
use of org.eclipse.jdt.core.dom.ITypeBinding 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;
}
Aggregations