Java Code Examples for org.eclipse.jdt.core.dom.IBinding#VARIABLE

The following examples show how to use org.eclipse.jdt.core.dom.IBinding#VARIABLE . 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: ASTResolving.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean isVariableDefinedInContext(IBinding binding, ITypeBinding typeVariable) {
	if (binding.getKind() == IBinding.VARIABLE) {
		IVariableBinding var= (IVariableBinding) binding;
		binding= var.getDeclaringMethod();
		if (binding == null) {
			binding= var.getDeclaringClass();
		}
	}
	if (binding instanceof IMethodBinding) {
		if (binding == typeVariable.getDeclaringMethod()) {
			return true;
		}
		binding= ((IMethodBinding) binding).getDeclaringClass();
	}

	while (binding instanceof ITypeBinding) {
		if (binding == typeVariable.getDeclaringClass()) {
			return true;
		}
		if (Modifier.isStatic(binding.getModifiers())) {
			break;
		}
		binding= ((ITypeBinding) binding).getDeclaringClass();
	}
	return false;
}
 
Example 2
Source File: ScopeAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Evaluates if the declaration is visible in a certain context.
 * @param binding The binding of the declaration to examine
 * @param context The context to test in
 * @return Returns
 */
public static boolean isVisible(IBinding binding, ITypeBinding context) {
	if (binding.getKind() == IBinding.VARIABLE && !((IVariableBinding) binding).isField()) {
		return true; // all local variables found are visible
	}
	ITypeBinding declaring= getDeclaringType(binding);
	if (declaring == null) {
		return false;
	}

	declaring= declaring.getTypeDeclaration();

	int modifiers= binding.getModifiers();
	if (Modifier.isPublic(modifiers) || declaring.isInterface()) {
		return true;
	} else if (Modifier.isProtected(modifiers) || !Modifier.isPrivate(modifiers)) {
		if (declaring.getPackage() == context.getPackage()) {
			return true;
		}
		return isTypeInScope(declaring, context, Modifier.isProtected(modifiers));
	}
	// private visibility
	return isTypeInScope(declaring, context, false);
}
 
Example 3
Source File: ScopeAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static String getSignature(IBinding binding) {
	if (binding != null) {
		switch (binding.getKind()) {
			case IBinding.METHOD:
				StringBuffer buf= new StringBuffer();
				buf.append('M');
				buf.append(binding.getName()).append('(');
				ITypeBinding[] parameters= ((IMethodBinding) binding).getParameterTypes();
				for (int i= 0; i < parameters.length; i++) {
					if (i > 0) {
						buf.append(',');
					}
					ITypeBinding paramType= parameters[i].getErasure();
					buf.append(paramType.getQualifiedName());
				}
				buf.append(')');
				return buf.toString();
			case IBinding.VARIABLE:
				return 'V' + binding.getName();
			case IBinding.TYPE:
				return 'T' + binding.getName();
		}
	}
	return null;
}
 
Example 4
Source File: SemanticHighlightings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean consumes(final SemanticToken token) {
	final SimpleName node= token.getNode();
	if (node.isDeclaration()) {
		return false;
	}

	final IBinding binding= token.getBinding();
	if (binding == null || binding.getKind() != IBinding.VARIABLE) {
		return false;
	}

	ITypeBinding currentType= Bindings.getBindingOfParentType(node);
	ITypeBinding declaringType= ((IVariableBinding) binding).getDeclaringClass();
	if (declaringType == null || currentType == declaringType)
		return false;

	return Bindings.isSuperType(declaringType, currentType);
}
 
Example 5
Source File: SemanticHighlightings.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean consumes(final SemanticToken token) {
	final SimpleName node = token.getNode();
	if (node.isDeclaration()) {
		return false;
	}

	final IBinding binding = token.getBinding();
	if (binding == null || binding.getKind() != IBinding.VARIABLE) {
		return false;
	}

	ITypeBinding currentType = Bindings.getBindingOfParentType(node);
	ITypeBinding declaringType = ((IVariableBinding) binding).getDeclaringClass();
	if (declaringType == null || currentType == declaringType) {
		return false;
	}

	return Bindings.isSuperType(declaringType, currentType);
}
 
