Java Code Examples for org.eclipse.jdt.core.dom.FieldAccess#getName()

The following examples show how to use org.eclipse.jdt.core.dom.FieldAccess#getName() . 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: MethodDeclarationUtility.java    From JDeodorant with MIT License 6 votes vote down vote up
public static SimpleName isGetter(MethodDeclaration methodDeclaration) {
	Block methodBody = methodDeclaration.getBody();
	List<SingleVariableDeclaration> parameters = methodDeclaration.parameters();
	if(methodBody != null) {
		List<Statement> statements = methodBody.statements();
		if(statements.size() == 1 && parameters.size() == 0) {
			Statement statement = statements.get(0);
    		if(statement instanceof ReturnStatement) {
    			ReturnStatement returnStatement = (ReturnStatement)statement;
    			Expression returnStatementExpression = returnStatement.getExpression();
    			if(returnStatementExpression instanceof SimpleName) {
    				return (SimpleName)returnStatementExpression;
    			}
    			else if(returnStatementExpression instanceof FieldAccess) {
    				FieldAccess fieldAccess = (FieldAccess)returnStatementExpression;
    				return fieldAccess.getName();
    			}
    		}
		}
	}
	return null;
}
 
Example 2
Source File: FullConstraintCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public ITypeConstraint[] create(FieldAccess access){
	Expression expression= access.getExpression();
	SimpleName name= access.getName();
	IBinding binding= name.resolveBinding();
	if (! (binding instanceof IVariableBinding))
		return new ITypeConstraint[0];
	IVariableBinding vb= (IVariableBinding)binding;
	return createConstraintsForAccessToField(vb, expression, access);
}
 
Example 3
Source File: MethodDeclarationUtility.java    From JDeodorant with MIT License 5 votes vote down vote up
public static SimpleName isSetter(MethodDeclaration methodDeclaration) {
	Block methodBody = methodDeclaration.getBody();
	List<SingleVariableDeclaration> parameters = methodDeclaration.parameters();
	if(methodBody != null) {
		List<Statement> statements = methodBody.statements();
		if(statements.size() == 1 && parameters.size() == 1) {
			Statement statement = statements.get(0);
    		if(statement instanceof ExpressionStatement) {
    			ExpressionStatement expressionStatement = (ExpressionStatement)statement;
    			Expression expressionStatementExpression = expressionStatement.getExpression();
    			if(expressionStatementExpression instanceof Assignment) {
    				Assignment assignment = (Assignment)expressionStatementExpression;
    				Expression rightHandSide = assignment.getRightHandSide();
    				if(rightHandSide instanceof SimpleName) {
    					SimpleName rightHandSideSimpleName = (SimpleName)rightHandSide;
    					if(rightHandSideSimpleName.resolveBinding().isEqualTo(parameters.get(0).resolveBinding())) {
    						Expression leftHandSide = assignment.getLeftHandSide();
    						if(leftHandSide instanceof SimpleName) {
    		    				return (SimpleName)leftHandSide;
    		    			}
    		    			else if(leftHandSide instanceof FieldAccess) {
    		    				FieldAccess fieldAccess = (FieldAccess)leftHandSide;
    		    				return fieldAccess.getName();
    		    			}
    					}
    				}
    			}
    		}
		}
	}
	return null;
}
 
Example 4
Source File: AbstractLoopUtilities.java    From JDeodorant with MIT License 5 votes vote down vote up
public static boolean isLengthFieldAccess(Expression expression)
{
	SimpleName name          = null;
	ITypeBinding typeBinding = null;
	if (expression instanceof QualifiedName)
	{
		QualifiedName qualifiedName = (QualifiedName) expression;
		name                        = qualifiedName.getName();
		Name qualifier              = qualifiedName.getQualifier();
		typeBinding                 = qualifier.resolveTypeBinding();
	}
	else if (expression instanceof FieldAccess)
	{
		FieldAccess fieldAccess           = (FieldAccess)expression;
		name                              = fieldAccess.getName();
		Expression fieldAsccessExpression = fieldAccess.getExpression();
		typeBinding                       = fieldAsccessExpression.resolveTypeBinding();
	}
	if (name != null && typeBinding != null)
	{
		IBinding nameBinding = name.resolveBinding();
		if (nameBinding != null && nameBinding.getKind() == IBinding.VARIABLE && typeBinding != null)
		{
			IVariableBinding nameVariableBinding = (IVariableBinding) nameBinding;
			return (nameVariableBinding.getName().equals("length") && typeBinding.isArray());
		}
	}
	return false;
}
 
