List of usage examples for org.eclipse.jdt.core IClasspathEntry CPE_LIBRARY
int CPE_LIBRARY
To view the source code for org.eclipse.jdt.core IClasspathEntry CPE_LIBRARY.
Click Source Link
From source file:org.eclipse.jst.common.project.facet.core.libprov.user.KeyClassesValidator.java
License:Open Source License
@Override public IStatus validate(final UserLibraryProviderInstallOperationConfig config) { final Map<String, Integer> classAppearanceCounts = new HashMap<String, Integer>(); for (String classFileName : this.classFileNames) { classAppearanceCounts.put(classFileName, 0); }//from w w w. j a va 2s . c o m for (IClasspathEntry cpe : config.resolve()) { if (cpe.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { final File file = cpe.getPath().toFile(); if (file.exists()) { ZipFile zip = null; try { zip = new ZipFile(file); for (Enumeration<? extends ZipEntry> itr = zip.entries(); itr.hasMoreElements();) { final ZipEntry zipEntry = itr.nextElement(); final String name = zipEntry.getName(); Integer count = classAppearanceCounts.get(name); if (count != null) { classAppearanceCounts.put(name, count + 1); } } } catch (IOException e) { } finally { if (zip != null) { try { zip.close(); } catch (IOException e) { } } } } } } for (Map.Entry<String, Integer> entry : classAppearanceCounts.entrySet()) { final int count = entry.getValue(); if (count != 1) { final String classFileName = entry.getKey(); final String className = this.classFileNameToClassName.get(classFileName); final String message; if (count == 0) { message = Resources.bind(Resources.classNotFound, className); } else { message = Resources.bind(Resources.classPresentMultipleTimes, className); } return new Status(IStatus.ERROR, PLUGIN_ID, message); } } return Status.OK_STATUS; }
From source file:org.eclipse.jst.j2ee.application.internal.operations.BinaryProjectHelper.java
License:Open Source License
/** * /* w w w .jav a 2 s. co m*/ */ public static void removeImportedClassesFromClasspathIfNecessary(IProject project) { IJavaProject javaProj = JavaCore.create(project); if (javaProj != null) { IClasspathEntry[] entries = javaProj.readRawClasspath(); if (entries != null) { IClasspathEntry entryToRemove = null; for (int i = 0; i < entries.length; i++) { if (entries[i].getEntryKind() == IClasspathEntry.CPE_LIBRARY && entries[i].getPath().toString().endsWith("imported_classes") //$NON-NLS-1$ && !project.getFolder("imported_classes").exists()) { //$NON-NLS-1$ entryToRemove = entries[i]; break; } } if (null != entryToRemove) { IClasspathEntry[] newEntries = new IClasspathEntry[entries.length - 1]; for (int i = 0, j = 0; i < newEntries.length && j < entries.length; j++) { if (entryToRemove != entries[j]) { newEntries[i] = entries[j]; i++; } } entries = newEntries; IPath output = javaProj.readOutputLocation(); if (output != null) try { javaProj.setRawClasspath(entries, output, null); } catch (JavaModelException e) { } } } } }
From source file:org.eclipse.jst.j2ee.classpathdep.ClasspathDependencyUtil.java
License:Open Source License
public static List<IClasspathEntry> getPotentialComponentClasspathDependencies( final IJavaProjectLite javaProjectLite, final boolean isLegacyJ2EE) throws CoreException { final List<IClasspathEntry> potentialRawEntries = new ArrayList<IClasspathEntry>(); if (javaProjectLite == null || !javaProjectLite.getProject().isAccessible()) { return Collections.emptyList(); }//w ww .j a va 2s . com final IProject project = javaProjectLite.getProject(); final boolean isWebApp = JavaEEProjectUtilities.isDynamicWebProject(project); final boolean isRAR = JavaEEProjectUtilities.isJCAProject(project); final ClasspathDependencyValidatorData data = new ClasspathDependencyValidatorData(project); final IClasspathEntry[] entries = javaProjectLite.readRawClasspath(); for (int i = 0; i < entries.length; i++) { final IClasspathEntry entry = entries[i]; final IClasspathAttribute attrib = checkForComponentDependencyAttribute(entry, DependencyAttributeType.DEPENDENCY_OR_NONDEPENDENCY, isLegacyJ2EE); if (attrib != null) { continue; // already has the attribute } // check validation logic for entry // always mark the "isWebApp" param as true so that we get both exported and non-exported entries; for non-web projects, // want to let a user have the option to see and select the non-exported entries and then just generate a validation // error if they happen to select one. final IMessage[] msgs = ClasspathDependencyValidator.validateVirtualComponentEntry(entry, null, true, project, data); boolean error = false; for (int j = 0; j < msgs.length; j++) { if (msgs[j].getSeverity() == IMessage.HIGH_SEVERITY) { error = true; break; } } if (error) { continue; } if (IClasspathEntry.CPE_LIBRARY == entry.getEntryKind()) { final boolean isFile = !ClasspathDependencyUtil.isClassFolderEntry(entry); if (isFile) { boolean foundEntry = false; IVirtualComponent component = ComponentCore.createComponent(project); if (isWebApp) { // checks for web libs IContainer[] webLibFolders = component.getRootFolder().getFolder(WEB_INF_LIB_PATH) .getUnderlyingFolders(); for (IContainer webLib : webLibFolders) { IPath webLibFolderPath = webLib.getFullPath(); if (webLibFolderPath.equals(entry.getPath().removeLastSegments(1))) { foundEntry = true; break; } } } else if (isRAR) { IContainer[] rootFolders = component.getRootFolder().getUnderlyingFolders(); for (IContainer root : rootFolders) { IPath rootPath = root.getFullPath(); if (rootPath.isPrefixOf(entry.getPath())) { foundEntry = true; break; } } } if (foundEntry) { continue; } // checks for manifest references List manifestRefs = J2EEModuleVirtualComponent.getManifestReferences(component, null); if (manifestRefs != null) { for (int j = 0; j < manifestRefs.size(); j++) { IVirtualReference ref = (IVirtualReference) manifestRefs.get(j); IVirtualComponent c = ref.getReferencedComponent(); if (c.isBinary()) { IFile file = (IFile) c.getAdapter(IFile.class); if (file != null && file.getFullPath().equals(entry.getPath())) { foundEntry = true; break; } } } if (foundEntry) { continue; } } // checks for ear library-directory entries IProject[] earProjects = EarUtilities.getReferencingEARProjects(project); for (IProject earProject : earProjects) { String earDDVersion = EarUtilities.getJ2EEDDProjectVersion(earProject); if (!earDDVersion.equals(J2EEVersionConstants.VERSION_1_2_TEXT) && !earDDVersion.equals(J2EEVersionConstants.VERSION_1_3_TEXT) && !earDDVersion.equals(J2EEVersionConstants.VERSION_1_4_TEXT)) { IVirtualComponent earComponent = ComponentCore.createComponent(earProject); Application app = (Application) ModelProviderManager.getModelProvider(earComponent) .getModelObject(); String libDir = app.getLibraryDirectory(); if (libDir == null) { // lib is the default if no library-directory is set libDir = "lib"; //$NON-NLS-1$ } IContainer[] earLibFolders = earComponent.getRootFolder().getFolder(new Path(libDir)) .getUnderlyingFolders(); for (IContainer earLib : earLibFolders) { IPath earLibFolderPath = earLib.getFullPath(); if (earLibFolderPath.equals(entry.getPath().removeLastSegments(1))) { foundEntry = true; break; } } if (foundEntry) { break; } } } if (foundEntry) { continue; } } } // entry can potentially be tagged as a component dependency potentialRawEntries.add(entry); } return potentialRawEntries; }
From source file:org.eclipse.jst.j2ee.classpathdep.ClasspathDependencyUtil.java
License:Open Source License
/** * Checks if the specified classpath entry represents a class folder. * @param entry The entry to check.// ww w. ja v a 2 s .c o m * @return True if it is a library entry that points to a class folder. False otherwise. */ public static boolean isClassFolderEntry(final IClasspathEntry entry) { if (entry == null || entry.getEntryKind() != IClasspathEntry.CPE_LIBRARY) { return false; } // does the path refer to a file or a folder? final IPath entryPath = entry.getPath(); IPath entryLocation = entryPath; final IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(entryPath); if (resource != null) { entryLocation = resource.getLocation(); } boolean isFile = true; // by default, assume a jar file if (entryLocation.toFile().isDirectory()) { isFile = false; } return !isFile; }
From source file:org.eclipse.jst.j2ee.classpathdep.ClasspathDependencyUtil.java
License:Open Source License
public static IClasspathEntry modifyDependencyPath(IClasspathEntry entry, IPath dependencyPath) { IClasspathEntry newEntry = null;/*from w w w . j av a 2 s . c o m*/ IClasspathAttribute[] newAttributes = modifyDependencyPath(entry.getExtraAttributes(), dependencyPath); switch (entry.getEntryKind()) { case IClasspathEntry.CPE_CONTAINER: newEntry = JavaCore.newContainerEntry(entry.getPath(), entry.getAccessRules(), newAttributes, entry.isExported()); break; case IClasspathEntry.CPE_LIBRARY: newEntry = JavaCore.newLibraryEntry(entry.getPath(), entry.getSourceAttachmentPath(), entry.getSourceAttachmentRootPath(), entry.getAccessRules(), newAttributes, entry.isExported()); break; case IClasspathEntry.CPE_VARIABLE: newEntry = JavaCore.newVariableEntry(entry.getPath(), entry.getSourceAttachmentPath(), entry.getSourceAttachmentRootPath(), entry.getAccessRules(), newAttributes, entry.isExported()); break; case IClasspathEntry.CPE_PROJECT: newEntry = JavaCore.newProjectEntry(entry.getPath(), entry.getAccessRules(), entry.combineAccessRules(), newAttributes, entry.isExported()); break; case IClasspathEntry.CPE_SOURCE: newEntry = JavaCore.newSourceEntry(entry.getPath(), entry.getInclusionPatterns(), entry.getExclusionPatterns(), entry.getOutputLocation(), newAttributes); break; } return newEntry; }
From source file:org.eclipse.jst.j2ee.internal.classpathdep.ClasspathDependencyValidator.java
License:Open Source License
/** * Checks if the specified Java classpath entry is a valid WTP virtual component reference. * Does not check the runtime path./*from w w w .j av a 2 s.c o m*/ * @param entry Raw or resolved classpath entry to validate. * @param attrib The WTP classpath component dependency attribute. Null if it has not yet been set. * @param isWebApp True if the target project is associated with a web project. * @param project The parent project. * @param data Data required for validation. Can be computed once for the project and reused. * @return IMessages representing validation results. */ public static IMessage[] validateVirtualComponentEntry(final IClasspathEntry entry, final IClasspathAttribute attrib, final boolean isWebApp, final IProject project, final ClasspathDependencyValidatorData data) { List results = new ArrayList(); if (entry == null) { return (IMessage[]) results.toArray(new IMessage[results.size()]); } final int kind = entry.getEntryKind(); final boolean isFile = !ClasspathDependencyUtil.isClassFolderEntry(entry); if (kind == IClasspathEntry.CPE_PROJECT) { // Project cp entry // Allow faceted projects only, and not plain java projects boolean isFacetedProject = false; IProject referencedProject = ResourcesPlugin.getWorkspace().getRoot() .getProject(entry.getPath().toString()); try { isFacetedProject = FacetedProjectFramework.isFacetedProject(referencedProject); } catch (CoreException ce) { //Ignore. Thrown when project metadata cannot be read. In that case we will treat the project as non faceted } if (!isFacetedProject) { results.add(new Message("classpathdependencyvalidator", //$NON-NLS-1$ IMessage.HIGH_SEVERITY, ProjectClasspathEntry, new String[] { entry.getPath().toString() }, project)); return (IMessage[]) results.toArray(new IMessage[results.size()]); } } else if (kind == IClasspathEntry.CPE_SOURCE) { // Source cp entry results.add(new Message("classpathdependencyvalidator", //$NON-NLS-1$ IMessage.HIGH_SEVERITY, SourceEntry, new String[] { entry.getPath().toString() }, project)); return (IMessage[]) results.toArray(new IMessage[results.size()]); } else if (kind == IClasspathEntry.CPE_CONTAINER) { // get the set of classpath container IDs that should be filtered List filteredIDs = ClasspathDependencyExtensions.get().getFilteredClasspathContainerIDs(); final IPath path = entry.getPath(); for (int i = 0; i < filteredIDs.size(); i++) { final String id = (String) filteredIDs.get(i); if (path.segment(0).equals(id)) { // filtered classpath container results.add(new Message("classpathdependencyvalidator", //$NON-NLS-1$ IMessage.HIGH_SEVERITY, FilteredContainer, new String[] { entry.getPath().toString() }, project)); return (IMessage[]) results.toArray(new IMessage[results.size()]); } } } else if (kind == IClasspathEntry.CPE_LIBRARY) { if (!isFile) { final IContainer[] mappedClassFolders = data.getMappedClassFolders(); final IResource resource = ClasspathDependencyUtil.getEntryResource(entry); if (resource != null) { final IPath fullClassFolderPath = resource.getFullPath(); boolean alreadyMapped = false; for (int j = 0; j < mappedClassFolders.length; j++) { if (fullClassFolderPath.equals(mappedClassFolders[j].getFullPath())) { // entry resolves to same file as existing class folder mapping, skip alreadyMapped = true; break; } } // Class folder reference; ensure this is not already mapped via the component file. if (alreadyMapped) { results.add(new Message("classpathdependencyvalidator", //$NON-NLS-1$ IMessage.HIGH_SEVERITY, DuplicateClassFolderEntry, new String[] { entry.getPath().toString() }, project)); } } } } // final IPath runtimePath = ClasspathDependencyUtil.getRuntimePath(attrib, isWebApp, !isFile); // if (!isWebApp) { // // only a ../ or / mapping is currently legal in a non-web context // if (!(runtimePath.equals(IClasspathDependencyConstants.RUNTIME_MAPPING_INTO_CONTAINER_PATH) // || runtimePath.equals(IClasspathDependencyConstants.RUNTIME_MAPPING_INTO_COMPONENT_PATH))) { // results.add(new Message("classpathdependencyvalidator", //$NON-NLS-1$ // IMessage.HIGH_SEVERITY, InvalidNonWebRuntimePath, new String[]{entry.getPath().toString(), runtimePath.toString()}, project)); // } // } else { // String pathStr = runtimePath.toString(); // // can only be ../, /WEB-INF/lib or /WEB-INF/classes // if (!runtimePath.equals(IClasspathDependencyConstants.RUNTIME_MAPPING_INTO_CONTAINER_PATH) // && !runtimePath.equals(IClasspathDependencyConstants.WEB_INF_LIB_PATH) // && !runtimePath.equals(IClasspathDependencyConstants.WEB_INF_CLASSES_PATH)) { // results.add(new Message("classpathdependencyvalidator", //$NON-NLS-1$ // IMessage.HIGH_SEVERITY, InvalidWebRuntimePath, new String[]{entry.getPath().toString(), pathStr}, project)); // } // } return (IMessage[]) results.toArray(new IMessage[results.size()]); }
From source file:org.eclipse.jst.j2ee.internal.classpathdep.UpdateClasspathAttributesOperation.java
License:Open Source License
/** * Updates the specified Java project so that only the specified classpath entries have * the WTP component dependency attribute. * @param javaProject Target Java project. * @param entries Classpath entries that should have the component dependency attribute. Map from IClasspathEntry * to the IClasspathAttribute for the WTP classpath component dependency. * @param modifyComponentDep True if modifying the dependency attribute, false if modifying the non-dependency attribute. * @throws CoreException Thrown if an error is encountered. *///w w w. j ava2 s . c o m private void updateDependencyAttributes(final IJavaProject javaProject, final Map entriesWithAttrib, final boolean modifyComponentDep, final boolean isLegacyJ2EE) throws CoreException { if (javaProject == null || !javaProject.getProject().isAccessible()) { return; } final List updatedClasspath = new ArrayList(); final IClasspathEntry[] rawClasspath = javaProject.getRawClasspath(); boolean needToUpdateClasspath = false; IClasspathAttribute attrib = UpdateClasspathAttributeUtil.createDependencyAttribute(); if (!modifyComponentDep) { attrib = UpdateClasspathAttributeUtil.createNonDependencyAttribute(); } for (int i = 0; i < rawClasspath.length; i++) { IClasspathEntry entry = rawClasspath[i]; final int kind = entry.getEntryKind(); boolean hasAttribute = ClasspathDependencyUtil .checkForComponentDependencyAttribute(entry, modifyComponentDep ? DependencyAttributeType.CLASSPATH_COMPONENT_DEPENDENCY : DependencyAttributeType.CLASSPATH_COMPONENT_NONDEPENDENCY, isLegacyJ2EE) != null; boolean shouldHaveAttribute = entriesWithAttrib.containsKey(entry); boolean updateAttributes = false; IClasspathAttribute[] updatedAttributes = null; if (shouldHaveAttribute) { if (!hasAttribute) { // should have the attribute and currently missing it attrib = (IClasspathAttribute) entriesWithAttrib.get(entry); updatedAttributes = updateAttributes(entry.getExtraAttributes(), attrib, true); needToUpdateClasspath = true; updateAttributes = true; } } else if (hasAttribute) { // should not have the attribute and currently has it updatedAttributes = updateAttributes(entry.getExtraAttributes(), attrib, false); needToUpdateClasspath = true; updateAttributes = true; } if (updateAttributes) { switch (kind) { case IClasspathEntry.CPE_CONTAINER: entry = JavaCore.newContainerEntry(entry.getPath(), entry.getAccessRules(), updatedAttributes, entry.isExported()); break; case IClasspathEntry.CPE_LIBRARY: entry = JavaCore.newLibraryEntry(entry.getPath(), entry.getSourceAttachmentPath(), entry.getSourceAttachmentRootPath(), entry.getAccessRules(), updatedAttributes, entry.isExported()); break; case IClasspathEntry.CPE_VARIABLE: entry = JavaCore.newVariableEntry(entry.getPath(), entry.getSourceAttachmentPath(), entry.getSourceAttachmentRootPath(), entry.getAccessRules(), updatedAttributes, entry.isExported()); break; case IClasspathEntry.CPE_PROJECT: // although project entries are not yet supported, allow the attribute here and let the validator flag as an error entry = JavaCore.newProjectEntry(entry.getPath(), entry.getAccessRules(), entry.combineAccessRules(), updatedAttributes, entry.isExported()); break; case IClasspathEntry.CPE_SOURCE: // although source entries are not supported, allow the attribute here and let the validator flag as an error entry = JavaCore.newSourceEntry(entry.getPath(), entry.getInclusionPatterns(), entry.getExclusionPatterns(), entry.getOutputLocation(), updatedAttributes); break; } } updatedClasspath.add(entry); } if (needToUpdateClasspath) { final IClasspathEntry[] updatedCPArray = (IClasspathEntry[]) updatedClasspath .toArray(new IClasspathEntry[updatedClasspath.size()]); javaProject.setRawClasspath(updatedCPArray, null); } }
From source file:org.eclipse.jst.j2ee.internal.common.operations.UpdateJavaBuildPathOperation.java
License:Open Source License
protected void ensureClasspathEntryIsExported(List cp, IClasspathEntry entry) { if (entry.isExported()) return;//w w w. ja va 2 s .c o m int index = getIndex(cp, entry); IClasspathEntry newEntry = null; switch (entry.getEntryKind()) { case IClasspathEntry.CPE_PROJECT: newEntry = JavaCore.newProjectEntry(entry.getPath(), true); break; case IClasspathEntry.CPE_LIBRARY: newEntry = JavaCore.newLibraryEntry(entry.getPath(), entry.getSourceAttachmentPath(), entry.getSourceAttachmentRootPath(), true); break; case IClasspathEntry.CPE_VARIABLE: newEntry = JavaCore.newVariableEntry(entry.getPath(), entry.getSourceAttachmentPath(), entry.getSourceAttachmentRootPath()); } cp.set(index, newEntry); }
From source file:org.eclipse.jst.j2ee.internal.dialogs.TypeJavaSearchScope.java
License:Open Source License
/** * Method addProject. This method adds all the classpath entries for the * current project to the search scope.//from w ww. j av a 2s . c om * * @param javaProject * @param includesPrereqProjects * @param visitedProjects * @throws JavaModelException */ public void addProject(IJavaProject javaProject, boolean includesPrereqProjects, HashSet visitedProjects) throws JavaModelException { IProject project = javaProject.getProject(); if (!project.isAccessible() || !visitedProjects.add(project)) return; this.addEnclosingProjectOrJar(project.getFullPath()); IClasspathEntry[] entries = javaProject.getResolvedClasspath(true); IJavaModel model = javaProject.getJavaModel(); for (int i = 0, length = entries.length; i < length; i++) { IClasspathEntry entry = entries[i]; switch (entry.getEntryKind()) { case IClasspathEntry.CPE_LIBRARY: IPath path = entry.getPath(); this.add(path, true); this.addEnclosingProjectOrJar(path); break; case IClasspathEntry.CPE_PROJECT: if (includesPrereqProjects) { this.add(model.getJavaProject(entry.getPath().lastSegment()), true, visitedProjects); } break; case IClasspathEntry.CPE_SOURCE: this.add(entry.getPath(), true); break; } } }
From source file:org.eclipse.jst.j2ee.internal.dialogs.TypeJavaSearchScope.java
License:Open Source License
/** * Method add. This method filters out all the classpath entries of the * project which are not exported./*from w w w. j a va2 s .c o m*/ * * @param javaProject * @param includesPrereqProjects * @param visitedProjects * @throws JavaModelException */ public void add(IJavaProject javaProject, boolean includesPrereqProjects, HashSet visitedProjects) throws JavaModelException { IProject project = javaProject.getProject(); if (!project.isAccessible() || !visitedProjects.add(project)) return; this.addEnclosingProjectOrJar(project.getFullPath()); IClasspathEntry[] entries = javaProject.getResolvedClasspath(true); IJavaModel model = javaProject.getJavaModel(); for (int i = 0, length = entries.length; i < length; i++) { IClasspathEntry entry = entries[i]; if (includeExportedClassPathEntriesOnly()) { if (!entry.isExported() && entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) continue; } switch (entry.getEntryKind()) { case IClasspathEntry.CPE_LIBRARY: IPath path = entry.getPath(); this.add(path, true); this.addEnclosingProjectOrJar(path); break; case IClasspathEntry.CPE_PROJECT: if (includesPrereqProjects) { this.add(model.getJavaProject(entry.getPath().lastSegment()), true, visitedProjects); } break; case IClasspathEntry.CPE_SOURCE: this.add(entry.getPath(), true); break; } } }