Example 6
Source File: CodeStyleFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static AddThisQualifierOperation getUnqualifiedFieldAccessResolveOperation(CompilationUnit compilationUnit, IProblemLocation problem) {
	SimpleName name= getName(compilationUnit, problem);
	if (name == null)
		return null;

	IBinding binding= name.resolveBinding();
	if (binding == null || binding.getKind() != IBinding.VARIABLE)
		return null;

	ImportRewrite imports= StubUtility.createImportRewrite(compilationUnit, true);

	String replacement= getThisExpressionQualifier(((IVariableBinding) binding).getDeclaringClass(), imports, name);
	if (replacement == null)
		return null;

	if (replacement.length() == 0)
		replacement= null;

	return new AddThisQualifierOperation(replacement, name);
}
 
Example 7
Source File: JavadocHover.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static Object getVariableBindingConstValue(ASTNode node, IField field) {
	if (node != null && node.getNodeType() == ASTNode.SIMPLE_NAME) {
		IBinding binding= ((SimpleName) node).resolveBinding();
		if (binding != null && binding.getKind() == IBinding.VARIABLE) {
			IVariableBinding variableBinding= (IVariableBinding) binding;
			if (field.equals(variableBinding.getJavaElement())) {
				return variableBinding.getConstantValue();
			}
		}
	}
	return null;
}
 
Example 8
Source File: NullAnnotationsFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isComplainingAboutArgument(ASTNode selectedNode) {
	if (!(selectedNode instanceof SimpleName))
		return false;
	SimpleName nameNode= (SimpleName) selectedNode;
	IBinding binding= nameNode.resolveBinding();
	if (binding.getKind() == IBinding.VARIABLE && ((IVariableBinding) binding).isParameter())
		return true;
	VariableDeclaration argDecl= (VariableDeclaration) ASTNodes.getParent(selectedNode, VariableDeclaration.class);
	if (argDecl != null)
		binding= argDecl.resolveBinding();
	if (binding.getKind() == IBinding.VARIABLE && ((IVariableBinding) binding).isParameter())
		return true;
	return false;
}
 
Example 9
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Tests if the given node is a declaration, not a instance of a generic type, method or field.
 * Declarations can be found in AST with CompilationUnit.findDeclaringNode
 * @param binding binding to test
 * @return returns <code>true</code> if the binding is a declaration binding
 */
public static boolean isDeclarationBinding(IBinding binding) {
	switch (binding.getKind()) {
		case IBinding.TYPE:
			return ((ITypeBinding) binding).getTypeDeclaration() == binding;
		case IBinding.VARIABLE:
			return ((IVariableBinding) binding).getVariableDeclaration() == binding;
		case IBinding.METHOD:
			return ((IMethodBinding) binding).getMethodDeclaration() == binding;
	}
	return true;
}
 
Example 10
Source File: PromoteTempToFieldRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(SimpleName node) {
	ITypeBinding typeBinding= node.resolveTypeBinding();
	if (typeBinding != null && typeBinding.isLocal()) {
		if (node.isDeclaration()) {
			fLocalDefinitions.add(typeBinding);
		} else if (! fLocalDefinitions.contains(typeBinding)) {
			fLocalReferencesToEnclosing.add(node);
		}
	}
	if (typeBinding != null && typeBinding.isTypeVariable()) {
		if (node.isDeclaration()) {
			fLocalDefinitions.add(typeBinding);
		} else if (! fLocalDefinitions.contains(typeBinding)) {
			if (fMethodTypeVariables.contains(typeBinding)) {
				fLocalReferencesToEnclosing.add(node);
			} else {
				fClassTypeVariablesUsed= true;
			}
		}
	}
	IBinding binding= node.resolveBinding();
	if (binding != null && binding.getKind() == IBinding.VARIABLE && ! ((IVariableBinding)binding).isField()) {
		if (node.isDeclaration()) {
			fLocalDefinitions.add(binding);
		} else if (! fLocalDefinitions.contains(binding)) {
			fLocalReferencesToEnclosing.add(node);
		}
	}
	return super.visit(node);
}
 
Example 11
Source File: SemanticHighlightings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean consumes(SemanticToken token) {
	IBinding binding= token.getBinding();
	if (binding != null && binding.getKind() == IBinding.VARIABLE && !((IVariableBinding) binding).isField()) {
		ASTNode decl= token.getRoot().findDeclaringNode(binding);
		return decl != null && decl.getLocationInParent() == MethodDeclaration.PARAMETERS_PROPERTY;
	}
	return false;
}
 
