diff options
9 files changed, 149 insertions, 11 deletions
diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/AdtUtils.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/AdtUtils.java index b090a1a..e3fc5fe 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/AdtUtils.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/AdtUtils.java @@ -16,6 +16,7 @@ package com.android.ide.eclipse.adt; +import com.android.annotations.NonNull; import com.android.ide.eclipse.adt.internal.project.BaseProjectHelper; import com.android.ide.eclipse.adt.internal.project.BaseProjectHelper.IProjectFilter; @@ -42,6 +43,8 @@ import org.eclipse.ui.PlatformUI; import org.eclipse.ui.texteditor.ITextEditor; import java.io.File; +import java.net.URISyntaxException; +import java.net.URL; import java.util.ArrayList; import java.util.List; @@ -242,6 +245,25 @@ public class AdtUtils { } /** + * Attempts to convert the given {@link URL} into a {@link File}. + * + * @param url the {@link URL} to be converted + * @return the corresponding {@link File}, which may not exist + */ + @NonNull + public static File getFile(@NonNull URL url) { + try { + // First try URL.toURI(): this will work for URLs that contain %20 for spaces etc. + // Unfortunately, it *doesn't* work for "broken" URLs where the URL contains + // spaces, which is often the case. + return new File(url.toURI()); + } catch (URISyntaxException e) { + // ...so as a fallback, go to the old url.getPath() method, which handles space paths. + return new File(url.getPath()); + } + } + + /** * Returns the file for the current editor, if any. * * @return the file for the current editor, or null if none diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/EclipseLintClient.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/EclipseLintClient.java index 8fe6764..59e29f4 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/EclipseLintClient.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/EclipseLintClient.java @@ -18,10 +18,14 @@ package com.android.ide.eclipse.adt.internal.lint; import static com.android.ide.eclipse.adt.AdtConstants.DOT_XML; import static com.android.ide.eclipse.adt.AdtConstants.MARKER_LINT; +import com.android.annotations.NonNull; +import com.android.annotations.Nullable; import com.android.ide.eclipse.adt.AdtPlugin; import com.android.ide.eclipse.adt.AdtUtils; import com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditorDelegate; +import com.android.ide.eclipse.adt.internal.preferences.AdtPrefs; import com.android.ide.eclipse.adt.internal.project.BaseProjectHelper; +import com.android.sdklib.SdkConstants; import com.android.tools.lint.checks.BuiltinIssueRegistry; import com.android.tools.lint.client.api.Configuration; import com.android.tools.lint.client.api.IDomParser; @@ -81,6 +85,8 @@ import org.w3c.dom.Node; import java.io.File; import java.io.IOException; +import java.net.URL; +import java.security.CodeSource; import java.util.Collections; import java.util.List; @@ -263,6 +269,34 @@ public class EclipseLintClient extends LintClient implements IDomParser { } } + @Override + @Nullable + public File findResource(@NonNull String relativePath) { + // First look within the Eclipse install directory; then look within the $ANDROID_SDK + CodeSource source = getClass().getProtectionDomain().getCodeSource(); + if (source != null) { + URL location = source.getLocation(); + File eclipseDir = AdtUtils.getFile(location); + if (eclipseDir.exists()) { + File file = new File(eclipseDir, "libs" + File.separator + relativePath); //$NON-NLS-1$ + if (file.exists()) { + return file; + } + } + } + + String sdkFolder = AdtPrefs.getPrefs().getOsSdkFolder(); + if (sdkFolder != null) { + File root = new File(sdkFolder, SdkConstants.FD_TOOLS); + File file = new File(root, "lib" + File.separator + relativePath); //$NON-NLS-1$ + if (file.exists()) { + return file; + } + } + + return null; + } + /** Clears any lint markers from the given resource (project, folder or file) */ static void clearMarkers(IResource resource) { clearMarkers(Collections.singletonList(resource)); diff --git a/lint/cli/src/com/android/tools/lint/LombokParser.java b/lint/cli/src/com/android/tools/lint/LombokParser.java index 9002e15..6d77462 100644 --- a/lint/cli/src/com/android/tools/lint/LombokParser.java +++ b/lint/cli/src/com/android/tools/lint/LombokParser.java @@ -1,11 +1,11 @@ /* * Copyright (C) 2011 The Android Open Source Project * - * Licensed under the Eclipse Public License, Version 1.0 (the "License"); + * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.eclipse.org/org/documents/epl-v10.php + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.android.tools.lint; import com.android.tools.lint.client.api.IJavaParser; diff --git a/lint/libs/lint_api/src/com/android/tools/lint/client/api/Lint.java b/lint/libs/lint_api/src/com/android/tools/lint/client/api/Lint.java index c6f9913..6b1d7db 100644 --- a/lint/libs/lint_api/src/com/android/tools/lint/client/api/Lint.java +++ b/lint/libs/lint_api/src/com/android/tools/lint/client/api/Lint.java @@ -579,8 +579,8 @@ public class Lint { private void runFileDetectors(@NonNull Project project, @Nullable Project main) { // Look up manifest information (but not for library projects) - File manifestFile = new File(project.getDir(), ANDROID_MANIFEST_XML); - if (!project.isLibrary() && manifestFile.exists()) { + File manifestFile = project.getManifestFile(); + if (!project.isLibrary() && manifestFile != null) { XmlContext context = new XmlContext(this, project, main, manifestFile); IDomParser parser = mClient.getDomParser(); context.document = parser.parseXml(context); @@ -1072,6 +1072,11 @@ public class Lint { public IJavaParser getJavaParser() { return mDelegate.getJavaParser(); } + + @Override + public File findResource(String relativePath) { + return mDelegate.findResource(relativePath); + } } /** diff --git a/lint/libs/lint_api/src/com/android/tools/lint/client/api/LintClient.java b/lint/libs/lint_api/src/com/android/tools/lint/client/api/LintClient.java index 91f6fe5..a3f9917 100644 --- a/lint/libs/lint_api/src/com/android/tools/lint/client/api/LintClient.java +++ b/lint/libs/lint_api/src/com/android/tools/lint/client/api/LintClient.java @@ -32,6 +32,7 @@ import org.xml.sax.InputSource; import java.io.File; import java.io.StringReader; +import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -178,6 +179,33 @@ public abstract class LintClient { } /** + * Locates a resource within the lint installation, usually located under + * {@code lib/}. + * <p> + * TODO: Consider switching to a {@link URL} return type instead. + * + * @param relativePath A relative path (using {@link File#separator} to + * separate path components) to the given resource + * @return a {@link File} pointing to the resource, or null if it does not + * exist + */ + @Nullable + public File findResource(@NonNull String relativePath) { + String path = System.getProperty("com.android.tools.lint.bindir"); //$NON-NLS-1$ + if (path == null) { + throw new IllegalArgumentException("Lint must be invoked with the System property " + + "com.android.tools.lint.bindir pointing to the ANDROID_SDK tools direectory"); + } + + File file = new File(path, "lib" + File.separator + relativePath); //$NON-NLS-1$ + if (file.exists()) { + return file; + } else { + return null; + } + } + + /** * Considers the given directory as an Eclipse project and returns either * its source or its output folders depending on the {@code attribute} parameter. */ diff --git a/lint/libs/lint_api/src/com/android/tools/lint/detector/api/Project.java b/lint/libs/lint_api/src/com/android/tools/lint/detector/api/Project.java index b0d7186..9960d55 100644 --- a/lint/libs/lint_api/src/com/android/tools/lint/detector/api/Project.java +++ b/lint/libs/lint_api/src/com/android/tools/lint/detector/api/Project.java @@ -18,6 +18,7 @@ package com.android.tools.lint.detector.api; import static com.android.tools.lint.detector.api.LintConstants.ANDROID_LIBRARY; import static com.android.tools.lint.detector.api.LintConstants.ANDROID_LIBRARY_REFERENCE_FORMAT; +import static com.android.tools.lint.detector.api.LintConstants.ANDROID_MANIFEST_XML; import static com.android.tools.lint.detector.api.LintConstants.ANDROID_URI; import static com.android.tools.lint.detector.api.LintConstants.ATTR_MIN_SDK_VERSION; import static com.android.tools.lint.detector.api.LintConstants.ATTR_PACKAGE; @@ -477,4 +478,18 @@ public class Project { return mSdkInfo; } + + /** + * Gets the path to the manifest file in this project, if it exists + * + * @return the path to the manifest file, or null if it does not exist + */ + public File getManifestFile() { + File manifestFile = new File(mDir, ANDROID_MANIFEST_XML); + if (manifestFile.exists()) { + return manifestFile; + } + + return null; + } } diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/AbstractCheckTest.java b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/AbstractCheckTest.java index 2678d9f..c42e050 100644 --- a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/AbstractCheckTest.java +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/AbstractCheckTest.java @@ -17,8 +17,8 @@ package com.android.tools.lint.checks; import com.android.tools.lint.LintCliXmlParser; -import com.android.tools.lint.Main; import com.android.tools.lint.LombokParser; +import com.android.tools.lint.Main; import com.android.tools.lint.client.api.Configuration; import com.android.tools.lint.client.api.IDomParser; import com.android.tools.lint.client.api.IJavaParser; @@ -41,6 +41,9 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.Writer; +import java.net.URISyntaxException; +import java.net.URL; +import java.security.CodeSource; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; @@ -49,7 +52,8 @@ import java.util.List; import junit.framework.TestCase; /** Common utility methods for the various lint check tests */ -abstract class AbstractCheckTest extends TestCase { +@SuppressWarnings("javadoc") +public abstract class AbstractCheckTest extends TestCase { protected abstract Detector getDetector(); protected List<Issue> getIssues() { @@ -254,7 +258,7 @@ abstract class AbstractCheckTest extends TestCase { return false; } - private class TestLintClient extends Main { + public class TestLintClient extends Main { private List<String> mErrors = new ArrayList<String>(); public List<String> getErrors() { @@ -357,6 +361,34 @@ abstract class AbstractCheckTest extends TestCase { public Configuration getConfiguration(Project project) { return new TestConfiguration(); } + + @Override + public File findResource(String relativePath) { + // First look within the Eclipse install directory; then look within the $ANDROID_SDK + CodeSource source = getClass().getProtectionDomain().getCodeSource(); + if (source != null) { + URL location = source.getLocation(); + try { + File dir = new File(location.toURI()); + assertTrue(dir.getPath(), dir.exists()); + File sdkDir = dir.getParentFile().getParentFile().getParentFile() + .getParentFile().getParentFile(); + assertEquals("sdk", sdkDir.getName()); + File lib = new File(sdkDir, "eclipse" + File.separator + "plugins" + + File.separator + "com.android.ide.eclipse.adt" + File.separator + + "libs"); + assertTrue(lib.getPath(), lib.exists()); + File file = new File(lib, relativePath); + assertTrue(file.getPath(), file.exists()); + return file; + } catch (URISyntaxException e) { + fail(e.getLocalizedMessage()); + } + } + + return super.findResource(relativePath); + } + } public class TestConfiguration extends Configuration { diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/client/api/DefaultSdkInfoTest.java b/lint/libs/lint_checks/tests/src/com/android/tools/lint/client/api/DefaultSdkInfoTest.java index 5b44c79..aae8eae 100644 --- a/lint/libs/lint_checks/tests/src/com/android/tools/lint/client/api/DefaultSdkInfoTest.java +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/client/api/DefaultSdkInfoTest.java @@ -1,11 +1,11 @@ /* * Copyright (C) 2011 The Android Open Source Project * - * Licensed under the Eclipse Public License, Version 1.0 (the "License"); + * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.eclipse.org/org/documents/epl-v10.php + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/detector/api/LintUtilsTest.java b/lint/libs/lint_checks/tests/src/com/android/tools/lint/detector/api/LintUtilsTest.java index 6f585b0..fafd191 100644 --- a/lint/libs/lint_checks/tests/src/com/android/tools/lint/detector/api/LintUtilsTest.java +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/detector/api/LintUtilsTest.java @@ -1,11 +1,11 @@ /* * Copyright (C) 2011 The Android Open Source Project * - * Licensed under the Eclipse Public License, Version 1.0 (the "License"); + * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.eclipse.org/org/documents/epl-v10.php + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.android.tools.lint.detector.api; import java.io.File; |