diff options
Diffstat (limited to 'eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse')
7 files changed, 223 insertions, 36 deletions
diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/build/builders/PreCompilerBuilder.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/build/builders/PreCompilerBuilder.java index c4e9caeac..b471c3faa 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/build/builders/PreCompilerBuilder.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/build/builders/PreCompilerBuilder.java @@ -24,6 +24,7 @@ import com.android.ide.eclipse.adt.internal.build.Messages; import com.android.ide.eclipse.adt.internal.build.RenderScriptProcessor; import com.android.ide.eclipse.adt.internal.build.SourceProcessor; import com.android.ide.eclipse.adt.internal.lint.EclipseLintClient; +import com.android.ide.eclipse.adt.internal.lint.LintDeltaProcessor; import com.android.ide.eclipse.adt.internal.preferences.AdtPrefs; import com.android.ide.eclipse.adt.internal.preferences.AdtPrefs.BuildVerbosity; import com.android.ide.eclipse.adt.internal.project.AndroidManifestHelper; @@ -294,6 +295,11 @@ public class PreCompilerBuilder extends BaseBuilder { dv = new PreCompilerDeltaVisitor(this, sourceFolderPathList, mProcessors); delta.accept(dv); + // Check for errors on save/build, if enabled + if (AdtPrefs.getPrefs().isLintOnSave()) { + LintDeltaProcessor.create().process(delta); + } + // Check to see if Manifest.xml, Manifest.java, or R.java have changed: mMustCompileResources |= dv.getCompileResources(); diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/AndroidXmlEditor.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/AndroidXmlEditor.java index 3c976b0c0..803478b39 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/AndroidXmlEditor.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/AndroidXmlEditor.java @@ -588,13 +588,15 @@ public abstract class AndroidXmlEditor extends FormEditor implements IResourceCh * Utility method that creates a Job to run Lint on the current document. * Does not wait for the job to finish - just returns immediately. * - * @see EclipseLintRunner#startLint(java.util.List, IDocument, boolean, boolean) + * @return a new job, or null + * @see EclipseLintRunner#startLint(java.util.List, IResource, IDocument, + * boolean, boolean) */ @Nullable public Job startLintJob() { IFile file = getInputFile(); if (file != null) { - return EclipseLintRunner.startLint(Collections.singletonList(file), + return EclipseLintRunner.startLint(Collections.singletonList(file), file, getStructuredDocument(), false /*fatalOnly*/, false /*show*/); } diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/EclipseLintRunner.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/EclipseLintRunner.java index 36a388a82..25e80fca0 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/EclipseLintRunner.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/EclipseLintRunner.java @@ -15,8 +15,12 @@ */ package com.android.ide.eclipse.adt.internal.lint; +import static com.android.ide.eclipse.adt.AdtConstants.DOT_CLASS; +import static com.android.ide.eclipse.adt.AdtConstants.DOT_JAVA; import static com.android.ide.eclipse.adt.AdtConstants.DOT_XML; +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.preferences.AdtPrefs; @@ -60,14 +64,19 @@ public class EclipseLintRunner { * true if fatal errors were found. * * @param resources the resources (project, folder or file) to be analyzed + * @param source if checking a single source file, the source file * @param doc the associated document, if known, or null * @param fatalOnly if true, only report fatal issues (severity=error) * @return true if any fatal errors were encountered. */ - private static boolean runLint(List<? extends IResource> resources, IDocument doc, + private static boolean runLint( + @NonNull List<? extends IResource> resources, + @Nullable IResource source, + @Nullable IDocument doc, boolean fatalOnly) { resources = addLibraries(resources); - CheckFileJob job = (CheckFileJob) startLint(resources, doc, fatalOnly, false /*show*/); + CheckFileJob job = (CheckFileJob) startLint(resources, source, doc, fatalOnly, + false /*show*/); try { job.join(); boolean fatal = job.isFatal(); @@ -85,23 +94,34 @@ public class EclipseLintRunner { } /** - * Runs lint and updates the markers. Does not wait for the job to - * finish - just returns immediately. + * Runs lint and updates the markers. Does not wait for the job to finish - + * just returns immediately. * * @param resources the resources (project, folder or file) to be analyzed + * @param source if checking a single source file, the source file. When + * single checking an XML file, this is typically the same as the + * file passed in the list in the first parameter, but when + * checking the .class files of a Java file for example, the + * .class file and all the inner classes of the Java file are + * passed in the first parameter, and the corresponding .java + * source file is passed here. * @param doc the associated document, if known, or null * @param fatalOnly if true, only report fatal issues (severity=error) * @param show if true, show the results in a {@link LintViewPart} * @return the job running lint in the background. */ - public static Job startLint(List<? extends IResource> resources, - IDocument doc, boolean fatalOnly, boolean show) { + public static Job startLint( + @NonNull List<? extends IResource> resources, + @Nullable IResource source, + @Nullable IDocument doc, + boolean fatalOnly, + boolean show) { if (resources != null && !resources.isEmpty()) { resources = addLibraries(resources); cancelCurrentJobs(false); - CheckFileJob job = new CheckFileJob(resources, doc, fatalOnly); + CheckFileJob job = new CheckFileJob(resources, source, doc, fatalOnly); job.schedule(); if (show) { @@ -125,8 +145,8 @@ public class EclipseLintRunner { */ public static boolean runLintOnExport(Shell shell, IProject project) { if (AdtPrefs.getPrefs().isLintOnExport()) { - boolean fatal = EclipseLintRunner.runLint(Collections.singletonList(project), null, - true /*fatalOnly*/); + boolean fatal = EclipseLintRunner.runLint(Collections.singletonList(project), + null, null, true /*fatalOnly*/); if (fatal) { MessageDialog.openWarning(shell, "Export Aborted", @@ -214,15 +234,20 @@ public class EclipseLintRunner { /** Job family */ private static final Object FAMILY_RUN_LINT = new Object(); private final List<? extends IResource> mResources; + private final IResource mSource; private final IDocument mDocument; private LintDriver mLint; private boolean mFatal; private boolean mFatalOnly; - private CheckFileJob(List<? extends IResource> resources, IDocument doc, + private CheckFileJob( + @NonNull List<? extends IResource> resources, + @Nullable IResource source, + @Nullable IDocument doc, boolean fatalOnly) { super("Running Android Lint"); mResources = resources; + mSource = source; mDocument = doc; mFatalOnly = fatalOnly; } @@ -245,36 +270,64 @@ public class EclipseLintRunner { try { monitor.beginTask("Looking for errors", IProgressMonitor.UNKNOWN); IssueRegistry registry = EclipseLintClient.getRegistry(); - EnumSet<Scope> scope = Scope.ALL; + EnumSet<Scope> scope = null; List<File> files = new ArrayList<File>(mResources.size()); for (IResource resource : mResources) { File file = AdtUtils.getAbsolutePath(resource).toFile(); files.add(file); - if (resource instanceof IProject) { + if (resource instanceof IProject && mSource == null) { scope = Scope.ALL; - } else if (resource instanceof IFile - && AdtUtils.endsWithIgnoreCase(resource.getName(), DOT_XML)) { - if (resource.getName().equals(SdkConstants.FN_ANDROID_MANIFEST_XML)) { - scope = EnumSet.of(Scope.MANIFEST); + } else { + String name = resource.getName(); + if (AdtUtils.endsWithIgnoreCase(name, DOT_XML)) { + if (name.equals(SdkConstants.FN_ANDROID_MANIFEST_XML)) { + scope = EnumSet.of(Scope.MANIFEST); + } else { + scope = Scope.RESOURCE_FILE_SCOPE; + } + } else if (name.endsWith(DOT_JAVA) && resource instanceof IFile) { + if (scope != null) { + if (!scope.contains(Scope.JAVA_FILE)) { + scope = EnumSet.copyOf(scope); + scope.add(Scope.JAVA_FILE); + } + } else { + scope = Scope.JAVA_FILE_SCOPE; + } + } else if (name.endsWith(DOT_CLASS) && resource instanceof IFile) { + if (scope != null) { + if (!scope.contains(Scope.CLASS_FILE)) { + scope = EnumSet.copyOf(scope); + scope.add(Scope.CLASS_FILE); + } + } else { + scope = Scope.CLASS_FILE_SCOPE; + } } else { - scope = Scope.RESOURCE_FILE_SCOPE; + return new Status(Status.ERROR, AdtPlugin.PLUGIN_ID, Status.ERROR, + "Only XML files are supported for single file lint", null); //$NON-NLS-1$ } - } else { - return new Status(Status.ERROR, AdtPlugin.PLUGIN_ID, Status.ERROR, - "Only XML files are supported for single file lint", null); //$NON-NLS-1$ } } - if (Scope.checkSingleFile(scope)) { + if (scope == null) { + scope = Scope.ALL; + } + if (mSource == null) { + assert !Scope.checkSingleFile(scope) : scope + " with " + mResources; + } + // Check single file? + if (mSource != null) { // Delete specific markers - for (IResource resource : mResources) { - IMarker[] markers = EclipseLintClient.getMarkers(resource); - for (IMarker marker : markers) { - String id = marker.getAttribute(MARKER_CHECKID_PROPERTY, ""); //$NON-NLS-1$ - Issue issue = registry.getIssue(id); - if (issue == null || issue.getScope().equals(scope)) { - marker.delete(); - } + IMarker[] markers = EclipseLintClient.getMarkers(mSource); + for (IMarker marker : markers) { + String id = marker.getAttribute(MARKER_CHECKID_PROPERTY, ""); //$NON-NLS-1$ + Issue issue = registry.getIssue(id); + if (issue == null) { + continue; + } + if (issue.isAdequate(scope)) { + marker.delete(); } } } else { diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/LintDeltaProcessor.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/LintDeltaProcessor.java new file mode 100644 index 000000000..e5fe46eb3 --- /dev/null +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/LintDeltaProcessor.java @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Eclipse Public License, Version 1.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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.ide.eclipse.adt.internal.lint; + +import static com.android.ide.eclipse.adt.AdtConstants.DOT_CLASS; +import static com.android.ide.eclipse.adt.AdtConstants.DOT_JAVA; + +import com.android.annotations.NonNull; +import com.android.ide.eclipse.adt.AdtPlugin; +import com.android.ide.eclipse.adt.AdtUtils; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IResourceDelta; +import org.eclipse.swt.widgets.Display; + +import java.util.ArrayList; +import java.util.List; + +/** + * Delta processor for Java files, which runs single-file lints if it finds that + * the currently active file has been updated. + */ +public class LintDeltaProcessor implements Runnable { + private List<IResource> mFiles; + private IFile mActiveFile; + + private LintDeltaProcessor() { + } + + /** + * Creates a new {@link LintDeltaProcessor} + * + * @return a visitor + */ + @NonNull + public static LintDeltaProcessor create() { + return new LintDeltaProcessor(); + } + + /** + * Process the given delta: update lint on any Java source and class files found. + * + * @param delta the delta describing recently changed files + */ + public void process(@NonNull IResourceDelta delta) { + // Get the active editor file, if any + Display display = AdtPlugin.getDisplay(); + if (display == null) { + return; + } + if (display.getThread() != Thread.currentThread()) { + display.syncExec(this); + } else { + run(); + } + + if (mActiveFile == null || !mActiveFile.getName().endsWith(DOT_JAVA)) { + return; + } + + mFiles = new ArrayList<IResource>(); + gatherFiles(delta); + + if (!mFiles.isEmpty()) { + EclipseLintRunner.startLint(mFiles, mActiveFile, null, + false /*fatalOnly*/, false /*show*/); + } + } + + /** + * Collect .java and .class files to be run in lint. Only collects files + * that match the active editor. + */ + private void gatherFiles(@NonNull IResourceDelta delta) { + IResource resource = delta.getResource(); + String name = resource.getName(); + if (name.endsWith(DOT_JAVA)) { + if (resource.equals(mActiveFile)) { + mFiles.add(resource); + } + } else if (name.endsWith(DOT_CLASS)) { + // Make sure this class corresponds to the .java file, meaning it has + // the same basename, or that it is an inner class of a class that + // matches the same basename. (We could potentially make sure the package + // names match too, but it's unlikely that the class names match without a + // package match, and there's no harm in including some extra classes here, + // since lint will resolve full paths and the resource markers won't go + // to the wrong place, we simply end up analyzing some extra files.) + String className = mActiveFile.getName(); + if (name.regionMatches(0, className, 0, className.length() - DOT_JAVA.length())) { + if (name.length() == className.length() - DOT_JAVA.length() + DOT_CLASS.length() + || name.charAt(className.length() - DOT_JAVA.length()) == '$') { + mFiles.add(resource); + } + } + } else { + IResourceDelta[] children = delta.getAffectedChildren(); + if (children != null && children.length > 0) { + for (IResourceDelta d : children) { + gatherFiles(d); + } + } + } + } + + @Override + public void run() { + // Get the active file: this must be run on the GUI thread + mActiveFile = AdtUtils.getActiveFile(); + } +} diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/LintViewPart.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/LintViewPart.java index e9f94cc8b..39c6d25ad 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/LintViewPart.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/LintViewPart.java @@ -474,7 +474,7 @@ public class LintViewPart extends ViewPart implements SelectionListener, IJobCha if (resources == null) { return; } - Job job = EclipseLintRunner.startLint(resources, null, + Job job = EclipseLintRunner.startLint(resources, null, null, false /*fatalOnly*/, false /*show*/); if (job != null && workbench != null) { job.addJobChangeListener(LintViewPart.this); diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/RunLintAction.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/RunLintAction.java index e77e91028..18f3db366 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/RunLintAction.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/RunLintAction.java @@ -76,7 +76,7 @@ public class RunLintAction implements IObjectActionDelegate, IMenuCreator, List<IProject> projects = getProjects(mSelection, true /* warn */); if (!projects.isEmpty()) { - EclipseLintRunner.startLint(projects, null, false /*fatalOnly*/, true /*show*/); + EclipseLintRunner.startLint(projects, null, null, false /*fatalOnly*/, true /*show*/); } else { MessageDialog.openWarning(AdtPlugin.getDisplay().getActiveShell(), "Lint", "Could not run Lint: Select a project first."); @@ -210,7 +210,7 @@ public class RunLintAction implements IObjectActionDelegate, IMenuCreator, ISharedImages images = PlatformUI.getWorkbench().getSharedImages(); ImageDescriptor clear = images.getImageDescriptor(ISharedImages.IMG_ELCL_REMOVEALL); - LintMenuAction clearAction = new LintMenuAction("Clear Lint Markers", clear, true, null); + LintMenuAction clearAction = new LintMenuAction("Clear Lint Warnings", clear, true, null); addSeparator(); addAction(clearAction); @@ -271,7 +271,8 @@ public class RunLintAction implements IObjectActionDelegate, IMenuCreator, if (mClear) { EclipseLintClient.clearMarkers(resources); } else { - EclipseLintRunner.startLint(resources, null, false /*fatalOnly*/, true /*show*/); + EclipseLintRunner.startLint(resources, null, null, false /*fatalOnly*/, + true /*show*/); } } } diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/preferences/LintPreferencePage.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/preferences/LintPreferencePage.java index bd2423ef7..827f8a468 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/preferences/LintPreferencePage.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/preferences/LintPreferencePage.java @@ -378,7 +378,7 @@ public class LintPreferencePage extends PropertyPage implements IWorkbenchPrefer } } - EclipseLintRunner.startLint(androidProjects, null, false /*fatalOnly*/, + EclipseLintRunner.startLint(androidProjects, null, null, false /*fatalOnly*/, true /*show*/); } } |