diff options
author | Tor Norbye <tnorbye@google.com> | 2012-01-26 15:51:26 -0800 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2012-01-26 15:51:26 -0800 |
commit | f90d22afc20e4b868cc16b6117a93283dc92fd7f (patch) | |
tree | a1687ccdaae91ec658cd4a7022361df10ff14069 /lint | |
parent | 5fcbc2ac776a058c460dc9a24f0ed14ae8a7793b (diff) | |
parent | 5dc20a5c893caa37deff6b2f9f928e571027a137 (diff) | |
download | sdk-f90d22afc20e4b868cc16b6117a93283dc92fd7f.zip sdk-f90d22afc20e4b868cc16b6117a93283dc92fd7f.tar.gz sdk-f90d22afc20e4b868cc16b6117a93283dc92fd7f.tar.bz2 |
Merge "Add file resource lookup to the Lint Client API"
Diffstat (limited to 'lint')
7 files changed, 93 insertions, 11 deletions
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; |