Java Code Examples for org.eclipse.jdt.core.IClasspathEntry#CPE_SOURCE

The following examples show how to use org.eclipse.jdt.core.IClasspathEntry#CPE_SOURCE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClasspathEntry.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the kind of a <code>PackageFragmentRoot</code> from its <code>String</code> form.
 */
static int kindFromString(String kindStr) {

	if (kindStr.equalsIgnoreCase("prj")) //$NON-NLS-1$
		return IClasspathEntry.CPE_PROJECT;
	if (kindStr.equalsIgnoreCase("var")) //$NON-NLS-1$
		return IClasspathEntry.CPE_VARIABLE;
	if (kindStr.equalsIgnoreCase("con")) //$NON-NLS-1$
		return IClasspathEntry.CPE_CONTAINER;
	if (kindStr.equalsIgnoreCase("src")) //$NON-NLS-1$
		return IClasspathEntry.CPE_SOURCE;
	if (kindStr.equalsIgnoreCase("lib")) //$NON-NLS-1$
		return IClasspathEntry.CPE_LIBRARY;
	if (kindStr.equalsIgnoreCase("output")) //$NON-NLS-1$
		return ClasspathEntry.K_OUTPUT;
	return -1;
}
 
Example 2
Source File: BazelClasspathContainer.java    From eclipse with Apache License 2.0 6 votes vote down vote up
private boolean isSourcePath(String path) throws JavaModelException, BackingStoreException {
  Path pp = new File(instance.getWorkspaceRoot().toString() + File.separator + path).toPath();
  IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
  for (IClasspathEntry entry : project.getRawClasspath()) {
    if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
      IResource res = root.findMember(entry.getPath());
      if (res != null) {
        String file = res.getLocation().toOSString();
        if (!file.isEmpty() && pp.startsWith(file)) {
          IPath[] inclusionPatterns = entry.getInclusionPatterns();
          if (!matchPatterns(pp, entry.getExclusionPatterns()) && (inclusionPatterns == null
              || inclusionPatterns.length == 0 || matchPatterns(pp, inclusionPatterns))) {
            return true;
          }
        }
      }
    }
  }
  return false;
}
 
Example 3
Source File: WebProjectUtil.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
/**
 * Return the set of Java source paths for the given project, relative to the project. Return an
 * empty list if not a Java project.
 */
public static List<IPath> getJavaSourcePaths(IProject project) {
  IJavaProject javaProject = JavaCore.create(project);
  if (!javaProject.exists()) {
    return Collections.emptyList();
  }
  try {
    IClasspathEntry[] classpathEntries = javaProject.getRawClasspath();
    List<IPath> paths = new ArrayList<>();
    for (IClasspathEntry entry : classpathEntries) {
      if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
        // source paths are absolute to the root folder of the project
        paths.add(entry.getPath().removeFirstSegments(1));
      }
    }
    return paths;
  } catch (JavaModelException ex) {
    return Collections.emptyList();
  }
}
 
Example 4
Source File: CPListElementSorter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public int category(Object obj) {
	if (obj instanceof CPListElement) {
		CPListElement element= (CPListElement) obj;
		if (element.getParentContainer() != null) {
			return CONTAINER_ENTRY;
		}
		switch (element.getEntryKind()) {
		case IClasspathEntry.CPE_LIBRARY:
			return LIBRARY;
		case IClasspathEntry.CPE_PROJECT:
			return PROJECT;
		case IClasspathEntry.CPE_SOURCE:
			return SOURCE;
		case IClasspathEntry.CPE_VARIABLE:
			return VARIABLE;
		case IClasspathEntry.CPE_CONTAINER:
			return CONTAINER;
		}
	} else if (obj instanceof CPListElementAttribute) {
		return ATTRIBUTE;
	} else if (obj instanceof IAccessRule) {
		return ATTRIBUTE;
	}
	return OTHER;
}
 
