org.eclipse.jdt.core.dom.VariableDeclarationExpression Java Examples

The following examples show how to use org.eclipse.jdt.core.dom.VariableDeclarationExpression. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VariableDeclarationWriter.java    From juniversal with MIT License 6 votes vote down vote up
@Override
public void write(ASTNode node) {
	// Variable declaration statements & expressions are quite similar, so we handle them both
	// here together

	if (node instanceof VariableDeclarationStatement) {
		VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement) node;

		writeVariableDeclaration(variableDeclarationStatement.modifiers(), variableDeclarationStatement.getType(),
				variableDeclarationStatement.fragments());
		copySpaceAndComments();

		matchAndWrite(";");
	} else {
		VariableDeclarationExpression variableDeclarationExpression = (VariableDeclarationExpression) node;

		writeVariableDeclaration(variableDeclarationExpression.modifiers(),
				variableDeclarationExpression.getType(), variableDeclarationExpression.fragments());
	}
}
 
Example #2
Source File: FlowAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean visit(TryStatement node) {
	if (traverseNode(node)) {
		fFlowContext.pushExcptions(node);
		for (Iterator<VariableDeclarationExpression> iterator = node.resources().iterator(); iterator.hasNext();) {
			iterator.next().accept(this);
		}
		node.getBody().accept(this);
		fFlowContext.popExceptions();
		List<CatchClause> catchClauses = node.catchClauses();
		for (Iterator<CatchClause> iter = catchClauses.iterator(); iter.hasNext();) {
			iter.next().accept(this);
		}
		Block finallyBlock = node.getFinally();
		if (finallyBlock != null) {
			finallyBlock.accept(this);
		}
	}
	return false;
}
 
Example #3
Source File: FlowAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void endVisit(TryStatement node) {
	if (skipNode(node)) {
		return;
	}
	TryFlowInfo info = createTry();
	setFlowInfo(node, info);
	for (Iterator<VariableDeclarationExpression> iterator = node.resources().iterator(); iterator.hasNext();) {
		info.mergeResources(getFlowInfo(iterator.next()), fFlowContext);
	}
	info.mergeTry(getFlowInfo(node.getBody()), fFlowContext);
	for (Iterator<CatchClause> iter = node.catchClauses().iterator(); iter.hasNext();) {
		CatchClause element = iter.next();
		info.mergeCatch(getFlowInfo(element), fFlowContext);
	}
	info.mergeFinally(getFlowInfo(node.getFinally()), fFlowContext);
}
 
Example #4
Source File: NodeUtils.java    From SimFix with GNU General Public License v2.0 6 votes vote down vote up
public boolean visit(VariableDeclarationExpression node) {
	ASTNode parent = node.getParent();
	while(parent != null){
		if(parent instanceof Block || parent instanceof ForStatement){
			break;
		}
		parent = parent.getParent();
	}
	if(parent != null) {
		int start = _unit.getLineNumber(node.getStartPosition());
		int end = _unit.getLineNumber(parent.getStartPosition() + parent.getLength());
		for (Object o : node.fragments()) {
			VariableDeclarationFragment vdf = (VariableDeclarationFragment) o;
			Pair<String, Type> pair = new Pair<String, Type>(vdf.getName().getFullyQualifiedName(), node.getType());
			Pair<Integer, Integer> range = new Pair<Integer, Integer>(start, end);
			_tmpVars.put(pair, range);
		}
	}
	return true;
}
 
Example #5
Source File: InOutFlowAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private void clearAccessMode(FlowInfo info, List<? extends ASTNode> nodes) {
	if (nodes == null || nodes.isEmpty() || info == null) {
		return;
	}
	for (Iterator<? extends ASTNode> iter = nodes.iterator(); iter.hasNext();) {
		Object node = iter.next();
		Iterator<VariableDeclarationFragment> fragments = null;
		if (node instanceof VariableDeclarationStatement) {
			fragments = ((VariableDeclarationStatement) node).fragments().iterator();
		} else if (node instanceof VariableDeclarationExpression) {
			fragments = ((VariableDeclarationExpression) node).fragments().iterator();
		}
		if (fragments != null) {
			while (fragments.hasNext()) {
				clearAccessMode(info, fragments.next());
			}
		}
	}
}
 
