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

The following examples show how to use org.eclipse.jdt.core.dom.ASTNode#CAST_EXPRESSION . 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: TypeMismatchSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static ASTRewriteCorrectionProposal createCastProposal(IInvocationContext context, ITypeBinding castTypeBinding, Expression nodeToCast, int relevance) {
	ICompilationUnit cu= context.getCompilationUnit();

	String label;
	String castType= BindingLabelProviderCore.getBindingLabel(castTypeBinding, JavaElementLabels.ALL_DEFAULT);
	if (nodeToCast.getNodeType() == ASTNode.CAST_EXPRESSION) {
		label= Messages.format(CorrectionMessages.TypeMismatchSubProcessor_changecast_description, castType);
	} else {
		label= Messages.format(CorrectionMessages.TypeMismatchSubProcessor_addcast_description, castType);
	}
	return new CastCorrectionProposal(label, cu, nodeToCast, castTypeBinding, relevance);
}
 
Example 2
Source File: NecessaryParenthesesChecker.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean expressionTypeNeedsParentheses(Expression expression) {
	int type= expression.getNodeType();
	return type == ASTNode.INFIX_EXPRESSION
			|| type == ASTNode.CONDITIONAL_EXPRESSION
			|| type == ASTNode.PREFIX_EXPRESSION
			|| type == ASTNode.POSTFIX_EXPRESSION
			|| type == ASTNode.CAST_EXPRESSION
			|| type == ASTNode.INSTANCEOF_EXPRESSION
			|| type == ASTNode.ARRAY_CREATION
			|| type == ASTNode.ASSIGNMENT;
}
 
Example 3
Source File: TypeMismatchSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static ASTRewriteCorrectionProposal createCastProposal(IInvocationContext context, ITypeBinding castTypeBinding, Expression nodeToCast, int relevance) {
	ICompilationUnit cu= context.getCompilationUnit();

	String label;
	String castType= BindingLabelProvider.getBindingLabel(castTypeBinding, JavaElementLabels.ALL_DEFAULT);
	if (nodeToCast.getNodeType() == ASTNode.CAST_EXPRESSION) {
		label= Messages.format(CorrectionMessages.TypeMismatchSubProcessor_changecast_description, castType);
	} else {
		label= Messages.format(CorrectionMessages.TypeMismatchSubProcessor_addcast_description, castType);
	}
	return new CastCorrectionProposal(label, cu, nodeToCast, castTypeBinding, relevance);
}
 
Example 4
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 5
Source File: JavaASTVisitor.java    From SnowGraph with Apache License 2.0 4 votes vote down vote up
private void parseExpression(MethodInfo methodInfo, Expression expression) {
    if (expression == null) {
        return;
    }//System.out.println(expression.toString()+" "+Annotation.nodeClassForType(expression.getNodeType()));
    if (expression.getNodeType() == ASTNode.ARRAY_INITIALIZER) {
        List<Expression> expressions = ((ArrayInitializer) expression).expressions();
        for (Expression expression2 : expressions) {
            parseExpression(methodInfo, expression2);
        }
    }
    if (expression.getNodeType() == ASTNode.CAST_EXPRESSION) {
        parseExpression(methodInfo, ((CastExpression) expression).getExpression());
    }
    if (expression.getNodeType() == ASTNode.CONDITIONAL_EXPRESSION) {
        parseExpression(methodInfo, ((ConditionalExpression) expression).getExpression());
        parseExpression(methodInfo, ((ConditionalExpression) expression).getElseExpression());
        parseExpression(methodInfo, ((ConditionalExpression) expression).getThenExpression());
    }
    if (expression.getNodeType() == ASTNode.INFIX_EXPRESSION) {
        parseExpression(methodInfo, ((InfixExpression) expression).getLeftOperand());
        parseExpression(methodInfo, ((InfixExpression) expression).getRightOperand());
    }
    if (expression.getNodeType() == ASTNode.INSTANCEOF_EXPRESSION) {
        parseExpression(methodInfo, ((InstanceofExpression) expression).getLeftOperand());
    }
    if (expression.getNodeType() == ASTNode.PARENTHESIZED_EXPRESSION) {
        parseExpression(methodInfo, ((ParenthesizedExpression) expression).getExpression());
    }
    if (expression.getNodeType() == ASTNode.POSTFIX_EXPRESSION) {
        parseExpression(methodInfo, ((PostfixExpression) expression).getOperand());
    }
    if (expression.getNodeType() == ASTNode.PREFIX_EXPRESSION) {
        parseExpression(methodInfo, ((PrefixExpression) expression).getOperand());
    }
    if (expression.getNodeType() == ASTNode.THIS_EXPRESSION) {
        parseExpression(methodInfo, ((ThisExpression) expression).getQualifier());
    }
    if (expression.getNodeType() == ASTNode.METHOD_INVOCATION) {
        List<Expression> arguments = ((MethodInvocation) expression).arguments();
        IMethodBinding methodBinding = ((MethodInvocation) expression).resolveMethodBinding();
        if (methodBinding != null)
            methodInfo.methodCalls.add(methodBinding);
        for (Expression exp : arguments)
            parseExpression(methodInfo, exp);
        parseExpression(methodInfo, ((MethodInvocation) expression).getExpression());
    }
    if (expression.getNodeType() == ASTNode.ASSIGNMENT) {
        parseExpression(methodInfo, ((Assignment) expression).getLeftHandSide());
        parseExpression(methodInfo, ((Assignment) expression).getRightHandSide());
    }
    if (expression.getNodeType() == ASTNode.QUALIFIED_NAME) {
        if (((QualifiedName) expression).getQualifier().resolveTypeBinding() != null) {
            String name = ((QualifiedName) expression).getQualifier().resolveTypeBinding().getQualifiedName() + "." + ((QualifiedName) expression).getName().getIdentifier();
            methodInfo.fieldUsesSet.add(name);
        }
        parseExpression(methodInfo, ((QualifiedName) expression).getQualifier());
    }
}
 
Example 6
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;
	}
}