Example 5
Source File: FatJarPackageWizardPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean isRootAt(IPackageFragmentRoot root, IPath entry) {
	try {
		IClasspathEntry cpe= root.getRawClasspathEntry();
		if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
			IPath outputLocation= cpe.getOutputLocation();
			if (outputLocation == null)
				outputLocation= root.getJavaProject().getOutputLocation();

			IPath location= ResourcesPlugin.getWorkspace().getRoot().findMember(outputLocation).getLocation();
			if (entry.equals(location))
				return true;
		}
	} catch (JavaModelException e) {
		JavaPlugin.log(e);
	}

	IResource resource= root.getResource();
	if (resource != null && entry.equals(resource.getLocation()))
		return true;

	IPath path= root.getPath();
	if (path != null && entry.equals(path))
		return true;

	return false;
}
 
Example 6
Source File: PDEClassPathGenerator.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
private static void resolveInWorkspace(IClasspathEntry classpathEntry, Set<String> classPath, Set<IProject> projectOnCp) {
    int entryKind = classpathEntry.getEntryKind();
    switch (entryKind) {
    case IClasspathEntry.CPE_PROJECT:
        Set<String> cp = resolveProjectClassPath(classpathEntry.getPath(), projectOnCp);
        classPath.addAll(cp);
        break;
    case IClasspathEntry.CPE_VARIABLE:
        classpathEntry = JavaCore.getResolvedClasspathEntry(classpathEntry);
        if (classpathEntry == null) {
            return;
        }
        //$FALL-THROUGH$
    case IClasspathEntry.CPE_LIBRARY:
        String lib = resolveLibrary(classpathEntry.getPath());
        if (lib != null) {
            classPath.add(lib);
        }
        break;
    case IClasspathEntry.CPE_SOURCE:
        // ???
        break;
    default:
        break;
    }
}
 
Example 7
Source File: SuperDevModeSrcArgumentProcessor.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Get the class path entries that are the source.
 *
 * @param javaProject the java project.
 * @param entry classpath entry value.
 * @return the path.
 */
private String getPathIfDir(IJavaProject javaProject, IClasspathEntry entry) {
  IPath p = entry.getPath();

  String projectName = javaProject.getProject().getName();

  String path = null;
  // src directories don't have an output
  // cpe source are src,test directories
  if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE
      && (entry.getOutputLocation() == null || (entry.getOutputLocation() != null && !entry
          .getOutputLocation().lastSegment().toString().equals("test-classes")))) {
    String dir = p.toString();
    // if the base segment has the project name,
    // lets remove that so its relative to project
    if (dir.contains(projectName)) {
      IPath relative = p.removeFirstSegments(1);
      path = relative.toString();
    }
  }

  return path;
}
 
Example 8
Source File: InitHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void removeExclusionPattern(IJavaProject javaProject) throws JavaModelException {
	IClasspathEntry[] classpath = javaProject.getRawClasspath();
	for (int i = 0; i < classpath.length; i++) {
		IClasspathEntry entry = classpath[i];
		if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
			IPath path = entry.getPath();
			if (path.toString().endsWith("resources")) {
				IClasspathEntry newEntry = JavaCore.newSourceEntry(entry.getPath());
				classpath[i] = newEntry;
				javaProject.setRawClasspath(classpath, monitor);
				return;
			}
		}
	}
}
 
Example 9
Source File: JavaDocLocations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static IClasspathEntry getConvertedEntry(IClasspathEntry entry, IJavaProject project, Map<IPath, String> oldLocationMap) {
	IPath path= null;
	switch (entry.getEntryKind()) {
		case IClasspathEntry.CPE_SOURCE:
		case IClasspathEntry.CPE_PROJECT:
			return null;
		case IClasspathEntry.CPE_CONTAINER:
			convertContainer(entry, project, oldLocationMap);
			return null;
		case IClasspathEntry.CPE_LIBRARY:
			path= entry.getPath();
			break;
		case IClasspathEntry.CPE_VARIABLE:
			path= JavaCore.getResolvedVariablePath(entry.getPath());
			break;
		default:
			return null;
	}
	if (path == null) {
		return null;
	}
	IClasspathAttribute[] extraAttributes= entry.getExtraAttributes();
	for (int i= 0; i < extraAttributes.length; i++) {
		if (IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME.equals(extraAttributes[i].getName())) {
			return null;
		}
	}
	String libraryJavadocLocation= oldLocationMap.get(path);
	if (libraryJavadocLocation != null) {
		CPListElement element= CPListElement.createFromExisting(entry, project);
		element.setAttribute(CPListElement.JAVADOC, libraryJavadocLocation);
		return element.getClasspathEntry();
	}
	return null;
}
 