Example 5
Source File: TypeCheckCodeFragmentAnalyzer.java    From JDeodorant with MIT License 5 votes vote down vote up
private Expression extractOperand(Expression operand) {
	if(operand instanceof SimpleName) {
		SimpleName operandSimpleName = (SimpleName)operand;
		return operandSimpleName;
	}
	else if(operand instanceof QualifiedName) {
		QualifiedName operandQualifiedName = (QualifiedName)operand;
		return operandQualifiedName.getName();
	}
	else if(operand instanceof FieldAccess) {
		FieldAccess operandFieldAccess = (FieldAccess)operand;
		return operandFieldAccess.getName();
	}
	else if(operand instanceof MethodInvocation) {
		MethodInvocation methodInvocation = (MethodInvocation)operand;
		for(MethodDeclaration method : methods) {
			SimpleName fieldInstruction = MethodDeclarationUtility.isGetter(method);
			if(fieldInstruction != null && method.resolveBinding().isEqualTo(methodInvocation.resolveMethodBinding())) {
				return fieldInstruction;
			}
			MethodInvocation delegateMethodInvocation = MethodDeclarationUtility.isDelegate(method);
			if(delegateMethodInvocation != null && method.resolveBinding().isEqualTo(methodInvocation.resolveMethodBinding())) {
				return delegateMethodInvocation;
			}
		}
		return methodInvocation;
	}
	return null;
}
 
Example 6
Source File: CodeStyleFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean visit(final FieldAccess node) {
	if (!fRemoveFieldQualifiers)
		return true;

	Expression expression= node.getExpression();
	if (!(expression instanceof ThisExpression))
		return true;

	final SimpleName name= node.getName();
	if (hasConflict(expression.getStartPosition(), name, ScopeAnalyzer.VARIABLES | ScopeAnalyzer.CHECK_VISIBILITY))
		return true;

	Name qualifier= ((ThisExpression) expression).getQualifier();
	if (qualifier != null) {
		ITypeBinding outerClass= (ITypeBinding) qualifier.resolveBinding();
		if (outerClass == null)
			return true;

		IVariableBinding nameBinding= (IVariableBinding) name.resolveBinding();
		if (nameBinding == null)
			return true;

		ITypeBinding variablesDeclaringClass= nameBinding.getDeclaringClass();
		if (outerClass != variablesDeclaringClass)
			//be conservative: We have a reference to a field of an outer type, and this type inherited
			//the field. It's possible that the inner type inherits the same field. We must not remove
			//the qualifier in this case.
			return true;
		
		ITypeBinding enclosingTypeBinding= Bindings.getBindingOfParentType(node);
		if (enclosingTypeBinding == null || Bindings.isSuperType(variablesDeclaringClass, enclosingTypeBinding))
			//We have a reference to a field of an outer type, and this type inherited
			//the field. The inner type inherits the same field. We must not remove
			//the qualifier in this case.
			return true;
	}

	fOperations.add(new CompilationUnitRewriteOperation() {
		@Override
		public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException {
			ASTRewrite rewrite= cuRewrite.getASTRewrite();
			TextEditGroup group= createTextEditGroup(FixMessages.CodeStyleFix_removeThis_groupDescription, cuRewrite);
			rewrite.replace(node, rewrite.createCopyTarget(name), group);
		}
	});
	return super.visit(node);
}