下面列出了org.eclipse.jdt.core.dom.FieldAccess#getName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
@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);
}
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;
}
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;
}
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;
}
/**
* {@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);
}