Example #6
Source File: ModifierRewrite.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private ListRewrite evaluateListRewrite(ASTRewrite rewrite, ASTNode declNode) {
	switch (declNode.getNodeType()) {
		case ASTNode.METHOD_DECLARATION:
			return rewrite.getListRewrite(declNode, MethodDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.FIELD_DECLARATION:
			return rewrite.getListRewrite(declNode, FieldDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
			return rewrite.getListRewrite(declNode, VariableDeclarationExpression.MODIFIERS2_PROPERTY);
		case ASTNode.VARIABLE_DECLARATION_STATEMENT:
			return rewrite.getListRewrite(declNode, VariableDeclarationStatement.MODIFIERS2_PROPERTY);
		case ASTNode.SINGLE_VARIABLE_DECLARATION:
			return rewrite.getListRewrite(declNode, SingleVariableDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.TYPE_DECLARATION:
			return rewrite.getListRewrite(declNode, TypeDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ENUM_DECLARATION:
			return rewrite.getListRewrite(declNode, EnumDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ANNOTATION_TYPE_DECLARATION:
			return rewrite.getListRewrite(declNode, AnnotationTypeDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ENUM_CONSTANT_DECLARATION:
			return rewrite.getListRewrite(declNode, EnumConstantDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
			return rewrite.getListRewrite(declNode, AnnotationTypeMemberDeclaration.MODIFIERS2_PROPERTY);
		default:
			throw new IllegalArgumentException("node has no modifiers: " + declNode.getClass().getName()); //$NON-NLS-1$
	}
}
 
Example #7
Source File: RefactoringUtility.java    From JDeodorant with MIT License 6 votes vote down vote up
private static Type extractType(VariableDeclaration variableDeclaration) {
	Type returnedVariableType = null;
	if(variableDeclaration instanceof SingleVariableDeclaration) {
		SingleVariableDeclaration singleVariableDeclaration = (SingleVariableDeclaration)variableDeclaration;
		returnedVariableType = singleVariableDeclaration.getType();
	}
	else if(variableDeclaration instanceof VariableDeclarationFragment) {
		VariableDeclarationFragment fragment = (VariableDeclarationFragment)variableDeclaration;
		if(fragment.getParent() instanceof VariableDeclarationStatement) {
			VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement)fragment.getParent();
			returnedVariableType = variableDeclarationStatement.getType();
		}
		else if(fragment.getParent() instanceof VariableDeclarationExpression) {
			VariableDeclarationExpression variableDeclarationExpression = (VariableDeclarationExpression)fragment.getParent();
			returnedVariableType = variableDeclarationExpression.getType();
		}
		else if(fragment.getParent() instanceof FieldDeclaration) {
			FieldDeclaration fieldDeclaration = (FieldDeclaration)fragment.getParent();
			returnedVariableType = fieldDeclaration.getType();
		}
	}
	return returnedVariableType;
}
 
Example #8
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean visit(final VariableDeclarationExpression it) {
  final Procedure2<VariableDeclarationFragment, Integer> _function = (VariableDeclarationFragment frag, Integer counter) -> {
    this.appendModifiers(it, it.modifiers());
    this.appendToBuffer(this._aSTFlattenerUtils.handleVariableDeclaration(it.modifiers()));
    this.appendSpaceToBuffer();
    it.getType().accept(this);
    this.appendSpaceToBuffer();
    frag.accept(this);
    int _size = it.fragments().size();
    int _minus = (_size - 1);
    boolean _lessThan = ((counter).intValue() < _minus);
    if (_lessThan) {
      this.appendToBuffer(",");
      this.appendSpaceToBuffer();
    }
  };
  IterableExtensions.<VariableDeclarationFragment>forEach(it.fragments(), _function);
  return false;
}
 
Example #9
Source File: VariableDeclaration.java    From RefactoringMiner with MIT License 6 votes vote down vote up
private static CodeElementType extractVariableDeclarationType(org.eclipse.jdt.core.dom.VariableDeclaration variableDeclaration) {
	if(variableDeclaration instanceof SingleVariableDeclaration) {
		return CodeElementType.SINGLE_VARIABLE_DECLARATION;
	}
	else if(variableDeclaration instanceof VariableDeclarationFragment) {
		VariableDeclarationFragment fragment = (VariableDeclarationFragment)variableDeclaration;
		if(fragment.getParent() instanceof VariableDeclarationStatement) {
			return CodeElementType.VARIABLE_DECLARATION_STATEMENT;
		}
		else if(fragment.getParent() instanceof VariableDeclarationExpression) {
			return CodeElementType.VARIABLE_DECLARATION_EXPRESSION;
		}
		else if(fragment.getParent() instanceof FieldDeclaration) {
			return CodeElementType.FIELD_DECLARATION;
		}
	}
	return null;
}
 
Example #10
Source File: ChangeTypeRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static Type getType(ASTNode node) {
	switch(node.getNodeType()){
		case ASTNode.SINGLE_VARIABLE_DECLARATION:
			return ((SingleVariableDeclaration) node).getType();
		case ASTNode.FIELD_DECLARATION:
			return ((FieldDeclaration) node).getType();
		case ASTNode.VARIABLE_DECLARATION_STATEMENT:
			return ((VariableDeclarationStatement) node).getType();
		case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
			return ((VariableDeclarationExpression) node).getType();
		case ASTNode.METHOD_DECLARATION:
			return ((MethodDeclaration)node).getReturnType2();
		case ASTNode.PARAMETERIZED_TYPE:
			return ((ParameterizedType)node).getType();
		default:
			Assert.isTrue(false);
			return null;
	}
}
 
Example #11
Source File: InferTypeArgumentsConstraintCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void endVisit(VariableDeclarationExpression node) {
	// Constrain the types of the child VariableDeclarationFragments to be equal to one
	// another, since the initializers in a 'for' statement can only have one type.
	// Pairwise constraints between adjacent variables is enough.
	Type type= node.getType();
	ConstraintVariable2 typeCv= getConstraintVariable(type);
	if (typeCv == null)
		return;

	setConstraintVariable(node, typeCv);

	List<VariableDeclarationFragment> fragments= node.fragments();
	for (Iterator<VariableDeclarationFragment> iter= fragments.iterator(); iter.hasNext();) {
		VariableDeclarationFragment fragment= iter.next();
		ConstraintVariable2 fragmentCv= getConstraintVariable(fragment);
		fTCModel.createElementEqualsConstraints(typeCv, fragmentCv);
	}
}
 
Example #12
Source File: InlineTempRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private RefactoringStatus checkSelection(VariableDeclaration decl) {
	ASTNode parent= decl.getParent();
	if (parent instanceof MethodDeclaration) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_method_parameter);
	}

	if (parent instanceof CatchClause) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_exceptions_declared);
	}

	if (parent instanceof VariableDeclarationExpression && parent.getLocationInParent() == ForStatement.INITIALIZERS_PROPERTY) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_for_initializers);
	}

	if (parent instanceof VariableDeclarationExpression && parent.getLocationInParent() == TryStatement.RESOURCES_PROPERTY) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineTempRefactoring_resource_in_try_with_resources);
	}

	if (decl.getInitializer() == null) {
		String message= Messages.format(RefactoringCoreMessages.InlineTempRefactoring_not_initialized, BasicElementLabels.getJavaElementName(decl.getName().getIdentifier()));
		return RefactoringStatus.createFatalErrorStatus(message);
	}

	return checkAssignments(decl);
}
 
