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

The following examples show how to use org.eclipse.jdt.core.dom.VariableDeclarationFragment. 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 Project: RefactoringMiner   Author: tsantalis   File: VariableDeclaration.java    License: 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 #2
Source Project: apidiff   Author: aserg-ufmg   File: FieldDiff.java    License: MIT License 6 votes vote down vote up
/**
 * @param fieldInVersion1
 * @param fieldInVersion2
 * @return True, if there is a difference between the fields.
 */
private Boolean thereAreDifferentDefaultValueField(FieldDeclaration fieldInVersion1, FieldDeclaration fieldInVersion2){
	
	List<VariableDeclarationFragment> variable1Fragments = fieldInVersion1.fragments();
	List<VariableDeclarationFragment> variable2Fragments = fieldInVersion2.fragments();
	
	Expression valueVersion1 = variable1Fragments.get(0).getInitializer();
	Expression valueVersion2 = variable2Fragments.get(0).getInitializer();
	
	//If default value was removed/changed
	if((valueVersion1 == null && valueVersion2 != null) || (valueVersion1 != null && valueVersion2 == null)){
		return true;
	}
	
	//If fields have default value and they are different
	if((valueVersion1 != null && valueVersion2 != null) && (!valueVersion1.toString().equals(valueVersion2.toString()))){
		return true;
	}
	
	return false;
}
 
Example #3
Source Project: JDeodorant   Author: tsantalis   File: StyledStringVisitor.java    License: MIT License 6 votes vote down vote up
public boolean visit(VariableDeclarationExpression expr) {
	/*
	  VariableDeclarationExpression:
   		{ ExtendedModifier } Type VariableDeclarationFragment
        	{ , VariableDeclarationFragment }
	 */
	activateDiffStyle(expr);
	// Append modifiers
	for (int i = 0; i < expr.modifiers().size(); i++) {
		handleModifier((IExtendedModifier) expr.modifiers().get(i));
		appendSpace();
	}
	// Append Type
	handleType(expr.getType());
	appendSpace();
	// Visit Fragments
	for (int i = 0; i < expr.fragments().size(); i++) {
		visit((VariableDeclarationFragment) expr.fragments().get(i));
		if(i < expr.fragments().size() - 1) {
			appendComma();
		}
	}
	// No semicolon needed as this is an expression
	deactivateDiffStyle(expr);
	return false;
}
 
Example #4
Source Project: JDeodorant   Author: tsantalis   File: MoveMethodRefactoring.java    License: MIT License 6 votes vote down vote up
private void addParameterToMovedMethod(MethodDeclaration newMethodDeclaration, SimpleName fieldName, ASTRewrite targetRewriter) {
	AST ast = newMethodDeclaration.getAST();
	SingleVariableDeclaration parameter = ast.newSingleVariableDeclaration();
	Type fieldType = null;
	FieldDeclaration[] fields = sourceTypeDeclaration.getFields();
	for(FieldDeclaration field : fields) {
		List<VariableDeclarationFragment> fragments = field.fragments();
		for(VariableDeclarationFragment fragment : fragments) {
			if(fragment.getName().getIdentifier().equals(fieldName.getIdentifier())) {
				fieldType = field.getType();
				break;
			}
		}
	}
	targetRewriter.set(parameter, SingleVariableDeclaration.TYPE_PROPERTY, fieldType, null);
	targetRewriter.set(parameter, SingleVariableDeclaration.NAME_PROPERTY, ast.newSimpleName(fieldName.getIdentifier()), null);
	ListRewrite parametersRewrite = targetRewriter.getListRewrite(newMethodDeclaration, MethodDeclaration.PARAMETERS_PROPERTY);
	parametersRewrite.insertLast(parameter, null);
	this.additionalArgumentsAddedToMovedMethod.add(fieldName.getIdentifier());
	this.additionalTypeBindingsToBeImportedInTargetClass.add(fieldType.resolveBinding());
	addParamTagElementToJavadoc(newMethodDeclaration, targetRewriter, fieldName.getIdentifier());
}
 
