List of usage examples for org.eclipse.jdt.core IJavaElement METHOD
int METHOD
To view the source code for org.eclipse.jdt.core IJavaElement METHOD.
Click Source Link
From source file:org.eclipse.che.jdt.internal.core.search.BasicSearchEngine.java
License:Open Source License
/** * Searches for all declarations of the methods invoked in the given element. * The element can be a compilation unit or a source type/method/field. * Reports the method declarations using the given requestor. * * @see org.eclipse.jdt.core.search.SearchEngine#searchDeclarationsOfSentMessages(org.eclipse.jdt.core.IJavaElement, org.eclipse.jdt * .core.search.SearchRequestor, IProgressMonitor) * for detailed comment//from ww w . ja v a 2 s.c o m */ public void searchDeclarationsOfSentMessages(IJavaElement enclosingElement, SearchRequestor requestor, IProgressMonitor monitor) throws JavaModelException { if (VERBOSE) { Util.verbose( "BasicSearchEngine.searchDeclarationsOfSentMessages(IJavaElement, SearchRequestor, SearchPattern, IProgressMonitor)"); //$NON-NLS-1$ } // Do not accept other kind of element type than those specified in the spec switch (enclosingElement.getElementType()) { case IJavaElement.FIELD: case IJavaElement.METHOD: case IJavaElement.TYPE: case IJavaElement.COMPILATION_UNIT: // valid element type break; default: throw new IllegalArgumentException(); } SearchPattern pattern = new DeclarationOfReferencedMethodsPattern(enclosingElement); searchDeclarations(enclosingElement, requestor, pattern, monitor); }
From source file:org.eclipse.che.jdt.internal.core.search.matching.MethodLocator.java
License:Open Source License
/** * @see org.eclipse.jdt.internal.core.search.matching.PatternLocator#matchReportReference(org.eclipse.jdt.internal.compiler.ast.ASTNode, * org.eclipse.jdt.core.IJavaElement, org.eclipse.jdt.internal.compiler.lookup.Binding, int, org.eclipse.jdt.internal.core.search * .matching.MatchLocator)/*from ww w . jav a2 s .co m*/ */ protected void matchReportReference(ASTNode reference, IJavaElement element, IJavaElement localElement, IJavaElement[] otherElements, Binding elementBinding, int accuracy, MatchLocator locator) throws CoreException { MethodBinding methodBinding = (reference instanceof MessageSend) ? ((MessageSend) reference).binding : ((elementBinding instanceof MethodBinding) ? (MethodBinding) elementBinding : null); if (this.isDeclarationOfReferencedMethodsPattern) { if (methodBinding == null) return; // need exact match to be able to open on type ref if (accuracy != SearchMatch.A_ACCURATE) return; // element that references the method must be included in the enclosing element DeclarationOfReferencedMethodsPattern declPattern = (DeclarationOfReferencedMethodsPattern) this.pattern; while (element != null && !declPattern.enclosingElement.equals(element)) element = element.getParent(); if (element != null) { reportDeclaration(methodBinding, locator, declPattern.knownMethods); } } else { MethodReferenceMatch methodReferenceMatch = locator.newMethodReferenceMatch(element, elementBinding, accuracy, -1, -1, false /*not constructor*/, false/*not synthetic*/, reference); methodReferenceMatch.setLocalElement(localElement); this.match = methodReferenceMatch; if (this.pattern.findReferences && reference instanceof MessageSend) { IJavaElement focus = this.pattern.focus; // verify closest match if pattern was bound // (see bug 70827) if (focus != null && focus.getElementType() == IJavaElement.METHOD) { if (methodBinding != null && methodBinding.declaringClass != null) { boolean isPrivate = Flags.isPrivate(((IMethod) focus).getFlags()); if (isPrivate && !CharOperation.equals(methodBinding.declaringClass.sourceName, focus.getParent().getElementName().toCharArray())) { return; // finally the match was not possible } } } matchReportReference((MessageSend) reference, locator, accuracy, ((MessageSend) reference).binding); } else { if (reference instanceof SingleMemberAnnotation) { reference = ((SingleMemberAnnotation) reference).memberValuePairs()[0]; this.match.setImplicit(true); } int offset; if (reference instanceof ReferenceExpression) { offset = ((ReferenceExpression) reference).nameSourceStart; } else { offset = reference.sourceStart; } int length = reference.sourceEnd - offset + 1; this.match.setOffset(offset); this.match.setLength(length); locator.report(this.match); } } }
From source file:org.eclipse.che.jdt.internal.core.util.HandleFactory.java
License:Open Source License
/** * Create handle by adding child to parent obtained by recursing into parent scopes. *///from w w w. j a va 2 s . c o m public IJavaElement createElement(Scope scope, int elementPosition, ICompilationUnit unit, HashSet existingElements, HashMap knownScopes) { IJavaElement newElement = (IJavaElement) knownScopes.get(scope); if (newElement != null) return newElement; switch (scope.kind) { case Scope.COMPILATION_UNIT_SCOPE: newElement = unit; break; case Scope.CLASS_SCOPE: IJavaElement parentElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes); switch (parentElement.getElementType()) { case IJavaElement.COMPILATION_UNIT: newElement = ((ICompilationUnit) parentElement) .getType(new String(scope.enclosingSourceType().sourceName)); break; case IJavaElement.TYPE: newElement = ((IType) parentElement).getType(new String(scope.enclosingSourceType().sourceName)); break; case IJavaElement.FIELD: case IJavaElement.INITIALIZER: case IJavaElement.METHOD: IMember member = (IMember) parentElement; if (member.isBinary()) { return null; } else { newElement = member.getType(new String(scope.enclosingSourceType().sourceName), 1); // increment occurrence count if collision is detected if (newElement != null) { while (!existingElements.add(newElement)) ((SourceRefElement) newElement).occurrenceCount++; } } break; } if (newElement != null) { knownScopes.put(scope, newElement); } break; case Scope.METHOD_SCOPE: if (scope.isLambdaScope()) { parentElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes); LambdaExpression expression = (LambdaExpression) scope.originalReferenceContext(); if (expression.resolvedType != null && expression.resolvedType.isValidBinding() && !(expression.descriptor instanceof ProblemMethodBinding)) { // chain in lambda element only if resolved properly. //newElement = new org.eclipse.jdt.internal.core.SourceLambdaExpression((JavaElement) parentElement, expression) // .getMethod(); newElement = LambdaFactory.createLambdaExpression((JavaElement) parentElement, expression) .getMethod(); knownScopes.put(scope, newElement); return newElement; } return parentElement; } IType parentType = (IType) createElement(scope.parent, elementPosition, unit, existingElements, knownScopes); MethodScope methodScope = (MethodScope) scope; if (methodScope.isInsideInitializer()) { // inside field or initializer, must find proper one TypeDeclaration type = methodScope.referenceType(); int occurenceCount = 1; int length = type.fields == null ? 0 : type.fields.length; for (int i = 0; i < length; i++) { FieldDeclaration field = type.fields[i]; if (field.declarationSourceStart <= elementPosition && elementPosition <= field.declarationSourceEnd) { switch (field.getKind()) { case AbstractVariableDeclaration.FIELD: case AbstractVariableDeclaration.ENUM_CONSTANT: newElement = parentType.getField(new String(field.name)); break; case AbstractVariableDeclaration.INITIALIZER: newElement = parentType.getInitializer(occurenceCount); break; } break; } else if (field.getKind() == AbstractVariableDeclaration.INITIALIZER) { occurenceCount++; } } } else { // method element AbstractMethodDeclaration method = methodScope.referenceMethod(); newElement = parentType.getMethod(new String(method.selector), Util.typeParameterSignatures(method)); if (newElement != null) { knownScopes.put(scope, newElement); } } break; case Scope.BLOCK_SCOPE: // standard block, no element per se newElement = createElement(scope.parent, elementPosition, unit, existingElements, knownScopes); break; } return newElement; }
From source file:org.eclipse.che.jdt.javadoc.JavaElementLinks.java
License:Open Source License
private static ITypeParameter resolveTypeVariable(IJavaElement baseElement, String typeVariableName) throws JavaModelException { while (baseElement != null) { switch (baseElement.getElementType()) { case IJavaElement.METHOD: IMethod method = (IMethod) baseElement; ITypeParameter[] typeParameters = method.getTypeParameters(); for (int i = 0; i < typeParameters.length; i++) { ITypeParameter typeParameter = typeParameters[i]; if (typeParameter.getElementName().equals(typeVariableName)) { return typeParameter; }/*from ww w.j av a2 s.com*/ } break; case IJavaElement.TYPE: IType type = (IType) baseElement; typeParameters = type.getTypeParameters(); for (int i = 0; i < typeParameters.length; i++) { ITypeParameter typeParameter = typeParameters[i]; if (typeParameter.getElementName().equals(typeVariableName)) { return typeParameter; } } break; case IJavaElement.JAVA_MODEL: case IJavaElement.JAVA_PROJECT: case IJavaElement.PACKAGE_FRAGMENT: case IJavaElement.PACKAGE_FRAGMENT_ROOT: case IJavaElement.CLASS_FILE: case IJavaElement.COMPILATION_UNIT: case IJavaElement.PACKAGE_DECLARATION: case IJavaElement.IMPORT_CONTAINER: case IJavaElement.IMPORT_DECLARATION: return null; default: break; } // look for type parameters in enclosing members: baseElement = baseElement.getParent(); } return null; }
From source file:org.eclipse.che.jdt.refactoring.RefactoringManager.java
License:Open Source License
private static RenameSupport createRenameSupport(IJavaElement element, String newName, int flags) throws CoreException { switch (element.getElementType()) { case IJavaElement.PACKAGE_FRAGMENT: return RenameSupport.create((IPackageFragment) element, newName, flags); case IJavaElement.COMPILATION_UNIT: return RenameSupport.create((ICompilationUnit) element, newName, flags); case IJavaElement.TYPE: return RenameSupport.create((IType) element, newName, flags); case IJavaElement.METHOD: final IMethod method = (IMethod) element; if (method.isConstructor()) return createRenameSupport(method.getDeclaringType(), newName, flags); else// ww w. j ava2 s .c om return RenameSupport.create((IMethod) element, newName, flags); case IJavaElement.FIELD: return RenameSupport.create((IField) element, newName, flags); case IJavaElement.TYPE_PARAMETER: return RenameSupport.create((ITypeParameter) element, newName, flags); case IJavaElement.LOCAL_VARIABLE: return RenameSupport.create((ILocalVariable) element, newName, flags); } return null; }
From source file:org.eclipse.che.jdt.refactoring.RefactoringManager.java
License:Open Source License
private RenameWizard getWizardType(IJavaElement element) throws JavaModelException { switch (element.getElementType()) { case IJavaElement.PACKAGE_FRAGMENT: return RenameWizard.PACKAGE; case IJavaElement.COMPILATION_UNIT: return RenameWizard.COMPILATION_UNIT; case IJavaElement.TYPE: return RenameWizard.TYPE; case IJavaElement.METHOD: final IMethod method = (IMethod) element; if (method.isConstructor()) return RenameWizard.TYPE; else// w w w.ja v a 2 s .c o m return RenameWizard.METHOD; case IJavaElement.FIELD: if (JdtFlags.isEnum((IMember) element)) { return RenameWizard.ENUM_CONSTANT; } return RenameWizard.FIELD; case IJavaElement.TYPE_PARAMETER: return RenameWizard.TYPE_PARAMETER; case IJavaElement.LOCAL_VARIABLE: return RenameWizard.LOCAL_VARIABLE; } return null; }
From source file:org.eclipse.che.jdt.refactoring.session.RenameLinkedModeRefactoringSession.java
License:Open Source License
/** * Creates a rename descriptor.//from www.jav a 2 s .c o m * * @param javaElement element to rename * @param newName new name * @return a rename descriptor with current settings as used in the refactoring dialogs * @throws JavaModelException if an error occurs while accessing the element */ private RenameJavaElementDescriptor createRenameDescriptor(IJavaElement javaElement, String newName) throws JavaModelException { String contributionId; // see RefactoringExecutionStarter#createRenameSupport(..): int elementType = javaElement.getElementType(); switch (elementType) { case IJavaElement.JAVA_PROJECT: contributionId = IJavaRefactorings.RENAME_JAVA_PROJECT; break; case IJavaElement.PACKAGE_FRAGMENT_ROOT: contributionId = IJavaRefactorings.RENAME_SOURCE_FOLDER; break; case IJavaElement.PACKAGE_FRAGMENT: contributionId = IJavaRefactorings.RENAME_PACKAGE; break; case IJavaElement.COMPILATION_UNIT: contributionId = IJavaRefactorings.RENAME_COMPILATION_UNIT; break; case IJavaElement.TYPE: contributionId = IJavaRefactorings.RENAME_TYPE; break; case IJavaElement.METHOD: final IMethod method = (IMethod) javaElement; if (method.isConstructor()) return createRenameDescriptor(method.getDeclaringType(), newName); else contributionId = IJavaRefactorings.RENAME_METHOD; break; case IJavaElement.FIELD: IField field = (IField) javaElement; if (field.isEnumConstant()) contributionId = IJavaRefactorings.RENAME_ENUM_CONSTANT; else contributionId = IJavaRefactorings.RENAME_FIELD; break; case IJavaElement.TYPE_PARAMETER: contributionId = IJavaRefactorings.RENAME_TYPE_PARAMETER; break; case IJavaElement.LOCAL_VARIABLE: contributionId = IJavaRefactorings.RENAME_LOCAL_VARIABLE; break; default: return null; } RenameJavaElementDescriptor descriptor = (RenameJavaElementDescriptor) RefactoringCore .getRefactoringContribution(contributionId).createDescriptor(); descriptor.setJavaElement(javaElement); descriptor.setNewName(newName); if (elementType != IJavaElement.PACKAGE_FRAGMENT_ROOT) descriptor.setUpdateReferences(true); // IDialogSettings javaSettings= JavaPlugin.getDefault().getDialogSettings(); // IDialogSettings refactoringSettings= javaSettings.getSection(RefactoringWizardPage.REFACTORING_SETTINGS); //TODO: undocumented API // if (refactoringSettings == null) { // refactoringSettings= javaSettings.addNewSection(RefactoringWizardPage.REFACTORING_SETTINGS); // } switch (elementType) { case IJavaElement.METHOD: case IJavaElement.FIELD: descriptor.setDeprecateDelegate( /*refactoringSettings.getBoolean(DelegateUIHelper.DELEGATE_DEPRECATION)*/false); descriptor .setKeepOriginal(/*refactoringSettings.getBoolean(DelegateUIHelper.DELEGATE_UPDATING)*/ false); } switch (elementType) { case IJavaElement.TYPE: // case IJavaElement.COMPILATION_UNIT: // TODO descriptor.setUpdateSimilarDeclarations( /*refactoringSettings.getBoolean(RenameRefactoringWizard.TYPE_UPDATE_SIMILAR_ELEMENTS)*/ false); int strategy; try { strategy = 1; //refactoringSettings.getInt(RenameRefactoringWizard.TYPE_SIMILAR_MATCH_STRATEGY); } catch (NumberFormatException e) { strategy = RenamingNameSuggestor.STRATEGY_EXACT; } descriptor.setMatchStrategy(strategy); } switch (elementType) { case IJavaElement.PACKAGE_FRAGMENT: descriptor.setUpdateHierarchy( /*refactoringSettings.getBoolean(RenameRefactoringWizard.PACKAGE_RENAME_SUBPACKAGES)*/true); } switch (elementType) { case IJavaElement.PACKAGE_FRAGMENT: case IJavaElement.TYPE: String fileNamePatterns = /*refactoringSettings.get(RenameRefactoringWizard.QUALIFIED_NAMES_PATTERNS)*/"*"; if (fileNamePatterns != null && fileNamePatterns.length() != 0) { descriptor.setFileNamePatterns(fileNamePatterns); boolean updateQualifiedNames = /*refactoringSettings.getBoolean(RenameRefactoringWizard.UPDATE_QUALIFIED_NAMES)*/false; descriptor.setUpdateQualifiedNames(updateQualifiedNames); // fShowPreview|= updateQualifiedNames; } } switch (elementType) { case IJavaElement.PACKAGE_FRAGMENT: case IJavaElement.TYPE: case IJavaElement.FIELD: boolean updateTextualOccurrences = false; //refactoringSettings.getBoolean(RenameRefactoringWizard.UPDATE_TEXTUAL_MATCHES); descriptor.setUpdateTextualOccurrences(updateTextualOccurrences); // fShowPreview|= updateTextualOccurrences; } switch (elementType) { case IJavaElement.FIELD: descriptor.setRenameGetters( /*refactoringSettings.getBoolean(RenameRefactoringWizard.FIELD_RENAME_GETTER)*/false); descriptor.setRenameSetters( /*refactoringSettings.getBoolean(RenameRefactoringWizard.FIELD_RENAME_SETTER)*/false); } return descriptor; }
From source file:org.eclipse.che.jdt.util.JavaModelUtil.java
License:Open Source License
/** * Evaluates if a member (possible from another package) is visible from * elements in a package.//from www . java 2 s. com * @param member The member to test the visibility for * @param pack The package in focus * @return returns <code>true</code> if the member is visible from the package * @throws JavaModelException thrown when the member can not be accessed */ public static boolean isVisible(IMember member, IPackageFragment pack) throws JavaModelException { int type = member.getElementType(); if (type == IJavaElement.INITIALIZER || (type == IJavaElement.METHOD && member.getElementName().startsWith("<"))) { //$NON-NLS-1$ return false; } int otherflags = member.getFlags(); IType declaringType = member.getDeclaringType(); if (Flags.isPublic(otherflags) || (declaringType != null && isInterfaceOrAnnotation(declaringType))) { return true; } else if (Flags.isPrivate(otherflags)) { return false; } IPackageFragment otherpack = (IPackageFragment) member.getAncestor(IJavaElement.PACKAGE_FRAGMENT); return (pack != null && otherpack != null && isSamePackage(pack, otherpack)); }
From source file:org.eclipse.che.plugin.java.testing.AbstractJavaTestRunner.java
License:Open Source License
@Override public List<TestPosition> detectTests(TestDetectionContext context) { IJavaProject javaProject = getJavaProject(context.getProjectPath()); if (javaProject == null || !javaProject.exists()) { return Collections.emptyList(); }/*from w w w . j a va2s . c o m*/ IProject project = javaProject.getProject(); if (project == null || !hasJavaNature(project)) { return Collections.emptyList(); } List<TestPosition> result = new ArrayList<>(); String filePath = context.getFilePath(); if (filePath.endsWith(".xml")) { if (isTestSuite(filePath, javaProject)) { TestPosition testPosition = DtoFactory.newDto(TestPosition.class).withFrameworkName(getName()); result.add(testPosition); } return result; } try { ICompilationUnit compilationUnit = findCompilationUnitByPath(javaProject, filePath); if (context.getOffset() == -1) { addAllTestsMethod(result, compilationUnit); } else { IJavaElement element = compilationUnit.getElementAt(context.getOffset()); if (element != null && element.getElementType() == IJavaElement.METHOD) { if (isTestMethod((IMethod) element, compilationUnit)) { result.add(createTestPosition((IMethod) element)); } } else { addAllTestsMethod(result, compilationUnit); } } } catch (JavaModelException e) { LOG.debug("Can't read all methods.", e); } return result; }
From source file:org.eclipse.e4.demo.simpleide.jdt.internal.editor.viewer.JavaElementLabelComposer.java
License:Open Source License
/** * Appends the label for a Java element with the flags as defined by this * class./*from www. j a v a 2 s . com*/ * * @param element * the element to render * @param flags * the rendering flags. */ public void appendElementLabel(IJavaElement element, long flags) { int type = element.getElementType(); IPackageFragmentRoot root = null; if (type != IJavaElement.JAVA_MODEL && type != IJavaElement.JAVA_PROJECT && type != IJavaElement.PACKAGE_FRAGMENT_ROOT) root = JavaModelUtil.getPackageFragmentRoot(element); if (root != null && getFlag(flags, JavaElementLabels.PREPEND_ROOT_PATH)) { appendPackageFragmentRootLabel(root, JavaElementLabels.ROOT_QUALIFIED); fBuffer.append(JavaElementLabels.CONCAT_STRING); } switch (type) { case IJavaElement.METHOD: appendMethodLabel((IMethod) element, flags); break; case IJavaElement.FIELD: appendFieldLabel((IField) element, flags); break; case IJavaElement.LOCAL_VARIABLE: appendLocalVariableLabel((ILocalVariable) element, flags); break; case IJavaElement.TYPE_PARAMETER: appendTypeParameterLabel((ITypeParameter) element, flags); break; case IJavaElement.INITIALIZER: appendInitializerLabel((IInitializer) element, flags); break; case IJavaElement.TYPE: appendTypeLabel((IType) element, flags); break; case IJavaElement.CLASS_FILE: appendClassFileLabel((IClassFile) element, flags); break; case IJavaElement.COMPILATION_UNIT: appendCompilationUnitLabel((ICompilationUnit) element, flags); break; case IJavaElement.PACKAGE_FRAGMENT: appendPackageFragmentLabel((IPackageFragment) element, flags); break; case IJavaElement.PACKAGE_FRAGMENT_ROOT: appendPackageFragmentRootLabel((IPackageFragmentRoot) element, flags); break; case IJavaElement.IMPORT_CONTAINER: case IJavaElement.IMPORT_DECLARATION: case IJavaElement.PACKAGE_DECLARATION: appendDeclarationLabel(element, flags); break; case IJavaElement.JAVA_PROJECT: case IJavaElement.JAVA_MODEL: fBuffer.append(element.getElementName()); break; default: fBuffer.append(element.getElementName()); } if (root != null && getFlag(flags, JavaElementLabels.APPEND_ROOT_PATH)) { int offset = fBuffer.length(); fBuffer.append(JavaElementLabels.CONCAT_STRING); appendPackageFragmentRootLabel(root, JavaElementLabels.ROOT_QUALIFIED); if (getFlag(flags, JavaElementLabels.COLORIZE)) { fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE); } } }