Java Code Examples for org.eclipse.jdt.core.dom.VariableDeclarationStatement#fragments()

The following examples show how to use org.eclipse.jdt.core.dom.VariableDeclarationStatement#fragments() . 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: MappingState.java    From JDeodorant with MIT License 6 votes vote down vote up
private boolean nodeIsUnmappedTemporaryVariableDeclaration(PDGNode srcPDGNode, AbstractVariable variable, PDGNodeMapping mapping, Set<PDGNode> mappedNodes) {
	if(srcPDGNode.declaresLocalVariable(variable.getInitialVariable()) && !mappedNodes.contains(srcPDGNode)) {
		if(srcPDGNode.getASTStatement() instanceof VariableDeclarationStatement) {
			VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement)srcPDGNode.getASTStatement();
			List<VariableDeclarationFragment> fragments = variableDeclarationStatement.fragments();
			for(VariableDeclarationFragment fragment : fragments) {
				if(fragment.resolveBinding().getKey().equals(variable.getInitialVariable().getVariableBindingKey())) {
					String initializer = fragment.getInitializer().toString();
					for(ASTNodeDifference difference : mapping.getNodeDifferences()) {
						String expr1 = difference.getExpression1().toString();
						String expr2 = difference.getExpression2().toString();
						if(expr1.contains(initializer) || expr2.contains(initializer)) {
							return true;
						}
					}
				}
			}
		}
	}
	return false;
}
 
