org.eclipse.jdt.core.dom.VariableDeclarationExpression#setType ( )源码实例Demo

下面列出了org.eclipse.jdt.core.dom.VariableDeclarationExpression#setType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

/**
 * Generates the initializer for an iterator based <code>for</code> loop, which declares and
 * initializes the variable to loop over.
 * 
 * @param rewrite the instance of {@link ASTRewrite}
 * @param loopVariableName the proposed name of the loop variable
 * @return a {@link VariableDeclarationExpression} to use as initializer
 */
private VariableDeclarationExpression getIteratorBasedForInitializer(ASTRewrite rewrite, SimpleName loopVariableName) {
	AST ast= rewrite.getAST();
	IMethodBinding iteratorMethodBinding= Bindings.findMethodInHierarchy(fExpressionType, "iterator", new ITypeBinding[] {}); //$NON-NLS-1$
	// initializing fragment
	VariableDeclarationFragment varDeclarationFragment= ast.newVariableDeclarationFragment();
	varDeclarationFragment.setName(loopVariableName);
	MethodInvocation iteratorExpression= ast.newMethodInvocation();
	iteratorExpression.setName(ast.newSimpleName(iteratorMethodBinding.getName()));
	iteratorExpression.setExpression((Expression) rewrite.createCopyTarget(fCurrentExpression));
	varDeclarationFragment.setInitializer(iteratorExpression);

	// declaration
	VariableDeclarationExpression varDeclarationExpression= ast.newVariableDeclarationExpression(varDeclarationFragment);
	varDeclarationExpression.setType(getImportRewrite().addImport(iteratorMethodBinding.getReturnType(), ast, new ContextSensitiveImportRewriteContext(fCurrentNode, getImportRewrite())));

	return varDeclarationExpression;
}
 
/**
 * Generates the Assignment in an iterator based for, used in the first statement of an iterator
 * based <code>for</code> loop body, to retrieve the next element of the {@link Iterable}
 * instance.
 * 
 * @param rewrite the current instance of {@link ASTRewrite}
 * @param loopOverType the {@link ITypeBinding} of the loop variable
 * @param loopVariableName the name of the loop variable
 * @return an {@link Assignment}, which retrieves the next element of the {@link Iterable} using
 *         the active {@link Iterator}
 */
private Assignment getIteratorBasedForBodyAssignment(ASTRewrite rewrite, ITypeBinding loopOverType, SimpleName loopVariableName) {
	AST ast= rewrite.getAST();
	Assignment assignResolvedVariable= ast.newAssignment();

	// left hand side
	SimpleName resolvedVariableName= resolveLinkedVariableNameWithProposals(rewrite, loopOverType.getName(), loopVariableName.getIdentifier(), false);
	VariableDeclarationFragment resolvedVariableDeclarationFragment= ast.newVariableDeclarationFragment();
	resolvedVariableDeclarationFragment.setName(resolvedVariableName);
	VariableDeclarationExpression resolvedVariableDeclaration= ast.newVariableDeclarationExpression(resolvedVariableDeclarationFragment);
	resolvedVariableDeclaration.setType(getImportRewrite().addImport(loopOverType, ast, new ContextSensitiveImportRewriteContext(fCurrentNode, getImportRewrite())));
	assignResolvedVariable.setLeftHandSide(resolvedVariableDeclaration);

	// right hand side
	MethodInvocation invokeIteratorNextExpression= ast.newMethodInvocation();
	invokeIteratorNextExpression.setName(ast.newSimpleName("next")); //$NON-NLS-1$
	SimpleName currentElementName= ast.newSimpleName(loopVariableName.getIdentifier());
	addLinkedPosition(rewrite.track(currentElementName), LinkedPositionGroup.NO_STOP, currentElementName.getIdentifier());
	invokeIteratorNextExpression.setExpression(currentElementName);
	assignResolvedVariable.setRightHandSide(invokeIteratorNextExpression);

	assignResolvedVariable.setOperator(Assignment.Operator.ASSIGN);

	return assignResolvedVariable;
}
 
public ExpressionStatement createInitializer(ParameterInfo pi, String paramName, CompilationUnitRewrite cuRewrite) {
	AST ast= cuRewrite.getAST();

	VariableDeclarationFragment fragment= ast.newVariableDeclarationFragment();
	fragment.setName(ast.newSimpleName(pi.getOldName()));
	fragment.setInitializer(createFieldReadAccess(pi, paramName, ast, cuRewrite.getCu().getJavaProject(), false, null));
	VariableDeclarationExpression declaration= ast.newVariableDeclarationExpression(fragment);
	IVariableBinding variable= pi.getOldBinding();
	declaration.setType(importBinding(pi.getNewTypeBinding(), cuRewrite));
	int modifiers= variable.getModifiers();
	List<Modifier> newModifiers= ast.newModifiers(modifiers);
	declaration.modifiers().addAll(newModifiers);
	return ast.newExpressionStatement(declaration);
}
 
