org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor Java Examples

The following examples show how to use org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor. 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: CombinedJvmJdtRenameProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean initialize(final IRenameElementContext renameElementContext) {
	Assert.isLegal(renameElementContext instanceof CombinedJvmJdtRenameContext);
	if (!super.initialize(renameElementContext))
		return false;
	jvmElements2jdtProcessors = newHashMap();
	for (Map.Entry<URI, IJavaElement> jvm2javaElement : ((CombinedJvmJdtRenameContext) renameElementContext)
			.getJvm2JavaElements().entrySet()) {
		JavaRenameProcessor jdtRenameProcessor = jdtRefactoringFactory.createRenameProcessor(jvm2javaElement
				.getValue());
		if (jdtRenameProcessor != null) {
			jvmElements2jdtProcessors.put(jvm2javaElement.getKey(), jdtRenameProcessor);
		} else {
			return false;
		}
	}
	return true;
}
 
Example #2
Source File: AbstractXtendRenameRefactoringTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected ProcessorBasedRefactoring createXtendRenameRefactoring(final XtextEditor editor, final int offset,
		String newName) {
	IRenameElementContext renameElementContext = createRenameElementContext(editor, offset);
	ProcessorBasedRefactoring renameRefactoring = renameRefactoringProvider
			.getRenameRefactoring(renameElementContext);
	RefactoringProcessor processor = renameRefactoring.getProcessor();
	if (processor instanceof AbstractRenameProcessor)
		((AbstractRenameProcessor) processor).setNewName(newName);
	else if (processor instanceof JavaRenameProcessor)
		((JavaRenameProcessor) processor).setNewElementName(newName);
	return renameRefactoring;
}
 
Example #3
Source File: RenameSupport.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static void initialize(JavaRenameProcessor processor, String newName, int flags) {

		setNewName(processor, newName);
		if (processor instanceof IReferenceUpdating) {
			IReferenceUpdating reference= (IReferenceUpdating) processor;
			reference.setUpdateReferences(updateReferences(flags));
		}

		if (processor instanceof ITextUpdating) {
			ITextUpdating text= (ITextUpdating) processor;
			text.setUpdateTextualMatches(updateTextualMatches(flags));
		}
	}
 
Example #4
Source File: GWTRenameParticipant.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * If we're renaming types, the initialize(Object) method will expect a
 * RenameTypeProcessor, so if we happened to be invoked by the
 * RenameCompilationUnitProcessor (e.g., the user did an Alt-Shift-R directly
 * on the .java file in the Package Explorer), we need to return the
 * processor's child RenameTypeProcessor instead of the original processor.
 */
protected JavaRenameProcessor getRenameProcessor() {
  JavaRenameProcessor processor = (JavaRenameProcessor) getProcessor();
  if (processor instanceof RenameCompilationUnitProcessor) {
    return ((RenameCompilationUnitProcessor) processor).getRenameTypeProcessor();
  }

  return processor;
}
 
Example #5
Source File: JdtRenameRefactoringProcessorFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public JavaRenameProcessor createRenameProcessor(IJavaElement element) {
	try {
		switch (element.getElementType()) {
			case IJavaElement.TYPE:
				return new RenameTypeProcessor((IType) element);
			case IJavaElement.FIELD:
				if (((IField) element).getDeclaringType().isEnum())
					return new RenameEnumConstProcessor((IField) element);
				else
					return new RenameFieldProcessor((IField) element);
			case IJavaElement.METHOD:
				if(((IMethod)element).isConstructor()) 
					break;
				if (Flags.isStatic(((IMethod) element).getFlags()) || Flags.isPrivate(((IMethod) element).getFlags()))
					return new RenameNonVirtualMethodProcessor((IMethod) element);
				else
					return new RenameVirtualMethodProcessor((IMethod) element);
	        case IJavaElement.TYPE_PARAMETER:
	        	return new RenameTypeParameterProcessor((ITypeParameter)element);
	        case IJavaElement.LOCAL_VARIABLE:
	        	return new RenameLocalVariableProcessor((ILocalVariable)element);

		}
	} catch (JavaModelException exc) {
		LOG.error("Error creating refactoring processor for " + element.getElementName(), exc);
	}
	return null;
}
 
