下面列出了怎么用org.eclipse.jdt.core.dom.SuperFieldAccess的API类实例代码及写法,或者点击链接到github查看源代码。
private SuperFieldAcc visit(SuperFieldAccess node) {
int startLine = _cunit.getLineNumber(node.getStartPosition());
int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength());
SuperFieldAcc superFieldAcc = new SuperFieldAcc(startLine, endLine, node);
SName identifier = (SName) process(node.getName());
identifier.setParent(superFieldAcc);
superFieldAcc.setIdentifier(identifier);
if(node.getQualifier() != null){
Label name = (Label) process(node.getQualifier());
name.setParent(superFieldAcc);
superFieldAcc.setName(name);
}
Pair<String, String> pair = NodeUtils.getTypeDecAndMethodDec(node);
Type exprType = ProjectInfo.getVariableType(pair.getFirst(), pair.getSecond(), node.getName().getFullyQualifiedName());
superFieldAcc.setType(exprType);
return superFieldAcc;
}
static boolean isLeftHandSideOfAssignment(ASTNode node) {
Assignment assignment = (Assignment) ASTNodes.getParent(node, ASTNode.ASSIGNMENT);
if (assignment != null) {
Expression leftHandSide = assignment.getLeftHandSide();
if (leftHandSide == node) {
return true;
}
if (ASTNodes.isParent(node, leftHandSide)) {
switch (leftHandSide.getNodeType()) {
case ASTNode.SIMPLE_NAME:
return true;
case ASTNode.FIELD_ACCESS:
return node == ((FieldAccess) leftHandSide).getName();
case ASTNode.QUALIFIED_NAME:
return node == ((QualifiedName) leftHandSide).getName();
case ASTNode.SUPER_FIELD_ACCESS:
return node == ((SuperFieldAccess) leftHandSide).getName();
default:
return false;
}
}
}
return false;
}
private Expression generateQualifier(String paramName, AST ast, boolean useSuper, Expression qualifier) {
SimpleName paramSimpleName= ast.newSimpleName(paramName);
if (useSuper) {
SuperFieldAccess sf= ast.newSuperFieldAccess();
sf.setName(paramSimpleName);
if (qualifier instanceof Name) {
sf.setQualifier((Name) qualifier);
}
return sf;
}
if (qualifier != null) {
FieldAccess parameterAccess= ast.newFieldAccess();
parameterAccess.setExpression(qualifier);
parameterAccess.setName(paramSimpleName);
return parameterAccess;
}
return paramSimpleName;
}
private void updateSimpleName(ASTRewrite rewriter, ParameterInfo pi, SimpleName node, List<SingleVariableDeclaration> enclosingParameters, IJavaProject project) {
AST ast= rewriter.getAST();
IBinding binding= node.resolveBinding();
Expression replacementNode= fParameterObjectFactory.createFieldReadAccess(pi, getParameterName(), ast, project, false, null);
if (binding instanceof IVariableBinding) {
IVariableBinding variable= (IVariableBinding) binding;
if (variable.isParameter() && variable.getName().equals(getNameInScope(pi, enclosingParameters))) {
rewriter.replace(node, replacementNode, null);
}
} else {
ASTNode parent= node.getParent();
if (!(parent instanceof QualifiedName || parent instanceof FieldAccess || parent instanceof SuperFieldAccess)) {
if (node.getIdentifier().equals(getNameInScope(pi, enclosingParameters))) {
rewriter.replace(node, replacementNode, null);
}
}
}
}
private static boolean isLeftHandSideOfAssignment(ASTNode node) {
Assignment assignment= (Assignment)ASTNodes.getParent(node, ASTNode.ASSIGNMENT);
if (assignment != null) {
Expression leftHandSide= assignment.getLeftHandSide();
if (leftHandSide == node) {
return true;
}
if (ASTNodes.isParent(node, leftHandSide)) {
switch (leftHandSide.getNodeType()) {
case ASTNode.SIMPLE_NAME:
return true;
case ASTNode.FIELD_ACCESS:
return node == ((FieldAccess)leftHandSide).getName();
case ASTNode.QUALIFIED_NAME:
return node == ((QualifiedName)leftHandSide).getName();
case ASTNode.SUPER_FIELD_ACCESS:
return node == ((SuperFieldAccess)leftHandSide).getName();
default:
return false;
}
}
}
return false;
}
public static IBinding resolveBinding(Expression expression){
if (expression instanceof Name)
return ((Name)expression).resolveBinding();
if (expression instanceof ParenthesizedExpression)
return resolveBinding(((ParenthesizedExpression)expression).getExpression());
else if (expression instanceof Assignment)
return resolveBinding(((Assignment)expression).getLeftHandSide());//TODO ???
else if (expression instanceof MethodInvocation)
return ((MethodInvocation)expression).resolveMethodBinding();
else if (expression instanceof SuperMethodInvocation)
return ((SuperMethodInvocation)expression).resolveMethodBinding();
else if (expression instanceof FieldAccess)
return ((FieldAccess)expression).resolveFieldBinding();
else if (expression instanceof SuperFieldAccess)
return ((SuperFieldAccess)expression).resolveFieldBinding();
else if (expression instanceof ConditionalExpression)
return resolveBinding(((ConditionalExpression)expression).getThenExpression());
return null;
}
/**
* Returns the binding of the variable written in an Assignment.
* @param assignment The assignment
* @return The binding or <code>null</code> if no bindings are available.
*/
public static IVariableBinding getAssignedVariable(Assignment assignment) {
Expression leftHand = assignment.getLeftHandSide();
switch (leftHand.getNodeType()) {
case ASTNode.SIMPLE_NAME:
return (IVariableBinding) ((SimpleName) leftHand).resolveBinding();
case ASTNode.QUALIFIED_NAME:
return (IVariableBinding) ((QualifiedName) leftHand).getName().resolveBinding();
case ASTNode.FIELD_ACCESS:
return ((FieldAccess) leftHand).resolveFieldBinding();
case ASTNode.SUPER_FIELD_ACCESS:
return ((SuperFieldAccess) leftHand).resolveFieldBinding();
default:
return null;
}
}
protected boolean isTypeHolder(Object o) {
if(o.getClass().equals(MethodInvocation.class) || o.getClass().equals(SuperMethodInvocation.class)
|| o.getClass().equals(NumberLiteral.class) || o.getClass().equals(StringLiteral.class)
|| o.getClass().equals(CharacterLiteral.class) || o.getClass().equals(BooleanLiteral.class)
|| o.getClass().equals(TypeLiteral.class) || o.getClass().equals(NullLiteral.class)
|| o.getClass().equals(ArrayCreation.class)
|| o.getClass().equals(ClassInstanceCreation.class)
|| o.getClass().equals(ArrayAccess.class) || o.getClass().equals(FieldAccess.class)
|| o.getClass().equals(SuperFieldAccess.class) || o.getClass().equals(ParenthesizedExpression.class)
|| o.getClass().equals(SimpleName.class) || o.getClass().equals(QualifiedName.class)
|| o.getClass().equals(CastExpression.class) || o.getClass().equals(InfixExpression.class)
|| o.getClass().equals(PrefixExpression.class) || o.getClass().equals(InstanceofExpression.class)
|| o.getClass().equals(ThisExpression.class) || o.getClass().equals(ConditionalExpression.class))
return true;
return false;
}
@Override
public void endVisit(SuperFieldAccess node) {
if (skipNode(node)) {
return;
}
processSequential(node, node.getQualifier(), node.getName());
}
@Override
public boolean visit(final SuperFieldAccess node) {
Name _qualifier = node.getQualifier();
boolean _tripleNotEquals = (_qualifier != null);
if (_tripleNotEquals) {
node.getQualifier().accept(this);
this.appendToBuffer(".");
}
this.appendToBuffer("super.");
node.getName().accept(this);
return false;
}
private Expression getQualifier(ASTNode parent) {
switch (parent.getNodeType()) {
case ASTNode.FIELD_ACCESS:
return ((FieldAccess) parent).getExpression();
case ASTNode.QUALIFIED_NAME:
return ((QualifiedName)parent).getQualifier();
case ASTNode.SUPER_FIELD_ACCESS:
return ((SuperFieldAccess)parent).getQualifier();
default:
return null;
}
}
/**
* Is the specified name a qualified entity, e.g. preceded by 'this',
* 'super' or part of a method invocation?
*
* @param name
* the name to check
* @return <code>true</code> if this entity is qualified,
* <code>false</code> otherwise
*/
protected static boolean isQualifiedEntity(final Name name) {
Assert.isNotNull(name);
final ASTNode parent= name.getParent();
if (parent instanceof QualifiedName && ((QualifiedName) parent).getName().equals(name) || parent instanceof FieldAccess && ((FieldAccess) parent).getName().equals(name) || parent instanceof SuperFieldAccess)
return true;
else if (parent instanceof MethodInvocation) {
final MethodInvocation invocation= (MethodInvocation) parent;
return invocation.getExpression() != null && invocation.getName().equals(name);
}
return false;
}
@Override
public final boolean visit(final SuperFieldAccess node) {
Assert.isNotNull(node);
fStatus.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.MoveInstanceMethodProcessor_uses_super, JavaStatusContext.create(fMethod.getCompilationUnit(), node)));
fResult.add(node);
return false;
}
@Override
public final void endVisit(final SuperFieldAccess node) {
final Name name= node.getName();
final IBinding binding= name.resolveBinding();
if (binding instanceof IVariableBinding)
endVisit((IVariableBinding) binding, null, node);
}
@Override
public final boolean visit(final SuperFieldAccess node) {
if (!fAnonymousClassDeclaration && !fTypeDeclarationStatement) {
final AST ast= node.getAST();
final FieldAccess access= ast.newFieldAccess();
access.setExpression(ast.newThisExpression());
access.setName(ast.newSimpleName(node.getName().getIdentifier()));
fRewrite.replace(node, access, null);
if (!fSourceRewriter.getCu().equals(fTargetRewriter.getCu()))
fSourceRewriter.getImportRemover().registerRemovedNode(node);
return true;
}
return false;
}
@Override
public ITypeConstraint[] create(SuperFieldAccess access){
SimpleName name= access.getName();
IBinding binding= name.resolveBinding();
if (! (binding instanceof IVariableBinding))
return new ITypeConstraint[0];
IVariableBinding vb= (IVariableBinding)binding;
return createConstraintsForAccessToField(vb, null, access);
}
public boolean visit(SuperFieldAccess expr) {
if (expr.getQualifier() != null) {
handleExpression(expr.getQualifier());
}
handleExpression(expr.getName());
return false;
}
public boolean visit(SuperFieldAccess expr) {
/*
* SuperFieldAccess: [ ClassName . ] super . Identifier
*/
activateDiffStyle(expr);
if (expr.getQualifier() != null) {
handleExpression(expr.getQualifier());
appendPeriod();
}
styledString.append("super", determineDiffStyle(expr, new StyledStringStyler(keywordStyle)));
appendPeriod();
handleExpression(expr.getName());
deactivateDiffStyle(expr);
return false;
}
@Override
public boolean visit(SuperFieldAccess node) {
fResult = false;
return false;
}
@Override
public boolean visit(SuperFieldAccess node) {
fResult = false;
return false;
}
@Override
public boolean visit(final SuperFieldAccess node) {
addToMap(identifiers, node, node.getName().toString());
return super.visit(node);
}
@Override
public boolean visit(final SuperFieldAccess node) {
addToMap(identifiers, node, node.getName().toString());
return super.visit(node);
}
@Override
public void endVisit(SuperFieldAccess node) {
if (skipNode(node))
return;
processSequential(node, node.getQualifier(), node.getName());
}
@Override
public boolean visit(SuperFieldAccess node) {
return visit(node.getName());
}
@Override
public boolean visit(SuperFieldAccess node) {
fResult= false;
return false;
}
@Override
public boolean visit(SuperFieldAccess node) {
fResult= false;
return false;
}
@Override
public boolean visit(SuperFieldAccess node) {
if (node.subtreeMatch(fMatcher, fNodeToMatch))
return matches(node);
return super.visit(node);
}
@Override
public boolean visit(SuperFieldAccess node) {
add(fCreator.create(node));
return true;
}
/**
* Resolve the binding (<em>not</em> the type binding) for the expression or a nested expression
* (e.g. nested in parentheses, cast, ...).
*
* @param expression an expression node
* @param goIntoCast iff <code>true</code>, go into a CastExpression's expression to resolve
* @return the expression binding, or <code>null</code> if the expression has no binding or the
* binding could not be resolved
*
* @see StubUtility#getVariableNameSuggestions(int, IJavaProject, ITypeBinding, Expression, java.util.Collection)
* @since 3.5
*/
public static IBinding resolveExpressionBinding(Expression expression, boolean goIntoCast) {
//TODO: search for callers of resolve*Binding() methods and replace with call to this method
// similar to StubUtility#getVariableNameSuggestions(int, IJavaProject, ITypeBinding, Expression, Collection)
switch (expression.getNodeType()) {
case ASTNode.SIMPLE_NAME:
case ASTNode.QUALIFIED_NAME:
return ((Name) expression).resolveBinding();
case ASTNode.FIELD_ACCESS:
return ((FieldAccess) expression).resolveFieldBinding();
case ASTNode.SUPER_FIELD_ACCESS:
return ((SuperFieldAccess) expression).resolveFieldBinding();
case ASTNode.METHOD_INVOCATION:
return ((MethodInvocation) expression).resolveMethodBinding();
case ASTNode.SUPER_METHOD_INVOCATION:
return ((SuperMethodInvocation) expression).resolveMethodBinding();
case ASTNode.CLASS_INSTANCE_CREATION:
return ((ClassInstanceCreation) expression).resolveConstructorBinding();
case ASTNode.MARKER_ANNOTATION:
case ASTNode.SINGLE_MEMBER_ANNOTATION:
case ASTNode.NORMAL_ANNOTATION:
return ((Annotation) expression).resolveAnnotationBinding();
case ASTNode.ARRAY_ACCESS:
return resolveExpressionBinding(((ArrayAccess) expression).getArray(), goIntoCast);
case ASTNode.CAST_EXPRESSION:
if (goIntoCast) {
return resolveExpressionBinding(((CastExpression) expression).getExpression(), true);
} else {
return null;
}
case ASTNode.PARENTHESIZED_EXPRESSION:
return resolveExpressionBinding(((ParenthesizedExpression) expression).getExpression(), goIntoCast);
case ASTNode.PREFIX_EXPRESSION:
return resolveExpressionBinding(((PrefixExpression) expression).getOperand(), goIntoCast);
case ASTNode.POSTFIX_EXPRESSION:
return resolveExpressionBinding(((PostfixExpression) expression).getOperand(), goIntoCast);
default:
return null;
}
}
@Override
public void endVisit(SuperFieldAccess node) {
endVisitNode(node);
}