List of usage examples for org.eclipse.jdt.core IJavaProject getOutputLocation
IPath getOutputLocation() throws JavaModelException;
From source file:com.android.ide.eclipse.common.project.BaseProjectHelper.java
License:Open Source License
/** * Returns the {@link IFolder} representing the output for the project. * <p>/*from w ww. ja v a 2s . com*/ * The project must be a java project and be opened, or the method will return null. * @param project the {@link IProject} * @return an IFolder item or null. */ public final static IFolder getOutputFolder(IProject project) { try { if (project.isOpen() && project.hasNature(JavaCore.NATURE_ID)) { // get a java project from the normal project object IJavaProject javaProject = JavaCore.create(project); IPath path = javaProject.getOutputLocation(); IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot(); IResource outputResource = wsRoot.findMember(path); if (outputResource != null && outputResource.getType() == IResource.FOLDER) { return (IFolder) outputResource; } } } catch (JavaModelException e) { // Let's do nothing and return null } catch (CoreException e) { // Let's do nothing and return null } return null; }
From source file:com.android.ide.eclipse.mock.Mocks.java
License:Open Source License
public static IJavaProject createProject(IClasspathEntry[] entries, IPath outputLocation) throws Exception { IJavaProject javaProject = createMock(IJavaProject.class); final Capture<IClasspathEntry[]> capturedEntries = new Capture<IClasspathEntry[]>(); Capture<IPath> capturedOutput = new Capture<IPath>(); capturedEntries.setValue(entries);/* ww w. j a va 2 s . co m*/ capturedOutput.setValue(outputLocation); IProject project = createProject(); expect(javaProject.getProject()).andReturn(project).anyTimes(); expect(javaProject.getOutputLocation()).andReturn(capturedOutput.getValue()).anyTimes(); expect(javaProject.getRawClasspath()).andAnswer(new IAnswer<IClasspathEntry[]>() { @Override public IClasspathEntry[] answer() throws Throwable { return capturedEntries.getValue(); } }).anyTimes(); javaProject.setRawClasspath(capture(capturedEntries), isA(IProgressMonitor.class)); expectLastCall().anyTimes(); javaProject.setRawClasspath(capture(capturedEntries), capture(capturedOutput), isA(IProgressMonitor.class)); expectLastCall().anyTimes(); final Capture<String> capturedCompliance = new Capture<String>(); capturedCompliance.setValue("1.4"); final Capture<String> capturedSource = new Capture<String>(); capturedSource.setValue("1.4"); final Capture<String> capturedTarget = new Capture<String>(); capturedTarget.setValue("1.4"); expect(javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true)).andAnswer(new IAnswer<String>() { @Override public String answer() throws Throwable { return capturedCompliance.getValue(); } }); expect(javaProject.getOption(JavaCore.COMPILER_SOURCE, true)).andAnswer(new IAnswer<String>() { @Override public String answer() throws Throwable { return capturedSource.getValue(); } }); expect(javaProject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true)) .andAnswer(new IAnswer<String>() { @Override public String answer() throws Throwable { return capturedTarget.getValue(); } }); javaProject.setOption(eq(JavaCore.COMPILER_COMPLIANCE), capture(capturedCompliance)); expectLastCall().anyTimes(); javaProject.setOption(eq(JavaCore.COMPILER_SOURCE), capture(capturedSource)); expectLastCall().anyTimes(); javaProject.setOption(eq(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM), capture(capturedTarget)); expectLastCall().anyTimes(); replay(javaProject); return javaProject; }
From source file:com.carrotgarden.eclipse.fileinstall.util.ProjectUtil.java
License:BSD License
/** * Discover project manifest from multiple locations. *//*from w w w. ja va2s . c o m*/ private static File manifestDiscover(final IProject project) { if (NatureUtil.hasJavaNature(project)) { try { final IJavaProject java = JavaCore.create(project); final IPath output = java.getOutputLocation().makeRelativeTo(project.getFullPath()); final IFile manifest = project.getFolder(output).getFile(MANIFEST_PATH); return manifest.getLocation().toFile(); } catch (final Throwable e) { Plugin.logErrr("ProjectUtil#manifest: failure", e); return manifestDefault(project); } } return manifestDefault(project); }
From source file:com.drgarbage.bytecode.BytecodeUtils.java
License:Apache License
/** * Returns the fully qualified type name for a given {@link IResource}. * For resources which are not included in the project's build path * returns always <code>null</code>. * //from w ww . j ava 2 s .c o m * @param classFileResource * @return the fully qualified name as a string or <code>null</code> */ public static String toFullyQualifiedTypeName(IResource classFileResource) { IProject project = classFileResource.getProject(); /* create java project */ IJavaProject javaProject = JavaCore.create(project); IPath wspacePath = classFileResource.getFullPath(); IPath outputDir = null; try { outputDir = javaProject.getOutputLocation(); } catch (JavaModelException e) { throw new RuntimeException(e); } IPath classToFind = null; if (outputDir.matchingFirstSegments(wspacePath) == outputDir.segmentCount()) { /* * if we are in the output directory strip the output directory as * we want the project relative path */ classToFind = wspacePath.removeFirstSegments(outputDir.segmentCount()); } if (classToFind != null) { classToFind = classToFind.removeFileExtension(); String typeToFind = classToFind.toString(); typeToFind = toJavaSrcName(typeToFind); return typeToFind; } return null; }
From source file:com.drgarbage.bytecodevisualizer.editors.BytecodeEditor.java
License:Apache License
/** * Creates page: SourceCode View/*from w ww .j a va 2 s. com*/ */ protected void createSourceCodeTab() { try { sourceCodeViewerComposite = new Composite(getTabFolder(), SWT.NONE); sourceCodeViewerComposite.setLayout(new FillLayout()); IEditorInput sourceCodeViewerInput = getSourceCodeViewerInput(); if (sourceCodeViewerInput instanceof IFileEditorInput) { /* a local class file */ IFileEditorInput fileEditorInput = (IFileEditorInput) sourceCodeViewerInput; IFile file = fileEditorInput.getFile(); IProject project = file.getProject(); /* create java project */ IJavaProject javaProject = JavaCore.create(project); try { IPath wspacePath = file.getFullPath(); IPath outputDir = javaProject.getOutputLocation(); IPath classToFind = null; if (outputDir.matchingFirstSegments(wspacePath) == outputDir.segmentCount()) { /* if we are in the output directory * strip the output directory * as we want the project relative path */ classToFind = wspacePath.removeFirstSegments(outputDir.segmentCount()); } if (classToFind != null) { classToFind = classToFind.removeFileExtension(); String typeToFind = classToFind.toString(); int indx = typeToFind.indexOf('$'); if (indx != -1) { typeToFind = typeToFind.substring(0, indx); } typeToFind = typeToFind.replace(IPath.SEPARATOR, '.'); sourceCodeViewer = new JavaSourceCodeViewer(); IType t = javaProject.findType(typeToFind); if (t != null) { /* create file input */ IFile sourceFile = (IFile) t.getCompilationUnit().getResource(); IEditorInput input = new FileEditorInput(sourceFile); sourceCodeViewer.init(getEditorSite(), input); } } else { /* try with class file viewer */ sourceCodeViewer = new ClassFileSourcecodeViewer(); sourceCodeViewer.init(getEditorSite(), sourceCodeViewerInput); } } catch (JavaModelException e) { String msg = MessageFormat.format(CoreMessages.ERROR_Cannot_get_Sourcecode, new Object[] { fileEditorInput.getName() }); BytecodeVisualizerPlugin .log(new Status(IStatus.WARNING, BytecodeVisualizerPlugin.PLUGIN_ID, msg, e)); /* try with class file viewer */ sourceCodeViewer = new ClassFileSourcecodeViewer(); sourceCodeViewer.init(getEditorSite(), sourceCodeViewerInput); } } else { sourceCodeViewer = new ClassFileSourcecodeViewer(); sourceCodeViewer.init(getEditorSite(), sourceCodeViewerInput); } sourceCodeViewer.addClassFileEditorReference(this); /* * hook the editor part. It is necessary for initialization of * actions: Toggle Breakpoint, Enable Breakpoint ... */ PartSite p = (PartSite) getSite(); p.setPart((IWorkbenchPart) sourceCodeViewer); sourceCodeViewer.createPartControl(sourceCodeViewerComposite); p.setPart(this); /* set the bytecode visualzer part */ /* create item for page only after createPartControl has succeeded */ int i = addPage(sourceCodeViewerComposite); setPageText(i, BytecodeVisualizerMessages.BytecodeEditor_tab_Source); CTabItem item = getItem(i); Image img = WorkbenchImages.getImage(ISharedImages.IMG_OBJ_FILE); item.setImage(img); } catch (PartInitException e) { Messages.error(CoreMessages.ERROR_CreateNested_Editor + CoreMessages.ExceptionAdditionalMessage); BytecodeVisualizerPlugin.log(BytecodeVisualizerPlugin.createErrorStatus(e.getMessage(), e)); } }
From source file:com.ebmwebsourcing.petals.common.internal.provisional.utils.JavaUtils.java
License:Open Source License
/** * Get the class path from Java project. * * @param javaProject//from ww w . j av a 2 s . c o m * @param getReferencedProjectClasspath * @param binaryDirectory * @return the class path as a list of string locations. */ public static List<String> getClasspath(IJavaProject javaProject, boolean getReferencedProjectClasspath, boolean binaryDirectory) { List<String> paths = new ArrayList<String>(); try { if (javaProject != null) { // Get the raw class path IClasspathEntry[] entries = javaProject.getRawClasspath(); for (IClasspathEntry entry : entries) { switch (entry.getEntryKind()) { case IClasspathEntry.CPE_PROJECT: if (!getReferencedProjectClasspath) break; String projectName = entry.getPath().toString(); IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); IJavaProject jProject = JavaCore.create(project); List<String> subPaths = getClasspath(jProject, true, binaryDirectory); paths.addAll(subPaths); break; case IClasspathEntry.CPE_LIBRARY: IPath path = entry.getPath(); paths.add(path.toString()); break; case IClasspathEntry.CPE_VARIABLE: entry = JavaCore.getResolvedClasspathEntry(entry); if (entry != null) { path = entry.getPath(); paths.add(path.toString()); } break; } } // Add the "bin" directory? if (binaryDirectory && javaProject.getOutputLocation() != null) { IPath path = ResourcesPlugin.getWorkspace().getRoot().getLocation(); path = path.append(javaProject.getOutputLocation()); paths.add(path.toString()); } } } catch (JavaModelException e) { PetalsCommonPlugin.log(e, IStatus.ERROR); } return paths; }
From source file:com.ebmwebsourcing.petals.common.internal.provisional.utils.JaxWsUtils.java
License:Open Source License
/** * Generates a WSDL from a JAX-WS annotated class. * <p>// w ww .j a v a 2s.c om * If an error occurs during the execution, it will appear in the returned * string builder. * </p> * * @param className the name of the annotated class * @param targetDirectory the target directory * @param jp the Java project that contains this class * @return a string builder containing the execution details * @throws IOException if WSgen was not found, or if the process could not be started * @throws InterruptedException * @throws JaxWsException if the generation seems to have failed */ public StringBuilder generateWsdl(String className, File targetDirectory, IJavaProject jp) throws IOException, InterruptedException, JaxWsException { // Create the temporary file synchronized (TEMP_FILE) { if (!TEMP_FILE.exists() && !TEMP_FILE.mkdir()) throw new IOException("The class directories could not be created."); } final StringBuilder outputBuffer = new StringBuilder(); int exitValue = 0; try { File executable = getJavaExecutable(true); // Build the class path StringBuilder classpath = new StringBuilder(); String separator = System.getProperty("path.separator"); for (String entry : JavaUtils.getClasspath(jp, true, true)) classpath.append(entry + separator); IPath binPath = null; try { binPath = jp.getOutputLocation(); } catch (JavaModelException e) { PetalsCommonPlugin.log(e, IStatus.ERROR); } if (binPath != null) { binPath = ResourcesPlugin.getWorkspace().getRoot().getLocation().append(binPath); File binFile = binPath.toFile(); if (!binFile.exists() && !binFile.mkdir()) PetalsCommonPlugin.log("The 'bin' directory could not be created.", IStatus.WARNING); } // Build the command line List<String> cmd = new ArrayList<String>(); cmd.add(executable.getAbsolutePath()); cmd.add("-verbose"); cmd.add("-wsdl"); cmd.add("-classpath"); cmd.add(classpath.toString()); cmd.add("-r"); cmd.add(targetDirectory.getAbsolutePath()); cmd.add("-d"); cmd.add(TEMP_FILE.getAbsolutePath()); cmd.add("-s"); cmd.add(TEMP_FILE.getAbsolutePath()); cmd.add(className); // Create the process ProcessBuilder pb = new ProcessBuilder(cmd); pb = pb.redirectErrorStream(true); final Process process = pb.start(); // Monitor the execution Thread loggingThread = new Thread() { @Override public void run() { try { BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = ""; try { while ((line = reader.readLine()) != null) outputBuffer.append(line + "\n"); } finally { reader.close(); } } catch (IOException ioe) { PetalsCommonPlugin.log(ioe, IStatus.ERROR); } } }; loggingThread.start(); loggingThread.join(); exitValue = process.waitFor(); } finally { // Free the temporary directory synchronized (TEMP_FILE) { IoUtils.deleteFilesRecursively(TEMP_FILE.listFiles()); } // Keep a trace of the generation JaxWsException loggingException; if (exitValue != 0) { loggingException = new JaxWsException( "The JAX-WS process did not return 0. An error may have occurred.") { private static final long serialVersionUID = -8585870927871804985L; @Override public void printStackTrace(PrintStream s) { s.print(outputBuffer.toString()); } @Override public void printStackTrace(PrintWriter s) { s.print(outputBuffer.toString()); } }; throw loggingException; } else if (PreferencesManager.logAllJaxWsTraces()) { loggingException = new JaxWsException( "Logging the WSDL generation trace (" + jp.getProject().getName() + ").") { private static final long serialVersionUID = -5344307338271840633L; @Override public void printStackTrace(PrintStream s) { s.print(outputBuffer.toString()); } @Override public void printStackTrace(PrintWriter s) { s.print(outputBuffer.toString()); } }; PetalsCommonPlugin.log(loggingException, IStatus.INFO); } } return outputBuffer; }
From source file:com.ecfeed.ui.common.EclipseLoaderProvider.java
License:Open Source License
public ModelClassLoader getLoader(boolean create, ClassLoader parent) { if ((fLoader == null) || create) { List<URL> urls = new ArrayList<URL>(); try {/*from w ww . j a va2s. c o m*/ IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); for (IProject project : projects) { if (project.isOpen() && project.hasNature(JavaCore.NATURE_ID)) { IJavaProject javaProject = JavaCore.create(project); IPath path = project.getLocation() .append(javaProject.getOutputLocation().removeFirstSegments(1)); urls.add(new URL("file", null, path.toOSString() + "/")); IClasspathEntry table[] = javaProject.getResolvedClasspath(true); for (int i = 0; i < table.length; ++i) { if (table[i].getEntryKind() == IClasspathEntry.CPE_LIBRARY) { urls.add(new URL("file", null, table[i].getPath().toOSString())); path = project.getLocation(); path = path.append(table[i].getPath().removeFirstSegments(1)); urls.add(new URL("file", null, path.toOSString())); } } } } if (fLoader != null) { fLoader.close(); } } catch (Throwable e) { } fLoader = new ModelClassLoader(urls.toArray(new URL[] {}), parent); } return fLoader; }
From source file:com.github.ko2ic.plugin.eclipse.taggen.core.service.WorkspaceClassLoader.java
License:Open Source License
private Set<URL> createUrls(IJavaProject javaProject) throws CoreException, MalformedURLException { Set<URL> urlList = new HashSet<>(); IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true); for (IClasspathEntry entry : classpathEntries) { if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { IPath path = entry.getOutputLocation(); if (path != null) { IPath outputPath = javaProject.getOutputLocation().removeFirstSegments(1); IPath outputFullPath = javaProject.getProject().getFolder(outputPath).getLocation(); outputFullPath.append(System.getProperty("line.separator")); URL url = outputFullPath.toFile().toURI().toURL(); urlList.add(url);//from w w w .jav a2 s.c om } } else if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { URL url = entry.getPath().toFile().toURI().toURL(); urlList.add(url); } } return urlList; }
From source file:com.google.gdt.eclipse.core.projects.ProjectChangeTimestampTracker.java
License:Open Source License
private static boolean isResourceInAnOutputPath(IResource resource) throws JavaModelException { IProject project = resource.getProject(); IJavaProject javaProject = JavaCore.create(project); if (javaProject != null) { IPath resourcePath = resource.getFullPath(); if (WebAppUtilities.isWebApp(project)) { if (WebAppUtilities.hasManagedWarOut(project) && WebAppUtilities.getManagedWarOut(project).getFullPath().isPrefixOf(resourcePath)) { return true; }/* w w w . j a v a 2 s. co m*/ IPath previousWarOutAbsPath = WebAppProjectProperties.getLastUsedWarOutLocation(project); if (previousWarOutAbsPath != null && previousWarOutAbsPath.isPrefixOf(resource.getLocation())) { return true; } } if (javaProject.getOutputLocation().isPrefixOf(resourcePath)) { return true; } IClasspathEntry[] resolvedClasspath = javaProject.getResolvedClasspath(false); for (IClasspathEntry classpathEntry : resolvedClasspath) { IPath outputLocation = classpathEntry.getOutputLocation(); if (outputLocation != null && outputLocation.isPrefixOf(resourcePath)) { return true; } } } return false; }