Example 10
Source File: NewSarlProjectWizard.java    From sarl with Apache License 2.0 5 votes vote down vote up
private static String buildInvalidOutputPathMessageFragment(IJavaProject javaProject) {
	final StringBuilder sourceFolders = new StringBuilder();
	try {
		for (final IClasspathEntry entry : javaProject.getRawClasspath()) {
			if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
				sourceFolders.append("\t"); //$NON-NLS-1$
				sourceFolders.append(entry.getPath().toOSString());
				sourceFolders.append("\n"); //$NON-NLS-1$
			}
		}
	} catch (Throwable exception) {
		//
	}
	return sourceFolders.toString();
}
 
Example 11
Source File: CPListElement.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public Object[] getChildren(boolean hideOutputFolder) {
	if (hideOutputFolder && fEntryKind == IClasspathEntry.CPE_SOURCE) {
		return getFilteredChildren(new String[] { OUTPUT });
	}
	/*if (isInContainer(JavaRuntime.JRE_CONTAINER)) {
		return getFilteredChildren(new String[] { COMBINE_ACCESSRULES, NATIVE_LIB_PATH });
	}*/
	if (fEntryKind == IClasspathEntry.CPE_PROJECT) {
		return getFilteredChildren(new String[] { COMBINE_ACCESSRULES });
	}
	return getFilteredChildren(new String[0]);
}
 
Example 12
Source File: FindBugsWorker.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @return map of all source folders to output folders, for current java
 *         project, where both are represented by absolute IPath objects
 *
 * @throws CoreException
 */
private Map<IPath, IPath> createOutputLocations() throws CoreException {

    Map<IPath, IPath> srcToOutputMap = new HashMap<>();

    // get the default location => relative to wsp
    IPath defaultOutputLocation = ResourceUtils.relativeToAbsolute(javaProject.getOutputLocation());
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    // path to the project without project name itself
    IClasspathEntry entries[] = javaProject.getResolvedClasspath(true);
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry classpathEntry = entries[i];
        if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPath outputLocation = ResourceUtils.getOutputLocation(classpathEntry, defaultOutputLocation);
            if (outputLocation == null) {
                continue;
            }
            IResource cpeResource = root.findMember(classpathEntry.getPath());
            // patch from 2891041: do not analyze derived "source" folders
            // because they probably contain auto-generated classes
            if (cpeResource != null && cpeResource.isDerived()) {
                continue;
            }
            // TODO not clear if it is absolute in workspace or in global FS
            IPath srcLocation = ResourceUtils.relativeToAbsolute(classpathEntry.getPath());
            if (srcLocation != null) {
                srcToOutputMap.put(srcLocation, outputLocation);
            }
        }
    }

    return srcToOutputMap;
}
 
