Java Code Examples for org.eclipse.jdt.internal.corext.dom.ASTNodes#getEnclosingType()

The following examples show how to use org.eclipse.jdt.internal.corext.dom.ASTNodes#getEnclosingType() . 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: ExtractMethodRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private void initializeDestinations() {
	List<ASTNode> result = new ArrayList<>();
	BodyDeclaration decl = fAnalyzer.getEnclosingBodyDeclaration();
	ASTNode current = ASTResolving.findParentType(decl.getParent());
	if (fAnalyzer.isValidDestination(current)) {
		result.add(current);
	}
	if (current != null && (decl instanceof MethodDeclaration || decl instanceof Initializer || decl instanceof FieldDeclaration)) {
		ITypeBinding binding = ASTNodes.getEnclosingType(current);
		ASTNode next = ASTResolving.findParentType(current.getParent());
		while (next != null && binding != null && binding.isNested()) {
			if (fAnalyzer.isValidDestination(next)) {
				result.add(next);
			}
			current = next;
			binding = ASTNodes.getEnclosingType(current);
			next = ASTResolving.findParentType(next.getParent());
		}
	}
	fDestinations = result.toArray(new ASTNode[result.size()]);
	fDestination = fDestinations[fDestinationIndex];
}
 
Example 2
Source File: ExtractMethodRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void initializeDestinations() {
	List<ASTNode> result= new ArrayList<ASTNode>();
	BodyDeclaration decl= fAnalyzer.getEnclosingBodyDeclaration();
	ASTNode current= ASTResolving.findParentType(decl.getParent());
	if (fAnalyzer.isValidDestination(current)) {
		result.add(current);
	}
	if (current != null && (decl instanceof MethodDeclaration || decl instanceof Initializer || decl instanceof FieldDeclaration)) {
		ITypeBinding binding= ASTNodes.getEnclosingType(current);
		ASTNode next= ASTResolving.findParentType(current.getParent());
		while (next != null && binding != null && binding.isNested()) {
			if (fAnalyzer.isValidDestination(next)) {
				result.add(next);
			}
			current= next;
			binding= ASTNodes.getEnclosingType(current);
			next= ASTResolving.findParentType(next.getParent());
		}
	}
	fDestinations= result.toArray(new ASTNode[result.size()]);
	fDestination= fDestinations[fDestinationIndex];
}
 
Example 3
Source File: ExtractMethodAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public void checkInput(RefactoringStatus status, String methodName, ASTNode destination) {
	ITypeBinding[] arguments = getArgumentTypes();
	ITypeBinding type = ASTNodes.getEnclosingType(destination);
	status.merge(Checks.checkMethodInType(type, methodName, arguments));
	ITypeBinding superClass = type.getSuperclass();
	if (superClass != null) {
		status.merge(Checks.checkMethodInHierarchy(superClass, methodName, null, arguments));
	}
	for (ITypeBinding superInterface : type.getInterfaces()) {
		status.merge(Checks.checkMethodInHierarchy(superInterface, methodName, null, arguments));
	}
}
 
Example 4
Source File: SourceProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private String getReceiver(CallContext context, int modifiers, ImportRewriteContext importRewriteContext) {
	String receiver= context.receiver;
	ITypeBinding invocationType= ASTNodes.getEnclosingType(context.invocation);
	ITypeBinding sourceType= fDeclaration.resolveBinding().getDeclaringClass();
	if (!context.receiverIsStatic && Modifier.isStatic(modifiers)) {
		if ("this".equals(receiver) && invocationType != null && Bindings.equals(invocationType, sourceType)) { //$NON-NLS-1$
			receiver= null;
		} else {
			receiver= context.importer.addImport(sourceType, importRewriteContext);
		}
	}
	return receiver;
}
 
