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

The following examples show how to use org.eclipse.jdt.core.dom.ASTNode#CLASS_INSTANCE_CREATION . 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: Invocations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 7 votes vote down vote up
public static Expression getExpression(ASTNode invocation) {
	switch (invocation.getNodeType()) {
		case ASTNode.METHOD_INVOCATION:
			return ((MethodInvocation)invocation).getExpression();
		case ASTNode.SUPER_METHOD_INVOCATION:
			return null;
			
		case ASTNode.CONSTRUCTOR_INVOCATION:
			return null;
		case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
			return ((SuperConstructorInvocation)invocation).getExpression();
			
		case ASTNode.CLASS_INSTANCE_CREATION:
			return ((ClassInstanceCreation)invocation).getExpression();
		case ASTNode.ENUM_CONSTANT_DECLARATION:
			return null;
			
		default:
			throw new IllegalArgumentException(invocation.toString());
	}
}
 
Example 2
Source File: Invocations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static IMethodBinding resolveBinding(ASTNode invocation) {
	switch (invocation.getNodeType()) {
		case ASTNode.METHOD_INVOCATION:
			return ((MethodInvocation)invocation).resolveMethodBinding();
		case ASTNode.SUPER_METHOD_INVOCATION:
			return ((SuperMethodInvocation)invocation).resolveMethodBinding();
			
		case ASTNode.CONSTRUCTOR_INVOCATION:
			return ((ConstructorInvocation)invocation).resolveConstructorBinding();
		case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
			return ((SuperConstructorInvocation)invocation).resolveConstructorBinding();
			
		case ASTNode.CLASS_INSTANCE_CREATION:
			return ((ClassInstanceCreation)invocation).resolveConstructorBinding();
		case ASTNode.ENUM_CONSTANT_DECLARATION:
			return ((EnumConstantDeclaration)invocation).resolveConstructorBinding();
			
		default:
			throw new IllegalArgumentException(invocation.toString());
	}
}
 
Example 3
Source File: DOMFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public boolean visit(AnonymousClassDeclaration node) {
	ASTNode name;
	ASTNode parent = node.getParent();
	switch (parent.getNodeType()) {
		case ASTNode.CLASS_INSTANCE_CREATION:
			name = ((ClassInstanceCreation) parent).getType();
			if (name.getNodeType() == ASTNode.PARAMETERIZED_TYPE) {
				name = ((ParameterizedType) name).getType();
			}
			break;
		case ASTNode.ENUM_CONSTANT_DECLARATION:
			name = ((EnumConstantDeclaration) parent).getName();
			break;
		default:
			return true;
	}
	if (found(node, name) && this.resolveBinding)
		this.foundBinding = node.resolveBinding();
	return true;
}
 
Example 4
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 5
Source File: IntroduceFactoryRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Finds and returns the <code>ASTNode</code> for the given source text
 * selection, if it is an entire constructor call or the class name portion
 * of a constructor call or constructor declaration, or null otherwise.
 * @param unit The compilation unit in which the selection was made
 * @param offset The textual offset of the start of the selection
 * @param length The length of the selection in characters
 * @return ClassInstanceCreation or MethodDeclaration
 */
private ASTNode getTargetNode(ICompilationUnit unit, int offset, int length) {
	ASTNode node= ASTNodes.getNormalizedNode(NodeFinder.perform(fCU, offset, length));
	if (node.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION)
		return node;
	if (node.getNodeType() == ASTNode.METHOD_DECLARATION && ((MethodDeclaration)node).isConstructor())
		return node;
	// we have some sub node. Make sure its the right child of the parent
	StructuralPropertyDescriptor location= node.getLocationInParent();
	ASTNode parent= node.getParent();
	if (location == ClassInstanceCreation.TYPE_PROPERTY) {
		return parent;
	} else if (location == MethodDeclaration.NAME_PROPERTY && ((MethodDeclaration)parent).isConstructor()) {
		return parent;
	}
	return null;
}
 
Example 6
Source File: Invocations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static ITypeBinding[] getInferredTypeArguments(Expression invocation) {
	IMethodBinding methodBinding;
	switch (invocation.getNodeType()) {
		case ASTNode.METHOD_INVOCATION:
			methodBinding= ((MethodInvocation) invocation).resolveMethodBinding();
			return methodBinding == null ? null : methodBinding.getTypeArguments();
		case ASTNode.SUPER_METHOD_INVOCATION:
			methodBinding= ((SuperMethodInvocation) invocation).resolveMethodBinding();
			return methodBinding == null ? null : methodBinding.getTypeArguments();
		case ASTNode.CLASS_INSTANCE_CREATION:
			Type type= ((ClassInstanceCreation) invocation).getType();
			ITypeBinding typeBinding= type.resolveBinding();
			return typeBinding == null ? null : typeBinding.getTypeArguments();
			
		default:
			throw new IllegalArgumentException(invocation.toString());
	}
}
 