Example #6
Source File: ChangeCombiningRenameRefactoring.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void setProcessor(RefactoringProcessor processor) {
	if(processor instanceof CombinedJvmJdtRenameProcessor) {
		for(JavaRenameProcessor subProcessor: ((CombinedJvmJdtRenameProcessor) processor).getSubProcessors()) {
			// LTK's API made RefactoringProcessor.setRefactoring package private
			// such that we cannot override it in CombinedJvmJdtRenameProcessor. We have to set
			// the processors iteratively from the refactoring side !? 
			super.setProcessor(subProcessor);
		}
	}
	super.setProcessor(processor);
}
 
Example #7
Source File: CombinedJvmJdtRenameProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public RefactoringParticipant[] loadParticipants(final RefactoringStatus status,
		final SharableParticipants sharedParticipants) throws CoreException {
	List<RefactoringParticipant> participants = newArrayList(super.loadParticipants(status, sharedParticipants));
	for (JavaRenameProcessor processor : jvmElements2jdtProcessors.values()) {
		for (RefactoringParticipant participant : processor.loadParticipants(status, sharedParticipants)) {
			if(participant instanceof JdtRenameParticipant) {
				((JdtRenameParticipant) participant).disableFor(getElements());
			}
			participants.add(participant);
		}
	}
	return toArray(participants, RefactoringParticipant.class);
}
 
Example #8
Source File: CombinedJvmJdtRenameProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
	SubMonitor monitor = SubMonitor.convert(pm, jvmElements2jdtProcessors.size() + 1);
	CompositeChange compositeChange = new CompositeChange(getProcessorName());
	compositeChange.add(super.createChange(monitor.newChild(1)));
	for (JavaRenameProcessor processor : jvmElements2jdtProcessors.values()) {
		if (monitor.isCanceled()) {
			throw new OperationCanceledException();
		}
		compositeChange.add(processor.createChange(monitor.newChild(1)));
	}
	return compositeChange;
}
 
Example #9
Source File: CombinedJvmJdtRenameProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm, CheckConditionsContext context)
		throws CoreException, OperationCanceledException {
	SubMonitor monitor = SubMonitor.convert(pm, jvmElements2jdtProcessors.size() + 1);
	RefactoringStatus status = super.checkFinalConditions(monitor.newChild(1), context);
	ResourceSet resourceSet = getResourceSet(getRenameElementContext());
	getRenameArguments().getRenameStrategy().applyDeclarationChange(getNewName(), resourceSet);
	for (Iterator<Map.Entry<URI, JavaRenameProcessor>> entryIterator = jvmElements2jdtProcessors.entrySet().iterator();
			entryIterator.hasNext();) {
		Map.Entry<URI, JavaRenameProcessor> jvmElement2jdtRefactoring = entryIterator.next();
		if (monitor.isCanceled()) {
			throw new OperationCanceledException();
		}
		URI renamedJvmElementURI = getRenameArguments().getNewElementURI(jvmElement2jdtRefactoring.getKey());
		EObject renamedJvmElement = resourceSet.getEObject(renamedJvmElementURI, false);
		if (!(renamedJvmElement instanceof JvmIdentifiableElement) || renamedJvmElement.eIsProxy()) {
			status.addError("Cannot find inferred JVM element after refactoring.");
		} else {
			JavaRenameProcessor jdtRefactoring = jvmElement2jdtRefactoring.getValue();
			String newName = ((JvmIdentifiableElement) renamedJvmElement).getSimpleName();
			if(jdtRefactoring.getCurrentElementName().equals(newName)) {
				entryIterator.remove();
			} else {
				jdtRefactoring.setNewElementName(newName);
				status.merge(jdtRefactoring.checkFinalConditions(monitor.newChild(1), context));
			}
		}
	}
	getRenameArguments().getRenameStrategy().revertDeclarationChange(resourceSet);
	return status;
}
 
