List of usage examples for org.eclipse.jdt.core IClasspathEntry getEntryKind
int getEntryKind();
From source file:org.ebayopensource.vjet.eclipse.internal.launching.LauncherUtil.java
License:Open Source License
public static void getTransitiveClosureProjectDependnecyList(IJavaProject javaProject, Map<String, IJavaProject> transitiveClosureProjectList) throws JavaModelException { IClasspathEntry[] classPathEntries = javaProject.getResolvedClasspath(true); if (classPathEntries != null) { for (IClasspathEntry classPathEntry : classPathEntries) { if (classPathEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) { IResource classPathProject = ResourcesPlugin.getWorkspace().getRoot() .findMember(classPathEntry.getPath()); if (classPathProject != null) { if (!transitiveClosureProjectList.containsKey(classPathProject.getName())) { IJavaProject subJavaProject = getJavaProject(classPathProject); transitiveClosureProjectList.put(classPathProject.getName(), subJavaProject); getTransitiveClosureProjectDependnecyList(subJavaProject, transitiveClosureProjectList); }/*from w w w .j a v a 2s . c o m*/ } } } } }
From source file:org.ebayopensource.vjet.eclipse.internal.launching.LauncherUtil.java
License:Open Source License
private static void getProjectDependentJars(Set<String> jars, IClasspathEntry[] classPathEntries) { if (classPathEntries != null) { for (IClasspathEntry classPathEntry : classPathEntries) { if (classPathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY || classPathEntry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) { IPath path = JavaCore.getResolvedClasspathEntry(classPathEntry).getPath(); if (path != null) { String s = path.toString(); if (!StringUtils.isBlankOrEmpty(s)) { jars.add(s);//from w w w.j a v a 2s .com } } } } } }
From source file:org.ebayopensource.vjet.eclipse.internal.launching.VjetInterpreterRunner.java
License:Open Source License
/** * Launches new jvm instance and runs within it interpreter. * /*from ww w. jav a 2 s . c o m*/ * @param config * launch configuration * @param launch * the result of launching a debug session. * @throws CoreException * if an error occurred when running interpreter * @see {@link ILaunch} * @see {@link } */ public void doRunImpl(InterpreterConfig config, ILaunch launch) throws CoreException { IScriptProject proj = AbstractScriptLaunchConfigurationDelegate .getScriptProject(launch.getLaunchConfiguration()); // do not cross boundary to java sdk - looks like we are tied to Java // IDE here IJavaProject myJavaProject = JavaCore.create(proj.getProject()); IVMInstall vmInstall = myJavaProject.exists() ? JavaRuntime.getVMInstall(myJavaProject) : JavaRuntime.getDefaultVMInstall(); if (vmInstall == null) { throw new CoreException( new Status(IStatus.ERROR, VjetPlugin.PLUGIN_ID, IStatus.ERROR, "No VMInstall", null)); } IVMRunner vmRunner = vmInstall.getVMRunner(m_mode); if (vmRunner == null) { throw new CoreException( new Status(IStatus.ERROR, VjetPlugin.PLUGIN_ID, IStatus.ERROR, "No VMRunner", null)); } IWorkspaceRoot workspaceRoot = proj.getProject().getWorkspace().getRoot(); // prepare js source search path String sourceSearchPath = null; // sourceSearchPath = prepareSourceSearchPath(proj, myJavaProject, // workspaceRoot, sourceSearchPath); // prepare java runtime class path String[] clzPath = VjoRunnerInfo.getClassPath(); if (DEBUG_LAUNCH) { for (String path : clzPath) { System.out.println(path); } } String[] jarPaths = null; if (myJavaProject.exists()) { // add this project's dependent jars (include those from all // dependent projects) // so the dependented javascript can be loaded from those jar files jarPaths = getJarPaths(myJavaProject); } else { jarPaths = getJarPaths(proj); } if (jarPaths.length > 0) { String[] newClzPath = new String[clzPath.length + jarPaths.length]; System.arraycopy(clzPath, 0, newClzPath, 0, clzPath.length); System.arraycopy(jarPaths, 0, newClzPath, clzPath.length, jarPaths.length); clzPath = newClzPath; } VMRunnerConfiguration vmConfig = new VMRunnerConfiguration(VjoRunnerInfo.getClassName(), clzPath); IPath scriptFilePath = config.getScriptFilePath(); if (scriptFilePath == null) { throw new CoreException(new Status(IStatus.ERROR, VjetPlugin.PLUGIN_ID, IStatus.ERROR, "Script File name is not specified...", null)); } else if (!proj.exists() && !myJavaProject.exists()) { throw new CoreException(new Status(IStatus.ERROR, VjetPlugin.PLUGIN_ID, IStatus.ERROR, "Must run js from a VJET or JAVA project!", null)); } // find vjo class String jsFile = scriptFilePath.toFile().getAbsolutePath(); String vjoClz = ""; if (!jsFile.endsWith(".js")) { // do nothing } else { // get vjo class name String sourceRoot = null; if (proj.exists()) {// get vjo class name from script project IModelElement[] children = proj.getChildren(); for (IModelElement elem : children) { if (elem instanceof IProjectFragment) { IProjectFragment melem = (IProjectFragment) elem; if (!melem.isReadOnly()) { String absolutePath = proj.getProject().getLocation().toFile().getAbsolutePath(); String srcDir = absolutePath.substring(0, absolutePath.indexOf(proj.getProject().getName())) + melem.getPath(); srcDir = srcDir.replace("/", "\\"); srcDir = srcDir.replace("\\\\", "\\"); if (jsFile.startsWith(srcDir)) { sourceRoot = srcDir; break; } } } } } if (StringUtils.isBlankOrEmpty(sourceRoot) && myJavaProject.exists()) {// get vjo class name from java // project IClasspathEntry[] classpathEntries = myJavaProject.getRawClasspath(); for (IClasspathEntry entry : classpathEntries) { if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { String path = getSourceFile(entry, workspaceRoot).getAbsolutePath(); if (jsFile.startsWith(path)) { sourceRoot = path; break; } } } } if (sourceRoot != null) { String vjoFile = jsFile.substring(sourceRoot.length() + 1); int suffixIndex = vjoFile.indexOf("."); if (suffixIndex > 0) { vjoFile = vjoFile.substring(0, suffixIndex); } vjoClz = vjoFile.replace("/", "."); vjoClz = vjoClz.replace("\\", "."); } } ILaunchConfiguration launchConf = launch.getLaunchConfiguration(); String args = launchConf.getAttribute(ScriptLaunchConfigurationConstants.ATTR_SCRIPT_ARGUMENTS, "").trim(); Map env = launchConf.getAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, (Map) new HashMap()); List<String> loadJSList = launchConf.getAttribute( org.ebayopensource.vjet.eclipse.launching.ILaunchConstants.ATTR_INCLUDE_PATH, new ArrayList<String>()); if (loadJSList.size() > 0) { StringBuilder loadJSStr = new StringBuilder(); for (String js : loadJSList) { //int kind = Integer.parseInt(js.substring(0, 1)); String path = js.substring(1); loadJSStr.append(path).append(JsRunner.LIST_SEPERATOR); } env.put(JsRunner.LOAD_JS_KEY, loadJSStr.toString()); } List<String> pArgs = new ArrayList<String>(); if (vjoClz == "" && jsFile.indexOf("htm") != -1) { env.put("type", "html"); } addEnvOptions(env, pArgs); // add env options pArgs.add(scriptFilePath.toFile().getAbsolutePath()); pArgs.add(vjoClz); // pArgs.add(findMainFunc(vjoClz, proj.getProject().getName())); // add script arguments as line pArgs.add(new ArgsNormalizer(args).normalize()); vmConfig.setProgramArguments(pArgs.toArray(new String[] {})); List<String> vmArgs = new ArrayList<String>(4); if (sourceSearchPath != null) { vmArgs.add("-Djava.source.path=" + sourceSearchPath); } IFileStore s = org.eclipse.core.filesystem.EFS.getStore(proj.getProject().getProject().getLocationURI()); File f = s.toLocalFile(0, null); if (m_mode.equals(ILaunchManager.DEBUG_MODE)) { vmArgs.add("-DVJETDebugHost=" + config.getProperty(DbgpConstants.HOST_PROP)); vmArgs.add("-DVJETDebugPort=" + config.getProperty(DbgpConstants.PORT_PROP)); vmArgs.add("-DVJETDebugSessionID=" + config.getProperty(DbgpConstants.SESSION_ID_PROP)); vmArgs.add("-DVJETProjectPath=" + f.getAbsolutePath()); } // org.eclipse.jdt.launching.PROJECT_ATTR String javaprojectprop = "org.eclipse.jdt.launching.PROJECT_ATTR"; String projectName = myJavaProject.getProject().getName(); ILaunchConfigurationWorkingCopy copy = launch.getLaunchConfiguration().getWorkingCopy(); copy.setAttribute(javaprojectprop, projectName); copy.doSave(); setupProxySrcLocator(launch, copy); vmConfig.setVMArguments(vmArgs.toArray(new String[vmArgs.size()])); // ILaunch launchr = new Launch(launch.ge, m_mode, null); // // ISourceLookupDirector sourceLocator = new JavaSourceLookupDirector(); // sourceLocator // .setSourcePathComputer(DebugPlugin.getDefault().getLaunchManager() // .getSourcePathComputer( // "org.eclipse.jdt.launching.sourceLookup.javaSourcePathComputer")); //$NON-NLS-1$ // sourceLocator.initializeDefaults(launchConf); // // launch.setSourceLocator(sourceLocator); vmRunner.run(vmConfig, launch, null); IDebugTarget[] debugTargets = launch.getDebugTargets(); if (debugTargets.length > 0) { VjetLaunchingPlugin.getDefault().getLog() .log(new Status( IStatus.INFO, VjetPlugin.PLUGIN_ID, IStatus.INFO, "!USAGE_TRACKING: NAME=" + VjetPlugin.PLUGIN_ID + ".debug; ACCESS_TIME=" + new Date().toString() + ";", null)); } else { VjetLaunchingPlugin.getDefault().getLog() .log(new Status( IStatus.INFO, VjetPlugin.PLUGIN_ID, IStatus.INFO, "!USAGE_TRACKING: NAME=" + VjetPlugin.PLUGIN_ID + ".run; ACCESS_TIME=" + new Date().toString() + ";", null)); } // for (int a = 0; a < debugTargets.length; a++) { // // launch.addDebugTarget(debugTargets[a]); // } // // IProcess[] processes = launch.getProcesses(); // for (int a = 0; a < processes.length; a++) { // launch.addProcess(processes[a]); // } }
From source file:org.ebayopensource.vjet.eclipse.internal.launching.VjetInterpreterRunner.java
License:Open Source License
private static void getProjectSourcePaths(List<File> sourcePaths, IWorkspaceRoot workspaceRoot, IClasspathEntry[] classPathEntries) { if (classPathEntries != null) { for (IClasspathEntry classPathEntry : classPathEntries) { if (classPathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { sourcePaths.add(getSourceFile(classPathEntry, workspaceRoot)); }// w ww . j a v a2s . c o m } } }
From source file:org.eclim.plugin.jdt.command.junit.JUnitCommand.java
License:Open Source License
private void createBatchTest(IJavaProject javaProject, JUnitTask junit, String pattern) throws Exception { if (!pattern.endsWith(".java")) { pattern += ".java"; }// w w w. ja va 2s . c o m BatchTest batch = junit.createBatchTest(); IClasspathEntry[] entries = javaProject.getResolvedClasspath(true); for (IClasspathEntry entry : entries) { if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { String path = ProjectUtils.getFilePath(javaProject.getProject(), entry.getPath().toOSString()); FileSet fileset = new FileSet(); fileset.setDir(new File(path)); fileset.setIncludes(pattern); batch.addFileSet(fileset); } } }
From source file:org.eclim.plugin.jdt.command.launching.JavaCommand.java
License:Open Source License
private String findMainClass(IJavaProject javaProject) throws Exception { ArrayList<IJavaElement> srcs = new ArrayList<IJavaElement>(); for (IClasspathEntry entry : javaProject.getResolvedClasspath(true)) { if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { for (IPackageFragmentRoot root : javaProject.findPackageFragmentRoots(entry)) { srcs.add(root);// w w w .j a v a 2 s . c o m } } } final ArrayList<IMethod> methods = new ArrayList<IMethod>(); int context = IJavaSearchConstants.DECLARATIONS; int type = IJavaSearchConstants.METHOD; int matchType = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE; IJavaSearchScope scope = SearchEngine.createJavaSearchScope(srcs.toArray(new IJavaElement[srcs.size()])); SearchPattern pattern = SearchPattern.createPattern("main(String[])", type, context, matchType); SearchRequestor requestor = new SearchRequestor() { public void acceptSearchMatch(SearchMatch match) { if (match.getAccuracy() != SearchMatch.A_ACCURATE) { return; } try { IMethod method = (IMethod) match.getElement(); String[] params = method.getParameterTypes(); if (params.length != 1) { return; } if (!Signature.SIG_VOID.equals(method.getReturnType())) { return; } int flags = method.getFlags(); if (!Flags.isPublic(flags) || !Flags.isStatic(flags)) { return; } methods.add(method); } catch (JavaModelException e) { // ignore } } }; SearchEngine engine = new SearchEngine(); SearchParticipant[] participants = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }; engine.search(pattern, participants, scope, requestor, null); // if we found only 1 result, we can use it. if (methods.size() == 1) { IMethod method = methods.get(0); ICompilationUnit cu = method.getCompilationUnit(); IPackageDeclaration[] packages = cu.getPackageDeclarations(); if (packages != null && packages.length > 0) { return packages[0].getElementName() + "." + cu.getElementName(); } return cu.getElementName(); } return null; }
From source file:org.eclim.plugin.jdt.project.JavaProjectManager.java
License:Open Source License
/** * Creates a new project.//from w ww . jav a2s. c o m * * @param project The project. * @param depends Comma separated project names this project depends on. */ protected void create(IProject project, String depends) throws Exception { // with scala-ide installed, apparently this needs to be explicitly done IProjectDescription desc = project.getDescription(); if (!desc.hasNature(PluginResources.NATURE)) { String[] natures = desc.getNatureIds(); String[] newNatures = new String[natures.length + 1]; System.arraycopy(natures, 0, newNatures, 0, natures.length); newNatures[natures.length] = PluginResources.NATURE; desc.setNatureIds(newNatures); project.setDescription(desc, new NullProgressMonitor()); } IJavaProject javaProject = JavaCore.create(project); ((JavaProject) javaProject).configure(); if (!project.getFile(CLASSPATH).exists()) { ArrayList<IClasspathEntry> classpath = new ArrayList<IClasspathEntry>(); boolean source = false; boolean container = false; ClassPathDetector detector = new ClassPathDetector(project, null); for (IClasspathEntry entry : detector.getClasspath()) { if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { source = true; } else if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) { container = true; } classpath.add(entry); } // default source folder if (!source) { IResource src; IPreferenceStore store = PreferenceConstants.getPreferenceStore(); String name = store.getString(PreferenceConstants.SRCBIN_SRCNAME); boolean srcBinFolders = store.getBoolean(PreferenceConstants.SRCBIN_FOLDERS_IN_NEWPROJ); if (srcBinFolders && name.length() > 0) { src = javaProject.getProject().getFolder(name); } else { src = javaProject.getProject(); } classpath.add(new CPListElement(javaProject, IClasspathEntry.CPE_SOURCE, src.getFullPath(), src) .getClasspathEntry()); File srcPath = new File(ProjectUtils.getFilePath(project, src.getFullPath().toString())); if (!srcPath.exists()) { srcPath.mkdirs(); } } // default containers if (!container) { for (IClasspathEntry entry : PreferenceConstants.getDefaultJRELibrary()) { classpath.add(entry); } } // dependencies on other projects for (IClasspathEntry entry : createOrUpdateDependencies(javaProject, depends)) { classpath.add(entry); } javaProject.setRawClasspath(classpath.toArray(new IClasspathEntry[classpath.size()]), null); // output location IPath output = detector.getOutputLocation(); if (output == null) { output = BuildPathsBlock.getDefaultOutputLocation(javaProject); } javaProject.setOutputLocation(output, null); } javaProject.makeConsistent(null); javaProject.save(null, false); }
From source file:org.eclim.plugin.jdt.project.JavaProjectManager.java
License:Open Source License
/** * Merge the supplied project's classpath with entries found in the specified * build file.//from w ww. j ava 2 s .c om * * @param project The project. * @param buildfile The path to the build file (pom.xml, ivy.xml, etc) * @return The classpath entries. */ protected IClasspathEntry[] mergeWithBuildfile(IJavaProject project, String buildfile) throws Exception { String filename = FileUtils.getBaseName(buildfile); Parser parser = PARSERS.get(filename); String var = parser.getClasspathVar(); Dependency[] dependencies = parser.parse(buildfile); IWorkspaceRoot root = project.getProject().getWorkspace().getRoot(); ArrayList<IClasspathEntry> results = new ArrayList<IClasspathEntry>(); // load the results with all the non library entries. IClasspathEntry[] entries = project.getRawClasspath(); for (IClasspathEntry entry : entries) { if (entry.getEntryKind() != IClasspathEntry.CPE_LIBRARY && entry.getEntryKind() != IClasspathEntry.CPE_VARIABLE) { results.add(entry); } else { IPath path = entry.getPath(); String prefix = path != null ? path.segment(0) : null; if ((!var.equals(prefix)) || preserve(entry)) { results.add(entry); } } } // merge the dependencies with the classpath entires. for (int ii = 0; ii < dependencies.length; ii++) { IClasspathEntry match = null; for (int jj = 0; jj < entries.length; jj++) { if (entries[jj].getEntryKind() == IClasspathEntry.CPE_LIBRARY || entries[jj].getEntryKind() == IClasspathEntry.CPE_VARIABLE) { String path = entries[jj].getPath().toOSString(); String pattern = dependencies[ii].getName() + Dependency.VERSION_SEPARATOR; // exact match if (path.endsWith(dependencies[ii].toString())) { match = entries[jj]; results.add(entries[jj]); break; // different version match } else if (path.indexOf(pattern) != -1) { break; } } else if (entries[jj].getEntryKind() == IClasspathEntry.CPE_PROJECT) { String path = entries[jj].getPath().toOSString(); if (path.endsWith(dependencies[ii].getName())) { match = entries[jj]; break; } } } if (match == null) { IClasspathEntry entry = createEntry(root, project, dependencies[ii]); results.add(entry); } else { match = null; } } return (IClasspathEntry[]) results.toArray(new IClasspathEntry[results.size()]); }
From source file:org.eclim.plugin.jdt.util.ClasspathUtils.java
License:Open Source License
/** * Gets an array of paths representing the project's source paths. * * @param project The java project instance. * @return Array of paths./* w ww . j a v a 2s . c o m*/ */ public static String[] getSrcPaths(IJavaProject project) throws Exception { ArrayList<String> paths = new ArrayList<String>(); IClasspathEntry[] entries = project.getResolvedClasspath(true); for (IClasspathEntry entry : entries) { if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { paths.add(ProjectUtils.getFilePath(project.getProject(), entry.getPath().toOSString())); } } return paths.toArray(new String[paths.size()]); }
From source file:org.eclim.plugin.jdt.util.ClasspathUtils.java
License:Open Source License
/** * Recursively collects classpath entries from the current and dependent * projects./*w ww. j ava 2 s.c o m*/ */ private static void collect(IJavaProject javaProject, List<String> paths, Set<IJavaProject> visited, boolean isFirstProject) throws Exception { if (visited.contains(javaProject)) { return; } visited.add(javaProject); try { IPath out = javaProject.getOutputLocation(); paths.add(ProjectUtils.getFilePath(javaProject.getProject(), out.addTrailingSeparator().toOSString())); } catch (JavaModelException ignore) { // ignore... just signals that no output dir was configured. } IProject project = javaProject.getProject(); String name = project.getName(); IClasspathEntry[] entries = null; try { entries = javaProject.getResolvedClasspath(true); } catch (JavaModelException jme) { // this may or may not be a problem. logger.warn("Unable to retrieve resolved classpath for project: " + name, jme); return; } final List<IJavaProject> nextProjects = new ArrayList<IJavaProject>(); for (IClasspathEntry entry : entries) { switch (entry.getEntryKind()) { case IClasspathEntry.CPE_LIBRARY: case IClasspathEntry.CPE_CONTAINER: case IClasspathEntry.CPE_VARIABLE: String path = entry.getPath().toOSString().replace('\\', '/'); if (path.startsWith("/" + name + "/")) { path = ProjectUtils.getFilePath(project, path); } paths.add(path); break; case IClasspathEntry.CPE_PROJECT: if (isFirstProject || entry.isExported()) { javaProject = JavaUtils.getJavaProject(entry.getPath().segment(0)); if (javaProject != null) { // breadth first, not depth first, to preserve dependency ordering nextProjects.add(javaProject); } } break; case IClasspathEntry.CPE_SOURCE: IPath out = entry.getOutputLocation(); if (out != null) { paths.add(ProjectUtils.getFilePath(javaProject.getProject(), out.addTrailingSeparator().toOSString())); } break; } } // depth second for (final IJavaProject nextProject : nextProjects) { collect(nextProject, paths, visited, false); } }