Search in sources :

Example 36 with IField

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

the class RenamePrivateFieldTest method helper2.

private void helper2(String fieldName, String newFieldName, boolean updateReferences, boolean updateTextualMatches, boolean renameGetter, boolean renameSetter, boolean expectedGetterRenameEnabled, boolean expectedSetterRenameEnabled) throws Exception {
    ParticipantTesting.reset();
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), "A");
    IType classA = getType(cu, "A");
    IField field = classA.getField(fieldName);
    RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_FIELD);
    descriptor.setJavaElement(field);
    descriptor.setNewName(newFieldName);
    descriptor.setUpdateReferences(updateReferences);
    descriptor.setUpdateTextualOccurrences(updateTextualMatches);
    descriptor.setRenameGetters(renameGetter);
    descriptor.setRenameSetters(renameSetter);
    RenameRefactoring refactoring = (RenameRefactoring) createRefactoring(descriptor);
    RenameFieldProcessor processor = (RenameFieldProcessor) refactoring.getProcessor();
    assertEquals("getter rename enabled", expectedGetterRenameEnabled, processor.canEnableGetterRenaming() == null);
    assertEquals("setter rename enabled", expectedSetterRenameEnabled, processor.canEnableSetterRenaming() == null);
    String newGetterName = processor.getNewGetterName();
    String newSetterName = processor.getNewSetterName();
    List elements = new ArrayList();
    elements.add(field);
    List args = new ArrayList();
    args.add(new RenameArguments(newFieldName, updateReferences));
    if (renameGetter && expectedGetterRenameEnabled) {
        elements.add(processor.getGetter());
        args.add(new RenameArguments(newGetterName, updateReferences));
    }
    if (renameSetter && expectedSetterRenameEnabled) {
        elements.add(processor.getSetter());
        args.add(new RenameArguments(newSetterName, updateReferences));
    }
    String[] renameHandles = ParticipantTesting.createHandles(elements.toArray());
    RefactoringStatus result = performRefactoring(refactoring);
    assertEquals("was supposed to pass", null, result);
    assertEqualLines("invalid renaming", getFileContents(getOutputTestFileName("A")), cu.getSource());
    ParticipantTesting.testRename(renameHandles, (RenameArguments[]) args.toArray(new RenameArguments[args.size()]));
    assertTrue("anythingToUndo", RefactoringCore.getUndoManager().anythingToUndo());
    assertTrue("! anythingToRedo", !RefactoringCore.getUndoManager().anythingToRedo());
    RefactoringCore.getUndoManager().performUndo(null, new NullProgressMonitor());
    assertEqualLines("invalid undo", getFileContents(getInputTestFileName("A")), cu.getSource());
    assertTrue("! anythingToUndo", !RefactoringCore.getUndoManager().anythingToUndo());
    assertTrue("anythingToRedo", RefactoringCore.getUndoManager().anythingToRedo());
    RefactoringCore.getUndoManager().performRedo(null, new NullProgressMonitor());
    assertEqualLines("invalid redo", getFileContents(getOutputTestFileName("A")), cu.getSource());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) RenameArguments(org.eclipse.ltk.core.refactoring.participants.RenameArguments) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) RenameFieldProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.RenameFieldProcessor) IField(org.eclipse.jdt.core.IField) IType(org.eclipse.jdt.core.IType) ArrayList(java.util.ArrayList) List(java.util.List) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)

Example 37 with IField

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

the class RefactoringAvailabilityTester method isGeneralizeTypeAvailable.

public static boolean isGeneralizeTypeAvailable(final IStructuredSelection selection) throws JavaModelException {
    if (selection.size() == 1) {
        final Object element = selection.getFirstElement();
        if (element instanceof IMethod) {
            final IMethod method = (IMethod) element;
            if (!method.exists())
                return false;
            final String type = method.getReturnType();
            if (PrimitiveType.toCode(Signature.toString(type)) == null)
                return Checks.isAvailable(method);
        } else if (element instanceof IField) {
            final IField field = (IField) element;
            if (!field.exists())
                return false;
            if (!JdtFlags.isEnum(field))
                return Checks.isAvailable(field);
        }
    }
    return false;
}
Also used : IMethod(org.eclipse.jdt.core.IMethod) IField(org.eclipse.jdt.core.IField)