/**
 * Creates an {@link Assignment} as first expression appearing in a <code>for</code> loop's
 * body. This Assignment declares a local variable and initializes it using the array's current
 * element identified by the loop index.
 * 
 * @param rewrite the current {@link ASTRewrite} instance
 * @param loopVariableName the name of the index variable in String representation
 * @return a completed {@link Assignment} containing the mentioned declaration and
 *         initialization
 */
private Assignment getForBodyAssignment(ASTRewrite rewrite, SimpleName loopVariableName) {
	AST ast= rewrite.getAST();
	ITypeBinding loopOverType= extractElementType(ast);

	Assignment assignResolvedVariable= ast.newAssignment();

	// left hand side
	SimpleName resolvedVariableName= resolveLinkedVariableNameWithProposals(rewrite, loopOverType.getName(), loopVariableName.getIdentifier(), false);
	VariableDeclarationFragment resolvedVariableDeclarationFragment= ast.newVariableDeclarationFragment();
	resolvedVariableDeclarationFragment.setName(resolvedVariableName);
	VariableDeclarationExpression resolvedVariableDeclaration= ast.newVariableDeclarationExpression(resolvedVariableDeclarationFragment);
	resolvedVariableDeclaration.setType(getImportRewrite().addImport(loopOverType, ast, new ContextSensitiveImportRewriteContext(fCurrentNode, getImportRewrite())));
	assignResolvedVariable.setLeftHandSide(resolvedVariableDeclaration);

	// right hand side
	ArrayAccess access= ast.newArrayAccess();
	access.setArray((Expression) rewrite.createCopyTarget(fCurrentExpression));
	SimpleName indexName= ast.newSimpleName(loopVariableName.getIdentifier());
	addLinkedPosition(rewrite.track(indexName), LinkedPositionGroup.NO_STOP, indexName.getIdentifier());
	access.setIndex(indexName);
	assignResolvedVariable.setRightHandSide(access);

	assignResolvedVariable.setOperator(Assignment.Operator.ASSIGN);

	return assignResolvedVariable;
}
 
/**
 * Generates a {@link VariableDeclarationExpression}, which initializes the loop variable to
 * iterate over an array.
 * 
 * @param ast the current {@link AST} instance
 * @param loopVariableName the name of the variable which should be initialized
 * @return a filled {@link VariableDeclarationExpression}, declaring a int variable, which is
 *         initializes with 0
 */
private VariableDeclarationExpression getForInitializer(AST ast, SimpleName loopVariableName) {
	// initializing fragment
	VariableDeclarationFragment firstDeclarationFragment= ast.newVariableDeclarationFragment();
	firstDeclarationFragment.setName(loopVariableName);
	NumberLiteral startIndex= ast.newNumberLiteral();
	firstDeclarationFragment.setInitializer(startIndex);

	// declaration
	VariableDeclarationExpression variableDeclaration= ast.newVariableDeclarationExpression(firstDeclarationFragment);
	PrimitiveType variableType= ast.newPrimitiveType(PrimitiveType.INT);
	variableDeclaration.setType(variableType);

	return variableDeclaration;
}
 
/**
 * Creates an {@link Assignment} as first expression appearing in an index based
 * <code>for</code> loop's body. This Assignment declares a local variable and initializes it
 * using the {@link List}'s current element identified by the loop index.
 * 
 * @param rewrite the current {@link ASTRewrite} instance
 * @param loopVariableName the name of the index variable in String representation
 * @return a completed {@link Assignment} containing the mentioned declaration and
 *         initialization
 */
private Expression getIndexBasedForBodyAssignment(ASTRewrite rewrite, SimpleName loopVariableName) {
	AST ast= rewrite.getAST();
	ITypeBinding loopOverType= extractElementType(ast);

	Assignment assignResolvedVariable= ast.newAssignment();

	// left hand side
	SimpleName resolvedVariableName= resolveLinkedVariableNameWithProposals(rewrite, loopOverType.getName(), loopVariableName.getIdentifier(), false);
	VariableDeclarationFragment resolvedVariableDeclarationFragment= ast.newVariableDeclarationFragment();
	resolvedVariableDeclarationFragment.setName(resolvedVariableName);
	VariableDeclarationExpression resolvedVariableDeclaration= ast.newVariableDeclarationExpression(resolvedVariableDeclarationFragment);
	resolvedVariableDeclaration.setType(getImportRewrite().addImport(loopOverType, ast, new ContextSensitiveImportRewriteContext(fCurrentNode, getImportRewrite())));
	assignResolvedVariable.setLeftHandSide(resolvedVariableDeclaration);

	// right hand side
	MethodInvocation invokeGetExpression= ast.newMethodInvocation();
	invokeGetExpression.setName(ast.newSimpleName("get")); //$NON-NLS-1$
	SimpleName indexVariableName= ast.newSimpleName(loopVariableName.getIdentifier());
	addLinkedPosition(rewrite.track(indexVariableName), LinkedPositionGroup.NO_STOP, indexVariableName.getIdentifier());
	invokeGetExpression.arguments().add(indexVariableName);
	invokeGetExpression.setExpression((Expression) rewrite.createCopyTarget(fCurrentExpression));
	assignResolvedVariable.setRightHandSide(invokeGetExpression);

	assignResolvedVariable.setOperator(Assignment.Operator.ASSIGN);

	return assignResolvedVariable;
}
 