Example #5
Source Project: JDeodorant   Author: tsantalis   File: StyledStringVisitor.java    License: MIT License 6 votes vote down vote up
public boolean visit(VariableDeclarationFragment expr) {
	/*
	 * Identifier { [] } [ = Expression ]
	 */
	activateDiffStyle(expr);
	handleExpression(expr.getName());
	for (int i = 0; i < expr.getExtraDimensions(); i++) {
		appendOpenBracket();
		appendClosedBracket();
	}
	if (expr.getInitializer() != null) {
		appendEquals();
		handleExpression(expr.getInitializer());
	}
	deactivateDiffStyle(expr);
	return false;
}
 
Example #6
Source Project: JDeodorant   Author: tsantalis   File: RefactoringUtility.java    License: MIT License 6 votes vote down vote up
public static VariableDeclaration findFieldDeclaration(AbstractVariable variable, TypeDeclaration typeDeclaration) {
	for(FieldDeclaration fieldDeclaration : typeDeclaration.getFields()) {
		List<VariableDeclarationFragment> fragments = fieldDeclaration.fragments();
		for(VariableDeclarationFragment fragment : fragments) {
			if(variable.getVariableBindingKey().equals(fragment.resolveBinding().getKey())) {
				return fragment;
			}
		}
	}
	//fragment was not found in typeDeclaration
	Type superclassType = typeDeclaration.getSuperclassType();
	if(superclassType != null) {
		String superclassQualifiedName = superclassType.resolveBinding().getQualifiedName();
		SystemObject system = ASTReader.getSystemObject();
		ClassObject superclassObject = system.getClassObject(superclassQualifiedName);
		if(superclassObject != null) {
			AbstractTypeDeclaration superclassTypeDeclaration = superclassObject.getAbstractTypeDeclaration();
			if(superclassTypeDeclaration instanceof TypeDeclaration) {
				return findFieldDeclaration(variable, (TypeDeclaration)superclassTypeDeclaration);
			}
		}
	}
	return null;
}
 
Example #7
/**
 * Returns the fields which reference the specified ast node.
 *
 * @param node
 *            the ast node
 * @param project
 *            the java project
 * @return the referencing fields
 * @throws JavaModelException
 *             if an error occurs
 */
protected final List<IField> getReferencingFields(final ASTNode node, final IJavaProject project) throws JavaModelException {
	List<IField> result= Collections.emptyList();
	if (node instanceof Type) {
		final BodyDeclaration parent= (BodyDeclaration) ASTNodes.getParent(node, BodyDeclaration.class);
		if (parent instanceof FieldDeclaration) {
			final List<VariableDeclarationFragment> fragments= ((FieldDeclaration) parent).fragments();
			result= new ArrayList<IField>(fragments.size());
			VariableDeclarationFragment fragment= null;
			for (final Iterator<VariableDeclarationFragment> iterator= fragments.iterator(); iterator.hasNext();) {
				fragment= iterator.next();
				final IField field= getCorrespondingField(fragment);
				if (field != null)
					result.add(field);
			}
		}
	}
	return result;
}
 
Example #8
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: InOutFlowAnalyzer.java    License: 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 #9
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: VariableDeclarationFix.java    License: 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 #10
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: VariableDeclarationFix.java    License: Eclipse Public License 1.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean visit(SimpleName node) {
	if (node.getParent() instanceof VariableDeclarationFragment)
		return super.visit(node);
	if (node.getParent() instanceof SingleVariableDeclaration)
		return super.visit(node);

	IBinding binding= node.resolveBinding();
	if (!(binding instanceof IVariableBinding))
		return super.visit(node);

	binding= ((IVariableBinding)binding).getVariableDeclaration();
	if (ASTResolving.isWriteAccess(node)) {
		List<SimpleName> list;
		if (fResult.containsKey(binding)) {
			list= fResult.get(binding);
		} else {
			list= new ArrayList<SimpleName>();
		}
		list.add(node);
		fResult.put(binding, list);
	}

	return super.visit(node);
}
 