Example 13
Source File: GeneratorSupport.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
private ResourceLoader createResourceLoader(final IProject project) {
  if (project != null) {
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject != null) {
      IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
      try {
        IClasspathEntry[] classPathEntries = javaProject.getResolvedClasspath(true);
        URL[] urls = new URL[classPathEntries.length];
        for (int i = 0; i < classPathEntries.length; i++) {
          IClasspathEntry entry = classPathEntries[i];
          IPath path = null;
          switch (entry.getEntryKind()) {
          case IClasspathEntry.CPE_PROJECT:
            IJavaProject requiredProject = JavaCore.create((IProject) workspaceRoot.findMember(entry.getPath()));
            if (requiredProject != null) {
              path = workspaceRoot.findMember(requiredProject.getOutputLocation()).getLocation();
            }
            break;
          case IClasspathEntry.CPE_SOURCE:
            path = workspaceRoot.findMember(entry.getPath()).getLocation();
            break;
          default:
            path = entry.getPath();
            break;
          }
          if (path != null) {
            urls[i] = path.toFile().toURI().toURL();
          }
        }
        ClassLoader parentClassLoader = javaProject.getClass().getClassLoader();
        URLClassLoader classLoader = new URLClassLoader(urls, parentClassLoader);
        return new CustomResourceLoader(classLoader);
      } catch (MalformedURLException | CoreException e) {
        LOG.warn("Failed to create class loader for project " + project.getName(), e);
      }
    }
  }
  return new ResourceLoaderImpl(GeneratorSupport.class.getClassLoader());
}
 
Example 14
Source File: ProblemsLabelDecorator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isIgnoringOptionalProblems(IClasspathEntry entry) {
	if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
		IClasspathAttribute[] extraAttributes= entry.getExtraAttributes();
		for (int i= 0; i < extraAttributes.length; i++) {
			IClasspathAttribute attrib= extraAttributes[i];
			if (IClasspathAttribute.IGNORE_OPTIONAL_PROBLEMS.equals(attrib.getName())) {
				return "true".equals(attrib.getValue()); //$NON-NLS-1$
			}
		}
	}
	return false;
}
 
Example 15
Source File: ClasspathEntry.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public boolean ignoreOptionalProblems() {
	if (this.entryKind == IClasspathEntry.CPE_SOURCE) {
		for (int i = 0; i < this.extraAttributes.length; i++) {
			IClasspathAttribute attrib = this.extraAttributes[i];
			if (IClasspathAttribute.IGNORE_OPTIONAL_PROBLEMS.equals(attrib.getName())) {
				return "true".equals(attrib.getValue()); //$NON-NLS-1$
			}
		}
	}
	return false;
}
 
Example 16
Source File: ClasspathEntry.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Answers an ID which is used to distinguish entries during package
 * fragment root computations
 */
public String rootID(){

	if (this.rootID == null) {
		switch(this.entryKind){
			case IClasspathEntry.CPE_LIBRARY :
				this.rootID = "[LIB]"+this.path;  //$NON-NLS-1$
				break;
			case IClasspathEntry.CPE_PROJECT :
				this.rootID = "[PRJ]"+this.path;  //$NON-NLS-1$
				break;
			case IClasspathEntry.CPE_SOURCE :
				this.rootID = "[SRC]"+this.path;  //$NON-NLS-1$
				break;
			case IClasspathEntry.CPE_VARIABLE :
				this.rootID = "[VAR]"+this.path;  //$NON-NLS-1$
				break;
			case IClasspathEntry.CPE_CONTAINER :
				this.rootID = "[CON]"+this.path;  //$NON-NLS-1$
				break;
			default :
				this.rootID = "";  //$NON-NLS-1$
				break;
		}
	}
	return this.rootID;
}
 
Example 17
Source File: ClasspathModifier.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static void insertAtEndOfCategory(CPListElement entry, List<CPListElement> existingEntries) {
	int length= existingEntries.size();
	CPListElement[] elements= existingEntries.toArray(new CPListElement[length]);
	int i= 0;
	while (i < length && elements[i].getClasspathEntry().getEntryKind() != entry.getClasspathEntry().getEntryKind()) {
		i++;
	}
	if (i < length) {
		i++;
		while (i < length && elements[i].getClasspathEntry().getEntryKind() == entry.getClasspathEntry().getEntryKind()) {
			i++;
		}
		existingEntries.add(i, entry);
		return;
	}

	switch (entry.getClasspathEntry().getEntryKind()) {
	case IClasspathEntry.CPE_SOURCE:
		existingEntries.add(0, entry);
		break;
	case IClasspathEntry.CPE_CONTAINER:
	case IClasspathEntry.CPE_LIBRARY:
	case IClasspathEntry.CPE_PROJECT:
	case IClasspathEntry.CPE_VARIABLE:
	default:
		existingEntries.add(entry);
		break;
	}
}
 
