Java Code Examples for org.eclipse.jdt.core.dom.ASTNode#equals()

The following examples show how to use org.eclipse.jdt.core.dom.ASTNode#equals() . 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: ExtractConstantRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static boolean canReplace(IASTFragment fragment) {
	ASTNode node = fragment.getAssociatedNode();
	ASTNode parent = node.getParent();
	if (parent instanceof VariableDeclarationFragment) {
		VariableDeclarationFragment vdf = (VariableDeclarationFragment) parent;
		if (node.equals(vdf.getName())) {
			return false;
		}
	}
	if (parent instanceof ExpressionStatement) {
		return false;
	}
	if (parent instanceof SwitchCase) {
		if (node instanceof Name) {
			Name name = (Name) node;
			ITypeBinding typeBinding = name.resolveTypeBinding();
			if (typeBinding != null) {
				return !typeBinding.isEnum();
			}
		}
	}
	return true;
}
 
Example 2
Source File: ControlVariable.java    From JDeodorant with MIT License 6 votes vote down vote up
private static List<Expression> removeExpressionsInAConditionalExpression(List<Expression> expressions, Statement containingStatement)
{
	ListIterator<Expression> it = expressions.listIterator();
	while (it.hasNext())
	{
		Expression currentUpdater = it.next();
		ASTNode parent = currentUpdater.getParent();
		while (parent != null && !parent.equals(containingStatement))
		{
			if (parent instanceof ConditionalExpression)
			{
				it.remove();
				break;
			}
			parent = parent.getParent();
		}
	}
	return expressions;
}
 
Example 3
Source File: ExtractTempRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean canReplace(IASTFragment fragment) {
	ASTNode node= fragment.getAssociatedNode();
	ASTNode parent= node.getParent();
	if (parent instanceof VariableDeclarationFragment) {
		VariableDeclarationFragment vdf= (VariableDeclarationFragment) parent;
		if (node.equals(vdf.getName()))
			return false;
	}
	if (isMethodParameter(node))
		return false;
	if (isThrowableInCatchBlock(node))
		return false;
	if (parent instanceof ExpressionStatement)
		return false;
	if (isLeftValue(node))
		return false;
	if (isReferringToLocalVariableFromFor((Expression) node))
		return false;
	if (isUsedInForInitializerOrUpdater((Expression) node))
		return false;
	if (parent instanceof SwitchCase)
		return false;
	return true;
}
 
Example 4
Source File: ExtractConstantRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean canReplace(IASTFragment fragment) {
	ASTNode node= fragment.getAssociatedNode();
	ASTNode parent= node.getParent();
	if (parent instanceof VariableDeclarationFragment) {
		VariableDeclarationFragment vdf= (VariableDeclarationFragment) parent;
		if (node.equals(vdf.getName()))
			return false;
	}
	if (parent instanceof ExpressionStatement)
		return false;
	if (parent instanceof SwitchCase) {
		if (node instanceof Name) {
			Name name= (Name) node;
			ITypeBinding typeBinding= name.resolveTypeBinding();
			if (typeBinding != null) {
				return !typeBinding.isEnum();
			}
		}
	}
	return true;
}
 
Example 5
Source File: ExtractFieldRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static boolean canReplace(IASTFragment fragment) {
	ASTNode node = fragment.getAssociatedNode();
	ASTNode parent = node.getParent();
	if (parent instanceof VariableDeclarationFragment) {
		VariableDeclarationFragment vdf = (VariableDeclarationFragment) parent;
		if (node.equals(vdf.getName())) {
			return false;
		}
	}
	if (isMethodParameter(node)) {
		return false;
	}
	if (isThrowableInCatchBlock(node)) {
		return false;
	}
	if (parent instanceof ExpressionStatement) {
		return false;
	}
	if (parent instanceof LambdaExpression) {
		return false;
	}
	if (isLeftValue(node)) {
		return false;
	}
	if (isReferringToLocalVariableFromFor((Expression) node)) {
		return false;
	}
	if (isUsedInForInitializerOrUpdater((Expression) node)) {
		return false;
	}
	if (parent instanceof SwitchCase) {
		return false;
	}
	return true;
}
 
Example 6
Source File: ExtractTempRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static boolean canReplace(IASTFragment fragment) {
	ASTNode node = fragment.getAssociatedNode();
	ASTNode parent = node.getParent();
	if (parent instanceof VariableDeclarationFragment) {
		VariableDeclarationFragment vdf = (VariableDeclarationFragment) parent;
		if (node.equals(vdf.getName())) {
			return false;
		}
	}
	if (isMethodParameter(node)) {
		return false;
	}
	if (isThrowableInCatchBlock(node)) {
		return false;
	}
	if (parent instanceof ExpressionStatement) {
		return false;
	}
	if (parent instanceof LambdaExpression) {
		return false;
	}
	if (isLeftValue(node)) {
		return false;
	}
	if (isReferringToLocalVariableFromFor((Expression) node)) {
		return false;
	}
	if (isUsedInForInitializerOrUpdater((Expression) node)) {
		return false;
	}
	if (parent instanceof SwitchCase) {
		return false;
	}
	return true;
}
 