Example #13
Source File: InOutFlowAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void clearAccessMode(FlowInfo info, List<? extends ASTNode> nodes) {
	if (nodes== null || nodes.isEmpty() || info == null)
		return;
	for (Iterator<? extends ASTNode> iter= nodes.iterator(); iter.hasNext(); ) {
		Object node= iter.next();
		Iterator<VariableDeclarationFragment> fragments= null;
		if (node instanceof VariableDeclarationStatement) {
			fragments= ((VariableDeclarationStatement)node).fragments().iterator();
		} else if (node instanceof VariableDeclarationExpression) {
			fragments= ((VariableDeclarationExpression)node).fragments().iterator();
		}
		if (fragments != null) {
			while (fragments.hasNext()) {
				clearAccessMode(info, fragments.next());
			}
		}
	}
}
 
Example #14
Source File: ExtractTempRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private RefactoringStatus checkExpression() throws JavaModelException {
	Expression selectedExpression= getSelectedExpression().getAssociatedExpression();
	if (selectedExpression != null) {
		final ASTNode parent= selectedExpression.getParent();
		if (selectedExpression instanceof NullLiteral) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_null_literals);
		} else if (selectedExpression instanceof ArrayInitializer) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_array_initializer);
		} else if (selectedExpression instanceof Assignment) {
			if (parent instanceof Expression && !(parent instanceof ParenthesizedExpression))
				return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_assignment);
			else
				return null;
		} else if (selectedExpression instanceof SimpleName) {
			if ((((SimpleName) selectedExpression)).isDeclaration())
				return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_names_in_declarations);
			if (parent instanceof QualifiedName && selectedExpression.getLocationInParent() == QualifiedName.NAME_PROPERTY || parent instanceof FieldAccess && selectedExpression.getLocationInParent() == FieldAccess.NAME_PROPERTY)
				return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_select_expression);
		} else if (selectedExpression instanceof VariableDeclarationExpression && parent instanceof TryStatement) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_resource_in_try_with_resources);
		}
	}

	return null;
}
 
