Java Code Examples for org.eclipse.jdt.core.dom.SingleVariableDeclaration#getInitializer()

The following examples show how to use org.eclipse.jdt.core.dom.SingleVariableDeclaration#getInitializer() . 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: CodeBlock.java    From SimFix with GNU General Public License v2.0 6 votes vote down vote up
private Svd visit(SingleVariableDeclaration node){
	int startLine = _cunit.getLineNumber(node.getStartPosition());
	int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength());
	Svd svd = new Svd(startLine, endLine, node);
	
	svd.setDecType(node.getType());
	if(node.getInitializer() != null){
		Expr initializer = (Expr) process(node.getInitializer());
		initializer.setParent(svd);
		svd.setInitializer(initializer);
	}
	
	SName name = (SName) process(node.getName());
	name.setParent(svd);
	svd.setName(name);
	
	return svd;
}
 
Example 2
Source File: VariableDeclaration.java    From RefactoringMiner with MIT License 6 votes vote down vote up
public VariableDeclaration(CompilationUnit cu, String filePath, SingleVariableDeclaration fragment) {
	this.annotations = new ArrayList<UMLAnnotation>();
	List<IExtendedModifier> extendedModifiers = fragment.modifiers();
	for(IExtendedModifier extendedModifier : extendedModifiers) {
		if(extendedModifier.isAnnotation()) {
			Annotation annotation = (Annotation)extendedModifier;
			this.annotations.add(new UMLAnnotation(cu, filePath, annotation));
		}
	}
	this.locationInfo = new LocationInfo(cu, filePath, fragment, extractVariableDeclarationType(fragment));
	this.variableName = fragment.getName().getIdentifier();
	this.initializer = fragment.getInitializer() != null ? new AbstractExpression(cu, filePath, fragment.getInitializer(), CodeElementType.VARIABLE_DECLARATION_INITIALIZER) : null;
	Type astType = extractType(fragment);
	this.type = UMLType.extractTypeObject(cu, filePath, astType, fragment.getExtraDimensions());
	int startOffset = fragment.getStartPosition();
	ASTNode scopeNode = getScopeNode(fragment);
	int endOffset = scopeNode.getStartPosition() + scopeNode.getLength();
	this.scope = new VariableScope(cu, filePath, startOffset, endOffset);
}
 
Example 3
Source File: FullConstraintCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public ITypeConstraint[] create(SingleVariableDeclaration svd){
	ITypeConstraint[] defines= fTypeConstraintFactory.createDefinesConstraint(
			fConstraintVariableFactory.makeExpressionOrTypeVariable(svd.getName(), getContext()),
			fConstraintVariableFactory.makeTypeVariable(svd.getType()));
	if (svd.getInitializer() == null)
		return defines;
	ITypeConstraint[] constraints = fTypeConstraintFactory.createSubtypeConstraint(
			fConstraintVariableFactory.makeExpressionOrTypeVariable(svd.getInitializer(), getContext()),
			fConstraintVariableFactory.makeExpressionOrTypeVariable(svd.getName(), getContext()));
	if (defines.length == 0 && constraints.length == 0){
		return new ITypeConstraint[0];
	} else if (defines.length == 0){
		return constraints;
	} else if (constraints.length == 0){
		return defines;
	} else {
		List<ITypeConstraint> all= new ArrayList<ITypeConstraint>();
		all.addAll(Arrays.asList(defines));
		all.addAll(Arrays.asList(constraints));
		return (ITypeConstraint[])all.toArray();
	}
}
 
Example 4
Source File: FlowAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void endVisit(SingleVariableDeclaration node) {
	if (skipNode(node)) {
		return;
	}

	IVariableBinding binding = node.resolveBinding();
	LocalFlowInfo nameInfo = null;
	Expression initializer = node.getInitializer();
	if (binding != null && !binding.isField() && initializer != null) {
		nameInfo = new LocalFlowInfo(binding, FlowInfo.WRITE, fFlowContext);
	}
	GenericSequentialFlowInfo info = processSequential(node, node.getType(), initializer);
	info.merge(nameInfo, fFlowContext);
}
 
Example 5
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean visit(final SingleVariableDeclaration it) {
  if ((((it.getParent() instanceof MethodDeclaration) || (it.getParent() instanceof CatchClause)) || 
    (it.getParent() instanceof EnhancedForStatement))) {
    final Function1<IExtendedModifier, Boolean> _function = (IExtendedModifier it_1) -> {
      return Boolean.valueOf(it_1.isAnnotation());
    };
    this.appendModifiers(it, IterableExtensions.<IExtendedModifier>filter(Iterables.<IExtendedModifier>filter(it.modifiers(), IExtendedModifier.class), _function));
  } else {
    this.appendModifiers(it, it.modifiers());
  }
  it.getType().accept(this);
  this.appendExtraDimensions(it.getExtraDimensions());
  boolean _isVarargs = it.isVarargs();
  if (_isVarargs) {
    this.appendToBuffer("...");
  }
  this.appendSpaceToBuffer();
  it.getName().accept(this);
  Expression _initializer = it.getInitializer();
  boolean _tripleNotEquals = (_initializer != null);
  if (_tripleNotEquals) {
    this.appendToBuffer("=");
    it.getInitializer().accept(this);
  }
  return false;
}
 