Example 18
Source File: JdtHelper.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean isFromOutputPath(IResource resource) {
	IProject project = resource.getProject();
	IJavaProject javaProject = JavaCore.create(project);
	if (javaProject != null && javaProject.exists()) {
		try {
			IPath defaultOutputLocation = javaProject.getOutputLocation();
			IPath resourcePath = resource.getFullPath();
			if (defaultOutputLocation != null && !defaultOutputLocation.isEmpty() && defaultOutputLocation.isPrefixOf(resourcePath)) {
				return true;
			}
			IClasspathEntry[] classpathEntries = javaProject.getRawClasspath();
			for(IClasspathEntry classpathEntry: classpathEntries) {
				if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
					IPath specializedOutputLocation = classpathEntry.getOutputLocation();
					if (specializedOutputLocation != null) {
						if (!specializedOutputLocation.equals(classpathEntry.getPath()) && 
								specializedOutputLocation.isPrefixOf(resourcePath)) {
							return true;
						}
					}
				}
			}
		} catch(CoreException e) {
			if (log.isDebugEnabled())
				log.debug("Error in isJavaTargetFolder():" + e.getMessage(), e);
		}
	}
	return false;
}
 
Example 19
Source File: CPListElement.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static void insert(CPListElement element, List<CPListElement> cpList) {
	int length= cpList.size();
	CPListElement[] elements= cpList.toArray(new CPListElement[length]);
	int i= 0;
	while (i < length && elements[i].getEntryKind() != element.getEntryKind()) {
		i++;
	}
	if (i < length) {
		i++;
		while (i < length && elements[i].getEntryKind() == element.getEntryKind()) {
			i++;
		}
		cpList.add(i, element);
		return;
	}

	switch (element.getEntryKind()) {
	case IClasspathEntry.CPE_SOURCE:
		cpList.add(0, element);
		break;
	case IClasspathEntry.CPE_CONTAINER:
	case IClasspathEntry.CPE_LIBRARY:
	case IClasspathEntry.CPE_PROJECT:
	case IClasspathEntry.CPE_VARIABLE:
	default:
		cpList.add(element);
		break;
	}
}
 
Example 20
Source File: PDEClassPathGenerator.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static void appendBundleToClasspath(BundleDescription bd, List<String> pdeClassPath, IPath defaultOutputLocation) {
    IPluginModelBase model = PluginRegistry.findModel(bd);
    if (model == null) {
        return;
    }
    ArrayList<IClasspathEntry> classpathEntries = new ArrayList<>();
    ClasspathUtilCore.addLibraries(model, classpathEntries);

    for (IClasspathEntry cpe : classpathEntries) {
        IPath location;
        if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            location = ResourceUtils.getOutputLocation(cpe, defaultOutputLocation);
        } else {
            location = cpe.getPath();
        }
        if (location == null) {
            continue;
        }
        String locationStr = location.toOSString();
        if (pdeClassPath.contains(locationStr)) {
            continue;
        }
        // extra cleanup for some directories on classpath
        String bundleLocation = bd.getLocation();
        if (bundleLocation != null && !"jar".equals(location.getFileExtension()) &&
                new File(bundleLocation).isDirectory()) {
            if (bd.getSymbolicName().equals(location.lastSegment())) {
                // ignore badly resolved plugin directories inside workspace
                // ("." as classpath is resolved as plugin root directory)
                // which is, if under workspace, NOT a part of the classpath
                continue;
            }
        }
        if (!location.isAbsolute()) {
            location = ResourceUtils.relativeToAbsolute(location);
        }
        if (!isValidPath(location)) {
            continue;
        }
        locationStr = location.toOSString();
        if (!pdeClassPath.contains(locationStr)) {
            pdeClassPath.add(locationStr);
        }
    }
}