Example 7
Source File: Invocations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static boolean isResolvedTypeInferredFromExpectedType(Expression invocation) {
	if (invocation == null)
		return false;
	
	switch (invocation.getNodeType()) {
		case ASTNode.METHOD_INVOCATION:
			return ((MethodInvocation) invocation).isResolvedTypeInferredFromExpectedType();
		case ASTNode.SUPER_METHOD_INVOCATION:
			return ((SuperMethodInvocation) invocation).isResolvedTypeInferredFromExpectedType();
		case ASTNode.CLASS_INSTANCE_CREATION:
			return ((ClassInstanceCreation) invocation).isResolvedTypeInferredFromExpectedType();
			
		default:
			return false;
	}
}
 
Example 8
Source File: Invocations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static boolean isInvocationWithArguments(ASTNode node) {
	switch (node.getNodeType()) {
		case ASTNode.METHOD_INVOCATION:
		case ASTNode.SUPER_METHOD_INVOCATION:
			
		case ASTNode.CONSTRUCTOR_INVOCATION:
		case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
			
		case ASTNode.CLASS_INSTANCE_CREATION:
		case ASTNode.ENUM_CONSTANT_DECLARATION:
			return true;
			
		default:
			return false;
	}
}
 
Example 9
Source File: Invocations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static ChildListPropertyDescriptor getArgumentsProperty(ASTNode invocation) {
	switch (invocation.getNodeType()) {
		case ASTNode.METHOD_INVOCATION:
			return MethodInvocation.ARGUMENTS_PROPERTY;
		case ASTNode.SUPER_METHOD_INVOCATION:
			return SuperMethodInvocation.ARGUMENTS_PROPERTY;
			
		case ASTNode.CONSTRUCTOR_INVOCATION:
			return ConstructorInvocation.ARGUMENTS_PROPERTY;
		case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
			return SuperConstructorInvocation.ARGUMENTS_PROPERTY;
			
		case ASTNode.CLASS_INSTANCE_CREATION:
			return ClassInstanceCreation.ARGUMENTS_PROPERTY;
		case ASTNode.ENUM_CONSTANT_DECLARATION:
			return EnumConstantDeclaration.ARGUMENTS_PROPERTY;
			
		default:
			throw new IllegalArgumentException(invocation.toString());
	}
}
 
Example 10
Source File: Invocations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static List<Expression> getArguments(ASTNode invocation) {
	switch (invocation.getNodeType()) {
		case ASTNode.METHOD_INVOCATION:
			return ((MethodInvocation)invocation).arguments();
		case ASTNode.SUPER_METHOD_INVOCATION:
			return ((SuperMethodInvocation)invocation).arguments();
			
		case ASTNode.CONSTRUCTOR_INVOCATION:
			return ((ConstructorInvocation)invocation).arguments();
		case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
			return ((SuperConstructorInvocation)invocation).arguments();
			
		case ASTNode.CLASS_INSTANCE_CREATION:
			return ((ClassInstanceCreation)invocation).arguments();
		case ASTNode.ENUM_CONSTANT_DECLARATION:
			return ((EnumConstantDeclaration)invocation).arguments();
			
		default:
			throw new IllegalArgumentException(invocation.toString());
	}
}
 
Example 11
Source File: ExtractClassRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Expression getAssignedValue(ParameterObjectFactory pof, String parameterName, IJavaProject javaProject, RefactoringStatus status, ASTRewrite rewrite, ParameterInfo pi, boolean useSuper, ITypeBinding typeBinding, Expression qualifier, ASTNode replaceNode, ITypeRoot typeRoot) {
	AST ast= rewrite.getAST();
	boolean is50OrHigher= JavaModelUtil.is50OrHigher(javaProject);
	Expression assignedValue= handleSimpleNameAssignment(replaceNode, pof, parameterName, ast, javaProject, useSuper);
	if (assignedValue == null) {
		NullLiteral marker= qualifier == null ? null : ast.newNullLiteral();
		Expression fieldReadAccess= pof.createFieldReadAccess(pi, parameterName, ast, javaProject, useSuper, marker);
		assignedValue= GetterSetterUtil.getAssignedValue(replaceNode, rewrite, fieldReadAccess, typeBinding, is50OrHigher);
		boolean markerReplaced= replaceMarker(rewrite, qualifier, assignedValue, marker);
		if (markerReplaced) {
			switch (qualifier.getNodeType()) {
				case ASTNode.METHOD_INVOCATION:
				case ASTNode.CLASS_INSTANCE_CREATION:
				case ASTNode.SUPER_METHOD_INVOCATION:
				case ASTNode.PARENTHESIZED_EXPRESSION:
					status.addWarning(RefactoringCoreMessages.ExtractClassRefactoring_warning_semantic_change, JavaStatusContext.create(typeRoot, replaceNode));
					break;
			}
		}
	}
	return assignedValue;
}
 
