Java Code Examples for org.eclipse.ltk.core.refactoring.RefactoringStatus#createErrorStatus()

The following examples show how to use org.eclipse.ltk.core.refactoring.RefactoringStatus#createErrorStatus() . 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: RenameTypeProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private RefactoringStatus checkTypesInPackage() throws CoreException {
	IType type= Checks.findTypeInPackage(fType.getPackageFragment(), getNewElementName());
	if (type == null || ! type.exists())
		return null;
	String msg= Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_exists,
			new String[] {getNewElementLabel(), JavaElementLabels.getElementLabel(fType.getPackageFragment(), JavaElementLabels.ALL_DEFAULT)});
	return RefactoringStatus.createErrorStatus(msg, JavaStatusContext.create(type));
}
 
Example 2
Source File: EcorePackageRenameStrategy.java    From sarl with Apache License 2.0 5 votes vote down vote up
/**
 * Validate the package name.
 *
 * @param newName the package name to be validated.
 * @return the status of the validation.
 */
public static RefactoringStatus validatePackageName(String newName) {
	if (!PACKAGE_NAME_PATTERN.matcher(newName).find()) {
		RefactoringStatus.createErrorStatus(MessageFormat.format(Messages.SARLJdtPackageRenameParticipant_0, newName));
	}
	return new RefactoringStatus();
}
 
Example 3
Source File: ReorgPolicyFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public RefactoringStatus verifyDestination(IReorgDestination destination) throws JavaModelException {
	if (destination instanceof JavaElementDestination) {
		return verifyDestination(((JavaElementDestination)destination).getJavaElement());
	} else if (destination instanceof ResourceDestination) {
		return verifyDestination(((ResourceDestination)destination).getResource());
	}

	return RefactoringStatus.createErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_invalidDestinationKind);
}
 
Example 4
Source File: IntroduceFactoryRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param methodName
 * @return a <code>RefactoringStatus</code> that identifies whether the
 * the name <code>newMethodName</code> is available to use as the name of
 * the new factory method within the factory-owner class (either a to-be-
 * created factory class or the constructor-owning class, depending on the
 * user options).
 */
private RefactoringStatus isUniqueMethodName(String methodName) {
	ITypeBinding declaringClass= fCtorBinding.getDeclaringClass();
	if (Bindings.findMethodInType(declaringClass, methodName, fCtorBinding.getParameterTypes()) != null) {
		String format= Messages.format(RefactoringCoreMessages.IntroduceFactory_duplicateMethodName, BasicElementLabels.getJavaElementName(methodName));
		return RefactoringStatus.createErrorStatus(format);
	}
		return new RefactoringStatus();
}
 
Example 5
Source File: IntroduceIndirectionRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param fullyQualifiedTypeName the fully qualified name of the intermediary method
 * @return status for type name. Use {@link #setIntermediaryMethodName(String)} to check for overridden methods.
 */
public RefactoringStatus setIntermediaryTypeName(String fullyQualifiedTypeName) {
	IType target= null;

	try {
		if (fullyQualifiedTypeName.length() == 0)
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_type_not_selected_error);

		// find type (now including secondaries)
		target= getProject().findType(fullyQualifiedTypeName, new NullProgressMonitor());
		if (target == null || !target.exists())
			return RefactoringStatus.createErrorStatus(Messages.format(RefactoringCoreMessages.IntroduceIndirectionRefactoring_type_does_not_exist_error, BasicElementLabels.getJavaElementName(fullyQualifiedTypeName)));
		if (target.isAnnotation())
			return RefactoringStatus.createErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_create_in_annotation);
		if (target.isInterface() && !(JavaModelUtil.is18OrHigher(target.getJavaProject()) && JavaModelUtil.is18OrHigher(getProject())))
			return RefactoringStatus.createErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_create_on_interface);
	} catch (JavaModelException e) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_unable_determine_declaring_type);
	}

	if (target.isReadOnly())
		return RefactoringStatus.createErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_create_in_readonly);

	if (target.isBinary())
		return RefactoringStatus.createErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_create_in_binary);

	fIntermediaryType= target;

	return new RefactoringStatus();
}
 