Example 38 with IField

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

the class RefactoringAvailabilityTester method isExtractSupertypeAvailable.

public static boolean isExtractSupertypeAvailable(IMember member) throws JavaModelException {
    if (!member.exists())
        return false;
    final int type = member.getElementType();
    if (type != IJavaElement.METHOD && type != IJavaElement.FIELD && type != IJavaElement.TYPE)
        return false;
    if (JdtFlags.isEnum(member) && type != IJavaElement.TYPE)
        return false;
    if (!Checks.isAvailable(member))
        return false;
    if (member instanceof IMethod) {
        final IMethod method = (IMethod) member;
        if (method.isConstructor())
            return false;
        if (JdtFlags.isNative(method))
            return false;
        member = method.getDeclaringType();
    } else if (member instanceof IField) {
        member = member.getDeclaringType();
    }
    if (member instanceof IType) {
        if (JdtFlags.isEnum(member) || JdtFlags.isAnnotation(member))
            return false;
        if (member.getDeclaringType() != null && !JdtFlags.isStatic(member))
            return false;
        if (((IType) member).isAnonymous())
            // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=253727
            return false;
        if (((IType) member).isLambda())
            return false;
    }
    return true;
}
Also used : IMethod(org.eclipse.jdt.core.IMethod) IField(org.eclipse.jdt.core.IField) IType(org.eclipse.jdt.core.IType)

Example 39 with IField

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

the class ParameterGuesser method createVariable.

public Variable createVariable(IJavaElement element, IType enclosingType, String expectedType, int positionScore) throws JavaModelException {
    int variableType;
    int elementType = element.getElementType();
    String elementName = element.getElementName();
    String typeSignature;
    switch(elementType) {
        case IJavaElement.FIELD:
            {
                IField field = (IField) element;
                if (field.getDeclaringType().equals(enclosingType)) {
                    variableType = Variable.FIELD;
                } else {
                    variableType = Variable.INHERITED_FIELD;
                }
                if (field.isResolved()) {
                    typeSignature = new BindingKey(field.getKey()).toSignature();
                } else {
                    typeSignature = field.getTypeSignature();
                }
                break;
            }
        case IJavaElement.LOCAL_VARIABLE:
            {
                ILocalVariable locVar = (ILocalVariable) element;
                variableType = Variable.LOCAL;
                typeSignature = locVar.getTypeSignature();
                break;
            }
        case IJavaElement.METHOD:
            {
                IMethod method = (IMethod) element;
                if (isMethodToSuggest(method)) {
                    if (method.getDeclaringType().equals(enclosingType)) {
                        variableType = Variable.METHOD;
                    } else {
                        variableType = Variable.INHERITED_METHOD;
                    }
                    if (method.isResolved()) {
                        typeSignature = Signature.getReturnType(new BindingKey(method.getKey()).toSignature());
                    } else {
                        typeSignature = method.getReturnType();
                    }
                    //$NON-NLS-1$
                    elementName = elementName + "()";
                } else {
                    return null;
                }
                break;
            }
        default:
            return null;
    }
    String type = Signature.toString(typeSignature);
    boolean isAutoboxMatch = isPrimitiveType(expectedType) != isPrimitiveType(type);
    return new Variable(type, elementName, variableType, isAutoboxMatch, positionScore, NO_TRIGGERS, getImageDescriptor(element));
}
Also used : ILocalVariable(org.eclipse.jdt.core.ILocalVariable) ILocalVariable(org.eclipse.jdt.core.ILocalVariable) BindingKey(org.eclipse.jdt.core.BindingKey) IMethod(org.eclipse.jdt.core.IMethod) IField(org.eclipse.jdt.core.IField)

