Home | History | Annotate | Download | only in api
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
      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 
     17 package com.android.tools.lint.detector.api;
     18 
     19 import com.android.annotations.NonNull;
     20 import com.google.common.annotations.Beta;
     21 
     22 import java.util.EnumSet;
     23 
     24 /**
     25  * The scope of a detector is the set of files a detector must consider when
     26  * performing its analysis. This can be used to determine when issues are
     27  * potentially obsolete, whether a detector should re-run on a file save, etc.
     28  * <p>
     29  * <b>NOTE: This is not a public or final API; if you rely on this be prepared
     30  * to adjust your code for the next tools release.</b>
     31  */
     32 @Beta
     33 public enum Scope {
     34     /**
     35      * The analysis only considers a single XML resource file at a time.
     36      * <p>
     37      * Issues which are only affected by a single resource file can be checked
     38      * for incrementally when a file is edited.
     39      */
     40     RESOURCE_FILE,
     41 
     42     /**
     43      * The analysis considers <b>all</b> the resource file. This scope must not
     44      * be used in conjunction with {@link #RESOURCE_FILE}; an issue scope is
     45      * either considering just a single resource file or all the resources, not
     46      * both.
     47      */
     48     ALL_RESOURCE_FILES,
     49 
     50     /**
     51      * The analysis only considers a single Java source file at a time.
     52      * <p>
     53      * Issues which are only affected by a single Java source file can be
     54      * checked for incrementally when a Java source file is edited.
     55      */
     56     JAVA_FILE,
     57 
     58     /**
     59      * The analysis considers <b>all</b> the Java source files together.
     60      * <p>
     61      * This flag is mutually exclusive with {@link #JAVA_FILE}.
     62      */
     63     ALL_JAVA_FILES,
     64 
     65     /**
     66      * The analysis only considers a single Java class file at a time.
     67      * <p>
     68      * Issues which are only affected by a single Java class file can be checked
     69      * for incrementally when a Java source file is edited and then recompiled.
     70      */
     71     CLASS_FILE,
     72 
     73     /** The analysis considers the manifest file */
     74     MANIFEST,
     75 
     76     /** The analysis considers the Proguard configuration file */
     77     PROGUARD_FILE,
     78 
     79     /**
     80      * The analysis considers classes in the libraries for this project. These
     81      * will be analyzed before the classes themselves.
     82      */
     83     JAVA_LIBRARIES;
     84 
     85     /**
     86      * Returns true if the given scope set corresponds to scanning a single file
     87      * rather than a whole project
     88      *
     89      * @param scopes the scope set to check
     90      * @return true if the scope set references a single file
     91      */
     92     public static boolean checkSingleFile(@NonNull EnumSet<Scope> scopes) {
     93         return scopes.size() == 1 &&
     94                 (scopes.contains(JAVA_FILE)
     95                         || scopes.contains(CLASS_FILE)
     96                         || scopes.contains(RESOURCE_FILE)
     97                         || scopes.contains(PROGUARD_FILE)
     98                         || scopes.contains(MANIFEST));
     99     }
    100 
    101     /**
    102      * Returns the intersection of two scope sets
    103      *
    104      * @param scope1 the first set to intersect
    105      * @param scope2 the second set to intersect
    106      * @return the intersection of the two sets
    107      */
    108     @NonNull
    109     public static EnumSet<Scope> intersect(
    110             @NonNull EnumSet<Scope> scope1,
    111             @NonNull EnumSet<Scope> scope2) {
    112         EnumSet<Scope> scope = EnumSet.copyOf(scope1);
    113         scope.retainAll(scope2);
    114 
    115         return scope;
    116     }
    117 
    118     /** All scopes: running lint on a project will check these scopes */
    119     public static final EnumSet<Scope> ALL = EnumSet.allOf(Scope.class);
    120     /** Scope-set used for detectors which are affected by a single resource file */
    121     public static final EnumSet<Scope> RESOURCE_FILE_SCOPE = EnumSet.of(RESOURCE_FILE);
    122     /** Scope-set used for detectors which scan all resources */
    123     public static final EnumSet<Scope> ALL_RESOURCES_SCOPE = EnumSet.of(ALL_RESOURCE_FILES);
    124     /** Scope-set used for detectors which are affected by a single Java source file */
    125     public static final EnumSet<Scope> JAVA_FILE_SCOPE = EnumSet.of(JAVA_FILE);
    126 }
    127