Example 12
Source File: Invocations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static ListRewrite getInferredTypeArgumentsRewrite(ASTRewrite rewrite, Expression invocation) {
	switch (invocation.getNodeType()) {
		case ASTNode.METHOD_INVOCATION:
			return rewrite.getListRewrite(invocation, MethodInvocation.TYPE_ARGUMENTS_PROPERTY);
		case ASTNode.SUPER_METHOD_INVOCATION:
			return rewrite.getListRewrite(invocation, SuperMethodInvocation.TYPE_ARGUMENTS_PROPERTY);
		case ASTNode.CLASS_INSTANCE_CREATION:
			Type type= ((ClassInstanceCreation) invocation).getType();
			return rewrite.getListRewrite(type, ParameterizedType.TYPE_ARGUMENTS_PROPERTY);
			
		default:
			throw new IllegalArgumentException(invocation.toString());
	}
}
 
Example 13
Source File: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isIntroduceFactoryAvailable(final JavaTextSelection selection) throws JavaModelException {
	final IJavaElement[] elements= selection.resolveElementAtOffset();
	if (elements.length == 1 && elements[0] instanceof IMethod)
		return isIntroduceFactoryAvailable((IMethod) elements[0]);

	// there's no IMethod for the default constructor
	if (!Checks.isAvailable(selection.resolveEnclosingElement()))
		return false;
	ASTNode node= selection.resolveCoveringNode();
	if (node == null) {
		ASTNode[] selectedNodes= selection.resolveSelectedNodes();
		if (selectedNodes != null && selectedNodes.length == 1) {
			node= selectedNodes[0];
			if (node == null)
				return false;
		} else {
			return false;
		}
	}

	if (node.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION)
		return true;

	node= ASTNodes.getNormalizedNode(node);
	if (node.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY)
		return true;

	return false;
}
 
Example 14
Source File: APICallVisitor.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
public String getFullyQualifiedMethodNameFor(final Expression exp, final String parentClassName,
		final Table<ASTNode, String, String> variableScopeNameTypes) {
	String fqName;
	String name;
	if (exp.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) {
		name = "<init>";
		fqName = getNameOfType(((ClassInstanceCreation) exp).getType());
	} else { // MethodInvocation
		final MethodInvocation node = (MethodInvocation) exp;
		name = node.getName().toString();
		if (node.getExpression() == null) {
			if (methodImports.containsKey(name))
				fqName = methodImports.get(name);
			else if (methodReturnTypes.containsKey(parentClassName + "." + name))
				fqName = parentClassName; // local
			else if (wildcardMethodImports.isEmpty())
				fqName = currentPackage; // package local method
			else
				fqName = "UNRESOLVED";
		} else if (node.getExpression().getNodeType() == ASTNode.THIS_EXPRESSION) {
			if (methodReturnTypes.containsKey(parentClassName + "." + name))
				fqName = parentClassName; // local
			else // superclass method
				fqName = "SUPER";
		} else {
			fqName = getFQMethodClassFor(node.getExpression(), parentClassName, variableScopeNameTypes);
		}
	}
	if (fqName.equals("UNRESOLVED"))
		unresMethods.add(name);
	return fqName + "." + name;
}
 