Example #11
Source Project: eclipse.jdt.ls   Author: eclipse   File: AnonymousTypeCompletionProposal.java    License: Eclipse Public License 2.0 6 votes vote down vote up
private IBinding getEnclosingDeclaration(ASTNode node) {
	while (node != null) {
		if (node instanceof AbstractTypeDeclaration) {
			return ((AbstractTypeDeclaration) node).resolveBinding();
		} else if (node instanceof AnonymousClassDeclaration) {
			return ((AnonymousClassDeclaration) node).resolveBinding();
		} else if (node instanceof MethodDeclaration) {
			return ((MethodDeclaration) node).resolveBinding();
		} else if (node instanceof FieldDeclaration) {
			List<?> fragments = ((FieldDeclaration) node).fragments();
			if (fragments.size() > 0) {
				return ((VariableDeclarationFragment) fragments.get(0)).resolveBinding();
			}
		} else if (node instanceof VariableDeclarationFragment) {
			IVariableBinding variableBinding = ((VariableDeclarationFragment) node).resolveBinding();
			if (variableBinding.getDeclaringMethod() != null || variableBinding.getDeclaringClass() != null) {
				return variableBinding;
				// workaround for incomplete wiring of DOM bindings: keep searching when variableBinding is unparented
			}
		}
		node = node.getParent();
	}
	return null;
}
 
Example #12
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: ParameterObjectFactory.java    License: Eclipse Public License 1.0 6 votes vote down vote up
private FieldDeclaration createField(ParameterInfo pi, CompilationUnitRewrite cuRewrite) throws CoreException {
	AST ast= cuRewrite.getAST();
	ICompilationUnit unit= cuRewrite.getCu();

	VariableDeclarationFragment fragment= ast.newVariableDeclarationFragment();
	String lineDelim= StubUtility.getLineDelimiterUsed(unit);
	SimpleName fieldName= ast.newSimpleName(pi.getNewName());
	fragment.setName(fieldName);
	FieldDeclaration declaration= ast.newFieldDeclaration(fragment);
	if (createComments(unit.getJavaProject())) {
		String comment= StubUtility.getFieldComment(unit, pi.getNewTypeName(), pi.getNewName(), lineDelim);
		if (comment != null) {
			Javadoc doc= (Javadoc) cuRewrite.getASTRewrite().createStringPlaceholder(comment, ASTNode.JAVADOC);
			declaration.setJavadoc(doc);
		}
	}
	List<Modifier> modifiers= new ArrayList<Modifier>();
	if (fCreateGetter) {
		modifiers.add(ast.newModifier(ModifierKeyword.PRIVATE_KEYWORD));
	} else {
		modifiers.add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
	}
	declaration.modifiers().addAll(modifiers);
	declaration.setType(importBinding(pi.getNewTypeBinding(), cuRewrite));
	return declaration;
}
 
Example #13
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: ASTNodeFactory.java    License: Eclipse Public License 1.0 6 votes vote down vote up
private static Type newType(LambdaExpression lambdaExpression, VariableDeclarationFragment declaration, AST ast, ImportRewrite importRewrite, ImportRewriteContext context) {
	IMethodBinding method= lambdaExpression.resolveMethodBinding();
	if (method != null) {
		ITypeBinding[] parameterTypes= method.getParameterTypes();
		int index= lambdaExpression.parameters().indexOf(declaration);
		ITypeBinding typeBinding= parameterTypes[index];
		if (importRewrite != null) {
			return importRewrite.addImport(typeBinding, ast, context);
		} else {
			String qualifiedName= typeBinding.getQualifiedName();
			if (qualifiedName.length() > 0) {
				return newType(ast, qualifiedName);
			}
		}
	}
	// fall-back
	return ast.newSimpleType(ast.newSimpleName("Object")); //$NON-NLS-1$
}
 
