List of usage examples for org.eclipse.jdt.core IJavaElement COMPILATION_UNIT
int COMPILATION_UNIT
To view the source code for org.eclipse.jdt.core IJavaElement COMPILATION_UNIT.
Click Source Link
From source file:org.eclipse.che.jdt.internal.core.search.TypeNameMatchRequestorWrapper.java
License:Open Source License
public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path, AccessRestriction access) { // Get type//from w ww . j av a2 s . c om try { IType type = null; if (this.handleFactory != null) { Openable openable = this.handleFactory.createOpenable(path, this.scope); if (openable == null) return; switch (openable.getElementType()) { case IJavaElement.COMPILATION_UNIT: ICompilationUnit cu = (ICompilationUnit) openable; if (enclosingTypeNames != null && enclosingTypeNames.length > 0) { type = cu.getType(new String(enclosingTypeNames[0])); for (int j = 1, l = enclosingTypeNames.length; j < l; j++) { type = type.getType(new String(enclosingTypeNames[j])); } type = type.getType(new String(simpleTypeName)); } else { type = cu.getType(new String(simpleTypeName)); } break; case IJavaElement.CLASS_FILE: type = ((IClassFile) openable).getType(); break; } } else { int separatorIndex = path.indexOf(IJavaSearchScope.JAR_FILE_ENTRY_SEPARATOR); type = separatorIndex == -1 ? createTypeFromPath(path, new String(simpleTypeName), enclosingTypeNames) : createTypeFromJar(path, separatorIndex); } // Accept match if the type has been found if (type != null) { // hierarchy scopes require one more check: if (!(this.scope instanceof HierarchyScope) || ((HierarchyScope) this.scope).enclosesFineGrained(type)) { // Create the match final JavaSearchTypeNameMatch match = new JavaSearchTypeNameMatch(type, modifiers); // Update match accessibility if (access != null) { switch (access.getProblemId()) { case IProblem.ForbiddenReference: match.setAccessibility(IAccessRule.K_NON_ACCESSIBLE); break; case IProblem.DiscouragedReference: match.setAccessibility(IAccessRule.K_DISCOURAGED); break; } } // Accept match this.requestor.acceptTypeNameMatch(match); } } } catch (JavaModelException e) { // skip } }
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 ww w . j av a 2 s . c om*/ 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 www . ja va2 s .co m*/ } 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// w ww. j a va 2 s . c o m 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/*from www .j ava 2s. 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.RenameTypeTest.java
License:Open Source License
private void checkMappers(Refactoring refactoring, IType type, String newCUName, IJavaElement[] someClassMembers) { RenameTypeProcessor rtp = (RenameTypeProcessor) ((RenameRefactoring) refactoring).getProcessor(); ICompilationUnit newUnit = (ICompilationUnit) rtp.getRefactoredJavaElement(type.getCompilationUnit()); assertTrue(newUnit.exists());//from w w w . j a v a 2 s . c o m assertTrue(newUnit.getElementName().equals(newCUName)); IFile newFile = (IFile) rtp.getRefactoredResource(type.getResource()); assertTrue(newFile.exists()); assertTrue(newFile.getName().equals(newCUName)); if ((type.getParent().getElementType() == IJavaElement.COMPILATION_UNIT) && type.getCompilationUnit().getElementName().equals(type.getElementName() + ".java")) { assertFalse(type.getCompilationUnit().exists()); assertFalse(type.getResource().exists()); } IPackageFragment oldPackage = (IPackageFragment) type.getCompilationUnit().getParent(); IPackageFragment newPackage = (IPackageFragment) rtp.getRefactoredJavaElement(oldPackage); assertEquals(oldPackage, newPackage); for (int i = 0; i < someClassMembers.length; i++) { IMember member = (IMember) someClassMembers[i]; IJavaElement refactoredMember = rtp.getRefactoredJavaElement(member); if (member instanceof IMethod && member.getElementName().equals(type.getElementName())) continue; // constructor assertTrue(refactoredMember.exists()); assertEquals(member.getElementName(), refactoredMember.getElementName()); assertFalse(refactoredMember.equals(member)); } }
From source file:org.eclipse.che.jdt.refactoring.session.RenameLinkedModeRefactoringSession.java
License:Open Source License
/** * Creates a rename descriptor./*from w ww . j av a 2 s. co 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.search.SearchManager.java
License:Open Source License
private FindUsagesResponse performFindUsageSearch(IJavaElement element) throws JavaModelException, BadLocationException { JavaSearchScopeFactory factory = JavaSearchScopeFactory.getInstance(); boolean isInsideJRE = factory.isInsideJRE(element); JavaSearchQuery query = new JavaSearchQuery(new ElementQuerySpecification(element, IJavaSearchConstants.REFERENCES, factory.createWorkspaceScope(isInsideJRE), "workspace scope")); NewSearchUI.runQueryInForeground(null, query); ISearchResult result = query.getSearchResult(); JavaSearchResult javaResult = ((JavaSearchResult) result); FindUsagesResponse response = DtoFactory.newDto(FindUsagesResponse.class); Map<String, List<org.eclipse.che.ide.ext.java.shared.dto.search.Match>> mapMaches = new HashMap<>(); JavaElementToDtoConverter converter = new JavaElementToDtoConverter(javaResult); for (Object o : javaResult.getElements()) { IJavaElement javaElement = (IJavaElement) o; IDocument document = null;/*from w w w . ja v a2 s . c o m*/ if (javaElement instanceof IMember) { IMember member = ((IMember) javaElement); if (member.isBinary()) { if (member.getClassFile().getSource() != null) { document = new Document(member.getClassFile().getSource()); } } else { document = getDocument(member.getCompilationUnit()); } } else if (javaElement instanceof IPackageDeclaration) { ICompilationUnit ancestor = (ICompilationUnit) (javaElement) .getAncestor(IJavaElement.COMPILATION_UNIT); document = getDocument(ancestor); } converter.addElementToProjectHierarchy(javaElement); Match[] matches = javaResult.getMatches(o); List<org.eclipse.che.ide.ext.java.shared.dto.search.Match> matchList = new ArrayList<>(); for (Match match : matches) { org.eclipse.che.ide.ext.java.shared.dto.search.Match dtoMatch = DtoFactory .newDto(org.eclipse.che.ide.ext.java.shared.dto.search.Match.class); if (document != null) { IRegion lineInformation = document.getLineInformationOfOffset(match.getOffset()); int offsetInLine = match.getOffset() - lineInformation.getOffset(); Region matchInLine = DtoFactory.newDto(Region.class).withOffset(offsetInLine) .withLength(match.getLength()); dtoMatch.setMatchInLine(matchInLine); dtoMatch.setMatchLineNumber(document.getLineOfOffset(match.getOffset())); dtoMatch.setMatchedLine(document.get(lineInformation.getOffset(), lineInformation.getLength())); } dtoMatch.setFileMatchRegion(DtoFactory.newDto(Region.class).withOffset(match.getOffset()) .withLength(match.getLength())); matchList.add(dtoMatch); } mapMaches.put(javaElement.getHandleIdentifier(), matchList); } List<JavaProject> projects = converter.getProjects(); response.setProjects(projects); response.setMatches(mapMaches); response.setSearchElementLabel(JavaElementLabels.getElementLabel(element, JavaElementLabels.ALL_DEFAULT)); return response; }
From source file:org.eclipse.che.jdt.util.JavaModelUtil.java
License:Open Source License
private static void addAllCus(HashSet<ICompilationUnit> collector, IJavaElement javaElement) throws JavaModelException { switch (javaElement.getElementType()) { case IJavaElement.JAVA_PROJECT: IJavaProject javaProject = (IJavaProject) javaElement; IPackageFragmentRoot[] packageFragmentRoots = javaProject.getPackageFragmentRoots(); for (int i = 0; i < packageFragmentRoots.length; i++) addAllCus(collector, packageFragmentRoots[i]); return;/*from w ww .ja v a2 s . c om*/ case IJavaElement.PACKAGE_FRAGMENT_ROOT: IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) javaElement; if (packageFragmentRoot.getKind() != IPackageFragmentRoot.K_SOURCE) return; IJavaElement[] packageFragments = packageFragmentRoot.getChildren(); for (int j = 0; j < packageFragments.length; j++) addAllCus(collector, packageFragments[j]); return; case IJavaElement.PACKAGE_FRAGMENT: IPackageFragment packageFragment = (IPackageFragment) javaElement; collector.addAll(Arrays.asList(packageFragment.getCompilationUnits())); return; case IJavaElement.COMPILATION_UNIT: collector.add((ICompilationUnit) javaElement); return; default: IJavaElement cu = javaElement.getAncestor(IJavaElement.COMPILATION_UNIT); if (cu != null) collector.add((ICompilationUnit) cu); } }
From source file:org.eclipse.che.plugin.java.server.jdt.refactoring.RenameTypeTest.java
License:Open Source License
private void checkMappers(Refactoring refactoring, IType type, String newCUName, IJavaElement[] someClassMembers) { RenameTypeProcessor rtp = (RenameTypeProcessor) ((RenameRefactoring) refactoring).getProcessor(); ICompilationUnit newUnit = (ICompilationUnit) rtp.getRefactoredJavaElement(type.getCompilationUnit()); TestCase.assertTrue(newUnit.exists()); TestCase.assertTrue(newUnit.getElementName().equals(newCUName)); IFile newFile = (IFile) rtp.getRefactoredResource(type.getResource()); TestCase.assertTrue(newFile.exists()); TestCase.assertTrue(newFile.getName().equals(newCUName)); if ((type.getParent().getElementType() == IJavaElement.COMPILATION_UNIT) && type.getCompilationUnit().getElementName().equals(type.getElementName() + ".java")) { Assert.assertFalse(type.getCompilationUnit().exists()); Assert.assertFalse(type.getResource().exists()); }//from w w w . j av a 2 s . com IPackageFragment oldPackage = (IPackageFragment) type.getCompilationUnit().getParent(); IPackageFragment newPackage = (IPackageFragment) rtp.getRefactoredJavaElement(oldPackage); Assert.assertEquals(oldPackage, newPackage); for (int i = 0; i < someClassMembers.length; i++) { IMember member = (IMember) someClassMembers[i]; IJavaElement refactoredMember = rtp.getRefactoredJavaElement(member); if (member instanceof IMethod && member.getElementName().equals(type.getElementName())) continue; // constructor TestCase.assertTrue(refactoredMember.exists()); Assert.assertEquals(member.getElementName(), refactoredMember.getElementName()); Assert.assertFalse(refactoredMember.equals(member)); } }