Java Code Examples for org.eclipse.jdt.core.dom.IVariableBinding#isParameter()

The following examples show how to use org.eclipse.jdt.core.dom.IVariableBinding#isParameter() . 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: IntroduceParameterObjectProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void updateSimpleName(ASTRewrite rewriter, ParameterInfo pi, SimpleName node, List<SingleVariableDeclaration> enclosingParameters, IJavaProject project) {
	AST ast= rewriter.getAST();
	IBinding binding= node.resolveBinding();
	Expression replacementNode= fParameterObjectFactory.createFieldReadAccess(pi, getParameterName(), ast, project, false, null);
	if (binding instanceof IVariableBinding) {
		IVariableBinding variable= (IVariableBinding) binding;
		if (variable.isParameter() && variable.getName().equals(getNameInScope(pi, enclosingParameters))) {
			rewriter.replace(node, replacementNode, null);
		}
	} else {
		ASTNode parent= node.getParent();
		if (!(parent instanceof QualifiedName || parent instanceof FieldAccess || parent instanceof SuperFieldAccess)) {
			if (node.getIdentifier().equals(getNameInScope(pi, enclosingParameters))) {
				rewriter.replace(node, replacementNode, null);
			}
		}
	}
}
 
Example 2
Source File: InJavaImporter.java    From jdt2famix with Eclipse Public License 1.0 5 votes vote down vote up
public StructuralEntity ensureStructuralEntityFromExpression(Expression expression) {
	if (expression instanceof SimpleName) {
		IBinding simpleNameBinding = ((SimpleName) expression).resolveBinding();
		if (simpleNameBinding instanceof IVariableBinding) {
			IVariableBinding binding = ((IVariableBinding) simpleNameBinding).getVariableDeclaration();
			if (binding.isField())
				return ensureAttributeForVariableBinding(binding);
			if (binding.isParameter())
				return ensureParameterWithinCurrentMethodFromVariableBinding(binding);
			if (binding.isEnumConstant())
				return ensureEnumValueFromVariableBinding(binding);
		}
	}
	return null;
}
 
Example 3
Source File: InJavaImporter.java    From jdt2famix with Eclipse Public License 1.0 5 votes vote down vote up
private Access createAccessFromVariableBinding(IVariableBinding binding, ASTNode node) {
	Access access = new Access();
	StructuralEntity variable = unknownVariable();
	if (binding != null) {
		/*
		 * It sometimes happen that the binding is null. Unfortunately, I was not able
		 * to isolate and reproduce the case, but we still need the guard condition.
		 */
		boolean isField = binding.isField();
		boolean isParameter = binding.isParameter();
		boolean isEnumConstant = binding.isEnumConstant();
		if (!isField && !isParameter && !isEnumConstant)
			// we only consider fields, parameters and enum constants
			return access;
		if (isField)
			variable = ensureAttributeForVariableBinding(binding);
		if (isParameter)
			variable = ensureParameterWithinCurrentMethodFromVariableBinding(binding);
		if (isEnumConstant)
			variable = ensureEnumValueFromVariableBinding(binding);
	}
	access.setVariable(variable);
	access.setIsWrite(false);
	if (topOfContainerStack() instanceof Method)
		access.setAccessor((Method) topOfContainerStack());
	if (topOfContainerStack() instanceof Type)
		/*
		 * This is ugly, but it happens when we have an access from within an annotation
		 * around a type or attribute like:
		 * 
		 * @Annotation(name="something" + AClass.DEFAULT)
		 */
		access.setAccessor(ensureInitializerMethod());
	createLightweightSourceAnchor(access, node);
	repository.add(access);
	return access;
}
 