Example #14
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: HierarchyProcessor.java    License: Eclipse Public License 1.0 6 votes vote down vote up
protected static FieldDeclaration createNewFieldDeclarationNode(final ASTRewrite rewrite, final CompilationUnit unit, final IField field, final VariableDeclarationFragment oldFieldFragment, final TypeVariableMaplet[] mapping, final IProgressMonitor monitor, final RefactoringStatus status, final int modifiers) throws JavaModelException {
	final VariableDeclarationFragment newFragment= rewrite.getAST().newVariableDeclarationFragment();
	copyExtraDimensions(oldFieldFragment, newFragment);
	if (oldFieldFragment.getInitializer() != null) {
		Expression newInitializer= null;
		if (mapping.length > 0)
			newInitializer= createPlaceholderForExpression(oldFieldFragment.getInitializer(), field.getCompilationUnit(), mapping, rewrite);
		else
			newInitializer= createPlaceholderForExpression(oldFieldFragment.getInitializer(), field.getCompilationUnit(), rewrite);
		newFragment.setInitializer(newInitializer);
	}
	newFragment.setName(((SimpleName) ASTNode.copySubtree(rewrite.getAST(), oldFieldFragment.getName())));
	final FieldDeclaration newField= rewrite.getAST().newFieldDeclaration(newFragment);
	final FieldDeclaration oldField= ASTNodeSearchUtil.getFieldDeclarationNode(field, unit);
	copyJavadocNode(rewrite, oldField, newField);
	copyAnnotations(oldField, newField);
	newField.modifiers().addAll(ASTNodeFactory.newModifiers(rewrite.getAST(), modifiers));
	final Type oldType= oldField.getType();
	Type newType= null;
	if (mapping.length > 0) {
		newType= createPlaceholderForType(oldType, field.getCompilationUnit(), mapping, rewrite);
	} else
		newType= createPlaceholderForType(oldType, field.getCompilationUnit(), rewrite);
	newField.setType(newType);
	return newField;
}
 
Example #15
Source Project: eclipse.jdt.ls   Author: eclipse   File: ExtractFieldRefactoring.java    License: Eclipse Public License 2.0 6 votes vote down vote up
private String[] getExcludedFieldNames() {
	if (fExcludedFieldNames == null) {
		List<String> result = new ArrayList<>();
		try {
			final ASTNode type = getEnclosingTypeDeclaration();
			if (type instanceof TypeDeclaration) {
				FieldDeclaration[] fields = ((TypeDeclaration) type).getFields();
				for (int i = 0; i < fields.length; i++) {
					for (Iterator<VariableDeclarationFragment> iter = fields[i].fragments().iterator(); iter.hasNext();) {
						VariableDeclarationFragment field = iter.next();
						result.add(field.getName().getIdentifier());
					}
				}
			}
		} catch (JavaModelException e) {
			// do nothing.
		}

		fExcludedFieldNames = result.toArray(new String[result.size()]);
	}

	return fExcludedFieldNames;
}
 
Example #16
Source Project: eclipse.jdt.ls   Author: eclipse   File: ExtractConstantRefactoring.java    License: Eclipse Public License 2.0 6 votes vote down vote up
private static boolean canReplace(IASTFragment fragment) {
	ASTNode node = fragment.getAssociatedNode();
	ASTNode parent = node.getParent();
	if (parent instanceof VariableDeclarationFragment) {
		VariableDeclarationFragment vdf = (VariableDeclarationFragment) parent;
		if (node.equals(vdf.getName())) {
			return false;
		}
	}
	if (parent instanceof ExpressionStatement) {
		return false;
	}
	if (parent instanceof SwitchCase) {
		if (node instanceof Name) {
			Name name = (Name) node;
			ITypeBinding typeBinding = name.resolveTypeBinding();
			if (typeBinding != null) {
				return !typeBinding.isEnum();
			}
		}
	}
	return true;
}
 
Example #17
Source Project: eclipse.jdt.ls   Author: eclipse   File: MoveHandler.java    License: Eclipse Public License 2.0 6 votes vote down vote up
private static RefactorWorkspaceEdit moveStaticMember(CodeActionParams params, String destinationTypeName, IProgressMonitor monitor) {
	final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri());
	if (unit == null) {
		return new RefactorWorkspaceEdit("Failed to move static member because cannot find the compilation unit associated with " + params.getTextDocument().getUri());
	}

	BodyDeclaration bodyDeclaration = getSelectedMemberDeclaration(unit, params);
	List<IJavaElement> elements = new ArrayList<>();
	if (bodyDeclaration instanceof MethodDeclaration) {
		elements.add(((MethodDeclaration) bodyDeclaration).resolveBinding().getJavaElement());
	} else if (bodyDeclaration instanceof FieldDeclaration) {
		for (Object fragment : ((FieldDeclaration) bodyDeclaration).fragments()) {
			elements.add(((VariableDeclarationFragment) fragment).resolveBinding().getJavaElement());
		}
	} else if (bodyDeclaration instanceof AbstractTypeDeclaration) {
		elements.add(((AbstractTypeDeclaration) bodyDeclaration).resolveBinding().getJavaElement());
	}

	IMember[] members = elements.stream().filter(element -> element instanceof IMember).map(element -> (IMember) element).toArray(IMember[]::new);
	return moveStaticMember(members, destinationTypeName, monitor);
}
 