Example 12
Source File: RefactoringUtility.java    From JDeodorant with MIT License 5 votes vote down vote up
private static boolean isEnumConstantInSwitchCaseExpression(SimpleName simpleName) {
	IBinding binding = simpleName.resolveBinding();
	if(binding != null && binding.getKind() == IBinding.VARIABLE) {
		IVariableBinding variableBinding = (IVariableBinding)binding;
		if(variableBinding.isField()) {
			return simpleName.getParent() instanceof SwitchCase && variableBinding.getDeclaringClass().isEnum();
		}
	}
	return false;
}
 
Example 13
Source File: JDTUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static Object getVariableBindingConstValue(ASTNode node, IField field) {
	if (node != null && node.getNodeType() == ASTNode.SIMPLE_NAME) {
		IBinding binding = ((SimpleName) node).resolveBinding();
		if (binding != null && binding.getKind() == IBinding.VARIABLE) {
			IVariableBinding variableBinding = (IVariableBinding) binding;
			if (field.equals(variableBinding.getJavaElement())) {
				return variableBinding.getConstantValue();
			}
		}
	}
	return null;
}
 
Example 14
Source File: SemanticHighlightings.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean consumes(SemanticToken token) {
	IBinding binding = token.getBinding();
	if (binding != null && binding.getKind() == IBinding.VARIABLE && !((IVariableBinding) binding).isField()) {
		ASTNode decl = token.getRoot().findDeclaringNode(binding);
		return decl != null && decl.getLocationInParent() == MethodDeclaration.PARAMETERS_PROPERTY;
	}
	return false;
}
 
Example 15
Source File: ASTNodeMatcher.java    From JDeodorant with MIT License 5 votes vote down vote up
private SimpleName setterMethodForField(MethodInvocation methodInvocation, SimpleName fieldName) {
	IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
	ITypeBinding declaringClassTypeBinding = methodBinding.getDeclaringClass();
	ClassObject declaringClass = ASTReader.getSystemObject().getClassObject(declaringClassTypeBinding.getQualifiedName());
	if(declaringClass != null) {
		ListIterator<MethodObject> methodIterator = declaringClass.getMethodIterator();
		while(methodIterator.hasNext()) {
			MethodObject method = methodIterator.next();
			MethodDeclaration methodDeclaration = method.getMethodDeclaration();
			if(methodDeclaration.resolveBinding().isEqualTo(methodInvocation.resolveMethodBinding())) {
				SimpleName setField = MethodDeclarationUtility.isSetter(methodDeclaration);
				if(setField != null) {
					if(setField.resolveBinding().getKind() == IBinding.VARIABLE &&
							fieldName.resolveBinding().getKind() == IBinding.VARIABLE) {
						IVariableBinding setFieldBinding = (IVariableBinding)setField.resolveBinding();
						IVariableBinding fieldNameBinding = (IVariableBinding)fieldName.resolveBinding();
						if(setFieldBinding.isEqualTo(fieldNameBinding) ||
								(setField.getIdentifier().equals(fieldName.getIdentifier()) &&
								setFieldBinding.getType().isEqualTo(fieldNameBinding.getType()) && setFieldBinding.getType().getQualifiedName().equals(fieldNameBinding.getType().getQualifiedName()))) {
							return setField;
						}
					}
				}
			}
		}
	}
	return null;
}
 
Example 16
Source File: SwitchControlCase.java    From JDeodorant with MIT License 5 votes vote down vote up
private String getConstantValue(Name name)
{
	IBinding nameBinding = name.resolveBinding();
	if (nameBinding.getKind() == IBinding.VARIABLE)
	{
		IVariableBinding nameVariableBinding = (IVariableBinding)nameBinding;
		Object valueObject = nameVariableBinding.getConstantValue();
		if (valueObject instanceof Integer || valueObject instanceof Byte || valueObject instanceof Character || valueObject instanceof String)
		{
			return valueObject.toString();
		}
	}
	return null;
}
 
Example 17
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 18
Source File: SemanticHighlightings.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean consumes(SemanticToken token) {
	IBinding binding = token.getBinding();
	return binding != null && binding.getKind() == IBinding.VARIABLE && ((IVariableBinding) binding).isField();
}
 
