Search in sources :

Example 71 with IJavaProject

use of org.eclipse.jdt.core.IJavaProject in project che by eclipse.

the class IntroduceFactoryRefactoring method createChange.

/* (non-Javadoc)
	 * @see org.eclipse.ltk.core.refactoring.Refactoring#createChange(org.eclipse.core.runtime.IProgressMonitor)
	 */
@Override
public Change createChange(IProgressMonitor pm) throws CoreException {
    try {
        pm.beginTask(RefactoringCoreMessages.IntroduceFactory_createChanges, fAllCallsTo.length);
        final ITypeBinding binding = fFactoryOwningClass.resolveBinding();
        final Map<String, String> arguments = new HashMap<String, String>();
        String project = null;
        IJavaProject javaProject = fCUHandle.getJavaProject();
        if (javaProject != null)
            project = javaProject.getElementName();
        int flags = JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING | RefactoringDescriptor.STRUCTURAL_CHANGE | RefactoringDescriptor.MULTI_CHANGE;
        if (binding.isNested() && !binding.isMember())
            flags |= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
        final String description = Messages.format(RefactoringCoreMessages.IntroduceFactoryRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(fCtorOwningClass.getName().getIdentifier()));
        final String header = Messages.format(RefactoringCoreMessages.IntroduceFactory_descriptor_description, new String[] { BasicElementLabels.getJavaElementName(fNewMethodName), BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED), BindingLabelProvider.getBindingLabel(fCtorBinding, JavaElementLabels.ALL_FULLY_QUALIFIED) });
        final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(project, this, header);
        comment.addSetting(Messages.format(RefactoringCoreMessages.IntroduceFactoryRefactoring_original_pattern, BindingLabelProvider.getBindingLabel(fCtorBinding, JavaElementLabels.ALL_FULLY_QUALIFIED)));
        comment.addSetting(Messages.format(RefactoringCoreMessages.IntroduceFactoryRefactoring_factory_pattern, BasicElementLabels.getJavaElementName(fNewMethodName)));
        comment.addSetting(Messages.format(RefactoringCoreMessages.IntroduceFactoryRefactoring_owner_pattern, BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED)));
        if (fProtectConstructor)
            comment.addSetting(RefactoringCoreMessages.IntroduceFactoryRefactoring_declare_private);
        final IntroduceFactoryDescriptor descriptor = RefactoringSignatureDescriptorFactory.createIntroduceFactoryDescriptor(project, description, comment.asString(), arguments, flags);
        arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fCUHandle));
        arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME, fNewMethodName);
        arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + 1, JavaRefactoringDescriptorUtil.elementToHandle(project, binding.getJavaElement()));
        //$NON-NLS-1$
        arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION, new Integer(fSelectionStart).toString() + " " + new Integer(fSelectionLength).toString());
        arguments.put(ATTRIBUTE_PROTECT, Boolean.valueOf(fProtectConstructor).toString());
        final DynamicValidationStateChange result = new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.IntroduceFactory_name);
        boolean hitInFactoryClass = false;
        boolean hitInCtorClass = false;
        for (int i = 0; i < fAllCallsTo.length; i++) {
            SearchResultGroup rg = fAllCallsTo[i];
            ICompilationUnit unitHandle = rg.getCompilationUnit();
            CompilationUnitChange cuChange = new CompilationUnitChange(getName(), unitHandle);
            if (addAllChangesFor(rg, unitHandle, cuChange))
                result.add(cuChange);
            if (unitHandle.equals(fFactoryUnitHandle))
                hitInFactoryClass = true;
            if (unitHandle.equals(ASTCreator.getCu(fCtorOwningClass)))
                hitInCtorClass = true;
            pm.worked(1);
            if (pm.isCanceled())
                throw new OperationCanceledException();
        }
        if (!hitInFactoryClass) {
            // Handle factory class if no search hits there
            CompilationUnitChange cuChange = new CompilationUnitChange(getName(), fFactoryUnitHandle);
            addAllChangesFor(null, fFactoryUnitHandle, cuChange);
            result.add(cuChange);
        }
        if (!hitInCtorClass && !fFactoryUnitHandle.equals(ASTCreator.getCu(fCtorOwningClass))) {
            // Handle constructor-owning class if no search hits there
            CompilationUnitChange cuChange = new CompilationUnitChange(getName(), ASTCreator.getCu(fCtorOwningClass));
            addAllChangesFor(null, ASTCreator.getCu(fCtorOwningClass), cuChange);
            result.add(cuChange);
        }
        return result;
    } finally {
        pm.done();
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) HashMap(java.util.HashMap) DynamicValidationRefactoringChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) DynamicValidationStateChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment) IntroduceFactoryDescriptor(org.eclipse.jdt.core.refactoring.descriptors.IntroduceFactoryDescriptor) IJavaProject(org.eclipse.jdt.core.IJavaProject) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 72 with IJavaProject