Example #18
private static boolean locationNeedsParentheses(StructuralPropertyDescriptor locationInParent) {
	if (locationInParent instanceof ChildListPropertyDescriptor && locationInParent != InfixExpression.EXTENDED_OPERANDS_PROPERTY) {
		// e.g. argument lists of MethodInvocation, ClassInstanceCreation, dimensions of ArrayCreation ...
		return false;
	}
	if (locationInParent == VariableDeclarationFragment.INITIALIZER_PROPERTY
			|| locationInParent == SingleVariableDeclaration.INITIALIZER_PROPERTY
			|| locationInParent == ReturnStatement.EXPRESSION_PROPERTY
			|| locationInParent == EnhancedForStatement.EXPRESSION_PROPERTY
			|| locationInParent == ForStatement.EXPRESSION_PROPERTY
			|| locationInParent == WhileStatement.EXPRESSION_PROPERTY
			|| locationInParent == DoStatement.EXPRESSION_PROPERTY
			|| locationInParent == AssertStatement.EXPRESSION_PROPERTY
			|| locationInParent == AssertStatement.MESSAGE_PROPERTY
			|| locationInParent == IfStatement.EXPRESSION_PROPERTY
			|| locationInParent == SwitchStatement.EXPRESSION_PROPERTY
			|| locationInParent == SwitchCase.EXPRESSION_PROPERTY
			|| locationInParent == ArrayAccess.INDEX_PROPERTY
			|| locationInParent == ThrowStatement.EXPRESSION_PROPERTY
			|| locationInParent == SynchronizedStatement.EXPRESSION_PROPERTY
			|| locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
		return false;
	}
	return true;
}
 
Example #19
private RefactoringStatus checkClashesWithExistingFields(){
     FieldDeclaration[] existingFields= getFieldDeclarations();
     for (int i= 0; i < existingFields.length; i++) {
         FieldDeclaration declaration= existingFields[i];
VariableDeclarationFragment[] fragments= (VariableDeclarationFragment[]) declaration.fragments().toArray(new VariableDeclarationFragment[declaration.fragments().size()]);
for (int j= 0; j < fragments.length; j++) {
             VariableDeclarationFragment fragment= fragments[j];
             if (fFieldName.equals(fragment.getName().getIdentifier())){
             	//cannot conflict with more than 1 name
             	RefactoringStatusContext context= JavaStatusContext.create(fCu, fragment);
             	return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_Name_conflict_with_field, context);
             }
         }
     }
     return null;
 }
 
Example #20
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: AccessorClassModifier.java    License: Eclipse Public License 1.0 5 votes vote down vote up
private FieldDeclaration getNewFinalStringFieldDeclaration(String name) {
	VariableDeclarationFragment variableDeclarationFragment= fAst.newVariableDeclarationFragment();
	variableDeclarationFragment.setName(fAst.newSimpleName(name));

	FieldDeclaration fieldDeclaration= fAst.newFieldDeclaration(variableDeclarationFragment);
	fieldDeclaration.setType(fAst.newSimpleType(fAst.newSimpleName("String"))); //$NON-NLS-1$
	fieldDeclaration.modifiers().add(fAst.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
	fieldDeclaration.modifiers().add(fAst.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));

	return fieldDeclaration;
}
 
Example #21
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: CallInliner.java    License: Eclipse Public License 1.0 5 votes vote down vote up
private VariableDeclarationStatement createLocalDeclaration(ITypeBinding type, String name, Expression initializer) {
	ImportRewriteContext context= new ContextSensitiveImportRewriteContext(fTargetNode, fImportRewrite);
	String typeName= fImportRewrite.addImport(type, context);
	VariableDeclarationStatement decl= (VariableDeclarationStatement)ASTNodeFactory.newStatement(
		fInvocation.getAST(), typeName + " " + name + ";"); //$NON-NLS-1$ //$NON-NLS-2$
	((VariableDeclarationFragment)decl.fragments().get(0)).setInitializer(initializer);
	return decl;
}
 