Example 7
Source File: NewVariableCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private int evaluateFieldModifiers(ASTNode newTypeDecl) {
	if (fSenderBinding.isAnnotation()) {
		return 0;
	}
	if (fSenderBinding.isInterface()) {
		// for interface members copy the modifiers from an existing field
		FieldDeclaration[] fieldDecls= ((TypeDeclaration) newTypeDecl).getFields();
		if (fieldDecls.length > 0) {
			return fieldDecls[0].getModifiers();
		}
		return 0;
	}
	int modifiers= 0;

	if (fVariableKind == CONST_FIELD) {
		modifiers |= Modifier.FINAL | Modifier.STATIC;
	} else {
		ASTNode parent= fOriginalNode.getParent();
		if (parent instanceof QualifiedName) {
			IBinding qualifierBinding= ((QualifiedName)parent).getQualifier().resolveBinding();
			if (qualifierBinding instanceof ITypeBinding) {
				modifiers |= Modifier.STATIC;
			}
		} else if (ASTResolving.isInStaticContext(fOriginalNode)) {
			modifiers |= Modifier.STATIC;
		}
	}
	ASTNode node= ASTResolving.findParentType(fOriginalNode, true);
	if (newTypeDecl.equals(node)) {
		modifiers |= Modifier.PRIVATE;
	} else if (node instanceof AnonymousClassDeclaration) {
		modifiers |= Modifier.PROTECTED;
	} else {
		modifiers |= Modifier.PUBLIC;
	}

	return modifiers;
}
 
Example 8
Source File: ConstructorReferenceFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isImplicitConstructorReferenceNodeInClassCreations(ASTNode node) {
	if (node instanceof Type) {
		final ASTNode parent= node.getParent();
		if (parent instanceof ClassInstanceCreation) {
			return (node.equals(((ClassInstanceCreation) parent).getType()));
		} else if (parent instanceof ParameterizedType) {
			final ASTNode grandParent= parent.getParent();
			if (grandParent instanceof ClassInstanceCreation) {
				final ParameterizedType type= (ParameterizedType) ((ClassInstanceCreation) grandParent).getType();
				return (node.equals(type.getType()));
			}
		}
	}
	return false;
}
 
Example 9
Source File: ControlDependenceTreeGenerator.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean isExpressionStatementWithConditionalExpression(PDGNode node) {
	Statement statement = node.getASTStatement();
	if(statement instanceof ExpressionStatement) {
		ExpressionExtractor expressionExtractor = new ExpressionExtractor();
		List<Expression> conditionalExpressions = expressionExtractor.getConditionalExpressions(statement);
		if(conditionalExpressions.size() == 1) {
			ConditionalExpression conditional = (ConditionalExpression)conditionalExpressions.get(0);
			ASTNode parent = conditional.getParent();
			ASTNode grandParent = parent.getParent();
			if(grandParent != null && grandParent.equals(statement)) {
				return true;
			}
			if(parent instanceof ParenthesizedExpression) {
				if(grandParent != null) {
					if(grandParent.getParent() != null && grandParent.getParent().equals(statement)) {
						return true;
					}
				}
			}
		}
	}
	else if (statement instanceof ReturnStatement) {
		ReturnStatement returnStatement = (ReturnStatement)statement;
		if (returnStatement.getExpression() instanceof ConditionalExpression) {
			return true;
		}
	}
	return false;
}
 
Example 10
Source File: NewVariableCorrectionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private int evaluateFieldModifiers(ASTNode newTypeDecl) {
	if (fSenderBinding.isAnnotation()) {
		return 0;
	}
	if (fSenderBinding.isInterface()) {
		// for interface members copy the modifiers from an existing field
		FieldDeclaration[] fieldDecls= ((TypeDeclaration) newTypeDecl).getFields();
		if (fieldDecls.length > 0) {
			return fieldDecls[0].getModifiers();
		}
		return 0;
	}
	int modifiers= 0;

	if (fVariableKind == CONST_FIELD) {
		modifiers |= Modifier.FINAL | Modifier.STATIC;
	} else {
		ASTNode parent= fOriginalNode.getParent();
		if (parent instanceof QualifiedName) {
			IBinding qualifierBinding= ((QualifiedName)parent).getQualifier().resolveBinding();
			if (qualifierBinding instanceof ITypeBinding) {
				modifiers |= Modifier.STATIC;
			}
		} else if (ASTResolving.isInStaticContext(fOriginalNode)) {
			modifiers |= Modifier.STATIC;
		}
	}
	ASTNode node= ASTResolving.findParentType(fOriginalNode, true);
	if (newTypeDecl.equals(node)) {
		modifiers |= Modifier.PRIVATE;
	} else if (node instanceof AnonymousClassDeclaration) {
		modifiers |= Modifier.PROTECTED;
	} else {
		modifiers |= Modifier.PUBLIC;
	}

	return modifiers;
}
 