Example 4
Source File: IntroduceParameterObjectProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isParameter(ParameterInfo pi, ASTNode node, List<SingleVariableDeclaration> enclosingMethodParameters, String qualifier) {
	if (node instanceof Name) {
		Name name= (Name) node;
		IVariableBinding binding= ASTNodes.getVariableBinding(name);
		if (binding != null && binding.isParameter()) {
			return binding.getName().equals(getNameInScope(pi, enclosingMethodParameters));
		} else {
			if (node instanceof QualifiedName) {
				QualifiedName qn= (QualifiedName) node;
				return qn.getFullyQualifiedName().equals(JavaModelUtil.concatenateName(qualifier, getNameInScope(pi, enclosingMethodParameters)));
			}
		}
	}
	return false;
}
 
Example 5
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the kind of the given binding.
 * 
 * @param binding variable binding
 * @return one of the <code>NamingConventions.VK_*</code> constants
 * @since 3.5
 */
private static int getKind(IVariableBinding binding) {
	if (binding.isField())
		return getFieldKind(binding.getModifiers());

	if (binding.isParameter())
		return NamingConventions.VK_PARAMETER;

	return NamingConventions.VK_LOCAL;
}
 
Example 6
Source File: VariableDeclarationFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean canAddFinal(IBinding binding, ASTNode declNode) {
	if (!(binding instanceof IVariableBinding))
		return false;

	IVariableBinding varbinding= (IVariableBinding)binding;
	int modifiers= varbinding.getModifiers();
	if (Modifier.isFinal(modifiers) || Modifier.isVolatile(modifiers) || Modifier.isTransient(modifiers))
		return false;

	ASTNode parent= ASTNodes.getParent(declNode, VariableDeclarationExpression.class);
	if (parent != null && ((VariableDeclarationExpression)parent).fragments().size() > 1)
		return false;

	if (varbinding.isField() && !Modifier.isPrivate(modifiers))
		return false;

	if (varbinding.isParameter()) {
		ASTNode varDecl= declNode.getParent();
		if (varDecl instanceof MethodDeclaration) {
			MethodDeclaration declaration= (MethodDeclaration)varDecl;
			if (declaration.getBody() == null)
				return false;
		}
	}

	return true;
}
 
Example 7
Source File: QuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean getInlineLocalProposal(IInvocationContext context, final ASTNode node, Collection<ICommandAccess> proposals) throws CoreException {
	if (!(node instanceof SimpleName))
		return false;

	SimpleName name= (SimpleName) node;
	IBinding binding= name.resolveBinding();
	if (!(binding instanceof IVariableBinding))
		return false;
	IVariableBinding varBinding= (IVariableBinding) binding;
	if (varBinding.isField() || varBinding.isParameter())
		return false;
	ASTNode decl= context.getASTRoot().findDeclaringNode(varBinding);
	if (!(decl instanceof VariableDeclarationFragment) || decl.getLocationInParent() != VariableDeclarationStatement.FRAGMENTS_PROPERTY)
		return false;

	if (proposals == null) {
		return true;
	}

	InlineTempRefactoring refactoring= new InlineTempRefactoring((VariableDeclaration) decl);
	if (refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
		String label= CorrectionMessages.QuickAssistProcessor_inline_local_description;
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
		RefactoringCorrectionProposal proposal= new RefactoringCorrectionProposal(label, context.getCompilationUnit(), refactoring, IProposalRelevance.INLINE_LOCAL, image);
		proposal.setCommandId(INLINE_LOCAL_ID);
		proposals.add(proposal);

	}
	return true;
}
 