Example #10
Source File: CombinedJvmJdtRenameProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException,
		OperationCanceledException {
	SubMonitor monitor = SubMonitor.convert(pm, jvmElements2jdtProcessors.size() + 1);
	RefactoringStatus status = super.checkInitialConditions(monitor.newChild(1));
	for (JavaRenameProcessor processor : jvmElements2jdtProcessors.values()) {
		if (monitor.isCanceled()) {
			throw new OperationCanceledException();
		}
		status.merge(processor.checkInitialConditions(monitor.newChild(1)));
	}
	return status;
}
 
Example #11
Source File: CombinedJvmJdtRenameProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean isApplicable() throws CoreException {
	if (!super.isApplicable())
		return false;
	for (JavaRenameProcessor processor : jvmElements2jdtProcessors.values()) {
		if (!processor.isApplicable())
			return false;
	}
	return true;
}
 
Example #12
Source File: CombinedJvmJdtRenameProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Object[] getElements() {
	List<Object> elements = newArrayList(super.getElements());
	for (JavaRenameProcessor jdtProcessor : jvmElements2jdtProcessors.values()) {
		for (Object element : jdtProcessor.getElements())
			elements.add(element);
	}
	return toArray(elements, Object.class);
}
 
Example #13
Source File: CombinedJvmJdtRenameProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected Iterable<JavaRenameProcessor> getSubProcessors() {
	return jvmElements2jdtProcessors.values();
}
 
Example #14
Source File: GWTRenameParticipant.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected boolean initialize(Object element) {
  if (element instanceof IType || element instanceof IMethod
      || element instanceof IField) {
    GWTRefactoringSupport support = getRefactoringSupport();

    IMember oldMember = (IMember) element;
    support.setOldElement(oldMember);

    try {
      /*
       * We can't trust our RenameArgument's getNewName() to always return the
       * correct new name. When the user sets the new name in the Rename
       * refactoring dialog, clicks Next to go to the Preview page, then
       * clicks Back and changes the new name, getNewName() returns the
       * original name, not the updated one. This appears to be a bug in the
       * JDT, which affects other built-in rename participants such as
       * BreakpointRenameTypeParticipant. It does not affect the core JDT
       * refactorings, though, since they are executed directly from
       * JavaRenameRefactoring and not as loaded rename participants.
       * 
       * The workaround here is to instead go directly to the refactoring
       * processor and query it for the new element, which will have the right
       * name.
       * 
       * TODO: file this as a JDT bug?
       */
      JavaRenameProcessor processor = getRenameProcessor();
      IJavaElement newElement = (IJavaElement) processor.getNewElement();
      IType declaringType = oldMember.getDeclaringType();
      String newElementName = newElement.getElementName();
      /*
       * Compute the new method by using the declaring type of the old method.
       * Otherwise when a RenameVirtualMethodProcessor instance is passed in,
       * we will end up looking at the topmost declaration of the method
       * instead of the one we actually want because
       * RenameVirtualMethodProcessor.getNewElement() actually points to the
       * topmost declaration.
       */
      if (element instanceof IField) {
        newElement = declaringType.getField(newElementName);
      } else if (element instanceof IMethod) {
        IMethod method = (IMethod) newElement;
        newElement = declaringType.getMethod(newElementName,
            method.getParameterTypes());
      } else {
        assert (element instanceof IType);
      }

      support.setNewElement(newElement);
      support.setUpdateReferences(getArguments().getUpdateReferences());

      return true;
    } catch (CoreException e) {
      GWTPluginLog.logError(e);
    }
  }

  return false;
}
 
Example #15
Source File: RenameSupport.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private RenameSupport(JavaRenameProcessor processor, String newName, int flags) {
	fRefactoring= new RenameRefactoring(processor);
	initialize(processor, newName, flags);
}
 
Example #16
Source File: RenameSupport.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private JavaRenameProcessor getJavaRenameProcessor() {
	return (JavaRenameProcessor) fRefactoring.getProcessor();
}
 
