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

The following examples show how to use org.eclipse.jdt.core.dom.ASTNode#SUPER_FIELD_ACCESS . 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: SnippetFinder.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: SnippetFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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;
	}
}
 
Example 4
Source File: ExtractClassRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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;
	}
}
 
Example 5
Source File: ASTResolving.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isWriteAccess(Name selectedNode) {
	ASTNode curr= selectedNode;
	ASTNode parent= curr.getParent();
	while (parent != null) {
		switch (parent.getNodeType()) {
			case ASTNode.QUALIFIED_NAME:
				if (((QualifiedName) parent).getQualifier() == curr) {
					return false;
				}
				break;
			case ASTNode.FIELD_ACCESS:
				if (((FieldAccess) parent).getExpression() == curr) {
					return false;
				}
				break;
			case ASTNode.SUPER_FIELD_ACCESS:
				break;
			case ASTNode.ASSIGNMENT:
				return ((Assignment) parent).getLeftHandSide() == curr;
			case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
			case ASTNode.SINGLE_VARIABLE_DECLARATION:
				return ((VariableDeclaration) parent).getName() == curr;
			case ASTNode.POSTFIX_EXPRESSION:
				return true;
			case ASTNode.PREFIX_EXPRESSION:
				PrefixExpression.Operator op= ((PrefixExpression) parent).getOperator();
				return op == PrefixExpression.Operator.DECREMENT || op == PrefixExpression.Operator.INCREMENT;
			default:
				return false;
		}

		curr= parent;
		parent= curr.getParent();
	}
	return false;
}
 
Example 6
Source File: CompilationUnitBuilder.java    From j2cl with Apache License 2.0 4 votes vote down vote up
private Expression convert(org.eclipse.jdt.core.dom.Expression expression) {
  switch (expression.getNodeType()) {
    case ASTNode.ARRAY_ACCESS:
      return convert((org.eclipse.jdt.core.dom.ArrayAccess) expression);
    case ASTNode.ARRAY_CREATION:
      return convert((org.eclipse.jdt.core.dom.ArrayCreation) expression);
    case ASTNode.ARRAY_INITIALIZER:
      return convert((org.eclipse.jdt.core.dom.ArrayInitializer) expression);
    case ASTNode.ASSIGNMENT:
      return convert((org.eclipse.jdt.core.dom.Assignment) expression);
    case ASTNode.BOOLEAN_LITERAL:
      return convert((org.eclipse.jdt.core.dom.BooleanLiteral) expression);
    case ASTNode.CAST_EXPRESSION:
      return convert((org.eclipse.jdt.core.dom.CastExpression) expression);
    case ASTNode.CHARACTER_LITERAL:
      return convert((org.eclipse.jdt.core.dom.CharacterLiteral) expression);
    case ASTNode.CLASS_INSTANCE_CREATION:
      return convert((org.eclipse.jdt.core.dom.ClassInstanceCreation) expression);
    case ASTNode.CONDITIONAL_EXPRESSION:
      return convert((org.eclipse.jdt.core.dom.ConditionalExpression) expression);
    case ASTNode.EXPRESSION_METHOD_REFERENCE:
      return convert((org.eclipse.jdt.core.dom.ExpressionMethodReference) expression);
    case ASTNode.CREATION_REFERENCE:
      return convert((org.eclipse.jdt.core.dom.CreationReference) expression);
    case ASTNode.TYPE_METHOD_REFERENCE:
      return convert((org.eclipse.jdt.core.dom.TypeMethodReference) expression);
    case ASTNode.SUPER_METHOD_REFERENCE:
      return convert((org.eclipse.jdt.core.dom.SuperMethodReference) expression);
    case ASTNode.FIELD_ACCESS:
      return convert((org.eclipse.jdt.core.dom.FieldAccess) expression);
    case ASTNode.INFIX_EXPRESSION:
      return convert((org.eclipse.jdt.core.dom.InfixExpression) expression);
    case ASTNode.INSTANCEOF_EXPRESSION:
      return convert((org.eclipse.jdt.core.dom.InstanceofExpression) expression);
    case ASTNode.LAMBDA_EXPRESSION:
      return convert((org.eclipse.jdt.core.dom.LambdaExpression) expression);
    case ASTNode.METHOD_INVOCATION:
      return convert((org.eclipse.jdt.core.dom.MethodInvocation) expression);
    case ASTNode.NULL_LITERAL:
      return NullLiteral.get();
    case ASTNode.NUMBER_LITERAL:
      return convert((org.eclipse.jdt.core.dom.NumberLiteral) expression);
    case ASTNode.PARENTHESIZED_EXPRESSION:
      return convert((org.eclipse.jdt.core.dom.ParenthesizedExpression) expression);
    case ASTNode.POSTFIX_EXPRESSION:
      return convert((org.eclipse.jdt.core.dom.PostfixExpression) expression);
    case ASTNode.PREFIX_EXPRESSION:
      return convert((org.eclipse.jdt.core.dom.PrefixExpression) expression);
    case ASTNode.QUALIFIED_NAME:
      return convert((org.eclipse.jdt.core.dom.QualifiedName) expression);
    case ASTNode.SIMPLE_NAME:
      return convert((org.eclipse.jdt.core.dom.SimpleName) expression);
    case ASTNode.STRING_LITERAL:
      return convert((org.eclipse.jdt.core.dom.StringLiteral) expression);
    case ASTNode.SUPER_FIELD_ACCESS:
      return convert((org.eclipse.jdt.core.dom.SuperFieldAccess) expression);
    case ASTNode.SUPER_METHOD_INVOCATION:
      return convert((org.eclipse.jdt.core.dom.SuperMethodInvocation) expression);
    case ASTNode.THIS_EXPRESSION:
      return convert((org.eclipse.jdt.core.dom.ThisExpression) expression);
    case ASTNode.TYPE_LITERAL:
      return convert((org.eclipse.jdt.core.dom.TypeLiteral) expression);
    case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
      return convert((org.eclipse.jdt.core.dom.VariableDeclarationExpression) expression);
    default:
      throw internalCompilerError(
          "Unexpected type for Expression: %s", expression.getClass().getName());
  }
}
 
