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.pde.api.tools.ui.internal.wizards.CompareOperation.java
License:Open Source License
/** * @param structuredSelection/* w w w .ja va 2 s.com*/ * @param monitor * @return the scope */ public static ApiScope walkStructureSelection(IStructuredSelection structuredSelection, IProgressMonitor monitor) { Object[] selected = structuredSelection.toArray(); ApiScope scope = new ApiScope(); IApiBaseline workspaceBaseline = ApiBaselineManager.getManager().getWorkspaceBaseline(); if (workspaceBaseline == null) { return scope; } Arrays.sort(selected, new Comparator<Object>() { @Override public int compare(Object o1, Object o2) { if (o1 instanceof IJavaElement && o2 instanceof IJavaElement) { IJavaElement element = (IJavaElement) o1; IJavaElement element2 = (IJavaElement) o2; return element.getElementType() - element2.getElementType(); } return 0; } }); int length = selected.length; for (int i = 0; i < length; i++) { Object currentSelection = selected[i]; if (currentSelection instanceof IJavaElement) { IJavaElement element = (IJavaElement) currentSelection; IJavaProject javaProject = element.getJavaProject(); try { switch (element.getElementType()) { case IJavaElement.COMPILATION_UNIT: { ICompilationUnit compilationUnit = (ICompilationUnit) element; IApiComponent apiComponent = workspaceBaseline .getApiComponent(javaProject.getElementName()); if (apiComponent != null) { addElementFor(compilationUnit, apiComponent, scope); } break; } case IJavaElement.PACKAGE_FRAGMENT: { IPackageFragment fragment = (IPackageFragment) element; IApiComponent apiComponent = workspaceBaseline .getApiComponent(javaProject.getElementName()); IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) fragment .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); boolean isArchive = false; if (packageFragmentRoot != null) { isArchive = packageFragmentRoot.isArchive(); } if (apiComponent != null) { addElementFor(fragment, isArchive, apiComponent, scope); } break; } case IJavaElement.PACKAGE_FRAGMENT_ROOT: { IPackageFragmentRoot fragmentRoot = (IPackageFragmentRoot) element; IApiComponent apiComponent = workspaceBaseline .getApiComponent(javaProject.getElementName()); if (apiComponent != null) { addElementFor(fragmentRoot, apiComponent, scope); } break; } case IJavaElement.JAVA_PROJECT: IApiComponent apiComponent = workspaceBaseline .getApiComponent(javaProject.getElementName()); if (apiComponent != null) { scope.addElement(apiComponent); } break; default: break; } } catch (JavaModelException e) { ApiPlugin.log(e); } catch (CoreException e) { ApiPlugin.log(e); } } } return scope; }
From source file:org.eclipse.pde.ds.internal.annotations.DSAnnotationCompilationParticipant.java
License:Open Source License
@Override public void buildFinished(IJavaProject project) { ProjectContext projectContext = processingContext.remove(project); if (projectContext != null) { ProjectState state = projectContext.getState(); // check if unprocessed CUs still exist; if not, their mapped files are now abandoned HashSet<String> abandoned = new HashSet<>(projectContext.getAbandoned()); for (String cuKey : projectContext.getUnprocessed()) { boolean exists = false; try { IJavaElement cu = project.findElement(new Path(cuKey)); IResource file;//from w w w. j av a 2s. com if (cu != null && cu.getElementType() == IJavaElement.COMPILATION_UNIT && (file = cu.getResource()) != null && file.exists()) exists = true; } catch (JavaModelException e) { Activator.log(e); } if (!exists) { if (debug.isDebugging()) debug.trace(String.format("Mapped CU %s no longer exists.", cuKey)); //$NON-NLS-1$ Collection<String> dsKeys = state.removeMappings(cuKey); if (dsKeys != null) abandoned.addAll(dsKeys); } } // retain abandoned files that are still mapped elsewhere HashSet<String> retained = new HashSet<>(); for (String cuKey : state.getCompilationUnits()) { Collection<String> dsKeys = state.getModelFiles(cuKey); if (dsKeys != null) retained.addAll(dsKeys); } abandoned.removeAll(retained); if (projectContext.isChanged()) { try { saveState(project.getProject(), state); } catch (IOException e) { Activator.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Error saving file mappings.", e)); //$NON-NLS-1$ } } // delete all abandoned files ArrayList<IStatus> deleteStatuses = new ArrayList<>(2); for (String dsKey : abandoned) { IPath path = Path.fromPortableString(dsKey); if (debug.isDebugging()) debug.trace(String.format("Deleting %s", path)); //$NON-NLS-1$ IFile file = PDEProject.getBundleRelativeFile(project.getProject(), path); if (file.exists()) { try { file.delete(true, null); } catch (CoreException e) { deleteStatuses.add(e.getStatus()); } } } if (!deleteStatuses.isEmpty()) Activator.log(new MultiStatus(Activator.PLUGIN_ID, 0, deleteStatuses.toArray(new IStatus[deleteStatuses.size()]), "Error deleting generated files.", null)); //$NON-NLS-1$ if (!retained.isEmpty() || !abandoned.isEmpty()) updateProject(project.getProject(), retained, abandoned); } if (debug.isDebugging()) debug.trace(String.format("Build finished for project: %s", project.getElementName())); //$NON-NLS-1$ }
From source file:org.eclipse.recommenders.internal.rcp.JavaElementSelections.java
License:Open Source License
public static JavaElementSelectionLocation resolveSelectionLocationFromJavaElement(final IJavaElement element) { ensureIsNotNull(element);/* w w w. j a v a2s . c o m*/ switch (element.getElementType()) { case IJavaElement.CLASS_FILE: case IJavaElement.COMPILATION_UNIT: case IJavaElement.PACKAGE_DECLARATION: case IJavaElement.IMPORT_DECLARATION: case IJavaElement.IMPORT_CONTAINER: case IJavaElement.TYPE: return TYPE_DECLARATION; case IJavaElement.METHOD: case IJavaElement.INITIALIZER: return METHOD_DECLARATION; case IJavaElement.FIELD: return FIELD_DECLARATION; case IJavaElement.LOCAL_VARIABLE: // shouldn't happen in a viewer selection, right? return METHOD_BODY; case IJavaElement.JAVA_MODEL: case IJavaElement.PACKAGE_FRAGMENT: case IJavaElement.PACKAGE_FRAGMENT_ROOT: case IJavaElement.ANNOTATION: default: return JavaElementSelectionLocation.UNKNOWN; } }
From source file:org.eclipse.wst.xml.ui.internal.contentassist.tapestry.TapestryRootComponentsProposalComputer.java
License:Open Source License
public List<Template> getRootComponentsAttributes(IProject project, String contextTypeId, String nodeName) { try {/* ww w. ja v a2 s . co m*/ IJavaElement[] elements = getComponentsJavaElements(project); components.clear(); for (IJavaElement ele : elements) { if (ele.getElementType() == IJavaElement.COMPILATION_UNIT && ele.getElementName().endsWith(".java")) { String name = ele.getElementName().substring(0, ele.getElementName().indexOf('.')); if (("t:" + name).toLowerCase().equals(nodeName.toLowerCase())) { goThroughClass((ICompilationUnit) ele, contextTypeId); } } } return components; } catch (JavaModelException e) { e.printStackTrace(); } return null; }
From source file:org.eclipse.wst.xml.ui.internal.contentassist.tapestry.TapestryRootComponentsProposalComputer.java
License:Open Source License
/** * Get components template list in situation:<span t:type="${component name here}"></span> * /*from w ww .j av a 2 s . c o m*/ * @param project * @param contextTypeId * @return */ public List<Template> getRootComponentsNameTemplates(IProject project, String contextTypeId) { try { IJavaElement[] elements = getComponentsJavaElements(project); List<Template> components = new ArrayList<Template>(); for (IJavaElement ele : elements) { if (ele.getElementType() == IJavaElement.COMPILATION_UNIT && ele.getElementName().endsWith(".java")) { TapestryCoreComponents component = new TapestryCoreComponents(); String name = ele.getElementName().substring(0, ele.getElementName().indexOf('.')); component.setName(name); component.setElementLabel("t:" + name.toLowerCase()); components.add(new Template("t:" + component.getName(), buildDescription(component, "root package"), contextTypeId, component.getName(), true)); } } return components; } catch (JavaModelException e) { e.printStackTrace(); } return null; }
From source file:org.eclipse.wst.xml.ui.internal.contentassist.tapestry.TapestryRootComponentsProposalComputer.java
License:Open Source License
public List<Template> getRootComponentsTemplates(IProject project, String contextTypeId, int type) { try {/* w w w .j a v a 2 s.c om*/ IJavaElement[] elements = getComponentsJavaElements(project); List<Template> components = new ArrayList<Template>(); for (IJavaElement ele : elements) { if (ele.getElementType() == IJavaElement.COMPILATION_UNIT && ele.getElementName().endsWith(".java")) { TapestryCoreComponents component = new TapestryCoreComponents(); String name = ele.getElementName().substring(0, ele.getElementName().indexOf('.')); component.setName(name); component.setElementLabel("t:" + name.toLowerCase()); components.add( new Template("t:" + component.getName(), buildDescription(component, "root package"), contextTypeId, buildInsertCode(component, type), true)); } } return components; } catch (JavaModelException e) { e.printStackTrace(); } return null; }
From source file:org.eclipse.wst.xml.ui.internal.contentassist.tapestry.TapestryRootComponentsProposalComputer.java
License:Open Source License
/** * Get custom component attribute templates * /*from www. ja v a2 s .c o m*/ * @param project * @param contextTypeId * @param nodeName * @param tapestryClassLoader * @return */ public List<Template> getCustomComponentsAttributes(IProject project, String contextTypeId, String nodeName, TapestryClassLoader tapestryClassLoader) { components.clear(); String prefix = nodeName.substring(0, nodeName.indexOf(':')); try { final IFile res = project.getFile(TapestryContants.CUSTOM_COMPONENTS_FILE); if (res == null || prefix == null) return components; List<ComponentPackage> packageList = loadCustomComponentsPackageListWithPrefix(res, prefix); IPackageFragmentRoot[] roots = JavaCore.create(project).getAllPackageFragmentRoots(); for (ComponentPackage cp : packageList) { for (IPackageFragmentRoot root : roots) { if (cp.isArchive() == root.isArchive() && cp.getFragmentRoot().equals(root.getElementName())) { IPackageFragment packInstance = root.getPackageFragment(cp.getPath()); if (!root.isArchive()) { //If current custom component is in source directory if (packInstance != null) { IJavaElement[] elements = packInstance.getChildren(); for (IJavaElement ele : elements) { if (ele.getElementType() == IJavaElement.COMPILATION_UNIT && ele.getElementName().endsWith(".java")) { String name = ele.getElementName().substring(0, ele.getElementName().indexOf('.')); if ((prefix + ":" + name).toLowerCase().equals(nodeName)) { goThroughClass((ICompilationUnit) ele, contextTypeId); return components; } } } } } else if (packInstance instanceof PackageFragment) { //Current custom component is in jar files for (Object packo : ((PackageFragment) packInstance) .getChildrenOfType(IJavaElement.CLASS_FILE)) { ClassFile packi = (ClassFile) packo; String className = packi.getElementName().substring(0, packi.getElementName().indexOf('.')); if (className.indexOf('$') < 0 && (prefix + ":" + className.toLowerCase()).equals(nodeName)) { TapestryCoreComponents component = null; try { component = tapestryClassLoader.loadComponentAttributesFromClassFile(root, prefix, packi); } catch (ClassFormatException e) { e.printStackTrace(); } if (component != null) { for (String paramName : component.getPamameters()) { Template template = new Template(paramName, "add attribute " + paramName, contextTypeId, buildAttributeInsertCode(paramName), true); components.add(template); } return components; } } } } } } } } catch (JavaModelException e) { e.printStackTrace(); } return null; }
From source file:org.eclipse.wst.xml.ui.internal.contentassist.tapestry.TapestryRootComponentsProposalComputer.java
License:Open Source License
private List<String> getCustomComponentsNameList(IPackageFragmentRoot[] roots, ComponentPackage cp) { List<String> componentNameList = new ArrayList<String>(); try {// w w w. j a va2 s . c om for (IPackageFragmentRoot root : roots) { if (root instanceof JarPackageFragmentRoot == cp.isArchive() && root.getElementName().equals(cp.getFragmentRoot())) { if (!root.isArchive()) { // Load custom components from source directory IPackageFragment packInstance = root.getPackageFragment(cp.getPath()); if (packInstance != null) { IJavaElement[] elements = packInstance.getChildren(); for (IJavaElement ele : elements) { if (ele.getElementType() == IJavaElement.COMPILATION_UNIT && ele.getElementName().endsWith(".java")) { String name = ele.getElementName().substring(0, ele.getElementName().indexOf('.')); componentNameList.add(name); } } } } else { // Load custom components from jar files for (IJavaElement pack : root.getChildren()) { if (pack != null && pack instanceof PackageFragment && pack.getElementName().equals(cp.getPath())) { for (Object packo : ((PackageFragment) pack) .getChildrenOfType(IJavaElement.CLASS_FILE)) { ClassFile packi = (ClassFile) packo; String itemName = packi.getElementName(); if (itemName.indexOf('$') < 0 && itemName.endsWith(".class")) componentNameList.add(itemName.substring(0, itemName.length() - 6)); } break; } } } return componentNameList; } } } catch (JavaModelException e) { e.printStackTrace(); } return componentNameList; }
From source file:org.eclipse.xtend.ide.javaconverter.ConvertJavaCodeHandler.java
License:Open Source License
private void collectCompilationUnits(IJavaElement elem, Set<ICompilationUnit> result) { try {/* w ww . ja va 2 s.co m*/ switch (elem.getElementType()) { case IJavaElement.TYPE: if (elem.getParent().getElementType() == IJavaElement.COMPILATION_UNIT) { result.add((ICompilationUnit) elem.getParent()); } break; case IJavaElement.COMPILATION_UNIT: result.add((ICompilationUnit) elem); break; case IJavaElement.PACKAGE_FRAGMENT: Collections.addAll(result, ((IPackageFragment) elem).getCompilationUnits()); break; case IJavaElement.PACKAGE_FRAGMENT_ROOT: for (IJavaElement child : ((IPackageFragmentRoot) elem).getChildren()) { collectCompilationUnits(child, result); } break; } } catch (JavaModelException e) { } }
From source file:org.eclipse.xtext.common.types.ui.navigation.LinkToOriginDetector.java
License:Open Source License
@Override public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) { try {//from ww w.jav a 2 s . c o m // very pessimistic guards - most things should never happen ITextEditor textEditor = (ITextEditor) getAdapter(ITextEditor.class); if (textEditor == null) return null; IEditorInput editorInput = textEditor.getEditorInput(); if (editorInput == null) return null; IJavaElement adaptedJavaElement = (IJavaElement) Platform.getAdapterManager().getAdapter(editorInput, IJavaElement.class); if (adaptedJavaElement == null) return null; ICompilationUnit compilationUnit = (ICompilationUnit) adaptedJavaElement .getAncestor(IJavaElement.COMPILATION_UNIT); if (compilationUnit == null) return null; try { IRegion selectedWord = org.eclipse.jdt.internal.ui.text.JavaWordFinder .findWord(textViewer.getDocument(), region.getOffset()); // the actual implementation - find the referenced Java type under the cursor and link // to its origin if it's contained in a 'derived' resource IJavaElement[] javaElements = compilationUnit.codeSelect(selectedWord.getOffset(), selectedWord.getLength()); for (IJavaElement javaElement : javaElements) { /** * if IDE 3.8 is available the default 'Open Declaration' navigation will already open the original editor * So we don't need the additional hyperlinks. */ boolean provideHyperlinkOnReferences = !is_ORG_ECLIPSE_UI_IDE_3_8_Enabled() || compilationUnit.equals(((IMember) javaElement).getCompilationUnit()); if (javaElement instanceof IMember && provideHyperlinkOnReferences) { IMember selectedMember = (IMember) javaElement; IResource resource = selectedMember.getResource(); if (resource instanceof IFile) { ITrace traceToSource = traceInformation.getTraceToSource((IStorage) resource); if (traceToSource == null) { return null; } ILocationInResource sourceInformation = IterableExtensions .head(traceToSource.getAllAssociatedLocations()); if (sourceInformation != null) { try { URI resourceURI = sourceInformation.getAbsoluteResourceURI().getURI(); if (resourceURI != null) { IResourceServiceProvider serviceProvider = serviceProviderRegistry .getResourceServiceProvider(resourceURI); if (serviceProvider == null) return null; LinkToOriginProvider provider = serviceProvider .get(LinkToOriginProvider.class); LinkToOrigin hyperlink = provider.createLinkToOrigin(sourceInformation, selectedWord, selectedMember, compilationUnit, Collections.<LinkToOrigin>emptyList()); if (hyperlink != null) { return new IHyperlink[] { hyperlink }; } } } catch (IllegalArgumentException e) { /* invalid URI - ignore */ } } return null; } } } return null; } catch (JavaModelException e) { return null; } } catch (Throwable t) { return null; } }