Example 6
Source File: RenameTypeProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private RefactoringStatus checkEnclosingTypes() {
	IType enclosingType= findEnclosingType(fType, getNewElementName());
	if (enclosingType == null)
		return null;

	String msg= Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_enclosed,
			new String[] { BasicElementLabels.getJavaElementName(fType.getFullyQualifiedName('.')), getNewElementLabel()});
	return RefactoringStatus.createErrorStatus(msg, JavaStatusContext.create(enclosingType));
}
 
Example 7
Source File: RenameTypeProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private RefactoringStatus checkEnclosedTypes() throws CoreException {
	IType enclosedType= findEnclosedType(fType, getNewElementName());
	if (enclosedType == null)
		return null;
	String msg= Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_encloses,
			new String[] { BasicElementLabels.getJavaElementName(fType.getFullyQualifiedName('.')), getNewElementLabel()});
	return RefactoringStatus.createErrorStatus(msg, JavaStatusContext.create(enclosedType));
}
 
Example 8
Source File: RenameTypeProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private RefactoringStatus checkTypesImportedInCu() throws CoreException {
	IImportDeclaration imp = getImportedType(fType.getCompilationUnit(), getNewElementName());

	if (imp == null) {
		return null;
	}

	String msg = Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_imported, new Object[] { getNewElementLabel(), BasicElementLabels.getPathLabel(fType.getCompilationUnit().getResource().getFullPath(), false) });
	IJavaElement grandParent = imp.getParent().getParent();
	if (grandParent instanceof ICompilationUnit) {
		return RefactoringStatus.createErrorStatus(msg, JavaStatusContext.create(imp));
	}

	return null;
}
 
Example 9
Source File: RenameTypeProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private RefactoringStatus checkTypesImportedInCu() throws CoreException {
	IImportDeclaration imp= getImportedType(fType.getCompilationUnit(), getNewElementName());

	if (imp == null)
		return null;

	String msg= Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_imported,
										new Object[]{getNewElementLabel(), BasicElementLabels.getPathLabel(fType.getCompilationUnit().getResource().getFullPath(), false)});
	IJavaElement grandParent= imp.getParent().getParent();
	if (grandParent instanceof ICompilationUnit)
		return RefactoringStatus.createErrorStatus(msg, JavaStatusContext.create(imp));

	return null;
}
 
Example 10
Source File: RenameJavaProjectProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected RefactoringStatus doCheckFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException {
	pm.beginTask("", 1); //$NON-NLS-1$
	try{
		if (isReadOnly()){
			String message= Messages.format(RefactoringCoreMessages.RenameJavaProjectRefactoring_read_only,
					BasicElementLabels.getJavaElementName(fProject.getElementName()));
			return RefactoringStatus.createErrorStatus(message);
		}
		return new RefactoringStatus();
	} finally{
		pm.done();
	}
}
 
Example 11
Source File: MoveInnerToTopRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private RefactoringStatus checkTypeNameInPackage() throws JavaModelException {
	IType type= Checks.findTypeInPackage(fType.getPackageFragment(), fType.getElementName());
	if (type == null || !type.exists() || fType.equals(type)) {
		return null;
	}
	String message= Messages.format(RefactoringCoreMessages.MoveInnerToTopRefactoring_type_exists, new String[] { BasicElementLabels.getJavaElementName(fType.getElementName()), JavaElementLabels.getElementLabel(fType.getPackageFragment(), JavaElementLabels.ALL_DEFAULT)});
	return RefactoringStatus.createErrorStatus(message);
}
 
Example 12
Source File: ReorgPolicyFactory.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public RefactoringStatus verifyDestination(IReorgDestination destination) throws JavaModelException {
	if (destination instanceof JavaElementDestination) {
		return verifyDestination(((JavaElementDestination)destination).getJavaElement());
	} else if (destination instanceof ResourceDestination) {
		return verifyDestination(((ResourceDestination)destination).getResource());
	}

	return RefactoringStatus.createErrorStatus(RefactoringCoreMessages.ReorgPolicyFactory_invalidDestinationKind);
}
 
