下面列出了org.eclipse.jdt.core.dom.ASTNode#ANONYMOUS_CLASS_DECLARATION 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static ASTNode getSelectedTypeNode(CompilationUnit root, IProblemLocation problem) {
ASTNode selectedNode= problem.getCoveringNode(root);
if (selectedNode == null)
return null;
if (selectedNode.getNodeType() == ASTNode.ANONYMOUS_CLASS_DECLARATION) { // bug 200016
selectedNode= selectedNode.getParent();
}
if (selectedNode.getLocationInParent() == EnumConstantDeclaration.NAME_PROPERTY) {
selectedNode= selectedNode.getParent();
}
if (selectedNode.getNodeType() == ASTNode.SIMPLE_NAME && selectedNode.getParent() instanceof AbstractTypeDeclaration) {
return selectedNode.getParent();
} else if (selectedNode.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) {
return ((ClassInstanceCreation) selectedNode).getAnonymousClassDeclaration();
} else if (selectedNode.getNodeType() == ASTNode.ENUM_CONSTANT_DECLARATION) {
EnumConstantDeclaration enumConst= (EnumConstantDeclaration) selectedNode;
if (enumConst.getAnonymousClassDeclaration() != null)
return enumConst.getAnonymousClassDeclaration();
return enumConst;
} else {
return null;
}
}
public boolean resolveInMethodBody() {
if (fInMethodBodyRequested)
return fInMethodBody;
fInMethodBodyRequested= true;
resolveSelectedNodes();
ASTNode node= getStartNode();
if (node == null) {
fInMethodBody= true;
} else {
while (node != null) {
int nodeType= node.getNodeType();
if (nodeType == ASTNode.BLOCK && node.getParent() instanceof BodyDeclaration) {
fInMethodBody= node.getParent().getNodeType() == ASTNode.METHOD_DECLARATION;
break;
} else if (nodeType == ASTNode.ANONYMOUS_CLASS_DECLARATION) {
fInMethodBody= false;
break;
}
node= node.getParent();
}
}
return fInMethodBody;
}
public boolean resolveInClassInitializer() {
if (fInClassInitializerRequested)
return fInClassInitializer;
fInClassInitializerRequested= true;
resolveSelectedNodes();
ASTNode node= getStartNode();
if (node == null) {
fInClassInitializer= true;
} else {
while (node != null) {
int nodeType= node.getNodeType();
if (node instanceof AbstractTypeDeclaration) {
fInClassInitializer= false;
break;
} else if (nodeType == ASTNode.ANONYMOUS_CLASS_DECLARATION) {
fInClassInitializer= false;
break;
} else if (nodeType == ASTNode.INITIALIZER) {
fInClassInitializer= true;
break;
}
node= node.getParent();
}
}
return fInClassInitializer;
}
public boolean resolveInVariableInitializer() {
if (fInVariableInitializerRequested)
return fInVariableInitializer;
fInVariableInitializerRequested= true;
resolveSelectedNodes();
ASTNode node= getStartNode();
ASTNode last= null;
while (node != null) {
int nodeType= node.getNodeType();
if (node instanceof AbstractTypeDeclaration) {
fInVariableInitializer= false;
break;
} else if (nodeType == ASTNode.ANONYMOUS_CLASS_DECLARATION) {
fInVariableInitializer= false;
break;
} else if (nodeType == ASTNode.VARIABLE_DECLARATION_FRAGMENT &&
((VariableDeclarationFragment)node).getInitializer() == last) {
fInVariableInitializer= true;
break;
} else if (nodeType == ASTNode.SINGLE_VARIABLE_DECLARATION &&
((SingleVariableDeclaration)node).getInitializer() == last) {
fInVariableInitializer= true;
break;
} else if (nodeType == ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION &&
((AnnotationTypeMemberDeclaration)node).getDefault() == last) {
fInVariableInitializer= true;
break;
}
last= node;
node= node.getParent();
}
return fInVariableInitializer;
}
private ASTRewrite doAddField(ASTRewrite rewrite, ASTNode nodeToAssign, ITypeBinding typeBinding, int index) {
boolean isParamToField= nodeToAssign.getNodeType() == ASTNode.SINGLE_VARIABLE_DECLARATION;
ASTNode newTypeDecl= ASTResolving.findParentType(nodeToAssign);
if (newTypeDecl == null) {
return null;
}
Expression expression= isParamToField ? ((SingleVariableDeclaration) nodeToAssign).getName() : ((ExpressionStatement) nodeToAssign).getExpression();
AST ast= newTypeDecl.getAST();
createImportRewrite((CompilationUnit) nodeToAssign.getRoot());
BodyDeclaration bodyDecl= ASTResolving.findParentBodyDeclaration(nodeToAssign);
Block body;
if (bodyDecl instanceof MethodDeclaration) {
body= ((MethodDeclaration) bodyDecl).getBody();
} else if (bodyDecl instanceof Initializer) {
body= ((Initializer) bodyDecl).getBody();
} else {
return null;
}
IJavaProject project= getCompilationUnit().getJavaProject();
boolean isAnonymous= newTypeDecl.getNodeType() == ASTNode.ANONYMOUS_CLASS_DECLARATION;
boolean isStatic= Modifier.isStatic(bodyDecl.getModifiers()) && !isAnonymous;
int modifiers= Modifier.PRIVATE;
if (isStatic) {
modifiers |= Modifier.STATIC;
}
VariableDeclarationFragment newDeclFrag= addFieldDeclaration(rewrite, newTypeDecl, modifiers, expression, nodeToAssign, typeBinding, index);
String varName= newDeclFrag.getName().getIdentifier();
Assignment assignment= ast.newAssignment();
assignment.setRightHandSide((Expression) rewrite.createCopyTarget(expression));
boolean needsThis= StubUtility.useThisForFieldAccess(project);
if (isParamToField) {
needsThis |= varName.equals(((SimpleName) expression).getIdentifier());
}
SimpleName accessName= ast.newSimpleName(varName);
if (needsThis) {
FieldAccess fieldAccess= ast.newFieldAccess();
fieldAccess.setName(accessName);
if (isStatic) {
String typeName= ((AbstractTypeDeclaration) newTypeDecl).getName().getIdentifier();
fieldAccess.setExpression(ast.newSimpleName(typeName));
} else {
fieldAccess.setExpression(ast.newThisExpression());
}
assignment.setLeftHandSide(fieldAccess);
} else {
assignment.setLeftHandSide(accessName);
}
ASTNode selectionNode;
if (isParamToField) {
// assign parameter to field
ExpressionStatement statement= ast.newExpressionStatement(assignment);
int insertIdx= findAssignmentInsertIndex(body.statements(), nodeToAssign) + index;
rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY).insertAt(statement, insertIdx, null);
selectionNode= statement;
} else {
if (needsSemicolon(expression)) {
rewrite.replace(expression, ast.newExpressionStatement(assignment), null);
} else {
rewrite.replace(expression, assignment, null);
}
selectionNode= nodeToAssign;
}
addLinkedPosition(rewrite.track(newDeclFrag.getName()), false, KEY_NAME + index);
if (!isParamToField) {
FieldDeclaration fieldDeclaration= (FieldDeclaration) newDeclFrag.getParent();
addLinkedPosition(rewrite.track(fieldDeclaration.getType()), false, KEY_TYPE);
}
addLinkedPosition(rewrite.track(accessName), true, KEY_NAME + index);
IVariableBinding variableBinding= newDeclFrag.resolveBinding();
if (variableBinding != null) {
SimpleName[] linkedNodes= LinkedNodeFinder.findByBinding(nodeToAssign.getRoot(), variableBinding);
for (int i= 0; i < linkedNodes.length; i++) {
addLinkedPosition(rewrite.track(linkedNodes[i]), false, KEY_NAME + index);
}
}
setEndPosition(rewrite.track(selectionNode));
return rewrite;
}
public boolean mustInnerClassBeStatic() {
ITypeBinding typeBinding = ((AbstractTypeDeclaration) ASTNodes.getParent(fAnonymousInnerClassNode, AbstractTypeDeclaration.class)).resolveBinding();
ASTNode current = fAnonymousInnerClassNode.getParent();
boolean ans = false;
while(current != null) {
switch(current.getNodeType()) {
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
case ASTNode.CONSTRUCTOR_INVOCATION:
return true;
case ASTNode.ANONYMOUS_CLASS_DECLARATION:
{
AnonymousClassDeclaration enclosingAnonymousClassDeclaration= (AnonymousClassDeclaration)current;
ITypeBinding binding= enclosingAnonymousClassDeclaration.resolveBinding();
if (binding != null && Bindings.isSuperType(typeBinding, binding.getSuperclass())) {
return false;
}
break;
}
case ASTNode.FIELD_DECLARATION:
{
FieldDeclaration enclosingFieldDeclaration= (FieldDeclaration)current;
if (Modifier.isStatic(enclosingFieldDeclaration.getModifiers())) {
ans = true;
}
break;
}
case ASTNode.METHOD_DECLARATION:
{
MethodDeclaration enclosingMethodDeclaration = (MethodDeclaration)current;
if (Modifier.isStatic(enclosingMethodDeclaration.getModifiers())) {
ans = true;
}
break;
}
case ASTNode.TYPE_DECLARATION:
{
return ans;
}
}
current = current.getParent();
}
return ans;
}