Example 19
Source File: SystemObject.java    From JDeodorant with MIT License 4 votes vote down vote up
private void inheritanceHierarchyMatchingWithStaticTypes(TypeCheckElimination typeCheckElimination,
		CompleteInheritanceDetection inheritanceDetection) {
	List<SimpleName> staticFields = typeCheckElimination.getStaticFields();
	String abstractClassType = typeCheckElimination.getAbstractClassType();
	InheritanceTree tree = null;
	if(abstractClassType != null)
		tree = inheritanceDetection.getTree(abstractClassType);
	if(tree != null) {
		DefaultMutableTreeNode rootNode = tree.getRootNode();
		DefaultMutableTreeNode leaf = rootNode.getFirstLeaf();
		List<String> inheritanceHierarchySubclassNames = new ArrayList<String>();
		while(leaf != null) {
			inheritanceHierarchySubclassNames.add((String)leaf.getUserObject());
			leaf = leaf.getNextLeaf();
		}
		int matchCounter = 0;
		for(SimpleName staticField : staticFields) {
			for(String subclassName : inheritanceHierarchySubclassNames) {
				ClassObject classObject = getClassObject(subclassName);
				AbstractTypeDeclaration abstractTypeDeclaration = classObject.getAbstractTypeDeclaration();
				if(abstractTypeDeclaration instanceof TypeDeclaration) {
					TypeDeclaration typeDeclaration = (TypeDeclaration)abstractTypeDeclaration;
					Javadoc javadoc = typeDeclaration.getJavadoc();
					if(javadoc != null) {
						List<TagElement> tagElements = javadoc.tags();
						for(TagElement tagElement : tagElements) {
							if(tagElement.getTagName() != null && tagElement.getTagName().equals(TagElement.TAG_SEE)) {
								List<ASTNode> fragments = tagElement.fragments();
								for(ASTNode fragment : fragments) {
									if(fragment instanceof MemberRef) {
										MemberRef memberRef = (MemberRef)fragment;
										IBinding staticFieldNameBinding = staticField.resolveBinding();
										ITypeBinding staticFieldNameDeclaringClass = null;
										if(staticFieldNameBinding != null && staticFieldNameBinding.getKind() == IBinding.VARIABLE) {
											IVariableBinding staticFieldNameVariableBinding = (IVariableBinding)staticFieldNameBinding;
											staticFieldNameDeclaringClass = staticFieldNameVariableBinding.getDeclaringClass();
										}
										if(staticFieldNameBinding.getName().equals(memberRef.getName().getIdentifier()) &&
												staticFieldNameDeclaringClass.getQualifiedName().equals(memberRef.getQualifier().getFullyQualifiedName())) {
											typeCheckElimination.putStaticFieldSubclassTypeMapping(staticField, subclassName);
											matchCounter++;
											break;
										}
									}
								}
							}
						}
					}
				}
			}
		}
		if(matchCounter == staticFields.size()) {
			typeCheckElimination.setInheritanceTreeMatchingWithStaticTypes(tree);
			return;
		}
	}
}
 
Example 20
Source File: SemanticTokensVisitor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean visit(SimpleName node) {
    ASTNode parent = node.getParent();
    if (parent instanceof Annotation) {
        addToken(node, TokenType.ANNOTATION);
        return false;
    }

    IBinding binding = node.resolveBinding();
    if (binding == null) {
        return super.visit(node);
    }

    TokenType tokenType = null;
    switch (binding.getKind()) {
        case IBinding.VARIABLE: {
            if (((IVariableBinding) binding).isField()) {
                tokenType = TokenType.PROPERTY;
            } else {
                tokenType = TokenType.VARIABLE;
            }
            break;
        }
        case IBinding.METHOD: {
            tokenType = TokenType.FUNCTION;
            break;
        }
        case IBinding.TYPE: {
            tokenType = TokenType.TYPE;
            break;
        }
        case IBinding.PACKAGE: {
            tokenType = TokenType.NAMESPACE;
            break;
        }
        default:
            break;
    }

    if (tokenType == null) {
        return super.visit(node);
    }

    switch (tokenType) {
        case FUNCTION:
        case VARIABLE:
        case PROPERTY:
        case MEMBER: {
            ITokenModifier[] modifiers = getModifiers(binding);
            addToken(node, tokenType, modifiers);
            break;
        }
        case TYPE:
        case NAMESPACE:
            addToken(node, tokenType, NO_MODIFIERS);
            break;
        default:
            break;
    }

    return super.visit(node);
}