Java Code Examples for org.eclipse.jdt.core.dom.ASTNode#ANONYMOUS_CLASS_DECLARATION

The following examples show how to use org.eclipse.jdt.core.dom.ASTNode#ANONYMOUS_CLASS_DECLARATION . 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: UnimplementedCodeFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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;
	}
}
 
Example 2
Source File: JavaTextSelection.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: JavaTextSelection.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: JavaTextSelection.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: AssignToVariableAssistProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
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;
}
 
Example 6
Source File: ConvertAnonymousToNestedRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
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;
}