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

The following examples show how to use org.eclipse.jdt.core.dom.FieldAccess#getExpression() . 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: MoveStaticMemberAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected void rewrite(FieldAccess node, ITypeBinding type) {
	Expression exp= node.getExpression();
	if (exp == null) {
		ImportRewriteContext context= new ContextSensitiveImportRewriteContext(node, fCuRewrite.getImportRewrite());
		Type result= fCuRewrite.getImportRewrite().addImport(type, fCuRewrite.getAST(), context);
		fCuRewrite.getImportRemover().registerAddedImport(type.getQualifiedName());
		exp= ASTNodeFactory.newName(fCuRewrite.getAST(), ASTFlattener.asString(result));
		fCuRewrite.getASTRewrite().set(node, FieldAccess.EXPRESSION_PROPERTY, exp,  fCuRewrite.createGroupDescription(REFERENCE_UPDATE));
		fNeedsImport= true;
	} else if (exp instanceof Name) {
		rewriteName((Name)exp, type);
	} else {
		rewriteExpression(node, exp, type);
	}
	fProcessed.add(node.getName());
}
 
Example 2
Source File: MovedMemberAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean visit(FieldAccess node) {
	IBinding binding= node.resolveFieldBinding();
	if (isSourceAccess(binding)) {
		if (isMovedMember(binding)) {
			if (node.getExpression() != null)
				rewrite(node, fTarget);
		} else
			rewrite(node, fSource);

	} else if (isTargetAccess(binding)) {
		fCuRewrite.getASTRewrite().remove(node.getExpression(), null);
		fCuRewrite.getImportRemover().registerRemovedNode(node.getExpression());
	}
	return super.visit(node);
}
 
Example 3
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 target access?
 *
 * @param name
 *            the name to check
 * @return <code>true</code> if this name is a target access,
 *         <code>false</code> otherwise
 */
protected boolean isTargetAccess(final Name name) {
	Assert.isNotNull(name);
	final IBinding binding= name.resolveBinding();
	if (Bindings.equals(fTarget, binding))
		return true;
	if (name.getParent() instanceof FieldAccess) {
		final FieldAccess access= (FieldAccess) name.getParent();
		final Expression expression= access.getExpression();
		if (expression instanceof Name)
			return isTargetAccess((Name) expression);
	} else if (name instanceof QualifiedName) {
		final QualifiedName qualified= (QualifiedName) name;
		if (qualified.getQualifier() != null)
			return isTargetAccess(qualified.getQualifier());
	}
	return false;
}
 
Example 4
Source File: MethodObject.java    From JDeodorant with MIT License 6 votes vote down vote up
public boolean containsFieldAccessOfEnclosingClass() {
	//check for field access like SegmentedTimeline.this.segmentsIncluded
	List<FieldInstructionObject> fieldInstructions = getFieldInstructions();
	for(FieldInstructionObject fieldInstruction : fieldInstructions) {
		SimpleName simpleName = fieldInstruction.getSimpleName();
		if(simpleName.getParent() instanceof FieldAccess) {
			FieldAccess fieldAccess = (FieldAccess)simpleName.getParent();
			Expression fieldAccessExpression = fieldAccess.getExpression();
			if(fieldAccessExpression instanceof ThisExpression) {
				ThisExpression thisExpression = (ThisExpression)fieldAccessExpression;
				if(thisExpression.getQualifier() != null) {
					return true;
				}
			}
		}
	}
	return false;
}
 
Example 5
Source File: DirectAceessFieldVisitor.java    From DesigniteJava with Apache License 2.0 5 votes vote down vote up
public boolean visit(FieldAccess node) {
	if (node.getExpression() instanceof ThisExpression) {
		if (!this.thisAccesses.contains(node)) {
			thisAccesses.add(node);
		}
	}
	return true;
}
 
Example 6
Source File: ExtractMethodRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean visit(FieldAccess node) {
	Expression exp = node.getExpression();
	if (exp != null) {
		fIgnore.add(node.getName());
	}
	return true;
}
 
Example 7
Source File: MoveInstanceMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public final boolean visit(final FieldAccess node) {
	Assert.isNotNull(node);
	final Expression expression= node.getExpression();
	final IVariableBinding variable= node.resolveFieldBinding();
	final AST ast= fRewrite.getAST();
	if (expression instanceof ThisExpression) {
		if (Bindings.equals(fTarget, variable)) {
			if (fAnonymousClass > 0) {
				final ThisExpression target= ast.newThisExpression();
				target.setQualifier(ast.newSimpleName(fTargetType.getElementName()));
				fRewrite.replace(node, target, null);
			} else
				fRewrite.replace(node, ast.newThisExpression(), null);
			return false;
		} else {
			expression.accept(this);
			return false;
		}
	} else if (expression instanceof FieldAccess) {
		final FieldAccess access= (FieldAccess) expression;
		final IBinding binding= access.getName().resolveBinding();
		if (access.getExpression() instanceof ThisExpression && Bindings.equals(fTarget, binding)) {
			ASTNode newFieldAccess= getFieldReference(node.getName(), fRewrite);
			fRewrite.replace(node, newFieldAccess, null);
			return false;
		}
	} else if (expression != null) {
		expression.accept(this);
		return false;
	}
	return true;
}
 