use of org.eclipse.jdt.core.IJavaProject in project che by eclipse.

the class ExtractMethodRefactoring method getRefactoringDescriptor.

private ExtractMethodDescriptor getRefactoringDescriptor() {
    final Map<String, String> arguments = new HashMap<String, String>();
    String project = null;
    IJavaProject javaProject = fCUnit.getJavaProject();
    if (javaProject != null)
        project = javaProject.getElementName();
    ITypeBinding type = null;
    if (fDestination instanceof AbstractTypeDeclaration) {
        final AbstractTypeDeclaration decl = (AbstractTypeDeclaration) fDestination;
        type = decl.resolveBinding();
    } else if (fDestination instanceof AnonymousClassDeclaration) {
        final AnonymousClassDeclaration decl = (AnonymousClassDeclaration) fDestination;
        type = decl.resolveBinding();
    }
    IMethodBinding method = null;
    final BodyDeclaration enclosing = fAnalyzer.getEnclosingBodyDeclaration();
    if (enclosing instanceof MethodDeclaration) {
        final MethodDeclaration node = (MethodDeclaration) enclosing;
        method = node.resolveBinding();
    }
    final int flags = RefactoringDescriptor.STRUCTURAL_CHANGE | JavaRefactoringDescriptor.JAR_REFACTORING | JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
    final String description = Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(fMethodName));
    final String label = method != null ? BindingLabelProvider.getBindingLabel(method, JavaElementLabels.ALL_FULLY_QUALIFIED) : '{' + JavaElementLabels.ELLIPSIS_STRING + '}';
    final String header = Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_descriptor_description, new String[] { BasicElementLabels.getJavaElementName(getSignature()), label, BindingLabelProvider.getBindingLabel(type, JavaElementLabels.ALL_FULLY_QUALIFIED) });
    final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(project, this, header);
    comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_name_pattern, BasicElementLabels.getJavaElementName(fMethodName)));
    comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_destination_pattern, BindingLabelProvider.getBindingLabel(type, JavaElementLabels.ALL_FULLY_QUALIFIED)));
    String visibility = JdtFlags.getVisibilityString(fVisibility);
    if (//$NON-NLS-1$
    "".equals(visibility))
        visibility = RefactoringCoreMessages.ExtractMethodRefactoring_default_visibility;
    comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_visibility_pattern, visibility));
    if (fThrowRuntimeExceptions)
        comment.addSetting(RefactoringCoreMessages.ExtractMethodRefactoring_declare_thrown_exceptions);
    if (fReplaceDuplicates)
        comment.addSetting(RefactoringCoreMessages.ExtractMethodRefactoring_replace_occurrences);
    if (fGenerateJavadoc)
        comment.addSetting(RefactoringCoreMessages.ExtractMethodRefactoring_generate_comment);
    final ExtractMethodDescriptor descriptor = RefactoringSignatureDescriptorFactory.createExtractMethodDescriptor(project, description, comment.asString(), arguments, flags);
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fCUnit));
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME, fMethodName);
    //$NON-NLS-1$
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION, new Integer(fSelectionStart).toString() + " " + new Integer(fSelectionLength).toString());
    arguments.put(ATTRIBUTE_VISIBILITY, new Integer(fVisibility).toString());
    arguments.put(ATTRIBUTE_DESTINATION, new Integer(fDestinationIndex).toString());
    arguments.put(ATTRIBUTE_EXCEPTIONS, Boolean.valueOf(fThrowRuntimeExceptions).toString());
    arguments.put(ATTRIBUTE_COMMENTS, Boolean.valueOf(fGenerateJavadoc).toString());
    arguments.put(ATTRIBUTE_REPLACE, Boolean.valueOf(fReplaceDuplicates).toString());
    return descriptor;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) HashMap(java.util.HashMap) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment) ExtractMethodDescriptor(org.eclipse.jdt.core.refactoring.descriptors.ExtractMethodDescriptor) IJavaProject(org.eclipse.jdt.core.IJavaProject) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 73 with IJavaProject

