List of usage examples for org.eclipse.jdt.core IClasspathEntry CPE_SOURCE
int CPE_SOURCE
To view the source code for org.eclipse.jdt.core IClasspathEntry CPE_SOURCE.
Click Source Link
From source file:com.android.ide.eclipse.adt.internal.project.ProjectHelperTest.java
License:Open Source License
public final void testFixProjectClasspathEntriesFromOldContainer() throws Exception { // create a project with a path to an android .zip IJavaProject javaProject = Mocks.createProject( new IClasspathEntry[] { Mocks.createClasspathEntry(new Path("Project/src"), //$NON-NLS-1$ IClasspathEntry.CPE_SOURCE), Mocks.createClasspathEntry(new Path(OLD_CONTAINER_ID), IClasspathEntry.CPE_CONTAINER), }, new Path("Project/bin")); ProjectHelper.fixProjectClasspathEntries(javaProject); IClasspathEntry[] fixedEntries = javaProject.getRawClasspath(); assertEquals(3, fixedEntries.length); assertEquals("Project/src", fixedEntries[0].getPath().toString()); assertEquals(OLD_CONTAINER_ID, fixedEntries[1].getPath().toString()); assertEquals(CONTAINER_ID, fixedEntries[2].getPath().toString()); }
From source file:com.android.ide.eclipse.adt.internal.wizards.exportgradle.BuildFileCreator.java
License:Open Source License
/** * Outputs a sourceSets block to the Android task that locates all of the various source * subdirectories in the project./*from w ww . j a v a2 s . c o m*/ */ private void createAndroidSourceSets() { IFolderWrapper projectFolder = new IFolderWrapper(mModule.getProject()); IAbstractFile mManifestFile = AndroidManifest.getManifest(projectFolder); if (mManifestFile == null) { return; } List<String> srcDirs = new ArrayList<String>(); for (IClasspathEntry entry : mModule.getJavaProject().readRawClasspath()) { if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE || SdkConstants.FD_GEN_SOURCES.equals(entry.getPath().lastSegment())) { continue; } IPath path = entry.getPath().removeFirstSegments(1); srcDirs.add("'" + path.toOSString() + "'"); //$NON-NLS-1$ } String srcPaths = Joiner.on(",").join(srcDirs); mBuildFile.append(" sourceSets {\n"); //$NON-NLS-1$ mBuildFile.append(" main {\n"); //$NON-NLS-1$ mBuildFile.append(" manifest.srcFile '" + SdkConstants.FN_ANDROID_MANIFEST_XML + "'\n"); //$NON-NLS-1$ mBuildFile.append(" java.srcDirs = [" + srcPaths + "]\n"); //$NON-NLS-1$ mBuildFile.append(" resources.srcDirs = [" + srcPaths + "]\n"); //$NON-NLS-1$ mBuildFile.append(" aidl.srcDirs = [" + srcPaths + "]\n"); //$NON-NLS-1$ mBuildFile.append(" renderscript.srcDirs = [" + srcPaths + "]\n"); //$NON-NLS-1$ mBuildFile.append(" res.srcDirs = ['res']\n"); //$NON-NLS-1$ mBuildFile.append(" assets.srcDirs = ['assets']\n"); //$NON-NLS-1$ mBuildFile.append(" }\n"); //$NON-NLS-1$ mBuildFile.append("\n"); //$NON-NLS-1$ mBuildFile.append(" // Move the tests to tests/java, tests/res, etc...\n"); //$NON-NLS-1$ mBuildFile.append(" instrumentTest.setRoot('tests')\n"); //$NON-NLS-1$ if (srcDirs.contains("'src'")) { mBuildFile.append("\n"); //$NON-NLS-1$ mBuildFile.append(" // Move the build types to build-types/<type>\n"); //$NON-NLS-1$ mBuildFile.append( " // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...\n"); //$NON-NLS-1$ mBuildFile.append( " // This moves them out of them default location under src/<type>/... which would\n"); //$NON-NLS-1$ mBuildFile.append(" // conflict with src/ being used by the main source set.\n"); //$NON-NLS-1$ mBuildFile.append(" // Adding new build types or product flavors should be accompanied\n"); //$NON-NLS-1$ mBuildFile.append(" // by a similar customization.\n"); //$NON-NLS-1$ mBuildFile.append(" debug.setRoot('build-types/debug')\n"); //$NON-NLS-1$ mBuildFile.append(" release.setRoot('build-types/release')\n"); //$NON-NLS-1$ } mBuildFile.append(" }\n"); //$NON-NLS-1$ }
From source file:com.android.ide.eclipse.adt.internal.wizards.exportgradle.BuildFileCreator.java
License:Open Source License
/** * Outputs a sourceSets block for non-Android projects to locate the source directories. *//* w ww. j a v a 2 s . c o m*/ private void createJavaSourceSets() { List<String> dirs = new ArrayList<String>(); for (IClasspathEntry entry : mModule.getJavaProject().readRawClasspath()) { if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) { continue; } IPath path = entry.getPath().removeFirstSegments(1); dirs.add("'" + path.toOSString() + "'"); //$NON-NLS-1$ } String srcPaths = Joiner.on(",").join(dirs); mBuildFile.append("sourceSets {\n"); //$NON-NLS-1$ mBuildFile.append(" main.java.srcDirs = [" + srcPaths + "]\n"); //$NON-NLS-1$ mBuildFile.append(" main.resources.srcDirs = [" + srcPaths + "]\n"); //$NON-NLS-1$ mBuildFile.append(" test.java.srcDirs = ['tests/java']\n"); //$NON-NLS-1$ mBuildFile.append(" test.resources.srcDirs = ['tests/resources']\n"); //$NON-NLS-1$ mBuildFile.append("}\n"); //$NON-NLS-1$ }
From source file:com.android.ide.eclipse.adt.project.ProjectHelper.java
License:Open Source License
/** * Fix the project classpath entries. The method ensures that: * <ul>/*from w w w . j av a 2 s . c o m*/ * <li>The project does not reference any old android.zip/android.jar archive.</li> * <li>The project does not use its output folder as a sourc folder.</li> * <li>The project does not reference a desktop JRE</li> * <li>The project references the AndroidClasspathContainer. * </ul> * @param javaProject The project to fix. * @throws JavaModelException */ public static void fixProjectClasspathEntries(IJavaProject javaProject) throws JavaModelException { // get the project classpath IClasspathEntry[] entries = javaProject.getRawClasspath(); IClasspathEntry[] oldEntries = entries; // check if the JRE is set as library int jreIndex = ProjectHelper.findClasspathEntryByPath(entries, JavaRuntime.JRE_CONTAINER, IClasspathEntry.CPE_CONTAINER); if (jreIndex != -1) { // the project has a JRE included, we remove it entries = ProjectHelper.removeEntryFromClasspath(entries, jreIndex); } // get the output folder IPath outputFolder = javaProject.getOutputLocation(); boolean foundContainer = false; for (int i = 0; i < entries.length;) { // get the entry and kind IClasspathEntry entry = entries[i]; int kind = entry.getEntryKind(); if (kind == IClasspathEntry.CPE_SOURCE) { IPath path = entry.getPath(); if (path.equals(outputFolder)) { entries = ProjectHelper.removeEntryFromClasspath(entries, i); // continue, to skip the i++; continue; } } else if (kind == IClasspathEntry.CPE_CONTAINER) { if (AndroidClasspathContainerInitializer.checkPath(entry.getPath())) { foundContainer = true; } } i++; } // if the framework container is not there, we add it if (foundContainer == false) { // add the android container to the array entries = ProjectHelper.addEntryToClasspath(entries, AndroidClasspathContainerInitializer.getContainerEntry()); } // set the new list of entries to the project if (entries != oldEntries) { javaProject.setRawClasspath(entries, new NullProgressMonitor()); } // If needed, check and fix compiler compliance and source compatibility ProjectHelper.checkAndFixCompilerCompliance(javaProject); }
From source file:com.android.ide.eclipse.adt.project.ProjectHelperTest.java
License:Open Source License
public final void testFixProjectClasspathEntriesFromOldContainer() throws JavaModelException { // create a project with a path to an android .zip JavaProjectMock javaProject = new JavaProjectMock( new IClasspathEntry[] { new ClasspathEntryMock(new Path("Project/src"), //$NON-NLS-1$ IClasspathEntry.CPE_SOURCE), new ClasspathEntryMock(new Path(OLD_CONTAINER_ID), IClasspathEntry.CPE_CONTAINER), }, new Path("Project/bin")); ProjectHelper.fixProjectClasspathEntries(javaProject); IClasspathEntry[] fixedEntries = javaProject.getRawClasspath(); assertEquals(3, fixedEntries.length); assertEquals("Project/src", fixedEntries[0].getPath().toString()); assertEquals(OLD_CONTAINER_ID, fixedEntries[1].getPath().toString()); assertEquals(CONTAINER_ID, fixedEntries[2].getPath().toString()); }
From source file:com.android.ide.eclipse.auidt.internal.build.builders.ResourceManagerBuilder.java
License:Open Source License
@SuppressWarnings("unchecked") @Override/* www . jav a2 s .c o m*/ protected IProject[] build(int kind, Map args, IProgressMonitor monitor) throws CoreException { // Get the project. final IProject project = getProject(); IJavaProject javaProject = JavaCore.create(project); // Clear the project of the generic markers removeMarkersFromContainer(project, AdtConstants.MARKER_ADT); // check for existing target marker, in which case we abort. // (this means: no SDK, no target, or unresolvable target.) try { abortOnBadSetup(javaProject); } catch (AbortBuildException e) { return null; } // Check the compiler compliance level, displaying the error message // since this is the first builder. Pair<Integer, String> result = ProjectHelper.checkCompilerCompliance(project); String errorMessage = null; switch (result.getFirst().intValue()) { case ProjectHelper.COMPILER_COMPLIANCE_LEVEL: errorMessage = Messages.Requires_Compiler_Compliance_s; break; case ProjectHelper.COMPILER_COMPLIANCE_SOURCE: errorMessage = Messages.Requires_Source_Compatibility_s; break; case ProjectHelper.COMPILER_COMPLIANCE_CODEGEN_TARGET: errorMessage = Messages.Requires_Class_Compatibility_s; break; } if (errorMessage != null) { errorMessage = String.format(errorMessage, result.getSecond() == null ? "(no value)" : result.getSecond()); markProject(AdtConstants.MARKER_ADT, errorMessage, IMarker.SEVERITY_ERROR); AdtPlugin.printErrorToConsole(project, errorMessage); return null; } // Check that the SDK directory has been setup. String osSdkFolder = AdtPlugin.getOsSdkFolder(); if (osSdkFolder == null || osSdkFolder.length() == 0) { AdtPlugin.printErrorToConsole(project, Messages.No_SDK_Setup_Error); markProject(AdtConstants.MARKER_ADT, Messages.No_SDK_Setup_Error, IMarker.SEVERITY_ERROR); return null; } // check the project has a target IAndroidTarget projectTarget = Sdk.getCurrent().getTarget(project); if (projectTarget == null) { // no target. marker has been set by the container initializer: exit silently. return null; } // check the 'gen' source folder is present boolean hasGenSrcFolder = false; // whether the project has a 'gen' source folder setup IClasspathEntry[] classpaths = javaProject.readRawClasspath(); if (classpaths != null) { for (IClasspathEntry e : classpaths) { if (e.getEntryKind() == IClasspathEntry.CPE_SOURCE) { IPath path = e.getPath(); if (path.segmentCount() == 2 && path.segment(1).equals(SdkConstants.FD_GEN_SOURCES)) { hasGenSrcFolder = true; break; } } } } boolean genFolderPresent = false; // whether the gen folder actually exists IResource resource = project.findMember(SdkConstants.FD_GEN_SOURCES); genFolderPresent = resource != null && resource.exists(); if (hasGenSrcFolder == false && genFolderPresent) { // No source folder setup for 'gen' in the project, but there's already a // 'gen' resource (file or folder). String message; if (resource.getType() == IResource.FOLDER) { // folder exists already! This is an error. If the folder had been created // by the NewProjectWizard, it'd be a source folder. message = String.format( "%1$s already exists but is not a source folder. Convert to a source folder or rename it.", resource.getFullPath().toString()); } else { // resource exists but is not a folder. message = String.format( "Resource %1$s is in the way. ADT needs a source folder called 'gen' to work. Rename or delete resource.", resource.getFullPath().toString()); } AdtPlugin.printErrorToConsole(project, message); markProject(AdtConstants.MARKER_ADT, message, IMarker.SEVERITY_ERROR); return null; } else if (hasGenSrcFolder == false || genFolderPresent == false) { // either there is no 'gen' source folder in the project (older SDK), // or the folder does not exist (was deleted, or was a fresh svn checkout maybe.) // In case we are migrating from an older SDK, we go through the current source // folders and delete the generated Java files. List<IPath> sourceFolders = BaseProjectHelper.getSourceClasspaths(javaProject); IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); for (IPath path : sourceFolders) { IResource member = root.findMember(path); if (member != null) { removeDerivedResources(member, monitor); } } // create the new source folder, if needed IFolder genFolder = project.getFolder(SdkConstants.FD_GEN_SOURCES); if (genFolderPresent == false) { AdtPlugin.printBuildToConsole(BuildVerbosity.VERBOSE, project, "Creating 'gen' source folder for generated Java files"); genFolder.create(true /* force */, true /* local */, new SubProgressMonitor(monitor, 10)); } // add it to the source folder list, if needed only (or it will throw) if (hasGenSrcFolder == false) { IClasspathEntry[] entries = javaProject.getRawClasspath(); entries = ProjectHelper.addEntryToClasspath(entries, JavaCore.newSourceEntry(genFolder.getFullPath())); javaProject.setRawClasspath(entries, new SubProgressMonitor(monitor, 10)); } // refresh specifically the gen folder first, as it may break the build // if it doesn't arrive in time then refresh the whole project as usual. genFolder.refreshLocal(IResource.DEPTH_ZERO, new SubProgressMonitor(monitor, 10)); project.refreshLocal(IResource.DEPTH_INFINITE, new SubProgressMonitor(monitor, 10)); // it seems like doing this fails to properly rebuild the project. the Java builder // running right after this builder will not see the gen folder, and will not be // restarted after this build. Therefore in this particular case, we start another // build asynchronously so that it's rebuilt after this build. launchJob(new Job("rebuild") { @Override protected IStatus run(IProgressMonitor m) { try { project.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, m); return Status.OK_STATUS; } catch (CoreException e) { return e.getStatus(); } } }); } // convert older projects which use bin as the eclipse output folder into projects // using bin/classes IFolder androidOutput = BaseProjectHelper.getAndroidOutputFolder(project); IFolder javaOutput = BaseProjectHelper.getJavaOutputFolder(project); if (androidOutput.exists() == false || javaOutput == null || javaOutput.getParent().equals(androidOutput) == false) { // get what we want as the new java output. IFolder newJavaOutput = androidOutput.getFolder(SdkConstants.FD_CLASSES_OUTPUT); if (androidOutput.exists() == false) { androidOutput.create(true /*force*/, true /*local*/, monitor); } if (newJavaOutput.exists() == false) { newJavaOutput.create(true /*force*/, true /*local*/, monitor); } // set the java output to this project. javaProject.setOutputLocation(newJavaOutput.getFullPath(), monitor); // need to do a full build. Can't build while we're already building, so launch a // job to build it right after this build launchJob(new Job("rebuild") { @Override protected IStatus run(IProgressMonitor jobMonitor) { try { project.build(IncrementalProjectBuilder.CLEAN_BUILD, jobMonitor); return Status.OK_STATUS; } catch (CoreException e) { return e.getStatus(); } } }); } // check that we have bin/res/ IFolder binResFolder = androidOutput.getFolder(SdkConstants.FD_RESOURCES); if (binResFolder.exists() == false) { binResFolder.create(true /* force */, true /* local */, new SubProgressMonitor(monitor, 10)); project.refreshLocal(IResource.DEPTH_ONE, new SubProgressMonitor(monitor, 10)); } // Check the preference to be sure we are supposed to refresh // the folders. if (AdtPrefs.getPrefs().getBuildForceResResfresh()) { AdtPlugin.printBuildToConsole(BuildVerbosity.VERBOSE, project, Messages.Refreshing_Res); // refresh the res folder. IFolder resFolder = project.getFolder(AdtConstants.WS_RESOURCES); resFolder.refreshLocal(IResource.DEPTH_INFINITE, monitor); // Also refresh the assets folder to make sure the ApkBuilder // will now it's changed and will force a new resource packaging. IFolder assetsFolder = project.getFolder(AdtConstants.WS_ASSETS); assetsFolder.refreshLocal(IResource.DEPTH_INFINITE, monitor); } return null; }
From source file:com.android.ide.eclipse.auidt.internal.lint.EclipseLintClient.java
License:Open Source License
@Override @NonNull// www . j a v a 2s.com protected ClassPathInfo getClassPath(@NonNull Project project) { ClassPathInfo info; if (mProjectInfo == null) { mProjectInfo = Maps.newHashMap(); info = null; } else { info = mProjectInfo.get(project); } if (info == null) { List<File> sources = null; List<File> classes = null; List<File> libraries = null; IProject p = getProject(project); if (p != null) { try { IJavaProject javaProject = BaseProjectHelper.getJavaProject(p); // Output path File file = workspacePathToFile(javaProject.getOutputLocation()); classes = Collections.singletonList(file); // Source path IClasspathEntry[] entries = javaProject.getRawClasspath(); sources = new ArrayList<File>(entries.length); libraries = new ArrayList<File>(entries.length); for (int i = 0; i < entries.length; i++) { IClasspathEntry entry = entries[i]; int kind = entry.getEntryKind(); if (kind == IClasspathEntry.CPE_VARIABLE) { entry = JavaCore.getResolvedClasspathEntry(entry); kind = entry.getEntryKind(); } if (kind == IClasspathEntry.CPE_SOURCE) { sources.add(workspacePathToFile(entry.getPath())); } else if (kind == IClasspathEntry.CPE_LIBRARY) { libraries.add(entry.getPath().toFile()); } // Note that we ignore IClasspathEntry.CPE_CONTAINER: // Normal Android Eclipse projects supply both // AdtConstants.CONTAINER_FRAMEWORK // and // AdtConstants.CONTAINER_LIBRARIES // here. We ignore the framework classes for obvious reasons, // but we also ignore the library container because lint will // process the libraries differently. When Eclipse builds a // project, it gets the .jar output of the library projects // from this container, which means it doesn't have to process // the library sources. Lint on the other hand wants to process // the source code, so instead it actually looks at the // project.properties file to find the libraries, and then it // iterates over all the library projects in turn and analyzes // those separately (but passing the main project for context, // such that the including project's manifest declarations // are used for data like minSdkVersion level). // // Note that this container will also contain *other* // libraries (Java libraries, not library projects) that we // *should* include. However, we can't distinguish these // class path entries from the library project jars, // so instead of looking at these, we simply listFiles() in // the libs/ folder after processing the classpath info } // Add in libraries File libs = new File(project.getDir(), FD_NATIVE_LIBS); if (libs.isDirectory()) { File[] jars = libs.listFiles(); if (jars != null) { for (File jar : jars) { if (AdtUtils.endsWith(jar.getPath(), DOT_JAR)) { libraries.add(jar); } } } } } catch (CoreException e) { AdtPlugin.log(e, null); } } if (sources == null) { sources = super.getClassPath(project).getSourceFolders(); } if (classes == null) { classes = super.getClassPath(project).getClassFolders(); } if (libraries == null) { libraries = super.getClassPath(project).getLibraries(); } info = new ClassPathInfo(sources, classes, libraries); mProjectInfo.put(project, info); } return info; }
From source file:com.android.ide.eclipse.auidt.internal.project.BaseProjectHelper.java
License:Open Source License
/** * returns a list of source classpath for a specified project * @param javaProject// ww w. j a va2 s.c o m * @return a list of path relative to the workspace root. */ public static List<IPath> getSourceClasspaths(IJavaProject javaProject) { ArrayList<IPath> sourceList = new ArrayList<IPath>(); IClasspathEntry[] classpaths = javaProject.readRawClasspath(); if (classpaths != null) { for (IClasspathEntry e : classpaths) { if (e.getEntryKind() == IClasspathEntry.CPE_SOURCE) { sourceList.add(e.getPath()); } } } return sourceList; }
From source file:com.android.ide.eclipse.auidt.internal.project.ProjectHelper.java
License:Open Source License
/** * Fix the project classpath entries. The method ensures that: * <ul>/*from w w w .j a va 2 s. c o m*/ * <li>The project does not reference any old android.zip/android.jar archive.</li> * <li>The project does not use its output folder as a sourc folder.</li> * <li>The project does not reference a desktop JRE</li> * <li>The project references the AndroidClasspathContainer. * </ul> * @param javaProject The project to fix. * @throws JavaModelException */ public static void fixProjectClasspathEntries(IJavaProject javaProject) throws JavaModelException { // get the project classpath IClasspathEntry[] entries = javaProject.getRawClasspath(); IClasspathEntry[] oldEntries = entries; // check if the JRE is set as library int jreIndex = ProjectHelper.findClasspathEntryByPath(entries, JavaRuntime.JRE_CONTAINER, IClasspathEntry.CPE_CONTAINER); if (jreIndex != -1) { // the project has a JRE included, we remove it entries = ProjectHelper.removeEntryFromClasspath(entries, jreIndex); } // get the output folder IPath outputFolder = javaProject.getOutputLocation(); boolean foundFrameworkContainer = false; boolean foundLibrariesContainer = false; for (int i = 0; i < entries.length;) { // get the entry and kind IClasspathEntry entry = entries[i]; int kind = entry.getEntryKind(); if (kind == IClasspathEntry.CPE_SOURCE) { IPath path = entry.getPath(); if (path.equals(outputFolder)) { entries = ProjectHelper.removeEntryFromClasspath(entries, i); // continue, to skip the i++; continue; } } else if (kind == IClasspathEntry.CPE_CONTAINER) { String path = entry.getPath().toString(); if (AdtConstants.CONTAINER_FRAMEWORK.equals(path)) { foundFrameworkContainer = true; } if (AdtConstants.CONTAINER_LIBRARIES.equals(path)) { foundLibrariesContainer = true; } } i++; } // same thing for the library container if (foundLibrariesContainer == false) { // add the android container to the array entries = ProjectHelper.addEntryToClasspath(entries, JavaCore.newContainerEntry(new Path(AdtConstants.CONTAINER_LIBRARIES))); } // if the framework container is not there, we add it if (foundFrameworkContainer == false) { // add the android container to the array entries = ProjectHelper.addEntryToClasspath(entries, JavaCore.newContainerEntry(new Path(AdtConstants.CONTAINER_FRAMEWORK))); } // set the new list of entries to the project if (entries != oldEntries) { javaProject.setRawClasspath(entries, new NullProgressMonitor()); } // If needed, check and fix compiler compliance and source compatibility ProjectHelper.checkAndFixCompilerCompliance(javaProject); }
From source file:com.android.ide.eclipse.common.project.BaseProjectHelper.java
License:Open Source License
/** * returns a list of source classpath for a specified project * @param javaProject/*from w w w. j av a2s .c o m*/ * @return a list of path relative to the workspace root. */ public static ArrayList<IPath> getSourceClasspaths(IJavaProject javaProject) { ArrayList<IPath> sourceList = new ArrayList<IPath>(); IClasspathEntry[] classpaths = javaProject.readRawClasspath(); if (classpaths != null) { for (IClasspathEntry e : classpaths) { if (e.getEntryKind() == IClasspathEntry.CPE_SOURCE) { sourceList.add(e.getPath()); } } } return sourceList; }