Java Code Examples for org.eclipse.jdt.core.IMethod#getDeclaringType()
The following examples show how to use
org.eclipse.jdt.core.IMethod#getDeclaringType() .
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: RefactoringAvailabilityTester.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
public static boolean isPullUpAvailable(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 IType) { if (!JdtFlags.isStatic(member) && !JdtFlags.isEnum(member) && !JdtFlags.isAnnotation(member)) return false; } if (member instanceof IMethod) { final IMethod method= (IMethod) member; if (method.isConstructor()) return false; if (JdtFlags.isNative(method)) return false; final IType declaring= method.getDeclaringType(); if (declaring != null && declaring.isAnnotation()) return false; } return true; }
Example 2
Source File: IntroduceParameterObjectProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private void initializeFields(IMethod method) { fParameterObjectFactory= new ParameterObjectFactory(); String methodName= method.getElementName(); String className= String.valueOf(Character.toUpperCase(methodName.charAt(0))); if (methodName.length() > 1) className+= methodName.substring(1); className+= PARAMETER_CLASS_APPENDIX; fParameterObjectReference= ParameterInfo.createInfoForAddedParameter(className, DEFAULT_PARAMETER_OBJECT_NAME); fParameterObjectFactory.setClassName(className); IType declaringType= method.getDeclaringType(); Assert.isNotNull(declaringType); fParameterObjectFactory.setPackage(declaringType.getPackageFragment().getElementName()); updateReferenceType(); }
Example 3
Source File: OpenSuperImplementationAction.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private boolean checkMethod(IMethod method) { try { int flags= method.getFlags(); if (!Flags.isStatic(flags) && !Flags.isPrivate(flags)) { IType declaringType= method.getDeclaringType(); if (SuperTypeHierarchyCache.hasInCache(declaringType)) { if (findSuperImplementation(method) == null) { return false; } } return true; } } catch (JavaModelException e) { if (!e.isDoesNotExist()) { JavaPlugin.log(e); } } return false; }
Example 4
Source File: FindLinksHandler.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
public static IMethod findOverriddenMethod(IJavaElement element, IProgressMonitor monitor) throws JavaModelException { if (!(element instanceof IMethod)) { return null; } IMethod method = (IMethod) element; IType type = method.getDeclaringType(); if (type == null || type.isInterface() || method.isConstructor()) { return null; } ITypeHierarchy hierarchy = type.newSupertypeHierarchy(monitor); MethodOverrideTester tester = new MethodOverrideTester(type, hierarchy); IMethod found = tester.findOverriddenMethod(method, true); if (found != null && !found.equals(method)) { return found; } return null; }
Example 5
Source File: InlineMethodRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private void checkOverridden(RefactoringStatus status, IProgressMonitor pm) throws JavaModelException { pm.beginTask("", 9); //$NON-NLS-1$ pm.setTaskName(RefactoringCoreMessages.InlineMethodRefactoring_checking_overridden); MethodDeclaration decl= fSourceProvider.getDeclaration(); IMethod method= (IMethod) decl.resolveBinding().getJavaElement(); if (method == null || Flags.isPrivate(method.getFlags())) { pm.worked(8); return; } IType type= method.getDeclaringType(); ITypeHierarchy hierarchy= type.newTypeHierarchy(new SubProgressMonitor(pm, 6)); checkSubTypes(status, method, hierarchy.getAllSubtypes(type), new SubProgressMonitor(pm, 1)); checkSuperClasses(status, method, hierarchy.getAllSuperclasses(type), new SubProgressMonitor(pm, 1)); checkSuperInterfaces(status, method, hierarchy.getAllSuperInterfaces(type), new SubProgressMonitor(pm, 1)); pm.setTaskName(""); //$NON-NLS-1$ }
Example 6
Source File: JavadocContentAccess.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
private static Reader findDocInHierarchy(IMethod method, boolean useAttachedJavadoc) throws JavaModelException { /* * Catch ExternalJavaProject in which case * no hierarchy can be built. */ if (!method.getJavaProject().exists()) { return null; } IType type= method.getDeclaringType(); ITypeHierarchy hierarchy= type.newSupertypeHierarchy(null); MethodOverrideTester tester= new MethodOverrideTester(type, hierarchy); IType[] superTypes= hierarchy.getAllSupertypes(type); for (IType curr : superTypes) { IMethod overridden= tester.findOverriddenMethodInType(curr, method); if (overridden != null) { Reader reader = getHTMLContentReader(overridden, false, useAttachedJavadoc); if (reader != null) { return reader; } } } return null; }
Example 7
Source File: JavadocContentAccess2.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * Finds the first available attached Javadoc in the hierarchy of the given method. * * @param method the method * @return the inherited Javadoc from the Javadoc attachment, or <code>null</code> if none * @throws JavaModelException unexpected problem */ private static String findAttachedDocInHierarchy(final IMethod method) throws JavaModelException { IType type= method.getDeclaringType(); ITypeHierarchy hierarchy= SuperTypeHierarchyCache.getTypeHierarchy(type); final MethodOverrideTester tester= SuperTypeHierarchyCache.getMethodOverrideTester(type); return (String) new InheritDocVisitor() { @Override public Object visit(IType currType) throws JavaModelException { IMethod overridden= tester.findOverriddenMethodInType(currType, method); if (overridden == null) return InheritDocVisitor.CONTINUE; if (overridden.getOpenable().getBuffer() == null) { // only if no source available //TODO: BaseURL for method can be wrong for attached Javadoc from overridden // (e.g. when overridden is from rt.jar). Fix would be to add baseURL here. String attachedJavadoc= overridden.getAttachedJavadoc(null); if (attachedJavadoc != null) return attachedJavadoc; } return CONTINUE; } }.visitInheritDoc(type, hierarchy); }
Example 8
Source File: JavaOutlineInformationControl.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private IType getDefiningType(Object element) throws JavaModelException { int kind= ((IJavaElement) element).getElementType(); if (kind != IJavaElement.METHOD && kind != IJavaElement.FIELD && kind != IJavaElement.INITIALIZER) { return null; } IType declaringType= ((IMember) element).getDeclaringType(); if (kind != IJavaElement.METHOD) { return declaringType; } ITypeHierarchy hierarchy= getSuperTypeHierarchy(declaringType); if (hierarchy == null) { return declaringType; } IMethod method= (IMethod) element; MethodOverrideTester tester= new MethodOverrideTester(declaringType, hierarchy); IMethod res= tester.findDeclaringMethod(method, true); if (res == null || method.equals(res)) { return declaringType; } return res.getDeclaringType(); }
Example 9
Source File: MethodChecks.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * Locates the topmost method of an override ripple and returns it. If none * is found, null is returned. * * @param method the IMethod which may be part of a ripple * @param typeHierarchy a ITypeHierarchy of the declaring type of the method. May be null * @param monitor an IProgressMonitor * @return the topmost method of the ripple, or null if none * @throws JavaModelException */ public static IMethod getTopmostMethod(IMethod method, ITypeHierarchy typeHierarchy, IProgressMonitor monitor) throws JavaModelException { Assert.isNotNull(method); ITypeHierarchy hierarchy= typeHierarchy; IMethod topmostMethod= null; final IType declaringType= method.getDeclaringType(); if (!declaringType.isInterface()) { if ((hierarchy == null) || !declaringType.equals(hierarchy.getType())) hierarchy= declaringType.newTypeHierarchy(monitor); IMethod inInterface= isDeclaredInInterface(method, hierarchy, monitor); if (inInterface != null && !inInterface.equals(method)) topmostMethod= inInterface; } if (topmostMethod == null) { if (hierarchy == null) hierarchy= declaringType.newSupertypeHierarchy(monitor); IMethod overrides= overridesAnotherMethod(method, hierarchy); if (overrides != null && !overrides.equals(method)) topmostMethod= overrides; } return topmostMethod; }
Example 10
Source File: MethodsLabelProvider.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
@Override public Color getForeground(Object element) { if (fMethodsViewer.isShowInheritedMethods() && element instanceof IMethod) { IMethod curr= (IMethod) element; IMember declaringType= curr.getDeclaringType(); if (!declaringType.equals(fMethodsViewer.getInput())) { return JFaceResources.getColorRegistry().get(ColoredViewersManager.INHERITED_COLOR_NAME); } } return null; }
Example 11
Source File: RefactoringAvailabilityTester.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
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()) return false; // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=253727 if (((IType)member).isLambda()) return false; } return true; }
Example 12
Source File: Binding2JavaModel.java From lapse-plus with GNU General Public License v3.0 | 5 votes |
private static boolean sameParameters(IMethodBinding method, IMethod candidate) throws JavaModelException { ITypeBinding[] methodParamters= method.getParameterTypes(); String[] candidateParameters= candidate.getParameterTypes(); if (methodParamters.length != candidateParameters.length) return false; IType scope= candidate.getDeclaringType(); for (int i= 0; i < methodParamters.length; i++) { ITypeBinding methodParameter= methodParamters[i]; String candidateParameter= candidateParameters[i]; if (!sameParameter(methodParameter, candidateParameter, scope)) return false; } return true; }
Example 13
Source File: RefactoringAvailabilityTester.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
public static boolean isPushDownAvailable(final IMember member) throws JavaModelException { if (!member.exists()) { return false; } final int type = member.getElementType(); if (type != IJavaElement.METHOD && type != IJavaElement.FIELD) { return false; } if (JdtFlags.isEnum(member)) { return false; } if (!Checks.isAvailable(member)) { return false; } if (JdtFlags.isStatic(member)) { return false; } if (type == IJavaElement.METHOD) { final IMethod method = (IMethod) member; if (method.isConstructor()) { return false; } if (JdtFlags.isNative(method)) { return false; } final IType declaring = method.getDeclaringType(); if (declaring != null && declaring.isAnnotation()) { return false; } } return true; }
Example 14
Source File: MethodChecks.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
/** * Locates the topmost method of an override ripple and returns it. If none * is found, null is returned. * * @param method the IMethod which may be part of a ripple * @param typeHierarchy a ITypeHierarchy of the declaring type of the method. May be null * @param monitor an IProgressMonitor * @return the topmost method of the ripple, or null if none * @throws JavaModelException */ public static IMethod getTopmostMethod(IMethod method, ITypeHierarchy typeHierarchy, IProgressMonitor monitor) throws JavaModelException { Assert.isNotNull(method); ITypeHierarchy hierarchy= typeHierarchy; IMethod topmostMethod= null; final IType declaringType= method.getDeclaringType(); if (!declaringType.isInterface()) { if ((hierarchy == null) || !declaringType.equals(hierarchy.getType())) { hierarchy= declaringType.newTypeHierarchy(monitor); } IMethod inInterface= isDeclaredInInterface(method, hierarchy, monitor); if (inInterface != null && !inInterface.equals(method)) { topmostMethod= inInterface; } } if (topmostMethod == null) { if (hierarchy == null) { hierarchy= declaringType.newSupertypeHierarchy(monitor); } IMethod overrides= overridesAnotherMethod(method, hierarchy); if (overrides != null && !overrides.equals(method)) { topmostMethod= overrides; } } return topmostMethod; }
Example 15
Source File: JavaDocLocations.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private static void appendMethodReference(IMethod meth, StringBuffer buf) throws JavaModelException { buf.append(meth.getElementName()); /* * The Javadoc tool for Java SE 8 changed the anchor syntax and now tries to avoid "strange" characters in URLs. * This breaks all clients that directly create such URLs. * We can't know what format is required, so we just guess by the project's compiler compliance. */ boolean is18OrHigher = JavaModelUtil.is18OrHigher(meth.getJavaProject()); buf.append(is18OrHigher ? '-' : '('); String[] params = meth.getParameterTypes(); IType declaringType = meth.getDeclaringType(); boolean isVararg = Flags.isVarargs(meth.getFlags()); int lastParam = params.length - 1; for (int i = 0; i <= lastParam; i++) { if (i != 0) { buf.append(is18OrHigher ? "-" : ", "); //$NON-NLS-1$ //$NON-NLS-2$ } String curr = Signature.getTypeErasure(params[i]); String fullName = JavaModelUtil.getResolvedTypeName(curr, declaringType); if (fullName == null) { // e.g. a type parameter "QE;" fullName = Signature.toString(Signature.getElementType(curr)); } if (fullName != null) { buf.append(fullName); int dim = Signature.getArrayCount(curr); if (i == lastParam && isVararg) { dim--; } while (dim > 0) { buf.append(is18OrHigher ? ":A" : "[]"); //$NON-NLS-1$ //$NON-NLS-2$ dim--; } if (i == lastParam && isVararg) { buf.append("..."); //$NON-NLS-1$ } } } buf.append(is18OrHigher ? '-' : ')'); }
Example 16
Source File: JavaDocLocations.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static void appendMethodReference(IMethod meth, StringBuffer buf) throws JavaModelException { buf.append(meth.getElementName()); /* * The Javadoc tool for Java SE 8 changed the anchor syntax and now tries to avoid "strange" characters in URLs. * This breaks all clients that directly create such URLs. * We can't know what format is required, so we just guess by the project's compiler compliance. */ boolean is18OrHigher= JavaModelUtil.is18OrHigher(meth.getJavaProject()); buf.append(is18OrHigher ? '-' : '('); String[] params= meth.getParameterTypes(); IType declaringType= meth.getDeclaringType(); boolean isVararg= Flags.isVarargs(meth.getFlags()); int lastParam= params.length - 1; for (int i= 0; i <= lastParam; i++) { if (i != 0) { buf.append(is18OrHigher ? "-" : ", "); //$NON-NLS-1$ //$NON-NLS-2$ } String curr= Signature.getTypeErasure(params[i]); String fullName= JavaModelUtil.getResolvedTypeName(curr, declaringType); if (fullName == null) { // e.g. a type parameter "QE;" fullName= Signature.toString(Signature.getElementType(curr)); } if (fullName != null) { buf.append(fullName); int dim= Signature.getArrayCount(curr); if (i == lastParam && isVararg) { dim--; } while (dim > 0) { buf.append(is18OrHigher ? ":A" : "[]"); //$NON-NLS-1$ //$NON-NLS-2$ dim--; } if (i == lastParam && isVararg) { buf.append("..."); //$NON-NLS-1$ } } } buf.append(is18OrHigher ? '-' : ')'); }
Example 17
Source File: RenameVirtualMethodProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private IMethod[] relatedTypeDeclaresMethodName(IProgressMonitor pm, IMethod method, String newName) throws CoreException { try{ Set<IMethod> result= new HashSet<>(); Set<IType> types= getRelatedTypes(); pm.beginTask("", types.size()); //$NON-NLS-1$ for (Iterator<IType> iter= types.iterator(); iter.hasNext(); ) { final IMethod found= Checks.findMethod(method, iter.next()); final IType declaring= found.getDeclaringType(); result.addAll(Arrays.asList(hierarchyDeclaresMethodName(new SubProgressMonitor(pm, 1), declaring.newTypeHierarchy(new SubProgressMonitor(pm, 1)), found, newName))); } return result.toArray(new IMethod[result.size()]); } finally { pm.done(); } }
Example 18
Source File: RenameVirtualMethodProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private IMethod[] relatedTypeDeclaresMethodName(IProgressMonitor pm, IMethod method, String newName) throws CoreException { try{ Set<IMethod> result= new HashSet<IMethod>(); Set<IType> types= getRelatedTypes(); pm.beginTask("", types.size()); //$NON-NLS-1$ for (Iterator<IType> iter= types.iterator(); iter.hasNext(); ) { final IMethod found= Checks.findMethod(method, iter.next()); final IType declaring= found.getDeclaringType(); result.addAll(Arrays.asList(hierarchyDeclaresMethodName(new SubProgressMonitor(pm, 1), declaring.newTypeHierarchy(new SubProgressMonitor(pm, 1)), found, newName))); } return result.toArray(new IMethod[result.size()]); } finally { pm.done(); } }
Example 19
Source File: MethodChecks.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public static IMethod overridesAnotherMethod(IMethod method, ITypeHierarchy hierarchy) throws JavaModelException { MethodOverrideTester tester= new MethodOverrideTester(method.getDeclaringType(), hierarchy); IMethod found= tester.findDeclaringMethod(method, true); boolean overrides= (found != null && !found.equals(method) && (!JdtFlags.isStatic(found)) && (!JdtFlags.isPrivate(found))); if (overrides) return found; else return null; }
Example 20
Source File: RenameMethodProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Initializes the refactoring from scripting arguments. * Used by {@link RenameVirtualMethodProcessor} and {@link RenameNonVirtualMethodProcessor} * * @param extended the arguments * @return the resulting status */ protected final RefactoringStatus initialize(JavaRefactoringArguments extended) { fInitialized= true; final String handle= extended.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT); if (handle != null) { final IJavaElement element= JavaRefactoringDescriptorUtil.handleToElement(extended.getProject(), handle, false); final String refactoring= getProcessorName(); if (element instanceof IMethod) { final IMethod method= (IMethod) element; final IType declaring= method.getDeclaringType(); if (declaring != null && declaring.exists()) { final IMethod[] methods= declaring.findMethods(method); if (methods != null && methods.length == 1 && methods[0] != null) { if (!methods[0].exists()) return JavaRefactoringDescriptorUtil.createInputFatalStatus(methods[0], refactoring, IJavaRefactorings.RENAME_METHOD); fMethod= methods[0]; initializeWorkingCopyOwner(); } else return JavaRefactoringDescriptorUtil.createInputFatalStatus(null, refactoring, IJavaRefactorings.RENAME_METHOD); } else return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, refactoring, IJavaRefactorings.RENAME_METHOD); } else return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, refactoring, IJavaRefactorings.RENAME_METHOD); } else return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT)); final String name= extended.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME); if (name != null && !"".equals(name)) //$NON-NLS-1$ setNewElementName(name); else return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME)); final String references= extended.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_REFERENCES); if (references != null) { fUpdateReferences= Boolean.valueOf(references).booleanValue(); } else return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_REFERENCES)); final String delegate= extended.getAttribute(ATTRIBUTE_DELEGATE); if (delegate != null) { fDelegateUpdating= Boolean.valueOf(delegate).booleanValue(); } else return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_DELEGATE)); final String deprecate= extended.getAttribute(ATTRIBUTE_DEPRECATE); if (deprecate != null) { fDelegateDeprecation= Boolean.valueOf(deprecate).booleanValue(); } else return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_DEPRECATE)); return new RefactoringStatus(); }