Example 13
Source File: RenameTypeProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private RefactoringStatus checkEnclosingTypes() {
	IType enclosingType = findEnclosingType(fType, getNewElementName());
	if (enclosingType == null) {
		return null;
	}

	String msg = Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_enclosed, new String[] { BasicElementLabels.getJavaElementName(fType.getFullyQualifiedName('.')), getNewElementLabel() });
	return RefactoringStatus.createErrorStatus(msg, JavaStatusContext.create(enclosingType));
}
 
Example 14
Source File: RenameTypeProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private RefactoringStatus checkEnclosedTypes() throws CoreException {
	IType enclosedType = findEnclosedType(fType, getNewElementName());
	if (enclosedType == null) {
		return null;
	}
	String msg = Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_encloses, new String[] { BasicElementLabels.getJavaElementName(fType.getFullyQualifiedName('.')), getNewElementLabel() });
	return RefactoringStatus.createErrorStatus(msg, JavaStatusContext.create(enclosedType));
}
 
Example 15
Source File: RenameTypeProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private RefactoringStatus checkTypesInPackage() throws CoreException {
	IType type = Checks.findTypeInPackage(fType.getPackageFragment(), getNewElementName());
	if (type == null || !type.exists()) {
		return null;
	}
	String msg = Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_exists, new String[] { getNewElementLabel(), JavaElementLabels.getElementLabel(fType.getPackageFragment(), JavaElementLabels.ALL_DEFAULT) });
	return RefactoringStatus.createErrorStatus(msg, JavaStatusContext.create(type));
}
 
Example 16
Source File: IntroduceIndirectionRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private RefactoringStatus adjustVisibility(IMember whoToAdjust, ModifierKeyword neededVisibility, boolean alsoIncreaseEnclosing, IProgressMonitor monitor) throws CoreException {

		Map<IMember, IncomingMemberVisibilityAdjustment> adjustments;
		if (isRewriteKept(whoToAdjust.getCompilationUnit()))
			adjustments= fIntermediaryAdjustments;
		else
			adjustments= new HashMap<IMember, IncomingMemberVisibilityAdjustment>();

		int existingAdjustments= adjustments.size();
		addAdjustment(whoToAdjust, neededVisibility, adjustments);

		if (alsoIncreaseEnclosing)
			while (whoToAdjust.getDeclaringType() != null) {
				whoToAdjust= whoToAdjust.getDeclaringType();
				addAdjustment(whoToAdjust, neededVisibility, adjustments);
			}

		boolean hasNewAdjustments= (adjustments.size() - existingAdjustments) > 0;
		if (hasNewAdjustments && ( (whoToAdjust.isReadOnly() || whoToAdjust.isBinary())))
			return RefactoringStatus.createErrorStatus(Messages.format(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_update_binary_target_visibility, new String[] { JavaElementLabels
					.getElementLabel(whoToAdjust, JavaElementLabels.ALL_DEFAULT) }), JavaStatusContext.create(whoToAdjust));

		RefactoringStatus status= new RefactoringStatus();

		// Don't create a rewrite if it is not necessary
		if (!hasNewAdjustments)
			return status;

		try {
			monitor.beginTask(RefactoringCoreMessages.MemberVisibilityAdjustor_adjusting, 2);
			Map<ICompilationUnit, CompilationUnitRewrite> rewrites;
			if (!isRewriteKept(whoToAdjust.getCompilationUnit())) {
				CompilationUnitRewrite rewrite= new CompilationUnitRewrite(whoToAdjust.getCompilationUnit());
				rewrite.setResolveBindings(false);
				rewrites= new HashMap<ICompilationUnit, CompilationUnitRewrite>();
				rewrites.put(whoToAdjust.getCompilationUnit(), rewrite);
				status.merge(rewriteVisibility(adjustments, rewrites, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)));
				rewrite.attachChange((CompilationUnitChange) fTextChangeManager.get(whoToAdjust.getCompilationUnit()), true, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
			}
		} finally {
			monitor.done();
		}
		return status;
	}
 