private ASTRewrite doAddLocal() {
	ASTNode nodeToAssign= fNodesToAssign.get(0);
	Expression expression= ((ExpressionStatement) nodeToAssign).getExpression();
	AST ast= nodeToAssign.getAST();

	ASTRewrite rewrite= ASTRewrite.create(ast);

	createImportRewrite((CompilationUnit) nodeToAssign.getRoot());

	String[] varNames= suggestLocalVariableNames(fTypeBinding, expression);
	for (int i= 0; i < varNames.length; i++) {
		addLinkedPositionProposal(KEY_NAME, varNames[i]);
	}

	VariableDeclarationFragment newDeclFrag= ast.newVariableDeclarationFragment();
	newDeclFrag.setName(ast.newSimpleName(varNames[0]));
	newDeclFrag.setInitializer((Expression) rewrite.createCopyTarget(expression));

	Type type= evaluateType(ast, nodeToAssign, fTypeBinding, KEY_TYPE, TypeLocation.LOCAL_VARIABLE);

	if (ASTNodes.isControlStatementBody(nodeToAssign.getLocationInParent())) {
		Block block= ast.newBlock();
		block.statements().add(rewrite.createMoveTarget(nodeToAssign));
		rewrite.replace(nodeToAssign, block, null);
	}

	if (needsSemicolon(expression)) {
		VariableDeclarationStatement varStatement= ast.newVariableDeclarationStatement(newDeclFrag);
		varStatement.setType(type);
		rewrite.replace(expression, varStatement, null);
	} else {
		// trick for bug 43248: use an VariableDeclarationExpression and keep the ExpressionStatement
		VariableDeclarationExpression varExpression= ast.newVariableDeclarationExpression(newDeclFrag);
		varExpression.setType(type);
		rewrite.replace(expression, varExpression, null);
	}

	addLinkedPosition(rewrite.track(newDeclFrag.getName()), true, KEY_NAME);
	addLinkedPosition(rewrite.track(type), false, KEY_TYPE);
	setEndPosition(rewrite.track(nodeToAssign)); // set cursor after expression statement

	return rewrite;
}
 
private ASTRewrite doAddLocal() {
	Expression expression= ((ExpressionStatement) fNodeToAssign).getExpression();
	AST ast= fNodeToAssign.getAST();

	ASTRewrite rewrite= ASTRewrite.create(ast);

	createImportRewrite((CompilationUnit) fNodeToAssign.getRoot());

	String[] varNames= suggestLocalVariableNames(fTypeBinding, expression);
	for (int i= 0; i < varNames.length; i++) {
		addLinkedPositionProposal(KEY_NAME, varNames[i], null);
	}

	VariableDeclarationFragment newDeclFrag= ast.newVariableDeclarationFragment();
	newDeclFrag.setName(ast.newSimpleName(varNames[0]));
	newDeclFrag.setInitializer((Expression) rewrite.createCopyTarget(expression));

	Type type= evaluateType(ast);

	if (ASTNodes.isControlStatementBody(fNodeToAssign.getLocationInParent())) {
		Block block= ast.newBlock();
		block.statements().add(rewrite.createMoveTarget(fNodeToAssign));
		rewrite.replace(fNodeToAssign, block, null);
	}

	if (needsSemicolon(expression)) {
		VariableDeclarationStatement varStatement= ast.newVariableDeclarationStatement(newDeclFrag);
		varStatement.setType(type);
		rewrite.replace(expression, varStatement, null);
	} else {
		// trick for bug 43248: use an VariableDeclarationExpression and keep the ExpressionStatement
		VariableDeclarationExpression varExpression= ast.newVariableDeclarationExpression(newDeclFrag);
		varExpression.setType(type);
		rewrite.replace(expression, varExpression, null);
	}

	addLinkedPosition(rewrite.track(newDeclFrag.getName()), true, KEY_NAME);
	addLinkedPosition(rewrite.track(type), false, KEY_TYPE);
	setEndPosition(rewrite.track(fNodeToAssign)); // set cursor after expression statement

	return rewrite;
}