Example #17
Source File: RenameSupport.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Creates a new rename support for the given {@link IJavaProject}.
 *
 * @param project the {@link IJavaProject} to be renamed.
 * @param newName the project's new name. <code>null</code> is a valid
 * value indicating that no new name is provided.
 * @param flags flags controlling additional parameters. Valid flags are
 * <code>UPDATE_REFERENCES</code> or <code>NONE</code>.
 * @return the {@link RenameSupport}.
 * @throws CoreException if an unexpected error occurred while creating
 * the {@link RenameSupport}.
 */
public static RenameSupport create(IJavaProject project, String newName, int flags) throws CoreException {
	JavaRenameProcessor processor= new RenameJavaProjectProcessor(project);
	return new RenameSupport(processor, newName, flags);
}
 
Example #18
Source File: RenameSupport.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Creates a new rename support for the given {@link IPackageFragmentRoot}.
 *
 * @param root the {@link IPackageFragmentRoot} to be renamed.
 * @param newName the package fragment root's new name. <code>null</code> is
 * a valid value indicating that no new name is provided.
 * @return the {@link RenameSupport}.
 * @throws CoreException if an unexpected error occurred while creating
 * the {@link RenameSupport}.
 */
public static RenameSupport create(IPackageFragmentRoot root, String newName) throws CoreException {
	JavaRenameProcessor processor= new RenameSourceFolderProcessor(root);
	return new RenameSupport(processor, newName, 0);
}
 
Example #19
Source File: RenameSupport.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Creates a new rename support for the given {@link IPackageFragment}.
 *
 * @param fragment the {@link IPackageFragment} to be renamed.
 * @param newName the package fragment's new name. <code>null</code> is a
 * valid value indicating that no new name is provided.
 * @param flags flags controlling additional parameters. Valid flags are
 * <code>UPDATE_REFERENCES</code>, and <code>UPDATE_TEXTUAL_MATCHES</code>,
 * or their bitwise OR, or <code>NONE</code>.
 * @return the {@link RenameSupport}.
 * @throws CoreException if an unexpected error occurred while creating
 * the {@link RenameSupport}.
 */
public static RenameSupport create(IPackageFragment fragment, String newName, int flags) throws CoreException {
	JavaRenameProcessor processor= new RenamePackageProcessor(fragment);
	return new RenameSupport(processor, newName, flags);
}
 
Example #20
Source File: RenameSupport.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Creates a new rename support for the given {@link ICompilationUnit}.
 *
 * @param unit the {@link ICompilationUnit} to be renamed.
 * @param newName the compilation unit's new name. <code>null</code> is a
 * valid value indicating that no new name is provided.
 * @param flags flags controlling additional parameters. Valid flags are
 * <code>UPDATE_REFERENCES</code>, and <code>UPDATE_TEXTUAL_MATCHES</code>,
 * or their bitwise OR, or <code>NONE</code>.
 * @return the {@link RenameSupport}.
 * @throws CoreException if an unexpected error occurred while creating
 * the {@link RenameSupport}.
 */
public static RenameSupport create(ICompilationUnit unit, String newName, int flags) throws CoreException {
	JavaRenameProcessor processor= new RenameCompilationUnitProcessor(unit);
	return new RenameSupport(processor, newName, flags);
}
 
Example #21
Source File: RenameSupport.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Creates a new rename support for the given {@link IType}.
 *
 * @param type the {@link IType} to be renamed.
 * @param newName the type's new name. <code>null</code> is a valid value
 * indicating that no new name is provided.
 * @param flags flags controlling additional parameters. Valid flags are
 * <code>UPDATE_REFERENCES</code>, and <code>UPDATE_TEXTUAL_MATCHES</code>,
 * or their bitwise OR, or <code>NONE</code>.
 * @return the {@link RenameSupport}.
 * @throws CoreException if an unexpected error occurred while creating
 * the {@link RenameSupport}.
 */
public static RenameSupport create(IType type, String newName, int flags) throws CoreException {
	JavaRenameProcessor processor= new RenameTypeProcessor(type);
	return new RenameSupport(processor, newName, flags);
}