use of org.eclipse.jdt.core.IJavaProject in project che by eclipse.

the class IntroduceIndirectionRefactoring method createChange.

// ******************** CHANGE CREATION ***********************
@Override
public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    final Map<String, String> arguments = new HashMap<String, String>();
    String project = null;
    IJavaProject javaProject = fTargetMethod.getJavaProject();
    if (javaProject != null)
        project = javaProject.getElementName();
    int flags = JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING | RefactoringDescriptor.STRUCTURAL_CHANGE | RefactoringDescriptor.MULTI_CHANGE;
    final IType declaring = fTargetMethod.getDeclaringType();
    try {
        if (declaring.isLocal() || declaring.isAnonymous())
            flags |= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
    } catch (JavaModelException exception) {
        JavaPlugin.log(exception);
    }
    final String description = Messages.format(RefactoringCoreMessages.IntroduceIndirectionRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(fTargetMethod.getElementName()));
    final String header = Messages.format(RefactoringCoreMessages.IntroduceIndirectionRefactoring_descriptor_description, new String[] { JavaElementLabels.getTextLabel(fTargetMethod, JavaElementLabels.ALL_FULLY_QUALIFIED), JavaElementLabels.getTextLabel(declaring, JavaElementLabels.ALL_FULLY_QUALIFIED) });
    final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(project, this, header);
    comment.addSetting(Messages.format(RefactoringCoreMessages.IntroduceIndirectionRefactoring_original_pattern, JavaElementLabels.getTextLabel(fTargetMethod, JavaElementLabels.ALL_FULLY_QUALIFIED)));
    comment.addSetting(Messages.format(RefactoringCoreMessages.IntroduceIndirectionRefactoring_method_pattern, BasicElementLabels.getJavaElementName(fIntermediaryMethodName)));
    comment.addSetting(Messages.format(RefactoringCoreMessages.IntroduceIndirectionRefactoring_declaring_pattern, JavaElementLabels.getTextLabel(fIntermediaryType, JavaElementLabels.ALL_FULLY_QUALIFIED)));
    if (fUpdateReferences)
        comment.addSetting(RefactoringCoreMessages.JavaRefactoringDescriptor_update_references);
    final IntroduceIndirectionDescriptor descriptor = RefactoringSignatureDescriptorFactory.createIntroduceIndirectionDescriptor(project, description, comment.asString(), arguments, flags);
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fTargetMethod));
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME, fIntermediaryMethodName);
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + 1, JavaRefactoringDescriptorUtil.elementToHandle(project, fIntermediaryType));
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_REFERENCES, Boolean.valueOf(fUpdateReferences).toString());
    return new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.IntroduceIndirectionRefactoring_introduce_indirection, fTextChangeManager.getAllChanges());
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IntroduceIndirectionDescriptor(org.eclipse.jdt.core.refactoring.descriptors.IntroduceIndirectionDescriptor) IJavaProject(org.eclipse.jdt.core.IJavaProject) HashMap(java.util.HashMap) DynamicValidationRefactoringChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange) IType(org.eclipse.jdt.core.IType) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment)

Example 74 with IJavaProject

use of org.eclipse.jdt.core.IJavaProject in project che by eclipse.

the class ExtractConstantRefactoring method createRefactoringDescriptor.