Example 8
Source File: QuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean getConvertLocalToFieldProposal(IInvocationContext context, final ASTNode node, Collection<ICommandAccess> proposals) throws CoreException {
	if (!(node instanceof SimpleName))
		return false;

	SimpleName name= (SimpleName) node;
	IBinding binding= name.resolveBinding();
	if (!(binding instanceof IVariableBinding))
		return false;
	IVariableBinding varBinding= (IVariableBinding) binding;
	if (varBinding.isField() || varBinding.isParameter())
		return false;
	ASTNode decl= context.getASTRoot().findDeclaringNode(varBinding);
	if (decl == null || decl.getLocationInParent() != VariableDeclarationStatement.FRAGMENTS_PROPERTY)
		return false;

	if (proposals == null) {
		return true;
	}

	PromoteTempToFieldRefactoring refactoring= new PromoteTempToFieldRefactoring((VariableDeclaration) decl);
	if (refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
		String label= CorrectionMessages.QuickAssistProcessor_convert_local_to_field_description;
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
		LinkedProposalModel linkedProposalModel= new LinkedProposalModel();
		refactoring.setLinkedProposalModel(linkedProposalModel);

		RefactoringCorrectionProposal proposal= new RefactoringCorrectionProposal(label, context.getCompilationUnit(), refactoring, IProposalRelevance.CONVERT_LOCAL_TO_FIELD, image);
		proposal.setLinkedProposalModel(linkedProposalModel);
		proposal.setCommandId(CONVERT_LOCAL_TO_FIELD_ID);
		proposals.add(proposal);
	}
	return true;
}
 
Example 9
Source File: SrcTreeGenerator.java    From sahagin-java with Apache License 2.0 5 votes vote down vote up
private Code generateLocalVarAssignCode(Expression expression,
        Expression left, Expression right, TestMethod parentMethod) {
    Code rightCode = expressionCode(right, parentMethod);
    if (rightCode instanceof UnknownCode) {
        // ignore left for UnknownCode assignment
        return rightCode;
    }
    if (!(left instanceof SimpleName)) {
        return rightCode; // ignore left
    }
    SimpleName simpleName = (SimpleName) left;
    IBinding leftBinding = simpleName.resolveBinding();
    if (!(leftBinding instanceof IVariableBinding)) {
        return rightCode; // ignore left
    }
    IVariableBinding varBinding = (IVariableBinding) leftBinding;
    if (varBinding.isField() || varBinding.isParameter()) {
        // ignore left for field assignment and method argument assignment
        // TODO should handle field assignment as VarAssign for Field ??
        return rightCode;
    }

    String classKey = varBinding.getType().getBinaryName();
    TestClass subClass = subClassTable.getByKey(classKey);
    if (subClass != null && subClass instanceof PageClass) {
        // ignore left for page type variable assignment
        // since usually page type variable is not used in other TestDoc
        return rightCode;
    }

    LocalVar localVar = generateLocalVarCode(simpleName, varBinding);
    VarAssign assign = new VarAssign();
    assign.setOriginal(expression.toString().trim());
    assign.setVariable(localVar);
    assign.setValue(rightCode);
    return assign;
}
 
Example 10
Source File: AbstractVariable.java    From JDeodorant with MIT License 5 votes vote down vote up
public AbstractVariable(VariableDeclaration name) {
	IVariableBinding variableBinding = name.resolveBinding();
	this.variableBindingKey = variableBinding.getKey();
	this.variableName = variableBinding.getName();
	this.variableType = variableBinding.getType().getQualifiedName();
	this.isField = variableBinding.isField();
	this.isParameter = variableBinding.isParameter();
	this.isStatic = (variableBinding.getModifiers() & Modifier.STATIC) != 0;
}
 
