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

The following examples show how to use org.eclipse.jdt.core.dom.IVariableBinding#isField() . 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: MoveInstanceMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Is the specified name a field access?
 *
 * @param name
 *            the name to check
 * @return <code>true</code> if this name is a field access,
 *         <code>false</code> otherwise
 */
protected static boolean isFieldAccess(final SimpleName name) {
	Assert.isNotNull(name);
	final IBinding binding= name.resolveBinding();
	if (!(binding instanceof IVariableBinding))
		return false;
	final IVariableBinding variable= (IVariableBinding) binding;
	if (!variable.isField())
		return false;
	if ("length".equals(name.getIdentifier())) { //$NON-NLS-1$
		final ASTNode parent= name.getParent();
		if (parent instanceof QualifiedName) {
			final QualifiedName qualified= (QualifiedName) parent;
			final ITypeBinding type= qualified.getQualifier().resolveTypeBinding();
			if (type != null && type.isArray())
				return false;
		}
	}
	return !Modifier.isStatic(variable.getModifiers());
}
 
Example 2
Source File: FlowAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void endVisit(SimpleName node) {
	if (skipNode(node) || node.isDeclaration()) {
		return;
	}
	IBinding binding = node.resolveBinding();
	if (binding instanceof IVariableBinding) {
		IVariableBinding variable = (IVariableBinding) binding;
		if (!variable.isField()) {
			setFlowInfo(node, new LocalFlowInfo(variable, FlowInfo.READ, fFlowContext));
		}
	} else if (binding instanceof ITypeBinding) {
		ITypeBinding type = (ITypeBinding) binding;
		if (type.isTypeVariable()) {
			setFlowInfo(node, new TypeVariableFlowInfo(type, fFlowContext));
		}
	}
}
 
Example 3
Source File: ExtractClassRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Expression handleSimpleNameAssignment(ASTNode replaceNode, ParameterObjectFactory pof, String parameterName, AST ast, IJavaProject javaProject, boolean useSuper) {
	if (replaceNode instanceof Assignment) {
		Assignment assignment= (Assignment) replaceNode;
		Expression rightHandSide= assignment.getRightHandSide();
		if (rightHandSide.getNodeType() == ASTNode.SIMPLE_NAME) {
			SimpleName sn= (SimpleName) rightHandSide;
			IVariableBinding binding= ASTNodes.getVariableBinding(sn);
			if (binding != null && binding.isField()) {
				if (fDescriptor.getType().getFullyQualifiedName().equals(binding.getDeclaringClass().getQualifiedName())) {
					FieldInfo fieldInfo= getFieldInfo(binding.getName());
					if (fieldInfo != null && binding == fieldInfo.pi.getOldBinding()) {
						return pof.createFieldReadAccess(fieldInfo.pi, parameterName, ast, javaProject, useSuper, null);
					}
				}
			}
		}
	}
	return null;
}
 
Example 4
Source File: GetterSetterCorrectionSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean addGetterSetterProposal(IInvocationContext context, ASTNode coveringNode, Collection<ICommandAccess> proposals, int relevance) {
	if (!(coveringNode instanceof SimpleName)) {
		return false;
	}
	SimpleName sn= (SimpleName) coveringNode;

	IBinding binding= sn.resolveBinding();
	if (!(binding instanceof IVariableBinding))
		return false;
	IVariableBinding variableBinding= (IVariableBinding) binding;
	if (!variableBinding.isField())
		return false;

	if (proposals == null)
		return true;

	ChangeCorrectionProposal proposal= getProposal(context.getCompilationUnit(), sn, variableBinding, relevance);
	if (proposal != null)
		proposals.add(proposal);
	return true;
}
 
Example 5
Source File: ConvertAnonymousToNestedRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isBindingToTemp(IVariableBinding variable) {
	if (variable.isField())
		return false;
	if (!Modifier.isFinal(variable.getModifiers()))
		return false;
	ASTNode declaringNode= fCompilationUnitNode.findDeclaringNode(variable);
	if (declaringNode == null)
		return false;
	if (ASTNodes.isParent(declaringNode, fAnonymousInnerClassNode))
		return false;
	return true;
}
 
Example 6
Source File: MovedMemberAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isTargetAccess(IBinding binding) {
	if (binding instanceof IMethodBinding) {
		IMethodBinding method= (IMethodBinding)binding;
		return Modifier.isStatic(method.getModifiers()) && Bindings.equals(fTarget, method.getDeclaringClass());
	} else if (binding instanceof ITypeBinding) {
		ITypeBinding type= (ITypeBinding)binding;
		return Modifier.isStatic(type.getModifiers()) && Bindings.equals(fTarget, type.getDeclaringClass());
	} else if (binding instanceof IVariableBinding) {
		IVariableBinding field= (IVariableBinding)binding;
		return field.isField() && Modifier.isStatic(field.getModifiers()) && Bindings.equals(fTarget, field.getDeclaringClass());
	}
	return false;
}
 
Example 7
Source File: ASTNodes.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static IVariableBinding getFieldBinding(Name node) {
	IVariableBinding result= getVariableBinding(node);
	if (result == null || !result.isField())
		return null;

	return result;
}
 
Example 8
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 9
Source File: MoveInnerToTopRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(SimpleName node) {
	IBinding binding= node.resolveBinding();
	if (binding instanceof IVariableBinding) {
		IVariableBinding variableBinding= (IVariableBinding) binding;
		if (variableBinding.isField()) {
			if (isAccessToOuter(variableBinding.getDeclaringClass())) {
				fSimpleNames.add(node);
			}
		}
	}
	return false;
}
 