Example 6
Source File: SuperTypeConstraintsCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public final void endVisit(final SingleVariableDeclaration node) {
	final ConstraintVariable2 ancestor= (ConstraintVariable2) node.getType().getProperty(PROPERTY_CONSTRAINT_VARIABLE);
	if (ancestor != null) {
		node.setProperty(PROPERTY_CONSTRAINT_VARIABLE, ancestor);
		final Expression expression= node.getInitializer();
		if (expression != null) {
			final ConstraintVariable2 descendant= (ConstraintVariable2) expression.getProperty(PROPERTY_CONSTRAINT_VARIABLE);
			if (descendant != null)
				fModel.createSubtypeConstraint(descendant, ancestor);
		}
	}
}
 
Example 7
Source File: InferTypeArgumentsConstraintCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
	public void endVisit(SingleVariableDeclaration node) {
		// used for formal method parameters and catch clauses
		//TODO: extra dimensions?

//		ConstraintVariable2 typeCv= getConstraintVariable(node.getType()); //TODO: who needs this?

//		ConstraintVariable2 nameCv;
//		switch (node.getParent().getNodeType()) {
//			case ASTNode.METHOD_DECLARATION :
//				MethodDeclaration parent= (MethodDeclaration) node.getParent();
//				int index= parent.parameters().indexOf(node);
//				nameCv= fTCFactory.makeParameterTypeVariable(parent.resolveBinding(), index, node.getType());
//				//store source range even if variable not used in constraint here. TODO: move to visit(MethodDeclaration)?
//				break;
//			case ASTNode.CATCH_CLAUSE :
//				nameCv= fTCFactory.makeVariableVariable(node.resolveBinding());
//
//				break;
//			default:
//				unexpectedNode(node.getParent());
//		}
//		setConstraintVariable(node, nameCv);

		//TODO: Move this into visit(SimpleName) or leave it here?
//		ExpressionVariable2 name= fTCFactory.makeExpressionVariable(node.getName());
//		TypeVariable2 type= fTCFactory.makeTypeVariable(node.getType());
//		ITypeConstraint2[] nameEqualsType= fTCFactory.createEqualsConstraint(name, type);
//		addConstraints(nameEqualsType);

		//TODO: When can a SingleVariableDeclaration have an initializer? Never up to Java 1.5?
		Expression initializer= node.getInitializer();
		if (initializer == null)
			return;

//		ConstraintVariable2 initializerCv= getConstraintVariable(initializer);
//		ConstraintVariable2 nameCv= getConstraintVariable(node);
		//TODO: check: property has been set in visit(CatchClause), visit(MethodDeclaration), visit(EnhancedForStatament)
		//fTCFactory.createSubtypeConstraint(initializerCv, nameCv); //TODO: not for augment raw container clients
	}
 
Example 8
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(SingleVariableDeclaration node) {
	if (skipNode(node))
		return;

	IVariableBinding binding= node.resolveBinding();
	LocalFlowInfo nameInfo= null;
	Expression initializer= node.getInitializer();
	if (binding != null && !binding.isField() && initializer != null) {
		nameInfo= new LocalFlowInfo(binding, FlowInfo.WRITE, fFlowContext);
	}
	GenericSequentialFlowInfo info= processSequential(node, node.getType(), initializer);
	info.merge(nameInfo, fFlowContext);
}
 
Example 9
Source File: UnusedCodeFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean hasSideEffect(SimpleName reference) {
	ASTNode parent= reference.getParent();
	while (parent instanceof QualifiedName) {
		parent= parent.getParent();
	}
	if (parent instanceof FieldAccess) {
		parent= parent.getParent();
	}

	ASTNode node= null;
	int nameParentType= parent.getNodeType();
	if (nameParentType == ASTNode.ASSIGNMENT) {
		Assignment assignment= (Assignment) parent;
		node= assignment.getRightHandSide();
	} else if (nameParentType == ASTNode.SINGLE_VARIABLE_DECLARATION) {
		SingleVariableDeclaration decl= (SingleVariableDeclaration)parent;
		node= decl.getInitializer();
		if (node == null)
			return false;
	} else if (nameParentType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
		node= parent;
	} else {
		return false;
	}

	ArrayList<Expression> sideEffects= new ArrayList<Expression>();
	node.accept(new SideEffectFinder(sideEffects));
	return sideEffects.size() > 0;
}