Home | History | Annotate | Download | only in lint
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.eclipse.org/org/documents/epl-v10.php
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.ide.eclipse.adt.internal.lint;
     17 
     18 import com.android.annotations.NonNull;
     19 import com.android.annotations.VisibleForTesting;
     20 import com.android.ide.eclipse.adt.AdtPlugin;
     21 import com.android.ide.eclipse.adt.AdtUtils;
     22 import com.android.tools.lint.client.api.Configuration;
     23 import com.android.tools.lint.client.api.DefaultConfiguration;
     24 import com.android.tools.lint.client.api.LintClient;
     25 import com.android.tools.lint.detector.api.Issue;
     26 import com.android.tools.lint.detector.api.Project;
     27 import com.android.tools.lint.detector.api.Severity;
     28 
     29 import org.eclipse.core.resources.IProject;
     30 import org.eclipse.core.runtime.CoreException;
     31 import org.eclipse.core.runtime.QualifiedName;
     32 
     33 import java.io.File;
     34 
     35 /** Configuration for Lint in Eclipse projects */
     36 class ProjectLintConfiguration extends DefaultConfiguration {
     37     private boolean mFatalOnly;
     38 
     39     private final static QualifiedName CONFIGURATION_NAME = new QualifiedName(AdtPlugin.PLUGIN_ID,
     40             "lintconfig"); //$NON-NLS-1$
     41 
     42     @VisibleForTesting
     43     ProjectLintConfiguration(LintClient client, Project project,
     44             Configuration parent, boolean fatalOnly) {
     45         super(client, project, parent);
     46         mFatalOnly = fatalOnly;
     47     }
     48 
     49     private static ProjectLintConfiguration create(LintClient client, IProject project,
     50             Configuration parent, boolean fatalOnly) {
     51         File dir = AdtUtils.getAbsolutePath(project).toFile();
     52         Project lintProject = client.getProject(dir, dir);
     53         return new ProjectLintConfiguration(client, lintProject, parent, fatalOnly);
     54     }
     55 
     56     public static ProjectLintConfiguration get(LintClient client, IProject project,
     57             boolean fatalOnly) {
     58         // Don't cache fatal-only configurations: they're only used occasionally and typically
     59         // not repeatedly
     60         if (fatalOnly) {
     61             return create(client, project, GlobalLintConfiguration.get(), true);
     62         }
     63 
     64         ProjectLintConfiguration configuration = null;
     65         try {
     66             Object value = project.getSessionProperty(CONFIGURATION_NAME);
     67             configuration = (ProjectLintConfiguration) value;
     68         } catch (CoreException e) {
     69             // Not a problem; we will just create a new one
     70         }
     71         if (configuration == null) {
     72             configuration = create(client, project, GlobalLintConfiguration.get(), false);
     73             try {
     74                 project.setSessionProperty(CONFIGURATION_NAME, configuration);
     75             } catch (CoreException e) {
     76                 AdtPlugin.log(e, "Can't store lint configuration");
     77             }
     78         }
     79         return configuration;
     80     }
     81 
     82     @Override
     83     public @NonNull Severity getSeverity(@NonNull Issue issue) {
     84         Severity severity = super.getSeverity(issue);
     85         if (mFatalOnly && severity != Severity.FATAL) {
     86             return Severity.IGNORE;
     87         }
     88         return severity;
     89     }
     90 }