Example 10
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 11
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 12
Source File: SourceAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(SimpleName node) {
	addReferencesToName(node);
	IBinding binding= node.resolveBinding();
	if (binding instanceof ITypeBinding) {
		ITypeBinding type= (ITypeBinding)binding;
		if (type.isTypeVariable()) {
			addTypeVariableReference(type, node);
		}
	} else if (binding instanceof IVariableBinding) {
		IVariableBinding vb= (IVariableBinding)binding;
		if (vb.isField() && ! isStaticallyImported(node)) {
			Name topName= ASTNodes.getTopMostName(node);
			if (node == topName || node == ASTNodes.getLeftMostSimpleName(topName)) {
				StructuralPropertyDescriptor location= node.getLocationInParent();
				if (location != SingleVariableDeclaration.NAME_PROPERTY
					&& location != VariableDeclarationFragment.NAME_PROPERTY) {
					fImplicitReceivers.add(node);
				}
			}
		} else if (!vb.isField()) {
			// we have a local. Check if it is a parameter.
			ParameterData data= fParameters.get(binding);
			if (data != null) {
				ASTNode parent= node.getParent();
				if (parent instanceof Expression) {
					int precedence= OperatorPrecedence.getExpressionPrecedence((Expression)parent);
					if (precedence != Integer.MAX_VALUE) {
						data.setOperatorPrecedence(precedence);
					}
				}
			}
		}
	}
	return true;
}
 
Example 13
Source File: JavaEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
boolean markOccurrencesOfType(IBinding binding) {

		if (binding == null)
			return false;

		int kind= binding.getKind();

		if (fMarkTypeOccurrences && kind == IBinding.TYPE)
			return true;

		if (fMarkMethodOccurrences && kind == IBinding.METHOD)
			return true;

		if (kind == IBinding.VARIABLE) {
			IVariableBinding variableBinding= (IVariableBinding)binding;
			if (variableBinding.isField()) {
				int constantModifier= IModifierConstants.ACC_STATIC | IModifierConstants.ACC_FINAL;
				boolean isConstant= (variableBinding.getModifiers() & constantModifier) == constantModifier;
				if (isConstant)
					return fMarkConstantOccurrences;
				else
					return fMarkFieldOccurrences;
			}

			return fMarkLocalVariableypeOccurrences;
		}

		return false;
	}
 
Example 14
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(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 15
Source File: CompilationUnitBuilder.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private Expression convert(org.eclipse.jdt.core.dom.SimpleName expression) {
  IBinding binding = expression.resolveBinding();
  if (binding instanceof IVariableBinding) {
    IVariableBinding variableBinding = (IVariableBinding) binding;
    if (variableBinding.isField()) {
      // It refers to a field.
      FieldDescriptor fieldDescriptor = JdtUtils.createFieldDescriptor(variableBinding);
      if (!fieldDescriptor.isStatic()
          && !fieldDescriptor.isMemberOf(getCurrentType().getTypeDescriptor())) {
        return FieldAccess.Builder.from(fieldDescriptor)
            .setQualifier(
                resolveImplicitOuterClassReference(
                    fieldDescriptor.getEnclosingTypeDescriptor()))
            .build();
      } else {
        return FieldAccess.Builder.from(fieldDescriptor).build();
      }
    } else {
      // It refers to a local variable or parameter in a method or block.
      Variable variable = checkNotNull(variableByJdtBinding.get(variableBinding));
      return resolveVariableReference(variable);
    }
  }

  if (binding instanceof ITypeBinding) {
    return null;
  }

  throw internalCompilerError(
      "Unexpected binding class for SimpleName: %s", expression.getClass().getName());
}
 
Example 16
Source File: SuperTypeRefactoringProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the field which corresponds to the specified variable declaration
 * fragment
 *
 * @param fragment
 *            the variable declaration fragment
 * @return the corresponding field
 * @throws JavaModelException
 *             if an error occurs
 */
protected final IField getCorrespondingField(final VariableDeclarationFragment fragment) throws JavaModelException {
	final IBinding binding= fragment.getName().resolveBinding();
	if (binding instanceof IVariableBinding) {
		final IVariableBinding variable= (IVariableBinding) binding;
		if (variable.isField()) {
			final ICompilationUnit unit= RefactoringASTParser.getCompilationUnit(fragment);
			final IJavaElement element= unit.getElementAt(fragment.getStartPosition());
			if (element instanceof IField)
				return (IField) element;
		}
	}
	return null;
}
 
Example 17
Source File: InOutFlowAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void clearAccessMode(FlowInfo info, SingleVariableDeclaration decl) {
	IVariableBinding binding= decl.resolveBinding();
	if (binding != null && !binding.isField())
		info.clearAccessMode(binding, fFlowContext);
}
 
Example 18
Source File: InOutFlowAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void clearAccessMode(FlowInfo info, VariableDeclarationFragment fragment) {
		IVariableBinding binding= fragment.resolveBinding();
		if (binding != null && !binding.isField())
			info.clearAccessMode(binding, fFlowContext);
}
 
Example 19
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);
}
 
Example 20
Source File: InOutFlowAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private void clearAccessMode(FlowInfo info, SingleVariableDeclaration decl) {
	IVariableBinding binding = decl.resolveBinding();
	if (binding != null && !binding.isField()) {
		info.clearAccessMode(binding, fFlowContext);
	}
}