Example #22
Source Project: SimFix   Author: xgdsmileboy   File: NodeUtils.java    License: GNU General Public License v2.0 5 votes vote down vote up
public boolean visit(FieldDeclaration node) {
	Type type = node.getType();
	for(Object object: node.fragments()){
		VariableDeclarationFragment vdf = (VariableDeclarationFragment) object;
		_vars.put(vdf.getName().toString(), type);
	}
	return true;
}
 
Example #23
Source Project: JDeodorant   Author: tsantalis   File: BindingSignatureVisitor.java    License: MIT License 5 votes vote down vote up
public boolean visit(VariableDeclarationExpression expr) {
	List modifiers = expr.modifiers();
	for (int i = 0; i < modifiers.size(); i++) {
		visit((Modifier) modifiers.get(i));
	}
	// Append Type
	handleType(expr.getType());
	// Visit Fragments
	List fragments = expr.fragments();
	for (int i = 0; i < fragments.size(); i++) {
		visit((VariableDeclarationFragment) fragments.get(i));
	}
	return false;
}
 
Example #24
Source Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: FlowAnalyzer.java    License: Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void endVisit(VariableDeclarationFragment 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, initializer);
	info.merge(nameInfo, fFlowContext);
}
 
Example #25
Source Project: DesigniteJava   Author: tushartushar   File: LocalVarVisitor.java    License: Apache License 2.0 5 votes vote down vote up
public boolean visit(VariableDeclarationStatement variable){
		for (Iterator iter = variable.fragments().iterator(); iter.hasNext();) {
			VariableDeclarationFragment fragment = (VariableDeclarationFragment) iter.next();
//			IVariableBinding binding = fragment.resolveBinding();
			
			SM_LocalVar newLocalVar = new SM_LocalVar(variable, fragment, parentMethod);
			//newLocalVar.setType(variable.getType());
			localVariables.add(newLocalVar);
		}
		return super.visit(variable);
	}
 
Example #26
Source Project: jdt2famix   Author: feenkcom   File: AstVisitor.java    License: Eclipse Public License 1.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public boolean visit(FieldDeclaration node) {
	if (node.fragments().stream().anyMatch(f -> ((VariableDeclarationFragment) f).getInitializer() != null))
		importer.pushOnContainerStack(importer.ensureInitializerMethod());
	node.fragments().stream().forEach(f -> visitFragment((VariableDeclarationFragment) f, node));
	return true;
}
 
Example #27
Source Project: sahagin-java   Author: SahaginOrg   File: SrcTreeGenerator.java    License: Apache License 2.0 5 votes vote down vote up
private IVariableBinding getVariableBinding(FieldDeclaration declaration) {
    for (Object fragment : declaration.fragments()) {
        if (fragment instanceof VariableDeclarationFragment) {
            VariableDeclarationFragment varDecl = (VariableDeclarationFragment) fragment;
            IVariableBinding binding = varDecl.resolveBinding();
            if (binding != null) {
                return binding;
            }
        }
    }
    return null;
}
 
Example #28
Source Project: JDeodorant   Author: tsantalis   File: BindingSignatureVisitor.java    License: MIT License 5 votes vote down vote up
public boolean visit(VariableDeclarationFragment expr) {
	handleExpression(expr.getName());
	if (expr.getInitializer() != null) {
		handleExpression(expr.getInitializer());
	}
	return false;
}
 
Example #29
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 #30
Source Project: eclipse.jdt.ls   Author: eclipse   File: SemanticHighlightings.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean consumes(SemanticToken token) {
	SimpleName node = token.getNode();
	StructuralPropertyDescriptor location = node.getLocationInParent();
	if (location == VariableDeclarationFragment.NAME_PROPERTY || location == SingleVariableDeclaration.NAME_PROPERTY) {
		ASTNode parent = node.getParent();
		if (parent instanceof VariableDeclaration) {
			parent = parent.getParent();
			return parent == null || !(parent instanceof FieldDeclaration);
		}
	}
	return false;
}