Example #15
Source File: ASTNodes.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the type node for the given declaration.
 * 
 * @param declaration the declaration
 * @return the type node or <code>null</code> if the given declaration represents a type
 *         inferred parameter in lambda expression
 */
public static Type getType(VariableDeclaration declaration) {
	if (declaration instanceof SingleVariableDeclaration) {
		return ((SingleVariableDeclaration)declaration).getType();
	} else if (declaration instanceof VariableDeclarationFragment) {
		ASTNode parent= ((VariableDeclarationFragment)declaration).getParent();
		if (parent instanceof VariableDeclarationExpression)
			return ((VariableDeclarationExpression)parent).getType();
		else if (parent instanceof VariableDeclarationStatement)
			return ((VariableDeclarationStatement)parent).getType();
		else if (parent instanceof FieldDeclaration)
			return ((FieldDeclaration)parent).getType();
		else if (parent instanceof LambdaExpression)
			return null;
	}
	Assert.isTrue(false, "Unknown VariableDeclaration"); //$NON-NLS-1$
	return null;
}
 
Example #16
Source File: ModifierRewrite.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private ListRewrite evaluateListRewrite(ASTRewrite rewrite, ASTNode declNode) {
	switch (declNode.getNodeType()) {
		case ASTNode.METHOD_DECLARATION:
			return rewrite.getListRewrite(declNode, MethodDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.FIELD_DECLARATION:
			return rewrite.getListRewrite(declNode, FieldDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
			return rewrite.getListRewrite(declNode, VariableDeclarationExpression.MODIFIERS2_PROPERTY);
		case ASTNode.VARIABLE_DECLARATION_STATEMENT:
			return rewrite.getListRewrite(declNode, VariableDeclarationStatement.MODIFIERS2_PROPERTY);
		case ASTNode.SINGLE_VARIABLE_DECLARATION:
			return rewrite.getListRewrite(declNode, SingleVariableDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.TYPE_DECLARATION:
			return rewrite.getListRewrite(declNode, TypeDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ENUM_DECLARATION:
			return rewrite.getListRewrite(declNode, EnumDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ANNOTATION_TYPE_DECLARATION:
			return rewrite.getListRewrite(declNode, AnnotationTypeDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ENUM_CONSTANT_DECLARATION:
			return rewrite.getListRewrite(declNode, EnumConstantDeclaration.MODIFIERS2_PROPERTY);
		case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
			return rewrite.getListRewrite(declNode, AnnotationTypeMemberDeclaration.MODIFIERS2_PROPERTY);
		default:
			throw new IllegalArgumentException("node has no modifiers: " + declNode.getClass().getName()); //$NON-NLS-1$
	}
}
 
Example #17
Source File: VariableDeclarationFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean visit(VariableDeclarationExpression node) {
	if (fAddFinalLocals && node.fragments().size() == 1) {
		SimpleName name= ((VariableDeclarationFragment)node.fragments().get(0)).getName();

		IBinding binding= name.resolveBinding();
		if (binding == null)
			return false;

		if (fWrittenVariables.containsKey(binding))
			return false;

		ModifierChangeOperation op= createAddFinalOperation(name, node);
		if (op == null)
			return false;

		fResult.add(op);
		return false;
	}
	return false;
}
 
Example #18
Source File: VariableDeclarationFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static ModifierChangeOperation createAddFinalOperation(SimpleName name, ASTNode decl) {
	if (decl == null)
		return null;

	IBinding binding= name.resolveBinding();
	if (!canAddFinal(binding, decl))
		return null;

	if (decl instanceof SingleVariableDeclaration) {
		return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
	} else if (decl instanceof VariableDeclarationExpression) {
		return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
	} else if (decl instanceof VariableDeclarationFragment){
		VariableDeclarationFragment frag= (VariableDeclarationFragment)decl;
		decl= decl.getParent();
		if (decl instanceof FieldDeclaration || decl instanceof VariableDeclarationStatement) {
			List<VariableDeclarationFragment> list= new ArrayList<VariableDeclarationFragment>();
			list.add(frag);
			return new ModifierChangeOperation(decl, list, Modifier.FINAL, Modifier.NONE);
		} else if (decl instanceof VariableDeclarationExpression) {
			return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
		}
	}

	return null;
}
 
Example #19
Source File: ExtractMethodFragmentRefactoring.java    From JDeodorant with MIT License 6 votes vote down vote up
protected TryStatement copyTryStatement(ASTRewrite sourceRewriter, AST ast, TryStatement tryStatementParent) {
	TryStatement newTryStatement = ast.newTryStatement();
	ListRewrite resourceRewrite = sourceRewriter.getListRewrite(newTryStatement, TryStatement.RESOURCES_PROPERTY);
	List<VariableDeclarationExpression> resources = tryStatementParent.resources();
	for(VariableDeclarationExpression expression : resources) {
		resourceRewrite.insertLast(expression, null);
	}
	ListRewrite catchClauseRewrite = sourceRewriter.getListRewrite(newTryStatement, TryStatement.CATCH_CLAUSES_PROPERTY);
	List<CatchClause> catchClauses = tryStatementParent.catchClauses();
	for(CatchClause catchClause : catchClauses) {
		catchClauseRewrite.insertLast(catchClause, null);
	}
	if(tryStatementParent.getFinally() != null) {
		sourceRewriter.set(newTryStatement, TryStatement.FINALLY_PROPERTY, tryStatementParent.getFinally(), null);
	}
	return newTryStatement;
}
 
Example #20
Source File: GenerateForLoopAssistProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #21
Source File: GenerateForLoopAssistProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #22
Source File: GenerateForLoopAssistProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #23
Source File: ConvertIterableLoopOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IStatus checkIteratorCondition() {

		List<Expression> initializers= getForStatement().initializers();
		if (initializers.size() != 1)
			return SEMANTIC_CHANGE_WARNING_STATUS;

		Expression expression= initializers.get(0);
		if (!(expression instanceof VariableDeclarationExpression))
			return SEMANTIC_CHANGE_WARNING_STATUS;

		VariableDeclarationExpression declaration= (VariableDeclarationExpression)expression;
		List<VariableDeclarationFragment> variableDeclarationFragments= declaration.fragments();
		if (variableDeclarationFragments.size() != 1)
			return SEMANTIC_CHANGE_WARNING_STATUS;

		VariableDeclarationFragment declarationFragment= variableDeclarationFragments.get(0);

		Expression initializer= declarationFragment.getInitializer();
		if (!(initializer instanceof MethodInvocation))
			return SEMANTIC_CHANGE_WARNING_STATUS;

		MethodInvocation methodInvocation= (MethodInvocation)initializer;
		String methodName= methodInvocation.getName().getIdentifier();
		if (!"iterator".equals(methodName) || methodInvocation.arguments().size() != 0) //$NON-NLS-1$
			return SEMANTIC_CHANGE_WARNING_STATUS;

		return StatusInfo.OK_STATUS;
	}
 
Example #24
Source File: ExtractMethodAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void endVisit(VariableDeclarationExpression node) {
	if (getSelection().getEndVisitSelectionMode(node) == Selection.SELECTED && getFirstSelectedNode() == node) {
		if (node.getLocationInParent() == TryStatement.RESOURCES_PROPERTY) {
			invalidSelection(RefactoringCoreMessages.ExtractMethodAnalyzer_resource_in_try_with_resources, JavaStatusContext.create(fCUnit, getSelection()));
		}
	}
	checkTypeInDeclaration(node.getType());
	super.endVisit(node);
}
 
Example #25
Source File: FlowAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void endVisit(VariableDeclarationExpression node) {
	if (skipNode(node)) {
		return;
	}
	GenericSequentialFlowInfo info = processSequential(node, node.getType());
	process(info, node.fragments());
}
 
Example #26
Source File: ExtractTempRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static List<IVariableBinding> getForInitializedVariables(VariableDeclarationExpression variableDeclarations) {
	List<IVariableBinding> forInitializerVariables= new ArrayList<IVariableBinding>(1);
	for (Iterator<VariableDeclarationFragment> iter= variableDeclarations.fragments().iterator(); iter.hasNext();) {
		VariableDeclarationFragment fragment= iter.next();
		IVariableBinding binding= fragment.resolveBinding();
		if (binding != null)
			forInitializerVariables.add(binding);
	}
	return forInitializerVariables;
}
 
Example #27
Source File: FlowAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void endVisit(VariableDeclarationExpression node) {
	if (skipNode(node))
		return;
	GenericSequentialFlowInfo info= processSequential(node, node.getType());
	process(info, node.fragments());
}
 
Example #28
Source File: ExtractTempRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static List<IVariableBinding> getForInitializedVariables(VariableDeclarationExpression variableDeclarations) {
	List<IVariableBinding> forInitializerVariables = new ArrayList<>(1);
	for (Iterator<VariableDeclarationFragment> iter = variableDeclarations.fragments().iterator(); iter.hasNext();) {
		VariableDeclarationFragment fragment = iter.next();
		IVariableBinding binding = fragment.resolveBinding();
		if (binding != null) {
			forInitializerVariables.add(binding);
		}
	}
	return forInitializerVariables;
}
 
Example #29
Source File: ExtractMethodAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void endVisit(VariableDeclarationExpression node) {
	if (getSelection().getEndVisitSelectionMode(node) == Selection.SELECTED && getFirstSelectedNode() == node) {
		if (node.getLocationInParent() == TryStatement.RESOURCES_PROPERTY) {
			invalidSelection(RefactoringCoreMessages.ExtractMethodAnalyzer_resource_in_try_with_resources, JavaStatusContext.create(fCUnit, getSelection()));
		}
	}
	checkTypeInDeclaration(node.getType());
	super.endVisit(node);
}
 
Example #30
Source File: TypeParseVisitor.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
public boolean visit(VariableDeclarationExpression node) {
	if(isAnonymousClass(node)){
		return true;
	}
	for (Object o : node.fragments()) {
		VariableDeclarationFragment vdf = (VariableDeclarationFragment) o;
		Type type = node.getType();
		if(vdf.getExtraDimensions() > 0){
			AST ast = AST.newAST(AST.JLS8);
			type = ast.newArrayType((Type) ASTNode.copySubtree(ast, type), vdf.getExtraDimensions());
		}
		map.put(vdf.getName().toString(), type);
	}
	return true;
}