Example 8
Source File: MoveInstanceMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public final boolean visit(final FieldAccess node) {
	Assert.isNotNull(node);
	if (node.getExpression() instanceof ThisExpression) {
		final IVariableBinding binding= (IVariableBinding) node.getName().resolveBinding();
		if (binding != null) {
			final String key= binding.getKey();
			if (!fFound.contains(key)) {
				fFound.add(key);
				fBindings.add(binding);
			}
		}
	}
	return true;
}
 
Example 9
Source File: ExtractMethodRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(FieldAccess node) {
	Expression exp= node.getExpression();
	if (exp != null)
		fIgnore.add(node.getName());
	return true;
}
 
Example 10
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 11
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 12
Source File: CreateMutableCloneResolution.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
private boolean isThisFieldAccess(FieldAccess access, String fieldName) {
    return (access.getExpression() instanceof ThisExpression) && access.getName().getIdentifier().equals(fieldName);
}
 
Example 13
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);
}
 
Example 14
Source File: CodeStyleFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static ToStaticAccessOperation[] createToStaticAccessOperations(CompilationUnit astRoot, HashMap<ASTNode, Block> createdBlocks, IProblemLocation problem, boolean conservative) {
	ASTNode selectedNode= problem.getCoveringNode(astRoot);
	if (selectedNode == null) {
		return null;
	}

	Expression qualifier= null;
	IBinding accessBinding= null;

	if (selectedNode instanceof SimpleName) {
		selectedNode= selectedNode.getParent();
	}
	if (selectedNode instanceof QualifiedName) {
		QualifiedName name= (QualifiedName) selectedNode;
		qualifier= name.getQualifier();
		accessBinding= name.resolveBinding();
	} else if (selectedNode instanceof MethodInvocation) {
		MethodInvocation methodInvocation= (MethodInvocation) selectedNode;
		qualifier= methodInvocation.getExpression();
		accessBinding= methodInvocation.getName().resolveBinding();
	} else if (selectedNode instanceof FieldAccess) {
		FieldAccess fieldAccess= (FieldAccess) selectedNode;
		qualifier= fieldAccess.getExpression();
		accessBinding= fieldAccess.getName().resolveBinding();
	}

	if (accessBinding != null && qualifier != null) {
		if (conservative && ASTResolving.findParentStatement(qualifier) == null)
			return null;
		
		ToStaticAccessOperation declaring= null;
		ITypeBinding declaringTypeBinding= getDeclaringTypeBinding(accessBinding);
		if (declaringTypeBinding != null) {
			declaringTypeBinding= declaringTypeBinding.getTypeDeclaration(); // use generic to avoid any type arguments

			declaring= new ToStaticAccessOperation(declaringTypeBinding, qualifier, createdBlocks);
		}
		ToStaticAccessOperation instance= null;
		ITypeBinding instanceTypeBinding= Bindings.normalizeTypeBinding(qualifier.resolveTypeBinding());
		if (instanceTypeBinding != null) {
			instanceTypeBinding= instanceTypeBinding.getTypeDeclaration();  // use generic to avoid any type arguments
			if (instanceTypeBinding.getTypeDeclaration() != declaringTypeBinding) {
				instance= new ToStaticAccessOperation(instanceTypeBinding, qualifier, createdBlocks);
			}
		}
		if (declaring != null && instance != null) {
			return new ToStaticAccessOperation[] {declaring, instance};
		} else {
			return new ToStaticAccessOperation[] {declaring};
		}
	}
	return null;
}
 
Example 15
Source File: MethodDeclarationUtility.java    From JDeodorant with MIT License 4 votes vote down vote up
public static AbstractVariable createVariable(SimpleName simpleName, AbstractVariable rightPart) {
	IBinding binding = simpleName.resolveBinding();
	if(binding != null && binding.getKind() == IBinding.VARIABLE) {
		IVariableBinding variableBinding = (IVariableBinding)binding;
		AbstractVariable currentVariable = null;
		if(rightPart == null)
			currentVariable = new PlainVariable(variableBinding);
		else
			currentVariable = new CompositeVariable(variableBinding, rightPart);
		
		if(simpleName.getParent() instanceof QualifiedName) {
			QualifiedName qualifiedName = (QualifiedName)simpleName.getParent();
			Name qualifier = qualifiedName.getQualifier();
			if(qualifier instanceof SimpleName) {
				SimpleName qualifierSimpleName = (SimpleName)qualifier;
				if(!qualifierSimpleName.equals(simpleName))
					return createVariable(qualifierSimpleName, currentVariable);
				else
					return currentVariable;
			}
			else if(qualifier instanceof QualifiedName) {
				QualifiedName qualifiedName2 = (QualifiedName)qualifier;
				return createVariable(qualifiedName2.getName(), currentVariable);
			}
		}
		else if(simpleName.getParent() instanceof FieldAccess) {
			FieldAccess fieldAccess = (FieldAccess)simpleName.getParent();
			Expression fieldAccessExpression = fieldAccess.getExpression();
			if(fieldAccessExpression instanceof FieldAccess) {
				FieldAccess fieldAccess2 = (FieldAccess)fieldAccessExpression;
				return createVariable(fieldAccess2.getName(), currentVariable);
			}
			else if(fieldAccessExpression instanceof SimpleName) {
				SimpleName fieldAccessSimpleName = (SimpleName)fieldAccessExpression;
				return createVariable(fieldAccessSimpleName, currentVariable);
			}
			else if(fieldAccessExpression instanceof ThisExpression) {
				return currentVariable;
			}
		}
		else {
			return currentVariable;
		}
	}
	return null;
}