Example 11
Source File: AbstractMethodFragment.java    From JDeodorant with MIT License 5 votes vote down vote up
protected void processConstructorInvocation(ConstructorInvocation constructorInvocation) {
	IMethodBinding methodBinding = constructorInvocation.resolveConstructorBinding();
	String originClassName = methodBinding.getDeclaringClass().getQualifiedName();
	TypeObject originClassTypeObject = TypeObject.extractTypeObject(originClassName);
	String methodInvocationName = methodBinding.getName();
	String qualifiedName = methodBinding.getReturnType().getQualifiedName();
	TypeObject returnType = TypeObject.extractTypeObject(qualifiedName);
	ConstructorInvocationObject constructorInvocationObject = new ConstructorInvocationObject(originClassTypeObject, methodInvocationName, returnType);
	constructorInvocationObject.setConstructorInvocation(constructorInvocation);
	ITypeBinding[] parameterTypes = methodBinding.getParameterTypes();
	for(ITypeBinding parameterType : parameterTypes) {
		String qualifiedParameterName = parameterType.getQualifiedName();
		TypeObject typeObject = TypeObject.extractTypeObject(qualifiedParameterName);
		constructorInvocationObject.addParameter(typeObject);
	}
	ITypeBinding[] thrownExceptionTypes = methodBinding.getExceptionTypes();
	for(ITypeBinding thrownExceptionType : thrownExceptionTypes) {
		constructorInvocationObject.addThrownException(thrownExceptionType.getQualifiedName());
	}
	if((methodBinding.getModifiers() & Modifier.STATIC) != 0)
		constructorInvocationObject.setStatic(true);
	addConstructorInvocation(constructorInvocationObject);
	List<Expression> arguments = constructorInvocation.arguments();
	for(Expression argument : arguments) {
		if(argument instanceof SimpleName) {
			SimpleName argumentName = (SimpleName)argument;
			IBinding binding = argumentName.resolveBinding();
			if(binding != null && binding.getKind() == IBinding.VARIABLE) {
				IVariableBinding variableBinding = (IVariableBinding)binding;
				if(variableBinding.isParameter()) {
					PlainVariable variable = new PlainVariable(variableBinding);
					addParameterPassedAsArgumentInConstructorInvocation(variable, constructorInvocationObject);
				}
			}
		}
	}
}
 
Example 12
Source File: RefactorProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static boolean getConvertVarTypeToResolvedTypeProposal(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> proposals) {
	CompilationUnit astRoot = context.getASTRoot();
	IJavaElement root = astRoot.getJavaElement();
	if (root == null) {
		return false;
	}
	IJavaProject javaProject = root.getJavaProject();
	if (javaProject == null) {
		return false;
	}
	if (!JavaModelUtil.is10OrHigher(javaProject)) {
		return false;
	}

	if (!(node instanceof SimpleName)) {
		return false;
	}
	SimpleName name = (SimpleName) node;
	IBinding binding = name.resolveBinding();
	if (!(binding instanceof IVariableBinding)) {
		return false;
	}
	IVariableBinding varBinding = (IVariableBinding) binding;
	if (varBinding.isField() || varBinding.isParameter()) {
		return false;
	}

	ASTNode varDeclaration = astRoot.findDeclaringNode(varBinding);
	if (varDeclaration == null) {
		return false;
	}

	ITypeBinding typeBinding = varBinding.getType();
	if (typeBinding == null || typeBinding.isAnonymous() || typeBinding.isIntersectionType() || typeBinding.isWildcardType()) {
		return false;
	}

	Type type = null;
	if (varDeclaration instanceof SingleVariableDeclaration) {
		type = ((SingleVariableDeclaration) varDeclaration).getType();
	} else if (varDeclaration instanceof VariableDeclarationFragment) {
		ASTNode parent = varDeclaration.getParent();
		if (parent instanceof VariableDeclarationStatement) {
			type = ((VariableDeclarationStatement) parent).getType();
		} else if (parent instanceof VariableDeclarationExpression) {
			type = ((VariableDeclarationExpression) parent).getType();
		}
	}
	if (type == null || !type.isVar()) {
		return false;
	}

	TypeChangeCorrectionProposal proposal = new TypeChangeCorrectionProposal(context.getCompilationUnit(), varBinding, astRoot, typeBinding, false, IProposalRelevance.CHANGE_VARIABLE);
	proposal.setKind(CodeActionKind.Refactor);
	proposals.add(proposal);
	return true;
}
 
Example 13
Source File: AbstractVariable.java    From JDeodorant with MIT License 4 votes vote down vote up
public AbstractVariable(IVariableBinding variableBinding) {
	this(variableBinding.getKey(), variableBinding.getName(), variableBinding.getType().getQualifiedName(),
			variableBinding.isField(), variableBinding.isParameter(), (variableBinding.getModifiers() & Modifier.STATIC) != 0);
}