Example 11
Source File: NewMethodCorrectionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private int evaluateModifiers(ASTNode targetTypeDecl) {
	if (getSenderBinding().isAnnotation()) {
		return 0;
	}
	if (getSenderBinding().isInterface()) {
		// for interface and annotation members copy the modifiers from an existing field
		MethodDeclaration[] methodDecls= ((TypeDeclaration) targetTypeDecl).getMethods();
		if (methodDecls.length > 0) {
			return methodDecls[0].getModifiers();
		}
		return 0;
	}
	ASTNode invocationNode= getInvocationNode();
	if (invocationNode instanceof MethodInvocation) {
		int modifiers= 0;
		Expression expression= ((MethodInvocation)invocationNode).getExpression();
		if (expression != null) {
			if (expression instanceof Name && ((Name) expression).resolveBinding().getKind() == IBinding.TYPE) {
				modifiers |= Modifier.STATIC;
			}
		} else if (ASTResolving.isInStaticContext(invocationNode)) {
			modifiers |= Modifier.STATIC;
		}
		ASTNode node= ASTResolving.findParentType(invocationNode);
		if (targetTypeDecl.equals(node)) {
			modifiers |= Modifier.PRIVATE;
		} else if (node instanceof AnonymousClassDeclaration && ASTNodes.isParent(node, targetTypeDecl)) {
			modifiers |= Modifier.PROTECTED;
			if (ASTResolving.isInStaticContext(node) && expression == null) {
				modifiers |= Modifier.STATIC;
			}
		} else {
			modifiers |= Modifier.PUBLIC;
		}
		return modifiers;
	}
	return Modifier.PUBLIC;
}
 
Example 12
Source File: PreconditionExaminer.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean isExpressionUnderStatement(ASTNode expression, Statement statement) {
	ASTNode parent = expression.getParent();
	if(parent.equals(statement))
		return true;
	if(!(parent instanceof Statement) && !(parent instanceof BodyDeclaration))
		return isExpressionUnderStatement(parent, statement);
	else
		return false;
}
 
Example 13
Source File: PreconditionExaminer.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean isExpressionWithinExpression(ASTNode expression, Expression parentExpression) {
	if(expression.equals(parentExpression))
		return true;
	ASTNode parent = expression.getParent();
	if(!(parent instanceof Statement))
		return isExpressionWithinExpression(parent, parentExpression);
	else
		return false;
}
 
Example 14
Source File: ExtractStatementsVisitor.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean isStartNodeNestedUnderAnonymousClassDeclaration(AnonymousClassDeclaration node) {
	ASTNode parent = startNode.getParent();
	while(parent != null) {
		if(parent.equals(node)) {
			return true;
		}
		parent = parent.getParent();
	}
	return false;
}
 
Example 15
Source File: NodeMapping.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean isExpressionWithinExpression(ASTNode expression, Expression parentExpression) {
	if(expression.equals(parentExpression))
		return true;
	ASTNode parent = expression.getParent();
	if(!(parent instanceof Statement))
		return isExpressionWithinExpression(parent, parentExpression);
	else
		return false;
}
 
Example 16
Source File: StyledStringVisitor.java    From JDeodorant with MIT License 4 votes vote down vote up
private void deactivateDiffStyle(ASTNode expr){
	if (expr.equals(currentCompositeDiffNode)){
		currentCompositeDiffNode = null;
	}
}
 
Example 17
Source File: ASTResolving.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static Type guessTypeForReference(AST ast, ASTNode node) {
	ASTNode parent= node.getParent();
	while (parent != null) {
		switch (parent.getNodeType()) {
			case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
				if (((VariableDeclarationFragment) parent).getInitializer() == node) {
					return ASTNodeFactory.newType(ast, (VariableDeclaration) parent);
				}
				return null;
			case ASTNode.SINGLE_VARIABLE_DECLARATION:
				if (((VariableDeclarationFragment) parent).getInitializer() == node) {
					return ASTNodeFactory.newType(ast, (VariableDeclaration) parent);
				}
				return null;
			case ASTNode.ARRAY_ACCESS:
				if (!((ArrayAccess) parent).getIndex().equals(node)) {
					Type type= guessTypeForReference(ast, parent);
					if (type != null) {
						return ASTNodeFactory.newArrayType(type);
					}
				}
				return null;
			case ASTNode.FIELD_ACCESS:
				if (node.equals(((FieldAccess) parent).getName())) {
					node= parent;
					parent= parent.getParent();
				} else {
					return null;
				}
				break;
			case ASTNode.SUPER_FIELD_ACCESS:
			case ASTNode.PARENTHESIZED_EXPRESSION:
				node= parent;
				parent= parent.getParent();
				break;
			case ASTNode.QUALIFIED_NAME:
				if (node.equals(((QualifiedName) parent).getName())) {
					node= parent;
					parent= parent.getParent();
				} else {
					return null;
				}
				break;
			default:
				return null;
		}
	}
	return null;
}
 
