List of usage examples for org.eclipse.jdt.core IPackageFragmentRoot getRawClasspathEntry
IClasspathEntry getRawClasspathEntry() throws JavaModelException;
From source file:org.eclipse.ajdt.internal.buildpath.UpdateInpathRestriction.java
License:Open Source License
public void selectionChanged(IAction action, ISelection sel) { boolean enable = false; if (sel instanceof IStructuredSelection) { IStructuredSelection selection = (IStructuredSelection) sel; Object element = selection.getFirstElement(); try {//from w w w .j a va2 s . c om if (element instanceof IPackageFragmentRoot) { IPackageFragmentRoot root = (IPackageFragmentRoot) element; project = root.getJavaProject().getProject(); cpEntry = root.getRawClasspathEntry(); if (cpEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) { fileName = root.getElementName(); enable = AspectJCorePreferences.isOnInpath(cpEntry); } else { fileName = null; cpEntry = null; project = null; enable = false; } } else { enable = false; } } catch (JavaModelException e) { } action.setEnabled(enable); } }
From source file:org.eclipse.ajdt.internal.core.ClasspathModifier.java
License:Open Source License
/** * Find out whether the <code>IResource</code> excluded or not. * // www . ja v a 2 s . c om * @param resource * the resource to be checked * @param project * the Java project * @return <code>true</code> if the resource is excluded, <code> * false</code> * otherwise * @throws JavaModelException */ public static boolean isExcluded(IResource resource, IJavaProject project) throws JavaModelException { IPackageFragmentRoot root = getFragmentRoot(resource, project, null); if (root == null) return false; String fragmentName = getName(resource.getFullPath(), root.getPath()); fragmentName = completeName(fragmentName); IClasspathEntry entry = root.getRawClasspathEntry(); return entry != null && contains(new Path(fragmentName), entry.getExclusionPatterns()); }
From source file:org.eclipse.ajdt.internal.core.ClasspathModifier.java
License:Open Source License
/** * Find out whether one of the <code>IResource</code>'s parents * is excluded.//from w ww .j a va 2 s. c o m * * @param resource check the resources parents whether they are * excluded or not * @param project the Java project * @return <code>true</code> if there is an excluded parent, * <code>false</code> otherwise * @throws JavaModelException */ public static boolean parentExcluded(IResource resource, IJavaProject project) throws JavaModelException { if (resource.getFullPath().equals(project.getPath())) return false; IPackageFragmentRoot root = getFragmentRoot(resource, project, null); if (root == null) { return true; } IPath path = resource.getFullPath().removeFirstSegments(root.getPath().segmentCount()); IClasspathEntry entry = root.getRawClasspathEntry(); if (entry == null) return true; // there is no build path entry, this is equal to the fact that the parent is excluded while (path.segmentCount() > 0) { if (contains(path, entry.getExclusionPatterns())) return true; path = path.removeLastSegments(1); } return false; }
From source file:org.eclipse.ajdt.internal.launching.LTWUtils.java
License:Open Source License
/** * Generate one aop-ajc.xml file for each source directory in the given project. * The aop-ajc.xml files will list all concrete aspects included in the active * build configuration./*from w w w .jav a 2s .co m*/ * @param project */ public static void generateLTWConfigFile(IJavaProject project) { try { // Get all the source folders in the project IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots(); for (int i = 0; i < roots.length; i++) { IPackageFragmentRoot root = roots[i]; if (!(root instanceof JarPackageFragmentRoot) && root.getJavaProject().equals(project)) { List aspects = getAspects(root); String path; if (root.getElementName().trim().equals("")) { //$NON-NLS-1$ path = AOP_XML_LOCATION; } else { path = root.getElementName().trim().concat("/").concat(AOP_XML_LOCATION); //$NON-NLS-1$ } IFile ltwConfigFile = (IFile) project.getProject().findMember(path); // If the source folder does not already contain an aop-ajc.xml file: if (ltwConfigFile == null) { if (aspects.size() != 0) { // If there are aspects in the list // Create the META-INF folder and the aop-ajc.xml file IFolder metainf = (IFolder) ((Workspace) ResourcesPlugin.getWorkspace()).newResource( project.getPath().append("/" + root.getElementName() + "/META-INF"), //$NON-NLS-1$ //$NON-NLS-2$ IResource.FOLDER); IFile aopFile = (IFile) ((Workspace) ResourcesPlugin.getWorkspace()) .newResource(project.getPath().append(path), IResource.FILE); if (metainf == null || !metainf.exists()) { metainf.create(true, true, null); } aopFile.create(new ByteArrayInputStream(new byte[0]), true, null); project.getProject().refreshLocal(4, null); // Add the xml content to the aop-ajc.xml file addAspectsToLTWConfigFile(false, aspects, aopFile); copyToOutputFolder(aopFile, project, root.getRawClasspathEntry()); } // Otherwise update the existing file } else { addAspectsToLTWConfigFile(true, aspects, ltwConfigFile); copyToOutputFolder(ltwConfigFile, project, root.getRawClasspathEntry()); } } } } catch (Exception e) { } }
From source file:org.eclipse.ajdt.internal.ui.wizards.exports.AJJarFileExportOperation.java
License:Open Source License
/** * Returns an iterator on a list with files that correspond to the * passed file and that are on the classpath of its project. * * @param file the file for which to find the corresponding classpath resources * @param pathInJar the path that the file has in the JAR (i.e. project and source folder segments removed) * @param javaProject the javaProject that contains the file * @param pkgRoot the package fragment root that contains the file * @return the iterator over the corresponding classpath files for the given file *//*w w w.j av a 2 s. co m*/ protected Iterator<IFile> filesOnClasspath(IFile file, IPath pathInJar, IJavaProject javaProject, IPackageFragmentRoot pkgRoot, IProgressMonitor progressMonitor) throws CoreException { // Allow JAR Package to provide its own strategy IFile[] classFiles = fJarPackage.findClassfilesFor(file); if (classFiles != null) return Arrays.asList(classFiles).iterator(); if (!isJavaFile(file)) return Collections.<IFile>emptyList().iterator(); IPath outputPath = null; if (pkgRoot != null) { IClasspathEntry cpEntry = pkgRoot.getRawClasspathEntry(); if (cpEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) outputPath = cpEntry.getOutputLocation(); } if (outputPath == null) // Use default output location outputPath = javaProject.getOutputLocation(); IContainer outputContainer; if (javaProject.getProject().getFullPath().equals(outputPath)) outputContainer = javaProject.getProject(); else { outputContainer = createFolderHandle(outputPath); if (outputContainer == null || !outputContainer.isAccessible()) { String msg = JarPackagerMessages.JarFileExportOperation_outputContainerNotAccessible; throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, msg, null)); } } // Java CU - search files with .class ending boolean hasErrors = hasCompileErrors(file); boolean hasWarnings = hasCompileWarnings(file); boolean canBeExported = canBeExported(hasErrors, hasWarnings); if (!canBeExported) return Collections.<IFile>emptyList().iterator(); reportPossibleCompileProblems(file, hasErrors, hasWarnings, canBeExported); IContainer classContainer = outputContainer; if (pathInJar.segmentCount() > 1) classContainer = outputContainer.getFolder(pathInJar.removeLastSegments(1)); if (fExportedClassContainers.contains(classContainer)) return Collections.<IFile>emptyList().iterator(); if (fClassFilesMapContainer == null || !fClassFilesMapContainer.equals(classContainer)) { fJavaNameToClassFilesMap = buildJavaToClassMap(classContainer); if (fJavaNameToClassFilesMap == null) { // Could not fully build map. fallback is to export whole directory IPath location = classContainer.getLocation(); String containerName = ""; //$NON-NLS-1$ if (location != null) containerName = location.toFile().toString(); String msg = Messages.format( JarPackagerMessages.JarFileExportOperation_missingSourceFileAttributeExportedAll, containerName); addInfo(msg, null); fExportedClassContainers.add(classContainer); return getClassesIn(classContainer); } fClassFilesMapContainer = classContainer; } ArrayList classFileList = (ArrayList) fJavaNameToClassFilesMap.get(file.getName()); if (classFileList == null || classFileList.isEmpty()) { String msg = Messages.format( JarPackagerMessages.JarFileExportOperation_classFileOnClasspathNotAccessible, file.getFullPath()); throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, msg, null)); } return classFileList.iterator(); }
From source file:org.eclipse.ajdt.internal.ui.wizards.exports.AJJarPackageWizardPage.java
License:Open Source License
protected boolean validateSourceGroup() { if (!(fExportClassFilesCheckbox.getSelection() || fExportOutputFoldersCheckbox.getSelection() || fExportJavaFilesCheckbox.getSelection())) { setErrorMessage(JarPackagerMessages.JarPackageWizardPage_error_noExportTypeChecked); return false; }/* w w w .j a v a2s . co m*/ if (getSelectedResources().size() == 0) { if (getErrorMessage() != null) setErrorMessage(null); return false; } if (fExportClassFilesCheckbox.getSelection() || fExportOutputFoldersCheckbox.getSelection()) return true; // Source file only export - check if there are source files Iterator iter = getSelectedResourcesIterator(); while (iter.hasNext()) { Object element = iter.next(); if (element instanceof IClassFile) { IPackageFragmentRoot root = (IPackageFragmentRoot) ((IClassFile) element) .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); if (root == null) continue; IClasspathEntry cpEntry; try { cpEntry = root.getRawClasspathEntry(); } catch (JavaModelException e) { continue; } if (cpEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { return true; } } else { return true; } } if (getErrorMessage() != null) setErrorMessage(null); return false; }
From source file:org.eclipse.buildship.core.launch.internal.LaunchConfigurationScope.java
License:Open Source License
/** * Creates a launch configuration scope from the target launch configuration. If the scope * information cannot be calculated then the result scope doesn't filter any entries. * * @param configuration the target launch configuration * @return the created scope//from w w w. j av a 2 s. c o m */ public static LaunchConfigurationScope from(ILaunchConfiguration configuration) { Set<String> result = Sets.newHashSet(); try { Set<IPackageFragmentRoot> soureFolders = SupportedLaunchConfigType.collectSourceFolders(configuration); for (IPackageFragmentRoot sourceFolder : soureFolders) { Set<String> scope = scopesFor(sourceFolder.getRawClasspathEntry()); if (scope == null) { return INCLUDE_ALL; } result.addAll(scope); } return new FilteringLaunchConfigurationScope(result); } catch (CoreException e) { CorePlugin.logger().warn("Cannot collect dependency scope information for launch configuration " + configuration.getName(), e); return INCLUDE_ALL; } }
From source file:org.eclipse.e4.demo.simpleide.jdt.internal.editor.viewer.JavaElementLabelComposer.java
License:Open Source License
private boolean appendVariableLabel(IPackageFragmentRoot root, long flags) { try {/* w ww. j av a 2 s. c om*/ IClasspathEntry rawEntry = root.getRawClasspathEntry(); if (rawEntry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) { IClasspathEntry entry = JavaModelUtil.getClasspathEntry(root); if (entry.getReferencingEntry() != null) { return false; // not the variable entry itself, but a // referenced entry } IPath path = rawEntry.getPath().makeRelative(); if (getFlag(flags, JavaElementLabels.REFERENCED_ROOT_POST_QUALIFIED)) { int segements = path.segmentCount(); if (segements > 0) { fBuffer.append(path.segment(segements - 1)); if (segements > 1) { int offset = fBuffer.length(); fBuffer.append(JavaElementLabels.CONCAT_STRING); fBuffer.append(path.removeLastSegments(1).toOSString()); if (getFlag(flags, JavaElementLabels.COLORIZE)) { fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE); } } } else { fBuffer.append(path.toString()); } } else { fBuffer.append(path.toString()); } int offset = fBuffer.length(); fBuffer.append(JavaElementLabels.CONCAT_STRING); if (root.isExternal()) fBuffer.append(root.getPath().toOSString()); else fBuffer.append(root.getPath().makeRelative().toString()); if (getFlag(flags, JavaElementLabels.COLORIZE)) { fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE); } return true; } } catch (JavaModelException e) { // problems with class path, ignore (bug 202792) return false; } return false; }
From source file:org.eclipse.jdt.internal.core.PackageFragmentRootInfo.java
License:Open Source License
/** * Starting at this folder, create non-java resources for this package fragment root * and add them to the non-java resources collection. * * @exception JavaModelException The resource associated with this package fragment does not exist */// w w w. j a v a2 s.c o m static Object[] computeFolderNonJavaResources(IPackageFragmentRoot root, IContainer folder, char[][] inclusionPatterns, char[][] exclusionPatterns) throws JavaModelException { IResource[] nonJavaResources = new IResource[5]; int nonJavaResourcesCounter = 0; JavaProject project = (JavaProject) root.getJavaProject(); try { // GROOVY start // here, we only care about non-source package roots in Groovy projects boolean isInterestingPackageRoot = LanguageSupportFactory.isInterestingProject(project.getProject()) && root.getRawClasspathEntry().getEntryKind() != IClasspathEntry.CPE_SOURCE; // GROOVY end IClasspathEntry[] classpath = project.getResolvedClasspath(); IResource[] members = folder.members(); int length = members.length; if (length > 0) { String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true); String complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true); nextResource: for (int i = 0; i < length; i++) { IResource member = members[i]; switch (member.getType()) { case IResource.FILE: String fileName = member.getName(); // ignore .java files that are not excluded // GROOVY start /* old { if (Util.isValidCompilationUnitName(fileName, sourceLevel, complianceLevel) && !Util.isExcluded(member, inclusionPatterns, exclusionPatterns)) } new */ if ((Util.isValidCompilationUnitName(fileName, sourceLevel, complianceLevel) && !Util.isExcluded(member, inclusionPatterns, exclusionPatterns)) && // we want to show groovy scripts that are coming from class folders !(isInterestingPackageRoot && LanguageSupportFactory.isInterestingSourceFile(fileName))) // GROOVY end continue nextResource; // ignore .class files if (Util.isValidClassFileName(fileName, sourceLevel, complianceLevel)) continue nextResource; // ignore .zip or .jar file on classpath if (isClasspathEntry(member.getFullPath(), classpath)) continue nextResource; break; case IResource.FOLDER: // ignore valid packages or excluded folders that correspond to a nested pkg fragment root if (Util.isValidFolderNameForPackage(member.getName(), sourceLevel, complianceLevel) && (!Util.isExcluded(member, inclusionPatterns, exclusionPatterns) || isClasspathEntry(member.getFullPath(), classpath))) continue nextResource; break; } if (nonJavaResources.length == nonJavaResourcesCounter) { // resize System.arraycopy(nonJavaResources, 0, (nonJavaResources = new IResource[nonJavaResourcesCounter * 2]), 0, nonJavaResourcesCounter); } nonJavaResources[nonJavaResourcesCounter++] = member; } } if (ExternalFoldersManager.isInternalPathForExternalFolder(folder.getFullPath())) { IJarEntryResource[] jarEntryResources = new IJarEntryResource[nonJavaResourcesCounter]; for (int i = 0; i < nonJavaResourcesCounter; i++) { jarEntryResources[i] = new NonJavaResource(root, nonJavaResources[i]); } return jarEntryResources; } else if (nonJavaResources.length != nonJavaResourcesCounter) { System.arraycopy(nonJavaResources, 0, (nonJavaResources = new IResource[nonJavaResourcesCounter]), 0, nonJavaResourcesCounter); } return nonJavaResources; } catch (CoreException e) { throw new JavaModelException(e); } }
From source file:org.eclipse.jst.j2ee.classpathdep.ClasspathDependencyUtil.java
License:Open Source License
/** * Returns all resolved classpath entries for the specified Java project that * have one of the special WTP classpath component dependency attributes. * /* w w w. j a va 2s .c om*/ * @param javaProject Java project whose component classpath dependencies are being retrieved. * @param isWebApp True if the target project is associated with a web project. * @param onlyValid If true, only valid dependencies will be returned. If false, the raw entry must be valid but the * resolved can be invalid. * @return Map from IClasspathEntry to IClasspathAttribute for classpath component dependencies. * @throws CoreException Thrown if an error is encountered accessing the unresolved classpath. */ public static Map<IClasspathEntry, IClasspathAttribute> getComponentClasspathDependencies( final IJavaProjectLite javaProjectLite, final boolean isLegacyJ2EE, final boolean onlyValid) throws CoreException { final ClasspathDependencyValidatorData data = new ClasspathDependencyValidatorData( javaProjectLite.getProject()); final boolean isWebApp = JavaEEProjectUtilities.isDynamicWebProject(javaProjectLite.getProject()); // get the raw entries final Map<IClasspathEntry, IClasspathAttribute> referencedRawEntries = getRawComponentClasspathDependencies( javaProjectLite, DependencyAttributeType.CLASSPATH_COMPONENT_DEPENDENCY, isLegacyJ2EE); final Map<IClasspathEntry, IClasspathAttribute> validRawEntries = new HashMap<IClasspathEntry, IClasspathAttribute>(); final Map<IClasspathEntry, IClasspathAttribute> validRawClassPathContainerEntries = new HashMap<IClasspathEntry, IClasspathAttribute>(); // filter out non-valid referenced raw entries final Iterator<IClasspathEntry> i = referencedRawEntries.keySet().iterator(); while (i.hasNext()) { final IClasspathEntry entry = i.next(); final IClasspathAttribute attrib = referencedRawEntries.get(entry); if (isValid(entry, attrib, isWebApp, javaProjectLite.getProject(), data)) { if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) { //Put in a separate map the classpath container entries, since they will be handled differently validRawClassPathContainerEntries.put(entry, attrib); } else { validRawEntries.put(entry, attrib); } } } // if we have no valid raw entries, return empty map if (validRawEntries.isEmpty() && validRawClassPathContainerEntries.isEmpty()) { return Collections.emptyMap(); } // XXX Would like to replace the code below with use of a public JDT API that returns // the raw IClasspathEntry for a given resolved IClasspathEntry (see see https://bugs.eclipse.org/bugs/show_bug.cgi?id=183995) // The code must currently leverage IPackageFragmentRoot to determine this // mapping and, because IPackageFragmentRoots do not maintain IClasspathEntry data, a prior // call is needed to getResolvedClasspath() and the resolved IClasspathEntries have to be stored in a Map from IPath-to-IClasspathEntry to // support retrieval using the resolved IPackageFragmentRoot // retrieve the resolved classpath //TODO this call to javaProject needs to be removed. Need to figure out what exactly this is attempting to do. IJavaProject javaProject = JavaCore.create(javaProjectLite.getProject()); //TODO this call to javaProject needs to be removed. Need to figure out what exactly this is attempting to do. final IClasspathEntry[] entries = javaProject.getResolvedClasspath(true); final Map<IPath, IClasspathEntry> pathToResolvedEntry = new HashMap<IPath, IClasspathEntry>(); // store in a map from path to entry for (int j = 0; j < entries.length; j++) { pathToResolvedEntry.put(entries[j].getPath(), entries[j]); } //Gather all resolved entries from the package roots and the classpath containers final Map<IClasspathEntry, IClasspathAttribute> resolvedEntries = new LinkedHashMap<IClasspathEntry, IClasspathAttribute>(); // grab all IPackageFragmentRoots // TODO this ignores project cp entries; can easily add in the raw project cp entries, however, do not have a good way to // map project cp entries resolved from cp containers back to the corresponding raw entry (and thereby determine if the // entry has the publish/export attribute) //TODO this call to javaProject needs to be removed. Need to figure out what exactly this is attempting to do. final IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots(); for (IPackageFragmentRoot root : roots) { final IClasspathEntry rawEntry = root.getRawClasspathEntry(); // is the raw entry valid? IClasspathAttribute attrib = validRawEntries.get(rawEntry); if (attrib == null) { continue; } final IPath pkgFragPath = root.getPath(); final IClasspathEntry resolvedEntry = pathToResolvedEntry.get(pkgFragPath); resolvedEntries.put(resolvedEntry, attrib); } // Add entries coming from classpath containers to the list of resolved entries for (Map.Entry<IClasspathEntry, IClasspathAttribute> entry : validRawClassPathContainerEntries.entrySet()) { IClasspathContainer classpathContainer = JavaCore.getClasspathContainer(entry.getKey().getPath(), javaProject); if (classpathContainer != null) { IClasspathEntry[] classpathContainerEntries = classpathContainer.getClasspathEntries(); if (classpathContainerEntries != null) { for (int j = 0; j < classpathContainerEntries.length; j++) { resolvedEntries.put(classpathContainerEntries[j], entry.getValue()); } } } } //Setup the final result final Map<IClasspathEntry, IClasspathAttribute> referencedEntries = new LinkedHashMap<IClasspathEntry, IClasspathAttribute>(); for (Map.Entry<IClasspathEntry, IClasspathAttribute> mapEntry : resolvedEntries.entrySet()) { final IClasspathEntry resolvedEntry = mapEntry.getKey(); IClasspathAttribute attrib = mapEntry.getValue(); final IClasspathAttribute resolvedAttrib = checkForComponentDependencyAttribute(resolvedEntry, DependencyAttributeType.DEPENDENCY_OR_NONDEPENDENCY, isLegacyJ2EE); // attribute for the resolved entry must either be unspecified or it must be the // dependency attribute for it to be included if (resolvedAttrib == null || resolvedAttrib.getName().equals(CLASSPATH_COMPONENT_DEPENDENCY)) { // filter out resolved entry if it doesn't pass the validation rules if (!onlyValid || isValid(resolvedEntry, resolvedAttrib != null ? resolvedAttrib : attrib, isWebApp, javaProjectLite.getProject(), data)) { if (resolvedAttrib != null) { // if there is an attribute on the sub-entry, use that attrib = resolvedAttrib; } referencedEntries.put(resolvedEntry, attrib); } } } return referencedEntries; }