Example 40 with IField

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

the class InlineConstantRefactoring method initialize.

private RefactoringStatus initialize(JavaRefactoringArguments arguments) {
    final String selection = arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION);
    if (selection != null) {
        int offset = -1;
        int length = -1;
        final StringTokenizer tokenizer = new StringTokenizer(selection);
        if (tokenizer.hasMoreTokens())
            offset = Integer.valueOf(tokenizer.nextToken()).intValue();
        if (tokenizer.hasMoreTokens())
            length = Integer.valueOf(tokenizer.nextToken()).intValue();
        if (offset >= 0 && length >= 0) {
            fSelectionStart = offset;
            fSelectionLength = length;
        } else
            return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object[] { selection, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION }));
    }
    final String handle = arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT);
    if (handle != null) {
        final IJavaElement element = JavaRefactoringDescriptorUtil.handleToElement(arguments.getProject(), handle, false);
        if (element == null || !element.exists())
            return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(), IJavaRefactorings.INLINE_CONSTANT);
        else {
            if (element instanceof ICompilationUnit) {
                fSelectionCu = (ICompilationUnit) element;
                if (selection == null)
                    return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION));
            } else if (element instanceof IField) {
                final IField field = (IField) element;
                try {
                    final ISourceRange range = field.getNameRange();
                    if (range != null) {
                        fSelectionStart = range.getOffset();
                        fSelectionLength = range.getLength();
                    } else
                        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, IJavaRefactorings.INLINE_CONSTANT));
                } catch (JavaModelException exception) {
                    return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(), IJavaRefactorings.INLINE_CONSTANT);
                }
                fSelectionCu = field.getCompilationUnit();
            } else
                return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object[] { handle, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT }));
            final ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
            parser.setResolveBindings(true);
            parser.setSource(fSelectionCu);
            final CompilationUnit unit = (CompilationUnit) parser.createAST(null);
            initialize(fSelectionCu, unit);
            if (checkStaticFinalConstantNameSelected().hasFatalError())
                return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(), IJavaRefactorings.INLINE_CONSTANT);
        }
    } else
        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT));
    final String replace = arguments.getAttribute(ATTRIBUTE_REPLACE);
    if (replace != null) {
        fReplaceAllReferences = Boolean.valueOf(replace).booleanValue();
    } else
        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_REPLACE));
    final String remove = arguments.getAttribute(ATTRIBUTE_REMOVE);
    if (remove != null)
        fRemoveDeclaration = Boolean.valueOf(remove).booleanValue();
    else
        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_REMOVE));
    return new RefactoringStatus();
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) StringTokenizer(java.util.StringTokenizer) JavaModelException(org.eclipse.jdt.core.JavaModelException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IField(org.eclipse.jdt.core.IField) ASTParser(org.eclipse.jdt.core.dom.ASTParser) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Aggregations

IField (org.eclipse.jdt.core.IField)58 IType (org.eclipse.jdt.core.IType)36 IMethod (org.eclipse.jdt.core.IMethod)21 ArrayList (java.util.ArrayList)18 IJavaElement (org.eclipse.jdt.core.IJavaElement)16 Test (org.junit.Test)16 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)15 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)14 RenameJavaElementDescriptor (org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)9 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)7 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)7 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)6 List (java.util.List)5 ILocalVariable (org.eclipse.jdt.core.ILocalVariable)5 JavaModelException (org.eclipse.jdt.core.JavaModelException)5 RenameArguments (org.eclipse.ltk.core.refactoring.participants.RenameArguments)5 IFile (org.eclipse.core.resources.IFile)4 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)4 IJavaProject (org.eclipse.jdt.core.IJavaProject)4 IMember (org.eclipse.jdt.core.IMember)4