Example 7
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;
}
 
Example 8
Source File: ExtractClassRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private RefactoringStatus replaceReferences(ParameterObjectFactory pof, SearchResultGroup group, CompilationUnitRewrite cuRewrite) {
	TextEditGroup writeGroup= cuRewrite.createGroupDescription(RefactoringCoreMessages.ExtractClassRefactoring_group_replace_write);
	TextEditGroup readGroup= cuRewrite.createGroupDescription(RefactoringCoreMessages.ExtractClassRefactoring_group_replace_read);
	ITypeRoot typeRoot= cuRewrite.getCu();
	IJavaProject javaProject= typeRoot.getJavaProject();
	AST ast= cuRewrite.getAST();

	RefactoringStatus status= new RefactoringStatus();
	String parameterName= fDescriptor.getFieldName();

	SearchMatch[] searchResults= group.getSearchResults();
	for (int j= 0; j < searchResults.length; j++) {
		SearchMatch searchMatch= searchResults[j];
		ASTNode node= NodeFinder.perform(cuRewrite.getRoot(), searchMatch.getOffset(), searchMatch.getLength());
		ASTNode parent= node.getParent();
		boolean isDeclaration= parent instanceof VariableDeclaration && ((VariableDeclaration)parent).getInitializer() != node;
		if (!isDeclaration && node instanceof SimpleName) {
			ASTRewrite rewrite= cuRewrite.getASTRewrite();
			if (parent.getNodeType() == ASTNode.SWITCH_CASE)
				status.addError(RefactoringCoreMessages.ExtractClassRefactoring_error_switch, JavaStatusContext.create(typeRoot, node));

			SimpleName name= (SimpleName) node;
			ParameterInfo pi= getFieldInfo(name.getIdentifier()).pi;
			boolean writeAccess= ASTResolving.isWriteAccess(name);
			if (writeAccess && fDescriptor.isCreateGetterSetter()) {
				boolean useSuper= parent.getNodeType() == ASTNode.SUPER_FIELD_ACCESS;
				Expression qualifier= getQualifier(parent);
				ASTNode replaceNode= getReplacementNode(parent, useSuper, qualifier);
				Expression assignedValue= getAssignedValue(pof, parameterName, javaProject, status, rewrite, pi, useSuper, name.resolveTypeBinding(), qualifier, replaceNode, typeRoot);
				if (assignedValue == null) {
					status.addError(RefactoringCoreMessages.ExtractClassRefactoring_error_unable_to_convert_node, JavaStatusContext.create(typeRoot, replaceNode));
				} else {
					NullLiteral marker= qualifier == null ? null : ast.newNullLiteral();
					Expression access= pof.createFieldWriteAccess(pi, parameterName, ast, javaProject, assignedValue, useSuper, marker);
					replaceMarker(rewrite, qualifier, access, marker);
					rewrite.replace(replaceNode, access, writeGroup);
				}
			} else {
				Expression fieldReadAccess= pof.createFieldReadAccess(pi, parameterName, ast, javaProject, false, null); //qualifier is already there
				rewrite.replace(name, fieldReadAccess, readGroup);
			}
		}
	}
	return status;
}
 
Example 9
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 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;
	}
}
 
Example 10
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;
}