Search in sources :

Example 41 with IMethod

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

the class RefactoringAvailabilityTester method isIntroduceFactoryAvailable.

public static boolean isIntroduceFactoryAvailable(final JavaTextSelection selection) throws JavaModelException {
    final IJavaElement[] elements = selection.resolveElementAtOffset();
    if (elements.length == 1 && elements[0] instanceof IMethod)
        return isIntroduceFactoryAvailable((IMethod) elements[0]);
    // there's no IMethod for the default constructor
    if (!Checks.isAvailable(selection.resolveEnclosingElement()))
        return false;
    ASTNode node = selection.resolveCoveringNode();
    if (node == null) {
        ASTNode[] selectedNodes = selection.resolveSelectedNodes();
        if (selectedNodes != null && selectedNodes.length == 1) {
            node = selectedNodes[0];
            if (node == null)
                return false;
        } else {
            return false;
        }
    }
    if (node.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION)
        return true;
    node = ASTNodes.getNormalizedNode(node);
    if (node.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY)
        return true;
    return false;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IMethod(org.eclipse.jdt.core.IMethod)

Example 42 with IMethod

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

the class RefactoringAvailabilityTester method isReplaceInvocationsAvailable.

public static boolean isReplaceInvocationsAvailable(final JavaTextSelection selection) throws JavaModelException {
    final IJavaElement[] elements = selection.resolveElementAtOffset();
    if (elements.length != 1)
        return false;
    IJavaElement element = elements[0];
    return (element instanceof IMethod) && isReplaceInvocationsAvailable(((IMethod) element));
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IMethod(org.eclipse.jdt.core.IMethod)

Example 43 with IMethod

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

the class MemberVisibilityAdjustor method computeOutgoingVisibilityThreshold.

/**
	 * Computes the visibility threshold for the referenced element.
	 *
	 * @param referencing the referencing element
	 * @param referenced the referenced element
	 * @param monitor the progress monitor to use
	 * @return the visibility keyword corresponding to the threshold, or <code>null</code> for default visibility
	 * @throws JavaModelException if the java elements could not be accessed
	 */
private ModifierKeyword computeOutgoingVisibilityThreshold(final IJavaElement referencing, final IMember referenced, final IProgressMonitor monitor) throws JavaModelException {
    Assert.isTrue(referencing instanceof ICompilationUnit || referencing instanceof IType || referencing instanceof IPackageFragment);
    Assert.isTrue(referenced instanceof IType || referenced instanceof IField || referenced instanceof IMethod);
    ModifierKeyword keyword = ModifierKeyword.PUBLIC_KEYWORD;
    try {
        //$NON-NLS-1$
        monitor.beginTask("", 1);
        monitor.setTaskName(RefactoringCoreMessages.MemberVisibilityAdjustor_checking);
        final int referencingType = referencing.getElementType();
        final int referencedType = referenced.getElementType();
        switch(referencedType) {
            case IJavaElement.TYPE:
                {
                    final IType typeReferenced = (IType) referenced;
                    switch(referencingType) {
                        case IJavaElement.COMPILATION_UNIT:
                            {
                                final ICompilationUnit unit = (ICompilationUnit) referencing;
                                final ICompilationUnit referencedUnit = typeReferenced.getCompilationUnit();
                                if (referencedUnit != null && referencedUnit.equals(unit))
                                    keyword = ModifierKeyword.PRIVATE_KEYWORD;
                                else if (referencedUnit != null && referencedUnit.getParent().equals(unit.getParent()))
                                    keyword = null;
                                break;
                            }
                        case IJavaElement.TYPE:
                            {
                                keyword = thresholdTypeToType((IType) referencing, typeReferenced, monitor);
                                break;
                            }
                        case IJavaElement.PACKAGE_FRAGMENT:
                            {
                                final IPackageFragment fragment = (IPackageFragment) referencing;
                                if (typeReferenced.getPackageFragment().equals(fragment))
                                    keyword = null;
                                break;
                            }
                        default:
                            Assert.isTrue(false);
                    }
                    break;
                }
            case IJavaElement.FIELD:
                {
                    final IField fieldReferenced = (IField) referenced;
                    final ICompilationUnit referencedUnit = fieldReferenced.getCompilationUnit();
                    switch(referencingType) {
                        case IJavaElement.COMPILATION_UNIT:
                            {
                                final ICompilationUnit unit = (ICompilationUnit) referencing;
                                if (referencedUnit != null && referencedUnit.equals(unit))
                                    keyword = ModifierKeyword.PRIVATE_KEYWORD;
                                else if (referencedUnit != null && referencedUnit.getParent().equals(unit.getParent()))
                                    keyword = null;
                                break;
                            }
                        case IJavaElement.TYPE:
                            {
                                keyword = thresholdTypeToField((IType) referencing, fieldReferenced, monitor);
                                break;
                            }
                        case IJavaElement.PACKAGE_FRAGMENT:
                            {
                                final IPackageFragment fragment = (IPackageFragment) referencing;
                                if (fieldReferenced.getDeclaringType().getPackageFragment().equals(fragment))
                                    keyword = null;
                                break;
                            }
                        default:
                            Assert.isTrue(false);
                    }
                    break;
                }
            case IJavaElement.METHOD:
                {
                    final IMethod methodReferenced = (IMethod) referenced;
                    final ICompilationUnit referencedUnit = methodReferenced.getCompilationUnit();
                    switch(referencingType) {
                        case IJavaElement.COMPILATION_UNIT:
                            {
                                final ICompilationUnit unit = (ICompilationUnit) referencing;
                                if (referencedUnit != null && referencedUnit.equals(unit))
                                    keyword = ModifierKeyword.PRIVATE_KEYWORD;
                                else if (referencedUnit != null && referencedUnit.getParent().equals(unit.getParent()))
                                    keyword = null;
                                break;
                            }
                        case IJavaElement.TYPE:
                            {
                                keyword = thresholdTypeToMethod((IType) referencing, methodReferenced, monitor);
                                break;
                            }
                        case IJavaElement.PACKAGE_FRAGMENT:
                            {
                                final IPackageFragment fragment = (IPackageFragment) referencing;
                                if (methodReferenced.getDeclaringType().getPackageFragment().equals(fragment))
                                    keyword = null;
                                break;
                            }
                        default:
                            Assert.isTrue(false);
                    }
                    break;
                }
            default:
                Assert.isTrue(false);
        }
    } finally {
        monitor.done();
    }
    return keyword;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ModifierKeyword(org.eclipse.jdt.core.dom.Modifier.ModifierKeyword) IMethod(org.eclipse.jdt.core.IMethod) IField(org.eclipse.jdt.core.IField) IType(org.eclipse.jdt.core.IType)

Example 44 with IMethod

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

the class MemberVisibilityAdjustor method getVisibilityThreshold.

/**
	 * Computes the visibility threshold for the referenced element.
	 *
	 * @param referencing the referencing element
	 * @param referenced the referenced element
	 * @param monitor the progress monitor to use
	 * @return the visibility keyword corresponding to the threshold, or <code>null</code> for default visibility
	 * @throws JavaModelException if the java elements could not be accessed
	 */
public ModifierKeyword getVisibilityThreshold(final IJavaElement referencing, final IMember referenced, final IProgressMonitor monitor) throws JavaModelException {
    Assert.isTrue(!(referencing instanceof IInitializer));
    Assert.isTrue(!(referenced instanceof IInitializer));
    ModifierKeyword keyword = ModifierKeyword.PUBLIC_KEYWORD;
    try {
        //$NON-NLS-1$
        monitor.beginTask("", 1);
        monitor.setTaskName(RefactoringCoreMessages.MemberVisibilityAdjustor_checking);
        final int referencingType = referencing.getElementType();
        final int referencedType = referenced.getElementType();
        switch(referencedType) {
            case IJavaElement.TYPE:
                {
                    final IType typeReferenced = (IType) referenced;
                    final ICompilationUnit referencedUnit = typeReferenced.getCompilationUnit();
                    switch(referencingType) {
                        case IJavaElement.TYPE:
                            {
                                keyword = thresholdTypeToType((IType) referencing, typeReferenced, monitor);
                                break;
                            }
                        case IJavaElement.FIELD:
                        case IJavaElement.METHOD:
                            {
                                final IMember member = (IMember) referencing;
                                if (typeReferenced.equals(member.getDeclaringType()))
                                    keyword = ModifierKeyword.PRIVATE_KEYWORD;
                                else if (referencedUnit != null && referencedUnit.equals(member.getCompilationUnit()))
                                    keyword = ModifierKeyword.PRIVATE_KEYWORD;
                                else if (typeReferenced.getPackageFragment().equals(member.getDeclaringType().getPackageFragment()))
                                    keyword = null;
                                break;
                            }
                        case IJavaElement.PACKAGE_FRAGMENT:
                            {
                                final IPackageFragment fragment = (IPackageFragment) referencing;
                                if (typeReferenced.getPackageFragment().equals(fragment))
                                    keyword = null;
                                break;
                            }
                        default:
                            Assert.isTrue(false);
                    }
                    break;
                }
            case IJavaElement.FIELD:
                {
                    final IField fieldReferenced = (IField) referenced;
                    final ICompilationUnit referencedUnit = fieldReferenced.getCompilationUnit();
                    switch(referencingType) {
                        case IJavaElement.TYPE:
                            {
                                keyword = thresholdTypeToField((IType) referencing, fieldReferenced, monitor);
                                break;
                            }
                        case IJavaElement.FIELD:
                        case IJavaElement.METHOD:
                            {
                                final IMember member = (IMember) referencing;
                                if (fieldReferenced.getDeclaringType().equals(member.getDeclaringType()))
                                    keyword = ModifierKeyword.PRIVATE_KEYWORD;
                                else if (referencedUnit != null && referencedUnit.equals(member.getCompilationUnit()))
                                    keyword = ModifierKeyword.PRIVATE_KEYWORD;
                                else if (fieldReferenced.getDeclaringType().getPackageFragment().equals(member.getDeclaringType().getPackageFragment()))
                                    keyword = null;
                                break;
                            }
                        case IJavaElement.PACKAGE_FRAGMENT:
                            {
                                final IPackageFragment fragment = (IPackageFragment) referencing;
                                if (fieldReferenced.getDeclaringType().getPackageFragment().equals(fragment))
                                    keyword = null;
                                break;
                            }
                        default:
                            Assert.isTrue(false);
                    }
                    break;
                }
            case IJavaElement.METHOD:
                {
                    final IMethod methodReferenced = (IMethod) referenced;
                    final ICompilationUnit referencedUnit = methodReferenced.getCompilationUnit();
                    switch(referencingType) {
                        case IJavaElement.TYPE:
                            {
                                keyword = thresholdTypeToMethod((IType) referencing, methodReferenced, monitor);
                                break;
                            }
                        case IJavaElement.FIELD:
                        case IJavaElement.METHOD:
                            {
                                final IMember member = (IMember) referencing;
                                if (methodReferenced.getDeclaringType().equals(member.getDeclaringType()))
                                    keyword = ModifierKeyword.PRIVATE_KEYWORD;
                                else if (referencedUnit != null && referencedUnit.equals(member.getCompilationUnit()))
                                    keyword = ModifierKeyword.PRIVATE_KEYWORD;
                                else if (methodReferenced.getDeclaringType().getPackageFragment().equals(member.getDeclaringType().getPackageFragment()))
                                    keyword = null;
                                break;
                            }
                        case IJavaElement.PACKAGE_FRAGMENT:
                            {
                                final IPackageFragment fragment = (IPackageFragment) referencing;
                                if (methodReferenced.getDeclaringType().getPackageFragment().equals(fragment))
                                    keyword = null;
                                break;
                            }
                        default:
                            Assert.isTrue(false);
                    }
                    break;
                }
            default:
                Assert.isTrue(false);
        }
    } finally {
        monitor.done();
    }
    return keyword;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ModifierKeyword(org.eclipse.jdt.core.dom.Modifier.ModifierKeyword) IInitializer(org.eclipse.jdt.core.IInitializer) IMethod(org.eclipse.jdt.core.IMethod) IField(org.eclipse.jdt.core.IField) IMember(org.eclipse.jdt.core.IMember) IType(org.eclipse.jdt.core.IType)

Example 45 with IMethod

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

the class GetterSetterCompletionProposal method evaluateProposals.

public static void evaluateProposals(IType type, String prefix, int offset, int length, int relevance, Set<String> suggestedMethods, Collection<IJavaCompletionProposal> result) throws CoreException {
    if (prefix.length() == 0) {
        relevance--;
    }
    IField[] fields = type.getFields();
    IMethod[] methods = type.getMethods();
    for (int i = 0; i < fields.length; i++) {
        IField curr = fields[i];
        if (!JdtFlags.isEnum(curr)) {
            String getterName = GetterSetterUtil.getGetterName(curr, null);
            if (Strings.startsWithIgnoreCase(getterName, prefix) && !hasMethod(methods, getterName)) {
                suggestedMethods.add(getterName);
                int getterRelevance = relevance;
                if (JdtFlags.isStatic(curr) && JdtFlags.isFinal(curr))
                    getterRelevance = relevance - 1;
                result.add(new GetterSetterCompletionProposal(curr, offset, length, true, getterRelevance));
            }
            if (!JdtFlags.isFinal(curr)) {
                String setterName = GetterSetterUtil.getSetterName(curr, null);
                if (Strings.startsWithIgnoreCase(setterName, prefix) && !hasMethod(methods, setterName)) {
                    suggestedMethods.add(setterName);
                    result.add(new GetterSetterCompletionProposal(curr, offset, length, false, relevance));
                }
            }
        }
    }
}
Also used : IMethod(org.eclipse.jdt.core.IMethod) StyledString(org.eclipse.jface.viewers.StyledString) IField(org.eclipse.jdt.core.IField)

Aggregations

IMethod (org.eclipse.jdt.core.IMethod)160 IType (org.eclipse.jdt.core.IType)87 IJavaElement (org.eclipse.jdt.core.IJavaElement)36 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)29 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)28 ArrayList (java.util.ArrayList)24 Test (org.junit.Test)22 IField (org.eclipse.jdt.core.IField)21 ITypeHierarchy (org.eclipse.jdt.core.ITypeHierarchy)14 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)13 IMember (org.eclipse.jdt.core.IMember)13 JavaModelException (org.eclipse.jdt.core.JavaModelException)12 RenameJavaElementDescriptor (org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)12 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)11 HashSet (java.util.HashSet)8 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)8 ILocalVariable (org.eclipse.jdt.core.ILocalVariable)7 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)7 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)7 RenameRefactoring (org.eclipse.ltk.core.refactoring.participants.RenameRefactoring)7