Example 2
Source File: NodeUtils.java    From SimFix with GNU General Public License v2.0 6 votes vote down vote up
public boolean visit(VariableDeclarationStatement node) {
	ASTNode parent = node.getParent();
	while(parent != null){
		if(parent instanceof Block){
			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 3
Source File: JavaApproximateVariableBindingExtractor.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Looks for local variable declarations. For every declaration of a
 * variable, the parent {@link Block} denoting the variable's scope is
 * stored in {@link #variableScope} map.
 *
 * @param node
 *            the node to visit
 */
@Override
public boolean visit(final VariableDeclarationStatement node) {
	for (final Object fragment : node.fragments()) {
		final VariableDeclarationFragment frag = (VariableDeclarationFragment) fragment;
		addBinding(node, frag.getName().getIdentifier());
	}
	return true;
}
 
Example 4
Source File: JavaApproximateVariableBindingExtractor.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Looks for local variable declarations. For every declaration of a
 * variable, the parent {@link Block} denoting the variable's scope is
 * stored in {@link #variableScope} map.
 *
 * @param node
 *            the node to visit
 */
@Override
public boolean visit(final VariableDeclarationStatement node) {
	for (final Object fragment : node.fragments()) {
		final VariableDeclarationFragment frag = (VariableDeclarationFragment) fragment;
		addBinding(node, frag.getName().getIdentifier());
	}
	return true;
}
 
Example 5
Source File: VariableScopeExtractor.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean visit(final VariableDeclarationStatement node) {
	final ASTNode parent = node.getParent();
	for (final Object fragment : node.fragments()) {
		final VariableDeclarationFragment frag = (VariableDeclarationFragment) fragment;
		variableScopes.put(parent, new Variable(frag.getName()
				.getIdentifier(), node.getType().toString(),
				ScopeType.SCOPE_LOCAL));
	}
	return false;
}
 
Example 6
Source File: MethodVisitor.java    From repositoryminer with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public boolean visit(VariableDeclarationStatement node) {
	ITypeBinding bind = node.getType().resolveBinding();
	String type = null;
	if (bind != null)
		type = bind.getQualifiedName();
		
	for (VariableDeclarationFragment frag : (List<VariableDeclarationFragment>) node.fragments()) {
		statements.add(new AbstractVariableDeclaration(frag.getName().getIdentifier(), type,
				frag.getInitializer() != null ? frag.getInitializer().toString() : null));
	}
	return true;
}
 
Example 7
Source File: SurroundWith.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Split the fragments in <code>statement</code> to multiple VariableDeclarationStatements whenever
 * <code>splitOperator.needsSplit</code> returns <code>true</code>.
 * i.e.:
 * int i, j; ---> int i; int j; (if splitOperator.needsSplit(i, j) == true)
 *
 * @param statement The VariableDeclarationStatement to split
 * @param splitOperator The operator to use to split
 * @param rewrite The rewriter to use to generate new VariableDeclarationStatements.
 */
private void splitVariableDeclarationStatement(VariableDeclarationStatement statement, ISplitOperation splitOperator, ASTRewrite rewrite) {

	List<VariableDeclarationFragment> fragments= statement.fragments();
	Iterator<VariableDeclarationFragment> iter= fragments.iterator();
	VariableDeclarationFragment lastFragment= iter.next();
	VariableDeclarationStatement lastStatement= statement;

	splitOperator.initializeStatement(lastStatement, lastFragment);

	ListRewrite fragmentsRewrite= null;
	while (iter.hasNext()) {
		VariableDeclarationFragment currentFragment= iter.next();

		if (splitOperator.needsSplit(lastFragment, currentFragment)) {

			VariableDeclarationStatement newStatement= getAst().newVariableDeclarationStatement((VariableDeclarationFragment)rewrite.createMoveTarget(currentFragment));

			ListRewrite modifierRewrite= rewrite.getListRewrite(newStatement, VariableDeclarationStatement.MODIFIERS2_PROPERTY);
			for (Iterator<IExtendedModifier> iterator= statement.modifiers().iterator(); iterator.hasNext();) {
				modifierRewrite.insertLast(rewrite.createCopyTarget((ASTNode)iterator.next()), null);
			}

			newStatement.setType((Type)rewrite.createCopyTarget(statement.getType()));

			splitOperator.initializeStatement(newStatement, currentFragment);

			fragmentsRewrite= rewrite.getListRewrite(newStatement, VariableDeclarationStatement.FRAGMENTS_PROPERTY);

			lastStatement= newStatement;
		} else if (fragmentsRewrite != null) {
			ASTNode fragment0= rewrite.createMoveTarget(currentFragment);
			fragmentsRewrite.insertLast(fragment0, null);
		}
		lastFragment= currentFragment;
	}
}
 
Example 8
Source File: UnusedCodeFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void splitUpDeclarations(ASTRewrite rewrite, TextEditGroup group, VariableDeclarationFragment frag, VariableDeclarationStatement originalStatement, List<Expression> sideEffects) {
	if (sideEffects.size() > 0) {
		ListRewrite statementRewrite= rewrite.getListRewrite(originalStatement.getParent(), (ChildListPropertyDescriptor) originalStatement.getLocationInParent());
		
		Statement previousStatement= originalStatement;
		for (int i= 0; i < sideEffects.size(); i++) {
			Expression sideEffect= sideEffects.get(i);
			Expression movedInit= (Expression) rewrite.createMoveTarget(sideEffect);
			ExpressionStatement wrapped= rewrite.getAST().newExpressionStatement(movedInit);
			statementRewrite.insertAfter(wrapped, previousStatement, group);
			previousStatement= wrapped;
		}

		VariableDeclarationStatement newDeclaration= null;
		List<VariableDeclarationFragment> fragments= originalStatement.fragments();
		int fragIndex= fragments.indexOf(frag);
		ListIterator<VariableDeclarationFragment> fragmentIterator= fragments.listIterator(fragIndex+1);
		while (fragmentIterator.hasNext()) {
			VariableDeclarationFragment currentFragment= fragmentIterator.next();
			VariableDeclarationFragment movedFragment= (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment);
			if (newDeclaration == null) {
				newDeclaration= rewrite.getAST().newVariableDeclarationStatement(movedFragment);
				Type copiedType= (Type) rewrite.createCopyTarget(originalStatement.getType());
				newDeclaration.setType(copiedType);
			} else {
				newDeclaration.fragments().add(movedFragment);
			}
		}
		if (newDeclaration != null){
			statementRewrite.insertAfter(newDeclaration, previousStatement, group);
			if (originalStatement.fragments().size() == newDeclaration.fragments().size() + 1){
				rewrite.remove(originalStatement, group);
			}
		}
	}
}
 
Example 9
Source File: PromoteTempToFieldRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void addLocalDeclarationRemoval(ASTRewrite rewrite) {
VariableDeclarationStatement tempDeclarationStatement= getTempDeclarationStatement();
  	List<VariableDeclarationFragment> fragments= tempDeclarationStatement.fragments();

  	int fragmentIndex= fragments.indexOf(fTempDeclarationNode);
  	Assert.isTrue(fragmentIndex != -1);
      VariableDeclarationFragment fragment= fragments.get(fragmentIndex);
      rewrite.remove(fragment, null);
      if (fragments.size() == 1)
	rewrite.remove(tempDeclarationStatement, null);
  }
 
Example 10
Source File: VariableScopeExtractor.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean visit(final VariableDeclarationStatement node) {
	final ASTNode parent = node.getParent();
	for (final Object fragment : node.fragments()) {
		final VariableDeclarationFragment frag = (VariableDeclarationFragment) fragment;
		variableScopes.put(parent, new Variable(frag.getName()
				.getIdentifier(), node.getType().toString(),
				ScopeType.SCOPE_LOCAL));
	}
	return false;
}
 
Example 11
Source File: IdentifierInformationScanner.java    From naturalize with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean visit(final VariableDeclarationStatement node) {
	for (final Object fragment : node.fragments()) {
		final VariableDeclarationFragment vdf = (VariableDeclarationFragment) fragment;
		final IdentifierInformation vd = new IdentifierInformation(SHA,
				file, vdf.getName().getIdentifier(), node.getType()
						.toString(), getLineNumber(vdf),
				getAstParentString(vdf));
		identifiers.add(vd);
	}
	return super.visit(node);
}
 
Example 12
Source File: JavaApproximateTypeInferencer.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Looks for local variable declarations. For every declaration of a
 * variable, the parent {@link Block} denoting the variable's scope is
 * stored in {@link #variableScope} map.
 *
 * @param node
 *            the node to visit
 */
@Override
public boolean visit(final VariableDeclarationStatement node) {
	for (final Object fragment : node.fragments()) {
		final VariableDeclarationFragment frag = (VariableDeclarationFragment) fragment;
		addBinding(node, frag.getName().getIdentifier(), node.getType());
	}
	return true;
}
 
Example 13
Source File: JavaApproximateVariableBindingExtractor.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Looks for local variable declarations. For every declaration of a
 * variable, the parent {@link Block} denoting the variable's scope is
 * stored in {@link #variableScope} map.
 *
 * @param node
 *            the node to visit
 */
@Override
public boolean visit(final VariableDeclarationStatement node) {
	for (final Object fragment : node.fragments()) {
		final VariableDeclarationFragment frag = (VariableDeclarationFragment) fragment;
		addBinding(node, frag.getName().getIdentifier());
	}
	return true;
}
 
Example 14
Source File: VariableScopeExtractor.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean visit(final VariableDeclarationStatement node) {
	final ASTNode parent = node.getParent();
	for (final Object fragment : node.fragments()) {
		final VariableDeclarationFragment frag = (VariableDeclarationFragment) fragment;
		variableScopes.put(parent, new Variable(frag.getName()
				.getIdentifier(), node.getType().toString(),
				ScopeType.SCOPE_LOCAL));
	}
	return false;
}
 
Example 15
Source File: TypeResolver.java    From KodeBeagle with Apache License 2.0 5 votes vote down vote up
/**
 * Looks for local variable declarations. For every declaration of a
 * variable, the parent {@link Block} denoting the variable's scope is
 * stored in {variableScope} map.
 *
 * @param node
 *            the node to visit
 */
@Override
public boolean visit(final VariableDeclarationStatement node) {
	for (final Object fragment : node.fragments()) {
		final VariableDeclarationFragment frag = (VariableDeclarationFragment) fragment;
		addBinding(node, frag.getName(), node.getType());
	}
	return true;
}
 
Example 16
Source File: ExtractTempRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void replaceSelectedExpressionWithTempDeclaration() throws CoreException {
	ASTRewrite rewrite = fCURewrite.getASTRewrite();
	Expression selectedExpression = getSelectedExpression().getAssociatedExpression(); // whole expression selected

	Expression initializer = (Expression) rewrite.createMoveTarget(selectedExpression);
	VariableDeclarationStatement tempDeclaration = createTempDeclaration(initializer);
	ASTNode replacement;

	ASTNode parent = selectedExpression.getParent();
	boolean isParentLambda = parent instanceof LambdaExpression;
	AST ast = rewrite.getAST();
	if (isParentLambda) {
		Block blockBody = ast.newBlock();
		blockBody.statements().add(tempDeclaration);
		if (!Bindings.isVoidType(((LambdaExpression) parent).resolveMethodBinding().getReturnType())) {
			List<VariableDeclarationFragment> fragments = tempDeclaration.fragments();
			SimpleName varName = fragments.get(0).getName();
			ReturnStatement returnStatement = ast.newReturnStatement();
			returnStatement.setExpression(ast.newSimpleName(varName.getIdentifier()));
			blockBody.statements().add(returnStatement);
		}
		replacement = blockBody;
	} else if (ASTNodes.isControlStatementBody(parent.getLocationInParent())) {
		Block block = ast.newBlock();
		block.statements().add(tempDeclaration);
		replacement = block;
	} else {
		replacement = tempDeclaration;
	}
	ASTNode replacee = isParentLambda || !ASTNodes.hasSemicolon((ExpressionStatement) parent, fCu) ? selectedExpression : parent;
	rewrite.replace(replacee, replacement, fCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractTempRefactoring_declare_local_variable));
}
 
Example 17
Source File: TypeParseVisitor.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
public boolean visit(VariableDeclarationStatement 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;
}
 
Example 18
Source File: ASTNodeMatcher.java    From JDeodorant with MIT License 4 votes vote down vote up
public boolean match(ExpressionStatement node, Object other) {
	if (AbstractControlStructureUtilities.hasOneConditionalExpression(node) != null && other instanceof IfStatement)
	{
		TernaryControlStructure nodeTernaryControlStructure = new TernaryControlStructure(node);
		return ifMatch(nodeTernaryControlStructure, other);
	}
	else if(node.getExpression() instanceof Assignment && other instanceof VariableDeclarationStatement) {
		VariableDeclarationStatement variableDeclarationStatement = (VariableDeclarationStatement)other;
		List fragments = variableDeclarationStatement.fragments();
		if(fragments.size() == 1) {
			VariableDeclarationFragment fragment = (VariableDeclarationFragment)fragments.get(0);
			Assignment assignment = (Assignment)node.getExpression();
			Expression leftHandSide = assignment.getLeftHandSide();
			if(leftHandSide instanceof SimpleName) {
				SimpleName simpleName = (SimpleName)leftHandSide;
				boolean variableMatch = safeSubtreeMatch(simpleName, fragment.getName());
				boolean variableTypeMatch = false;
				IBinding simpleNameBinding = simpleName.resolveBinding();
				IBinding fragmentNameBinding = fragment.getName().resolveBinding();
				if(simpleNameBinding.getKind() == IBinding.VARIABLE && fragmentNameBinding.getKind() == IBinding.VARIABLE) {
					IVariableBinding simpleNameVariableBinding = (IVariableBinding)simpleNameBinding;
					IVariableBinding fragmentNameVariableBinding = (IVariableBinding)fragmentNameBinding;
					variableTypeMatch = simpleNameVariableBinding.getType().isEqualTo(fragmentNameVariableBinding.getType()) &&
							simpleNameVariableBinding.getType().getQualifiedName().equals(fragmentNameVariableBinding.getType().getQualifiedName());
				}
				boolean initializerMatch = false;
				boolean initializerTypeMatch = false;
				Expression initializer = fragment.getInitializer();
				Expression rightHandSide = assignment.getRightHandSide();
				if(initializer != null && initializer.getNodeType() == rightHandSide.getNodeType()) {
					initializerMatch = safeSubtreeMatch(rightHandSide, initializer);
					initializerTypeMatch = initializer.resolveTypeBinding().isEqualTo(rightHandSide.resolveTypeBinding()) &&
							initializer.resolveTypeBinding().getQualifiedName().equals(rightHandSide.resolveTypeBinding().getQualifiedName());
				}
				if(variableMatch && variableTypeMatch && initializerMatch && initializerTypeMatch) {
					VariableDeclaration variableDeclaration = AbstractLoopUtilities.getVariableDeclaration(simpleName);
					if(variableDeclaration != null && hasEmptyInitializer(variableDeclaration)) {
						safeSubtreeMatch(variableDeclaration.getName(), fragment.getName());
						List<ASTNode> astNodes = new ArrayList<ASTNode>();
						astNodes.add(variableDeclaration);
						ASTInformationGenerator.setCurrentITypeRoot(typeRoot1);
						reportAdditionalFragments(astNodes, this.additionallyMatchedFragments1);
						return true;
					}
				}
			}
		}
	}
	return super.match(node, other);
}
 
Example 19
Source File: ASTNodeMatcher.java    From JDeodorant with MIT License 4 votes vote down vote up
public boolean match(VariableDeclarationStatement node, Object other) {
	List fragments = node.fragments();
	if(fragments.size() == 1 && other instanceof ExpressionStatement) {
		VariableDeclarationFragment fragment = (VariableDeclarationFragment)fragments.get(0);
		ExpressionStatement expressionStatement = (ExpressionStatement)other;
		Expression expression = expressionStatement.getExpression();
		if(expression instanceof Assignment) {
			Assignment assignment = (Assignment)expression;
			Expression leftHandSide = assignment.getLeftHandSide();
			if(leftHandSide instanceof SimpleName) {
				SimpleName simpleName = (SimpleName)leftHandSide;
				boolean variableMatch = safeSubtreeMatch(fragment.getName(), simpleName);
				boolean variableTypeMatch = false;
				IBinding simpleNameBinding = simpleName.resolveBinding();
				IBinding fragmentNameBinding = fragment.getName().resolveBinding();
				if(simpleNameBinding.getKind() == IBinding.VARIABLE && fragmentNameBinding.getKind() == IBinding.VARIABLE) {
					IVariableBinding simpleNameVariableBinding = (IVariableBinding)simpleNameBinding;
					IVariableBinding fragmentNameVariableBinding = (IVariableBinding)fragmentNameBinding;
					variableTypeMatch = simpleNameVariableBinding.getType().isEqualTo(fragmentNameVariableBinding.getType()) &&
							simpleNameVariableBinding.getType().getQualifiedName().equals(fragmentNameVariableBinding.getType().getQualifiedName());;
				}
				boolean initializerMatch = false;
				boolean initializerTypeMatch = false;
				Expression initializer = fragment.getInitializer();
				Expression rightHandSide = assignment.getRightHandSide();
				if(initializer != null && initializer.getNodeType() == rightHandSide.getNodeType()) {
					initializerMatch = safeSubtreeMatch(initializer, rightHandSide);
					initializerTypeMatch = initializer.resolveTypeBinding().isEqualTo(rightHandSide.resolveTypeBinding()) &&
							initializer.resolveTypeBinding().getQualifiedName().equals(rightHandSide.resolveTypeBinding().getQualifiedName());
				}
				if(variableMatch && variableTypeMatch && initializerMatch && initializerTypeMatch) {
					VariableDeclaration variableDeclaration = AbstractLoopUtilities.getVariableDeclaration(simpleName);
					if(variableDeclaration != null && hasEmptyInitializer(variableDeclaration)) {
						safeSubtreeMatch(fragment.getName(), variableDeclaration.getName());
						List<ASTNode> astNodes = new ArrayList<ASTNode>();
						astNodes.add(variableDeclaration);
						ASTInformationGenerator.setCurrentITypeRoot(typeRoot2);
						reportAdditionalFragments(astNodes, this.additionallyMatchedFragments2);
						return true;
					}
				}
			}
		}
	}
	return super.match(node, other);
}