Example 15
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 16
Source File: ExtractMethodAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void initReturnType(ImportRewrite rewriter) {
	AST ast= fEnclosingBodyDeclaration.getAST();
	fReturnType= null;
	fReturnTypeBinding= null;
	switch (fReturnKind) {
		case ACCESS_TO_LOCAL:
			VariableDeclaration declaration= ASTNodes.findVariableDeclaration(fReturnValue, fEnclosingBodyDeclaration);
			fReturnType= ASTNodeFactory.newType(ast, declaration, rewriter, new ContextSensitiveImportRewriteContext(declaration, rewriter));
			if (declaration.resolveBinding() != null) {
				fReturnTypeBinding= declaration.resolveBinding().getType();
			}
			break;
		case EXPRESSION:
			Expression expression= (Expression)getFirstSelectedNode();
			if (expression.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) {
				fExpressionBinding= ((ClassInstanceCreation)expression).getType().resolveBinding();
			} else {
				fExpressionBinding= expression.resolveTypeBinding();
			}
			if (fExpressionBinding != null) {
				if (fExpressionBinding.isNullType()) {
					getStatus().addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_null_type, JavaStatusContext.create(fCUnit, expression));
				} else {
					ITypeBinding normalizedBinding= Bindings.normalizeForDeclarationUse(fExpressionBinding, ast);
					if (normalizedBinding != null) {
						ImportRewriteContext context= new ContextSensitiveImportRewriteContext(fEnclosingBodyDeclaration, rewriter);
						fReturnType= rewriter.addImport(normalizedBinding, ast, context);
						fReturnTypeBinding= normalizedBinding;
					}
				}
			} else {
				fReturnType= ast.newPrimitiveType(PrimitiveType.VOID);
				fReturnTypeBinding= ast.resolveWellKnownType("void"); //$NON-NLS-1$
				getStatus().addError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_determine_return_type, JavaStatusContext.create(fCUnit, expression));
			}
			break;
		case RETURN_STATEMENT_VALUE:
			LambdaExpression enclosingLambdaExpr= ASTResolving.findEnclosingLambdaExpression(getFirstSelectedNode());
			if (enclosingLambdaExpr != null) {
				fReturnType= ASTNodeFactory.newReturnType(enclosingLambdaExpr, ast, rewriter, null);
				IMethodBinding methodBinding= enclosingLambdaExpr.resolveMethodBinding();
				fReturnTypeBinding= methodBinding != null ? methodBinding.getReturnType() : null;
			} else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
				fReturnType= ((MethodDeclaration) fEnclosingBodyDeclaration).getReturnType2();
				fReturnTypeBinding= fReturnType != null ? fReturnType.resolveBinding() : null;
			}
			break;
		default:
			fReturnType= ast.newPrimitiveType(PrimitiveType.VOID);
			fReturnTypeBinding= ast.resolveWellKnownType("void"); //$NON-NLS-1$
	}
	if (fReturnType == null) {
		fReturnType= ast.newPrimitiveType(PrimitiveType.VOID);
		fReturnTypeBinding= ast.resolveWellKnownType("void"); //$NON-NLS-1$
	}
}
 
Example 17
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 18
Source File: ExtractMethodAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private void initReturnType(ImportRewrite rewriter) {
	AST ast = fEnclosingBodyDeclaration.getAST();
	fReturnType = null;
	fReturnTypeBinding = null;
	switch (fReturnKind) {
		case ACCESS_TO_LOCAL:
			VariableDeclaration declaration = ASTNodes.findVariableDeclaration(fReturnValue, fEnclosingBodyDeclaration);
			fReturnType = ASTNodeFactory.newType(ast, declaration, rewriter, new ContextSensitiveImportRewriteContext(declaration, rewriter));
			if (declaration.resolveBinding() != null) {
				fReturnTypeBinding = declaration.resolveBinding().getType();
			}
			break;
		case EXPRESSION:
			Expression expression = (Expression) getFirstSelectedNode();
			if (expression.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) {
				fExpressionBinding = ((ClassInstanceCreation) expression).getType().resolveBinding();
			} else {
				fExpressionBinding = expression.resolveTypeBinding();
			}
			if (fExpressionBinding != null) {
				if (fExpressionBinding.isNullType()) {
					getStatus().addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_null_type, JavaStatusContext.create(fCUnit, expression));
				} else {
					ITypeBinding normalizedBinding = Bindings.normalizeForDeclarationUse(fExpressionBinding, ast);
					if (normalizedBinding != null) {
						ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fEnclosingBodyDeclaration, rewriter);
						fReturnType = rewriter.addImport(normalizedBinding, ast, context, TypeLocation.RETURN_TYPE);
						fReturnTypeBinding = normalizedBinding;
					}
				}
			} else {
				fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
				fReturnTypeBinding = ast.resolveWellKnownType("void"); //$NON-NLS-1$
				getStatus().addError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_determine_return_type, JavaStatusContext.create(fCUnit, expression));
			}
			break;
		case RETURN_STATEMENT_VALUE:
			LambdaExpression enclosingLambdaExpr = ASTResolving.findEnclosingLambdaExpression(getFirstSelectedNode());
			if (enclosingLambdaExpr != null) {
				fReturnType = ASTNodeFactory.newReturnType(enclosingLambdaExpr, ast, rewriter, null);
				IMethodBinding methodBinding = enclosingLambdaExpr.resolveMethodBinding();
				fReturnTypeBinding = methodBinding != null ? methodBinding.getReturnType() : null;
			} else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
				fReturnType = ((MethodDeclaration) fEnclosingBodyDeclaration).getReturnType2();
				fReturnTypeBinding = fReturnType != null ? fReturnType.resolveBinding() : null;
			}
			break;
		default:
			fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
			fReturnTypeBinding = ast.resolveWellKnownType("void"); //$NON-NLS-1$
	}
	if (fReturnType == null) {
		fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
		fReturnTypeBinding = ast.resolveWellKnownType("void"); //$NON-NLS-1$
	}
}