private ExtractConstantDescriptor createRefactoringDescriptor() {
    final Map<String, String> arguments = new HashMap<String, String>();
    String project = null;
    IJavaProject javaProject = fCu.getJavaProject();
    if (javaProject != null)
        project = javaProject.getElementName();
    int flags = JavaRefactoringDescriptor.JAR_REFACTORING | JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
    if (JdtFlags.getVisibilityCode(fVisibility) != Modifier.PRIVATE)
        flags |= RefactoringDescriptor.STRUCTURAL_CHANGE;
    final String expression = ASTNodes.asString(fSelectedExpression.getAssociatedExpression());
    final String description = Messages.format(RefactoringCoreMessages.ExtractConstantRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(fConstantName));
    final String header = Messages.format(RefactoringCoreMessages.ExtractConstantRefactoring_descriptor_description, new String[] { BasicElementLabels.getJavaElementName(fConstantName), BasicElementLabels.getJavaCodeString(expression) });
    final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(project, this, header);
    comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractConstantRefactoring_constant_name_pattern, BasicElementLabels.getJavaElementName(fConstantName)));
    comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractConstantRefactoring_constant_expression_pattern, BasicElementLabels.getJavaCodeString(expression)));
    String visibility = fVisibility;
    if (//$NON-NLS-1$
    "".equals(visibility))
        visibility = RefactoringCoreMessages.ExtractConstantRefactoring_default_visibility;
    comment.addSetting(Messages.format(RefactoringCoreMessages.ExtractConstantRefactoring_visibility_pattern, visibility));
    if (fReplaceAllOccurrences)
        comment.addSetting(RefactoringCoreMessages.ExtractConstantRefactoring_replace_occurrences);
    if (fQualifyReferencesWithDeclaringClassName)
        comment.addSetting(RefactoringCoreMessages.ExtractConstantRefactoring_qualify_references);
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fCu));
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME, fConstantName);
    //$NON-NLS-1$
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION, new Integer(fSelectionStart).toString() + " " + new Integer(fSelectionLength).toString());
    arguments.put(ATTRIBUTE_REPLACE, Boolean.valueOf(fReplaceAllOccurrences).toString());
    arguments.put(ATTRIBUTE_QUALIFY, Boolean.valueOf(fQualifyReferencesWithDeclaringClassName).toString());
    arguments.put(ATTRIBUTE_VISIBILITY, new Integer(JdtFlags.getVisibilityCode(fVisibility)).toString());
    ExtractConstantDescriptor descriptor = RefactoringSignatureDescriptorFactory.createExtractConstantDescriptor(project, description, comment.asString(), arguments, flags);
    return descriptor;
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) HashMap(java.util.HashMap) ExtractConstantDescriptor(org.eclipse.jdt.core.refactoring.descriptors.ExtractConstantDescriptor) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment)

Example 75 with IJavaProject

use of org.eclipse.jdt.core.IJavaProject in project che by eclipse.

the class JavaContext method suggestVariableName.

private String[] suggestVariableName(String type, String[] excludes) throws IllegalArgumentException {
    int dim = 0;
    while (type.endsWith("[]")) {
        //$NON-NLS-1$
        dim++;
        type = type.substring(0, type.length() - 2);
    }
    IJavaProject project = getJavaProject();
    if (project != null)
        return StubUtility.getVariableNameSuggestions(NamingConventions.VK_LOCAL, project, type, dim, Arrays.asList(excludes), true);
    // fallback if we lack proper context: roll-our own lowercasing
    return new String[] { Signature.getSimpleName(type).toLowerCase() };
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject)

Aggregations

IJavaProject (org.eclipse.jdt.core.IJavaProject)743 IProject (org.eclipse.core.resources.IProject)178 IFile (org.eclipse.core.resources.IFile)149 Test (org.junit.Test)146 CoreException (org.eclipse.core.runtime.CoreException)127 JavaModelException (org.eclipse.jdt.core.JavaModelException)124 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)123 IPath (org.eclipse.core.runtime.IPath)110 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)103 ArrayList (java.util.ArrayList)87 IFolder (org.eclipse.core.resources.IFolder)78 IResource (org.eclipse.core.resources.IResource)75 IJavaElement (org.eclipse.jdt.core.IJavaElement)63 IType (org.eclipse.jdt.core.IType)63 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)62 Path (org.eclipse.core.runtime.Path)57 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)56 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)47 StringInputStream (org.eclipse.xtext.util.StringInputStream)39 HashMap (java.util.HashMap)38