Example 18
Source File: NewMethodCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private int evaluateModifiers(ASTNode targetTypeDecl) {
	if (getSenderBinding().isAnnotation()) {
		return 0;
	}
	boolean isTargetInterface= getSenderBinding().isInterface();
	if (isTargetInterface && !JavaModelUtil.is18OrHigher(getCompilationUnit().getJavaProject())) {
		// only abstract methods are allowed for interface present in less than Java 1.8
		return getInterfaceMethodModifiers(targetTypeDecl, true);
	}
	ASTNode invocationNode= getInvocationNode();
	if (invocationNode instanceof MethodInvocation) {
		int modifiers= 0;
		Expression expression= ((MethodInvocation)invocationNode).getExpression();
		if (expression != null) {
			if (expression instanceof Name && ((Name) expression).resolveBinding().getKind() == IBinding.TYPE) {
				modifiers |= Modifier.STATIC;
			}
		} else if (ASTResolving.isInStaticContext(invocationNode)) {
			modifiers |= Modifier.STATIC;
		}
		ASTNode node= ASTResolving.findParentType(invocationNode);
		boolean isParentInterface= node instanceof TypeDeclaration && ((TypeDeclaration) node).isInterface();
		if (isTargetInterface || isParentInterface) {
			if (expression == null && !targetTypeDecl.equals(node)) {
				modifiers|= Modifier.STATIC;
				if (isTargetInterface) {
					modifiers|= getInterfaceMethodModifiers(targetTypeDecl, false);
				} else {
					modifiers|= Modifier.PROTECTED;
				}
			} else if (modifiers == Modifier.STATIC) {
				modifiers= getInterfaceMethodModifiers(targetTypeDecl, false) | Modifier.STATIC;
			} else {
				modifiers= getInterfaceMethodModifiers(targetTypeDecl, true);
			}
		} else if (targetTypeDecl.equals(node)) {
			modifiers |= Modifier.PRIVATE;
		} else if (node instanceof AnonymousClassDeclaration && ASTNodes.isParent(node, targetTypeDecl)) {
			modifiers |= Modifier.PROTECTED;
			if (ASTResolving.isInStaticContext(node) && expression == null) {
				modifiers |= Modifier.STATIC;
			}
		} else {
			modifiers |= Modifier.PUBLIC;
		}
		return modifiers;
	}
	return Modifier.PUBLIC;
}
 
Example 19
Source File: ASTResolving.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public static Type guessTypeForReference(AST ast, ASTNode node) {
	ASTNode parent= node.getParent();
	while (parent != null) {
		switch (parent.getNodeType()) {
			case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
				if (((VariableDeclarationFragment) parent).getInitializer() == node) {
					return ASTNodeFactory.newType(ast, (VariableDeclaration) parent);
				}
				return null;
			case ASTNode.SINGLE_VARIABLE_DECLARATION:
				if (((VariableDeclarationFragment) parent).getInitializer() == node) {
					return ASTNodeFactory.newType(ast, (VariableDeclaration) parent);
				}
				return null;
			case ASTNode.ARRAY_ACCESS:
				if (!((ArrayAccess) parent).getIndex().equals(node)) {
					Type type = guessTypeForReference(ast, parent);
					if (type != null) {
						return ASTNodeFactory.newArrayType(type);
					}
				}
				return null;
			case ASTNode.FIELD_ACCESS:
				if (node.equals(((FieldAccess) parent).getName())) {
					node = parent;
					parent = parent.getParent();
				} else {
					return null;
				}
				break;
			case ASTNode.SUPER_FIELD_ACCESS:
			case ASTNode.PARENTHESIZED_EXPRESSION:
				node= parent;
				parent= parent.getParent();
				break;
			case ASTNode.QUALIFIED_NAME:
				if (node.equals(((QualifiedName) parent).getName())) {
					node = parent;
					parent = parent.getParent();
				} else {
					return null;
				}
				break;
			default:
				return null;
		}
	}
	return null;
}