Example 5
Source File: ExtractMethodAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void checkInput(RefactoringStatus status, String methodName, ASTNode destination) {
	ITypeBinding[] arguments= getArgumentTypes();
	ITypeBinding type= ASTNodes.getEnclosingType(destination);
	status.merge(Checks.checkMethodInType(type, methodName, arguments));
	ITypeBinding superClass= type.getSuperclass();
	if (superClass != null) {
		status.merge(Checks.checkMethodInHierarchy(superClass, methodName, null, arguments));
	}
	for (ITypeBinding superInterface : type.getInterfaces()) {
		status.merge(Checks.checkMethodInHierarchy(superInterface, methodName, null, arguments));
	}
}
 
Example 6
Source File: RefactorProposalUtility.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static String getEnclosingType(ASTNode declaration) {
	ASTNode node = declaration == null ? null : declaration.getParent();
	ITypeBinding type = ASTNodes.getEnclosingType(node);
	return type == null ? null : type.getQualifiedName();
}
 
Example 7
Source File: ExtractFieldRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private String getEnclosingTypeName() throws JavaModelException {
	ASTNode node = getEnclosingTypeDeclaration();
	ITypeBinding typeBinding = ASTNodes.getEnclosingType(node);
	return typeBinding == null ? "" : typeBinding.getName();
}
 
Example 8
Source File: ExtractMethodAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void endVisit(CompilationUnit node) {
	RefactoringStatus status= getStatus();
	superCall: {
		if (status.hasFatalError())
			break superCall;
		if (!hasSelectedNodes()) {
			ASTNode coveringNode= getLastCoveringNode();
			if (coveringNode instanceof Block && coveringNode.getParent() instanceof MethodDeclaration) {
				MethodDeclaration methodDecl= (MethodDeclaration)coveringNode.getParent();
				Message[] messages= ASTNodes.getMessages(methodDecl, ASTNodes.NODE_ONLY);
				if (messages.length > 0) {
					status.addFatalError(Messages.format(
						RefactoringCoreMessages.ExtractMethodAnalyzer_compile_errors,
						BasicElementLabels.getJavaElementName(methodDecl.getName().getIdentifier())), JavaStatusContext.create(fCUnit, methodDecl));
					break superCall;
				}
			}
			status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_invalid_selection);
			break superCall;
		}
		fEnclosingBodyDeclaration= (BodyDeclaration)ASTNodes.getParent(getFirstSelectedNode(), BodyDeclaration.class);
		if (fEnclosingBodyDeclaration == null ||
				(fEnclosingBodyDeclaration.getNodeType() != ASTNode.METHOD_DECLARATION &&
				 fEnclosingBodyDeclaration.getNodeType() != ASTNode.FIELD_DECLARATION &&
				 fEnclosingBodyDeclaration.getNodeType() != ASTNode.INITIALIZER)) {
			status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_invalid_selection);
			break superCall;
		} else if (ASTNodes.getEnclosingType(fEnclosingBodyDeclaration) == null) {
			status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_compile_errors_no_parent_binding);
			break superCall;
		} else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
			fEnclosingMethodBinding= ((MethodDeclaration)fEnclosingBodyDeclaration).resolveBinding();
		}
		if (!isSingleExpressionOrStatementSet()) {
			status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_single_expression_or_set);
			break superCall;
		}
		if (isExpressionSelected()) {
			ASTNode expression= getFirstSelectedNode();
			if (expression instanceof Name) {
				Name name= (Name)expression;
				if (name.resolveBinding() instanceof ITypeBinding) {
					status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_type_reference);
					break superCall;
				}
				if (name.resolveBinding() instanceof IMethodBinding) {
					status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_method_name_reference);
					break superCall;
				}
				if (name.resolveBinding() instanceof IVariableBinding) {
					StructuralPropertyDescriptor locationInParent= name.getLocationInParent();
					if (locationInParent == QualifiedName.NAME_PROPERTY || (locationInParent == FieldAccess.NAME_PROPERTY && !(((FieldAccess) name.getParent()).getExpression() instanceof ThisExpression)))  {
						status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_part_of_qualified_name);
						break superCall;
					}
				}
				if (name.isSimpleName() && ((SimpleName)name).isDeclaration()) {
					status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_name_in_declaration);
					break superCall;
				}
			}
			fForceStatic=
				ASTNodes.getParent(expression, ASTNode.SUPER_CONSTRUCTOR_INVOCATION) != null ||
				ASTNodes.getParent(expression, ASTNode.CONSTRUCTOR_INVOCATION) != null;
		}
		status.merge(LocalTypeAnalyzer.perform(fEnclosingBodyDeclaration, getSelection()));
		computeLastStatementSelected();
	}
	super.endVisit(node);
}
 
