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

The following examples show how to use org.eclipse.jdt.internal.corext.dom.ASTNodes#getMessages() . 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: CodeRefactoringUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static RefactoringStatus checkMethodSyntaxErrors(int selectionStart, int selectionLength, CompilationUnit cuNode, String invalidSelectionMessage) {
	SelectionAnalyzer analyzer = new SelectionAnalyzer(Selection.createFromStartLength(selectionStart, selectionLength), true);
	cuNode.accept(analyzer);
	ASTNode coveringNode = analyzer.getLastCoveringNode();
	if (!(coveringNode instanceof Block) || !(coveringNode.getParent() instanceof MethodDeclaration)) {
		return RefactoringStatus.createFatalErrorStatus(invalidSelectionMessage);
	}
	if (ASTNodes.getMessages(coveringNode, ASTNodes.NODE_ONLY).length == 0) {
		return RefactoringStatus.createFatalErrorStatus(invalidSelectionMessage);
	}

	MethodDeclaration methodDecl = (MethodDeclaration) coveringNode.getParent();
	String message = Messages.format(RefactoringCoreMessages.CodeRefactoringUtil_error_message, BasicElementLabels.getJavaElementName(methodDecl.getName().getIdentifier()));
	return RefactoringStatus.createFatalErrorStatus(message);
}
 
Example 2
Source File: SelfEncapsulateFieldRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private boolean processCompilerError(RefactoringStatus result, ASTNode node) {
	Message[] messages = ASTNodes.getMessages(node, ASTNodes.INCLUDE_ALL_PARENTS);
	if (messages.length == 0) {
		return false;
	}
	result.addFatalError(Messages.format(RefactoringCoreMessages.SelfEncapsulateField_compiler_errors_field, new String[] { BasicElementLabels.getJavaElementName(fField.getElementName()), messages[0].getMessage() }));
	return true;
}
 
Example 3
Source File: IntroduceParameterObjectProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean processCompilerError(RefactoringStatus result, ASTNode node) {
	Message[] messages= ASTNodes.getMessages(node, ASTNodes.INCLUDE_ALL_PARENTS);
	if (messages.length == 0)
		return false;
	result.addFatalError(Messages.format(RefactoringCoreMessages.IntroduceParameterObjectRefactoring_cannotanalysemethod_compilererror,
			new String[] { messages[0].getMessage() }));
	return true;
}
 
Example 4
Source File: CodeRefactoringUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static RefactoringStatus checkMethodSyntaxErrors(int selectionStart, int selectionLength, CompilationUnit cuNode, String invalidSelectionMessage){
	SelectionAnalyzer analyzer= new SelectionAnalyzer(Selection.createFromStartLength(selectionStart, selectionLength), true);
	cuNode.accept(analyzer);
	ASTNode coveringNode= analyzer.getLastCoveringNode();
	if (! (coveringNode instanceof Block) || ! (coveringNode.getParent() instanceof MethodDeclaration))
		return RefactoringStatus.createFatalErrorStatus(invalidSelectionMessage);
	if (ASTNodes.getMessages(coveringNode, ASTNodes.NODE_ONLY).length == 0)
		return RefactoringStatus.createFatalErrorStatus(invalidSelectionMessage);

	MethodDeclaration methodDecl= (MethodDeclaration)coveringNode.getParent();
	String message= Messages.format(RefactoringCoreMessages.CodeRefactoringUtil_error_message, BasicElementLabels.getJavaElementName(methodDecl.getName().getIdentifier()));
	return RefactoringStatus.createFatalErrorStatus(message);
}
 
Example 5
Source File: SelfEncapsulateFieldRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean processCompilerError(RefactoringStatus result, ASTNode node) {
	Message[] messages= ASTNodes.getMessages(node, ASTNodes.INCLUDE_ALL_PARENTS);
	if (messages.length == 0)
		return false;
	result.addFatalError(Messages.format(
		RefactoringCoreMessages.SelfEncapsulateField_compiler_errors_field,
		new String[] { BasicElementLabels.getJavaElementName(fField.getElementName()), messages[0].getMessage()}));
	return true;
}
 
Example 6
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);
}