Methods Summary |
---|
public static org.eclipse.jdt.core.IClasspathEntry[] | addEntryToClasspath(org.eclipse.jdt.core.IClasspathEntry[] entries, org.eclipse.jdt.core.IClasspathEntry new_entry)Adds the corresponding source folder to the class path entries.
int n = entries.length;
IClasspathEntry[] newEntries = new IClasspathEntry[n + 1];
System.arraycopy(entries, 0, newEntries, 0, n);
newEntries[n] = new_entry;
return newEntries;
|
public static final void | checkAndFixCompilerCompliance(org.eclipse.core.resources.IProject project)Checks, and fixes if needed, the compiler compliance level, and the source compatibility
level
// get the java project from the IProject resource object
IJavaProject javaProject = JavaCore.create(project);
// Now we check the compiler compliance level and make sure it is valid
checkAndFixCompilerCompliance(javaProject);
|
public static final void | checkAndFixCompilerCompliance(org.eclipse.jdt.core.IJavaProject javaProject)Checks, and fixes if needed, the compiler compliance level, and the source compatibility
level
if (checkCompilerCompliance(javaProject) != COMPILER_COMPLIANCE_OK) {
// setup the preferred compiler compliance level.
javaProject.setOption(JavaCore.COMPILER_COMPLIANCE,
AndroidConstants.COMPILER_COMPLIANCE_PREFERRED);
javaProject.setOption(JavaCore.COMPILER_SOURCE,
AndroidConstants.COMPILER_COMPLIANCE_PREFERRED);
javaProject.setOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM,
AndroidConstants.COMPILER_COMPLIANCE_PREFERRED);
// clean the project to make sure we recompile
try {
javaProject.getProject().build(IncrementalProjectBuilder.CLEAN_BUILD,
new NullProgressMonitor());
} catch (CoreException e) {
AdtPlugin.printErrorToConsole(javaProject.getProject(),
"Project compiler settings changed. Clean your project.");
}
}
|
public static final int | checkCompilerCompliance(org.eclipse.jdt.core.IJavaProject javaProject)Checks the project compiler compliance level is supported.
// get the project compliance level option
String compliance = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
// check it against a list of valid compliance level strings.
if (checkCompliance(compliance) == false) {
// if we didn't find the proper compliance level, we return an error
return COMPILER_COMPLIANCE_LEVEL;
}
// otherwise we check source compatibility
String source = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
// check it against a list of valid compliance level strings.
if (checkCompliance(source) == false) {
// if we didn't find the proper compliance level, we return an error
return COMPILER_COMPLIANCE_SOURCE;
}
// otherwise check codegen level
String codeGen = javaProject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true);
// check it against a list of valid compliance level strings.
if (checkCompliance(codeGen) == false) {
// if we didn't find the proper compliance level, we return an error
return COMPILER_COMPLIANCE_CODEGEN_TARGET;
}
return COMPILER_COMPLIANCE_OK;
|
public static final int | checkCompilerCompliance(org.eclipse.core.resources.IProject project)Checks the project compiler compliance level is supported.
// get the java project from the IProject resource object
IJavaProject javaProject = JavaCore.create(project);
// check and return the result.
return checkCompilerCompliance(javaProject);
|
private static boolean | checkCompliance(java.lang.String optionValue)Checks a Java project compiler level option against a list of supported versions.
for (String s : AndroidConstants.COMPILER_COMPLIANCE) {
if (s != null && s.equals(optionValue)) {
return true;
}
}
return false;
|
public static org.eclipse.core.resources.IProject | findAndroidProjectByAppName(java.lang.String applicationName)Returns a {@link IProject} by its running application name, as it returned by the AVD.
applicationName will in most case be the package declared in the manifest, but
can, in some cases, be a custom process name declared in the manifest, in the
application , activity , receiver , or
service nodes.
// Get the list of project for the current workspace
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IProject[] projects = workspace.getRoot().getProjects();
// look for a project that matches the packageName of the app
// we're trying to debug
for (IProject p : projects) {
if (p.isOpen()) {
try {
if (p.hasNature(AndroidConstants.NATURE) == false) {
// ignore non android projects
continue;
}
} catch (CoreException e) {
// failed to get the nature? skip project.
continue;
}
// check that there is indeed a manifest file.
IFile manifestFile = AndroidManifestParser.getManifest(p);
if (manifestFile == null) {
// no file? skip this project.
continue;
}
AndroidManifestParser parser = null;
try {
parser = AndroidManifestParser.parseForData(manifestFile);
} catch (CoreException e) {
// ignore, handled below.
}
if (parser == null) {
// skip this project.
continue;
}
String manifestPackage = parser.getPackage();
if (manifestPackage != null && manifestPackage.equals(applicationName)) {
// this is the project we were looking for!
return p;
} else {
// if the package and application name don't match,
// we look for other possible process names declared in the manifest.
String[] processes = parser.getProcesses();
for (String process : processes) {
if (process.equals(applicationName)) {
return p;
}
}
}
}
}
return null;
|
public static int | findClasspathEntryByName(org.eclipse.jdt.core.IClasspathEntry[] entries, java.lang.String entryName, int entryKind, int startIndex)Look for a specific classpath entry for file name only and return its
index.
if (startIndex < 0) {
startIndex = 0;
}
for (int i = startIndex ; i < entries.length ; i++) {
IClasspathEntry entry = entries[i];
int kind = entry.getEntryKind();
if (kind == entryKind || entryKind == 0) {
// get the path
IPath path = entry.getPath();
String name = path.segment(path.segmentCount()-1);
if (name.equals(entryName)) {
return i;
}
}
}
// not found, return bad index.
return -1;
|
public static int | findClasspathEntryByPath(org.eclipse.jdt.core.IClasspathEntry[] entries, java.lang.String entryPath, int entryKind)Look for a specific classpath entry by full path and return its index.
for (int i = 0 ; i < entries.length ; i++) {
IClasspathEntry entry = entries[i];
int kind = entry.getEntryKind();
if (kind == entryKind || entryKind == 0) {
// get the path
IPath path = entry.getPath();
String osPathString = path.toOSString();
if (osPathString.equals(entryPath)) {
return i;
}
}
}
// not found, return bad index.
return -1;
|
public static void | fixProject(org.eclipse.core.resources.IProject project)Fix the project. This checks the SDK location.
if (AdtPlugin.getOsSdkFolder().length() == 0) {
AdtPlugin.printToConsole(project, "Unknown SDK Location, project not fixed.");
return;
}
// get a java project
IJavaProject javaProject = JavaCore.create(project);
fixProjectClasspathEntries(javaProject);
|
public static void | fixProjectClasspathEntries(org.eclipse.jdt.core.IJavaProject javaProject)Fix the project classpath entries. The method ensures that:
- The project does not reference any old android.zip/android.jar archive.
- The project does not use its output folder as a sourc folder.
- The project does not reference a desktop JRE
- The project references the AndroidClasspathContainer.
// 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);
|
public static void | fixProjectNatureOrder(org.eclipse.core.resources.IProject project)
IProjectDescription description = project.getDescription();
String[] natures = description.getNatureIds();
// if the android nature is not the first one, we reorder them
if (AndroidConstants.NATURE.equals(natures[0]) == false) {
// look for the index
for (int i = 0 ; i < natures.length ; i++) {
if (AndroidConstants.NATURE.equals(natures[i])) {
// if we try to just reorder the array in one pass, this doesn't do
// anything. I guess JDT check that we are actually adding/removing nature.
// So, first we'll remove the android nature, and then add it back.
// remove the android nature
removeNature(project, AndroidConstants.NATURE);
// now add it back at the first index.
description = project.getDescription();
natures = description.getNatureIds();
String[] newNatures = new String[natures.length + 1];
// first one is android
newNatures[0] = AndroidConstants.NATURE;
// next the rest that was before the android nature
System.arraycopy(natures, 0, newNatures, 1, natures.length);
// set the new natures
description.setNatureIds(newNatures);
project.setDescription(description, null);
// and stop
break;
}
}
}
|
public static java.util.List | getAndroidProjectDependencies(org.eclipse.jdt.core.IJavaProject javaProject)Find the list of projects on which this JavaProject is dependent on at the compilation level.
String[] requiredProjectNames = javaProject.getRequiredProjectNames();
// Go from java project name to JavaProject name
IJavaModel javaModel = javaProject.getJavaModel();
// loop through all dependent projects and keep only those that are Android projects
List<IJavaProject> projectList = new ArrayList<IJavaProject>(requiredProjectNames.length);
for (String javaProjectName : requiredProjectNames) {
IJavaProject androidJavaProject = javaModel.getJavaProject(javaProjectName);
//Verify that the project has also the Android Nature
try {
if (!androidJavaProject.getProject().hasNature(AndroidConstants.NATURE)) {
continue;
}
} catch (CoreException e) {
continue;
}
projectList.add(androidJavaProject);
}
return projectList;
|
public static java.lang.String | getApkFilename(org.eclipse.core.resources.IProject project, java.lang.String config)Returns the apk filename for the given project
if (config != null) {
return project.getName() + "-" + config + AndroidConstants.DOT_ANDROID_PACKAGE; //$NON-NLS-1$
}
return project.getName() + AndroidConstants.DOT_ANDROID_PACKAGE;
|
public static org.eclipse.core.resources.IFile | getApplicationPackage(org.eclipse.core.resources.IProject project)Returns the android package file as an IFile object for the specified
project.
// get the output folder
IFolder outputLocation = BaseProjectHelper.getOutputFolder(project);
if (outputLocation == null) {
AdtPlugin.printErrorToConsole(project,
"Failed to get the output location of the project. Check build path properties"
);
return null;
}
// get the package path
String packageName = project.getName() + AndroidConstants.DOT_ANDROID_PACKAGE;
IResource r = outputLocation.findMember(packageName);
// check the package is present
if (r instanceof IFile && r.exists()) {
return (IFile)r;
}
String msg = String.format("Could not find %1$s!", packageName);
AdtPlugin.printErrorToConsole(project, msg);
return null;
|
public static java.lang.String | getJavaDocPath(java.lang.String javaDocOSLocation)Converts a OS specific path into a path valid for the java doc location
attributes of a project.
// first thing we do is convert the \ into /
String javaDoc = javaDocOSLocation.replaceAll("\\\\", //$NON-NLS-1$
AndroidConstants.WS_SEP);
// then we add file: at the beginning for unix path, and file:/ for non
// unix path
if (javaDoc.startsWith(AndroidConstants.WS_SEP)) {
return "file:" + javaDoc; //$NON-NLS-1$
}
return "file:/" + javaDoc; //$NON-NLS-1$
|
public static org.eclipse.core.resources.IProject[] | getReferencedProjects(org.eclipse.core.resources.IProject project)Returns the list of referenced project that are opened and Java projects.
IProject[] projects = project.getReferencedProjects();
ArrayList<IProject> list = new ArrayList<IProject>();
for (IProject p : projects) {
if (p.isOpen() && p.hasNature(JavaCore.NATURE_ID)) {
list.add(p);
}
}
return list.toArray(new IProject[list.size()]);
|
public static boolean | hasError(org.eclipse.core.resources.IProject project, boolean includeReferencedProjects)Returns if the project has error level markers.
IMarker[] markers = project.findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE);
if (markers != null && markers.length > 0) {
// the project has marker(s). even though they are "problem" we
// don't know their severity. so we loop on them and figure if they
// are warnings or errors
for (IMarker m : markers) {
int s = m.getAttribute(IMarker.SEVERITY, -1);
if (s == IMarker.SEVERITY_ERROR) {
return true;
}
}
}
// test the referenced projects if needed.
if (includeReferencedProjects) {
IProject[] projects = getReferencedProjects(project);
for (IProject p : projects) {
if (hasError(p, false)) {
return true;
}
}
}
return false;
|
public static boolean | loadBooleanProperty(org.eclipse.core.resources.IResource resource, java.lang.String propertyName, boolean defaultValue)Loads a boolean property from the persistent storage of the project.
String value = loadStringProperty(resource, propertyName);
if (value != null) {
return Boolean.parseBoolean(value);
}
return defaultValue;
|
public static org.eclipse.core.resources.IResource | loadResourceProperty(org.eclipse.core.resources.IResource resource, java.lang.String propertyName)Loads the path of a resource from the persistent storage of the project, and returns the
corresponding IResource object, if it exists in the same project as resource .
String value = loadStringProperty(resource, propertyName);
if (value != null && value.length() > 0) {
return resource.getProject().findMember(value);
}
return null;
|
public static java.lang.String | loadStringProperty(org.eclipse.core.resources.IResource resource, java.lang.String propertyName)Loads a String property from the persistent storage of a resource.
QualifiedName qname = new QualifiedName(AdtPlugin.PLUGIN_ID, propertyName);
try {
String value = resource.getPersistentProperty(qname);
return value;
} catch (CoreException e) {
return null;
}
|
public static org.eclipse.jdt.core.IClasspathEntry[] | removeEntryFromClasspath(org.eclipse.jdt.core.IClasspathEntry[] entries, int index)Remove a classpath entry from the array.
int n = entries.length;
IClasspathEntry[] newEntries = new IClasspathEntry[n-1];
// copy the entries before index
System.arraycopy(entries, 0, newEntries, 0, index);
// copy the entries after index
System.arraycopy(entries, index + 1, newEntries, index,
entries.length - index - 1);
return newEntries;
|
public static void | removeNature(org.eclipse.core.resources.IProject project, java.lang.String nature)Removes a specific nature from a project.
IProjectDescription description = project.getDescription();
String[] natures = description.getNatureIds();
// check if the project already has the android nature.
for (int i = 0; i < natures.length; ++i) {
if (nature.equals(natures[i])) {
String[] newNatures = new String[natures.length - 1];
if (i > 0) {
System.arraycopy(natures, 0, newNatures, 0, i);
}
System.arraycopy(natures, i + 1, newNatures, i, natures.length - i - 1);
description.setNatureIds(newNatures);
project.setDescription(description, null);
return;
}
}
|
public static boolean | saveBooleanProperty(org.eclipse.core.resources.IResource resource, java.lang.String propertyName, boolean value)Saves a property into the persistent storage of a resource.
return saveStringProperty(resource, propertyName, Boolean.toString(value));
|
public static boolean | saveResourceProperty(org.eclipse.core.resources.IResource resource, java.lang.String propertyName, org.eclipse.core.resources.IResource value)Saves the path of a resource into the persistent storate of the project.
if (value != null) {
IPath iPath = value.getProjectRelativePath();
return saveStringProperty(resource, propertyName, iPath.toString());
}
return saveStringProperty(resource, propertyName, ""); //$NON-NLS-1$
|
public static boolean | saveStringProperty(org.eclipse.core.resources.IResource resource, java.lang.String propertyName, java.lang.String value)Saves a String property into the persistent storage of a resource.
QualifiedName qname = new QualifiedName(AdtPlugin.PLUGIN_ID, propertyName);
try {
resource.setPersistentProperty(qname, value);
} catch (CoreException e) {
return false;
}
return true;
|