private IRegion getRegion(IJavaElement element) { IRegion result = JavaCore.newRegion(); if (element.getElementType() == IJavaElement.JAVA_PROJECT) { // for projects only add the contained source folders try { IPackageFragmentRoot[] packageFragmentRoots = ((IJavaProject) element).getPackageFragmentRoots(); for (IPackageFragmentRoot packageFragmentRoot : packageFragmentRoots) { if (!packageFragmentRoot.isArchive()) { result.add(packageFragmentRoot); } } } catch (JavaModelException e) { LOG.info("Can't read source folders.", e); } } else { result.add(element); } return result; }
private ITypeHierarchy createTypeHierarchy(IJavaElement[] elements, IProgressMonitor pm) throws JavaModelException { if (elements.length == 1 && elements[0].getElementType() == IJavaElement.TYPE) { IType type= (IType)elements[0]; if (fIsSuperTypesOnly) { return type.newSupertypeHierarchy(pm); } else { return type.newTypeHierarchy(pm); } } else { IRegion region= JavaCore.newRegion(); for (int i= 0; i < elements.length; i++) { if (elements[i].getElementType() == IJavaElement.JAVA_PROJECT) { // for projects only add the contained source folders IPackageFragmentRoot[] roots= ((IJavaProject)elements[i]).getPackageFragmentRoots(); for (int j= 0; j < roots.length; j++) { if (!roots[j].isExternal()) { region.add(roots[j]); } } } else { region.add(elements[i]); } } return JavaCore.newTypeHierarchy(region, null, pm); } }
/** * @see IJavaProject */ public ITypeHierarchy newTypeHierarchy( IRegion region, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException { if (region == null) { throw new IllegalArgumentException(Messages.hierarchy_nullRegion); } ICompilationUnit[] workingCopies = JavaModelManager.getJavaModelManager().getWorkingCopies(owner, true/*add primary working copies*/); CreateTypeHierarchyOperation op = new CreateTypeHierarchyOperation(region, workingCopies, null, true); op.runOperation(monitor); return op.getResult(); }
/** * @see IJavaProject */ public ITypeHierarchy newTypeHierarchy( IType type, IRegion region, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException { if (type == null) { throw new IllegalArgumentException(Messages.hierarchy_nullFocusType); } if (region == null) { throw new IllegalArgumentException(Messages.hierarchy_nullRegion); } ICompilationUnit[] workingCopies = JavaModelManager.getJavaModelManager().getWorkingCopies(owner, true/*add primary working copies*/); CreateTypeHierarchyOperation op = new CreateTypeHierarchyOperation(region, workingCopies, type, true/*compute subtypes*/); op.runOperation(monitor); return op.getResult(); }
private IRegion getRegion( IJavaElement element ) throws JavaModelException { IRegion result = JavaCore.newRegion( ); if ( element.getElementType( ) == IJavaElement.JAVA_PROJECT ) { // for projects only add the contained source folders IPackageFragmentRoot[] roots = ( (IJavaProject) element ).getPackageFragmentRoots( ); for ( int i = 0; i < roots.length; i++ ) { if ( !roots[i].isArchive( ) ) { result.add( roots[i] ); } } } else { result.add( element ); } return result; }
private static IFile getClassfile(ITypeBinding typeBinding) throws CoreException { // bug 191943 IType type = (IType) typeBinding.getJavaElement(); if (type == null || type.getCompilationUnit() == null || type.getJavaProject() == null || ProjectsManager.DEFAULT_PROJECT_NAME.equals(type.getJavaProject().getProject().getName())) { return null; } IRegion region = JavaCore.newRegion(); region.add(type.getCompilationUnit()); String name = typeBinding.getBinaryName(); if (name != null) { int packStart = name.lastIndexOf('.'); if (packStart != -1) { name = name.substring(packStart + 1); } } else { throw new CoreException(new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID, CorrectionMessages.SerialVersionHashOperation_error_classnotfound)); } name += ".class"; //$NON-NLS-1$ IResource[] classFiles = JavaCore.getGeneratedResources(region, false); for (int i = 0; i < classFiles.length; i++) { IResource resource = classFiles[i]; if (resource.getType() == IResource.FILE && resource.getName().equals(name) && resource.exists()) { try (InputStream contents = ((IFile) resource).getContents()) { } catch (Exception e) { continue; } return (IFile) resource; } } return null; // throw new CoreException(new Status(IStatus.ERROR, JavaLanguageServerPlugin.PLUGIN_ID, CorrectionMessages.SerialVersionHashOperation_error_classnotfound)); }
private void createHierarchyOfDeclarations(IProgressMonitor pm, WorkingCopyOwner owner) throws JavaModelException { IRegion region = JavaCore.newRegion(); for (Iterator<IMethod> iter = fDeclarations.iterator(); iter.hasNext();) { IType declaringType = iter.next().getDeclaringType(); region.add(declaringType); } fHierarchy = JavaCore.newTypeHierarchy(region, owner, pm); }
private void createHierarchyOfDeclarations(IProgressMonitor pm, WorkingCopyOwner owner) throws JavaModelException { IRegion region = JavaCore.newRegion(); for (Iterator<IMethod> iter = fDeclarations.iterator(); iter.hasNext(); ) { IType declaringType = iter.next().getDeclaringType(); region.add(declaringType); } fHierarchy = JavaCore.newTypeHierarchy(region, owner, pm); }
private void createHierarchyOfDeclarations(IProgressMonitor pm, WorkingCopyOwner owner) throws JavaModelException { IRegion region= JavaCore.newRegion(); for (Iterator<IMethod> iter= fDeclarations.iterator(); iter.hasNext();) { IType declaringType= iter.next().getDeclaringType(); region.add(declaringType); } fHierarchy= JavaCore.newTypeHierarchy(region, owner, pm); }
private static IFile getClassfile(ITypeBinding typeBinding) throws CoreException { // bug 191943 IType type= (IType) typeBinding.getJavaElement(); if (type == null || type.getCompilationUnit() == null) { return null; } IRegion region= JavaCore.newRegion(); region.add(type.getCompilationUnit()); String name= typeBinding.getBinaryName(); if (name != null) { int packStart= name.lastIndexOf('.'); if (packStart != -1) { name= name.substring(packStart + 1); } } else { throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, CorrectionMessages.SerialVersionHashOperation_error_classnotfound)); } name += ".class"; //$NON-NLS-1$ IResource[] classFiles= JavaCore.getGeneratedResources(region, false); for (int i= 0; i < classFiles.length; i++) { IResource resource= classFiles[i]; if (resource.getType() == IResource.FILE && resource.getName().equals(name)) { return (IFile) resource; } } throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, CorrectionMessages.SerialVersionHashOperation_error_classnotfound)); }
/** * Deletes this element from its compilation unit. * @see MultiOperation */ protected void processElement(IJavaElement element) throws JavaModelException { ICompilationUnit cu = (ICompilationUnit) element; // keep track of the import statements - if all are removed, delete // the import container (and report it in the delta) int numberOfImports = cu.getImports().length; JavaElementDelta delta = new JavaElementDelta(cu); IJavaElement[] cuElements = ((IRegion) this.childrenToRemove.get(cu)).getElements(); for (int i = 0, length = cuElements.length; i < length; i++) { IJavaElement e = cuElements[i]; if (e.exists()) { deleteElement(e, cu); delta.removed(e); if (e.getElementType() == IJavaElement.IMPORT_DECLARATION) { numberOfImports--; if (numberOfImports == 0) { delta.removed(cu.getImportContainer()); } } } } if (delta.getAffectedChildren().length > 0) { cu.save(getSubProgressMonitor(1), this.force); if (!cu.isWorkingCopy()) { // if unit is working copy, then save will have already fired the delta addDelta(delta); setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE); } } }
/** * @see MultiOperation */ protected void verify(IJavaElement element) throws JavaModelException { IJavaElement[] children = ((IRegion) this.childrenToRemove.get(element)).getElements(); for (int i = 0; i < children.length; i++) { IJavaElement child = children[i]; if (child.getCorrespondingResource() != null) error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, child); if (child.isReadOnly()) error(IJavaModelStatusConstants.READ_ONLY, child); } }
/** * @see IJavaProject */ public ITypeHierarchy newTypeHierarchy( IRegion region, IProgressMonitor monitor) throws JavaModelException { return newTypeHierarchy(region, DefaultWorkingCopyOwner.PRIMARY, monitor); }
/** * @see IJavaProject */ public ITypeHierarchy newTypeHierarchy( IType type, IRegion region, IProgressMonitor monitor) throws JavaModelException { return newTypeHierarchy(type, region, DefaultWorkingCopyOwner.PRIMARY, monitor); }
@Override public ITypeHierarchy newTypeHierarchy(IRegion region, IProgressMonitor monitor) throws JavaModelException { throw new UnsupportedOperationException(); }
@Override public ITypeHierarchy newTypeHierarchy( IRegion region, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException { throw new UnsupportedOperationException(); }
@Override public ITypeHierarchy newTypeHierarchy(IType type, IRegion region, IProgressMonitor monitor) throws JavaModelException { throw new UnsupportedOperationException(); }
@Override public ITypeHierarchy newTypeHierarchy( IType type, IRegion region, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException { throw new UnsupportedOperationException(); }
private String getAnnotations( IJavaElement element, ITypeRoot editorInputElement, IRegion hoverRegion) throws URISyntaxException, JavaModelException { if (!(element instanceof IPackageFragment)) { if (!(element instanceof IAnnotatable)) return null; if (((IAnnotatable) element).getAnnotations().length == 0) return null; } IBinding binding = null; // TODO ASTNode node = null; // getHoveredASTNode(editorInputElement, hoverRegion); if (node == null) { // todo use ast ported parser,that uses our java model // ASTParser p = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL); // p.setProject(element.getJavaProject()); // p.setBindingsRecovery(true); // try { // binding = p.createBindings(new IJavaElement[]{element}, null)[0]; // } catch (OperationCanceledException e) { // return null; // } } else { binding = resolveBinding(node); } if (binding == null) return null; IAnnotationBinding[] annotations = binding.getAnnotations(); if (annotations.length == 0) return null; StringBuffer buf = new StringBuffer(); for (int i = 0; i < annotations.length; i++) { // TODO: skip annotations that don't have an @Documented annotation? addAnnotation(buf, element, annotations[i]); buf.append("<br>"); // $NON-NLS-1$ } return buf.toString(); }
private List<String> findClassesInContainer( IJavaElement container, String testMethodAnnotation, String testClassAnnotation) { List<String> result = new LinkedList<>(); IRegion region = getRegion(container); try { ITypeHierarchy hierarchy = JavaCore.newTypeHierarchy(region, null, null); IType[] allClasses = hierarchy.getAllClasses(); // search for all types with references to RunWith and Test and all subclasses HashSet<IType> candidates = new HashSet<>(allClasses.length); SearchRequestor requestor = new AnnotationSearchRequestor(hierarchy, candidates); IJavaSearchScope scope = SearchEngine.createJavaSearchScope(allClasses, IJavaSearchScope.SOURCES); int matchRule = SearchPattern.R_CASE_SENSITIVE; SearchPattern testPattern = SearchPattern.createPattern( testMethodAnnotation, IJavaSearchConstants.ANNOTATION_TYPE, IJavaSearchConstants.ANNOTATION_TYPE_REFERENCE, matchRule); SearchPattern runWithPattern = isNullOrEmpty(testClassAnnotation) ? testPattern : SearchPattern.createPattern( testClassAnnotation, IJavaSearchConstants.ANNOTATION_TYPE, IJavaSearchConstants.ANNOTATION_TYPE_REFERENCE, matchRule); SearchPattern annotationsPattern = SearchPattern.createOrPattern(runWithPattern, testPattern); SearchParticipant[] searchParticipants = new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()}; new SearchEngine().search(annotationsPattern, searchParticipants, scope, requestor, null); // find all classes in the region for (IType candidate : candidates) { if (isAccessibleClass(candidate) && !Flags.isAbstract(candidate.getFlags()) && region.contains(candidate)) { result.add(candidate.getFullyQualifiedName()); } } } catch (CoreException e) { LOG.info("Can't build project hierarchy.", e); } return result; }
/** * Constructs an operation to create a type hierarchy for the * given type within the specified region, in the context of * the given project. */ public CreateTypeHierarchyOperation(IRegion region, ICompilationUnit[] workingCopies, IType element, boolean computeSubtypes) { super(element); this.typeHierarchy = new RegionBasedTypeHierarchy(region, workingCopies, element, computeSubtypes); }