Example 17
Source File: TypeContextChecker.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private ITypeBinding[] resolveBindings(String[] types, RefactoringStatus[] results, boolean firstPass) throws CoreException {
	//TODO: split types into parameterTypes and returnType
	int parameterCount= types.length - 1;
	ITypeBinding[] typeBindings= new ITypeBinding[types.length];

	StringBuffer cuString= new StringBuffer();
	cuString.append(fStubTypeContext.getBeforeString());
	int offsetBeforeMethodName= appendMethodDeclaration(cuString, types, parameterCount);
	cuString.append(fStubTypeContext.getAfterString());

	// need a working copy to tell the parser where to resolve (package visible) types
	ICompilationUnit wc= fMethod.getCompilationUnit().getWorkingCopy(new WorkingCopyOwner() {/*subclass*/}, new NullProgressMonitor());
	try {
		wc.getBuffer().setContents(cuString.toString());
		CompilationUnit compilationUnit= new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(wc, true);
		ASTNode method= NodeFinder.perform(compilationUnit, offsetBeforeMethodName, METHOD_NAME.length()).getParent();
		Type[] typeNodes= new Type[types.length];
		if (method instanceof MethodDeclaration) {
			MethodDeclaration methodDeclaration= (MethodDeclaration) method;
			typeNodes[parameterCount]= methodDeclaration.getReturnType2();
			List<SingleVariableDeclaration> parameters= methodDeclaration.parameters();
			for (int i= 0; i < parameterCount; i++)
				typeNodes[i]= parameters.get(i).getType();

		} else if (method instanceof AnnotationTypeMemberDeclaration) {
			typeNodes[0]= ((AnnotationTypeMemberDeclaration) method).getType();
		}

		for (int i= 0; i < types.length; i++) {
			Type type= typeNodes[i];
			if (type == null) {
				String msg= Messages.format(RefactoringCoreMessages.TypeContextChecker_couldNotResolveType, BasicElementLabels.getJavaElementName(types[i]));
				results[i]= RefactoringStatus.createErrorStatus(msg);
				continue;
			}
			results[i]= new RefactoringStatus();
			IProblem[] problems= ASTNodes.getProblems(type, ASTNodes.NODE_ONLY, ASTNodes.PROBLEMS);
			if (problems.length > 0) {
				for (int p= 0; p < problems.length; p++)
					if (isError(problems[p], type))
						results[i].addError(problems[p].getMessage());
			}
			ITypeBinding binding= handleBug84585(type.resolveBinding());
			if (firstPass && (binding == null || binding.isRecovered())) {
				types[i]= qualifyTypes(type, results[i]);
			}
			typeBindings[i]= binding;
		}
		return typeBindings;
	} finally {
		wc.discardWorkingCopy();
	}
}
 
Example 18
Source File: ExtractConstantRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * This method performs checks on the constant name which are quick enough to be
 * performed every time the ui input component contents are changed.
 *
 * @return return the resulting status
 * @throws JavaModelException
 *             thrown when the operation could not be executed
 */
public RefactoringStatus checkConstantNameOnChange() throws JavaModelException {
	if (Arrays.asList(getExcludedVariableNames()).contains(fConstantName)) {
		return RefactoringStatus.createErrorStatus(Messages.format(RefactoringCoreMessages.ExtractConstantRefactoring_another_variable, BasicElementLabels.getJavaElementName(getConstantName())));
	}
	return Checks.checkConstantName(fConstantName, fCu);
}
 
Example 19
Source File: ExtractConstantRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * This method performs checks on the constant name which are
 * quick enough to be performed every time the ui input component
 * contents are changed.
 *
 * @return return the resulting status
 * @throws JavaModelException thrown when the operation could not be executed
 */
public RefactoringStatus checkConstantNameOnChange() throws JavaModelException {
	if (Arrays.asList(getExcludedVariableNames()).contains(fConstantName))
		return RefactoringStatus.createErrorStatus(Messages.format(RefactoringCoreMessages.ExtractConstantRefactoring_another_variable, BasicElementLabels.getJavaElementName(getConstantName())));
	return Checks.checkConstantName(fConstantName, fCu);
}