Example 9
Source File: IntroduceIndirectionRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Attempts to qualify a "this" expression for a method invocation with an appropriate qualifier.
 * The invoked method is analyzed according to the following specs:
 *
 * 'this' must be qualified iff method is declared in an enclosing type or a supertype of an enclosing type
 *
 * 1) The method is declared somewhere outside of the cu of the invocation
 *      1a) inside a supertype of the current type
 *      1b) inside a supertype of an enclosing type
 * 2) The method is declared inside of the cu of the invocation
 * 		2a) inside the type of the invocation
 * 		2b) outside the type of the invocation
 *
 * In case of 1a) and 2b), qualify with the enclosing type.
 * @param expr a {@link ThisExpression}
 * @param originalInvocation the original method invocation
 * @param enclosing the enclosing member of the original method invocation
 * @param unitRewriter the rewrite
 * @return resulting status
 *
 */
private RefactoringStatus qualifyThisExpression(ThisExpression expr, MethodInvocation originalInvocation, IMember enclosing, CompilationUnitRewrite unitRewriter) {

	RefactoringStatus status= new RefactoringStatus();

	IMethodBinding methodBinding= originalInvocation.resolveMethodBinding();
	MethodDeclaration methodDeclaration= (MethodDeclaration) ASTNodes.findDeclaration(methodBinding, originalInvocation.getRoot());

	ITypeBinding currentTypeBinding= null;
	if (methodDeclaration != null) {
		// Case 1) : Declaring type is inside this cu => use its name if it's declared in an enclosing type
		if (ASTNodes.isParent(originalInvocation, methodDeclaration.getParent()))
			currentTypeBinding= methodBinding.getDeclaringClass();
		else
			currentTypeBinding= ASTNodes.getEnclosingType(originalInvocation);
	} else {
		// Case 2) : Declaring type is outside of this cu => find subclass in this cu
		ASTNode currentTypeDeclaration= getEnclosingTypeDeclaration(originalInvocation);
		currentTypeBinding= ASTNodes.getEnclosingType(currentTypeDeclaration);
		while (currentTypeDeclaration != null && (Bindings.findMethodInHierarchy(currentTypeBinding, methodBinding.getName(), methodBinding.getParameterTypes()) == null)) {
			currentTypeDeclaration= getEnclosingTypeDeclaration(currentTypeDeclaration.getParent());
			currentTypeBinding= ASTNodes.getEnclosingType(currentTypeDeclaration);
		}
	}

	if (currentTypeBinding == null) {
		status.merge(createWarningAboutCall(enclosing, originalInvocation, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_declaring_type_not_found));
		return status;
	}

	currentTypeBinding= currentTypeBinding.getTypeDeclaration();

	ITypeBinding typeOfCall= ASTNodes.getEnclosingType(originalInvocation);
	if (!typeOfCall.equals(currentTypeBinding)) {
		if (currentTypeBinding.isAnonymous()) {
			// Cannot qualify, see bug 115277
			status.merge(createWarningAboutCall(enclosing, originalInvocation, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_anonymous_cannot_qualify));
		} else {
			expr.setQualifier(unitRewriter.getAST().newSimpleName(currentTypeBinding.getName()));
		}
	} else {
		// do not qualify, only use "this.".
	}

	return status;
}