Search in sources :

Example 86 with NonNull

use of com.android.annotations.NonNull in project kotlin by JetBrains.

the class ClassContext method getLocation.

/**
     * Returns a location for the given {@link MethodNode}.
     *
     * @param methodNode the class in the current context
     * @param classNode the class containing the method
     * @return a location pointing to the class declaration, or as close to it
     *         as possible
     */
@NonNull
public Location getLocation(@NonNull MethodNode methodNode, @NonNull ClassNode classNode) {
    // Attempt to find a proper location for this class. This is tricky
    // since classes do not have line number entries in the class file; we need
    // to find a method, look up the corresponding line number then search
    // around it for a suitable tag, such as the class name.
    String pattern;
    SearchDirection searchMode;
    if (methodNode.name.equals(CONSTRUCTOR_NAME)) {
        searchMode = EOL_BACKWARD;
        if (isAnonymousClass(classNode.name)) {
            pattern = classNode.superName.substring(classNode.superName.lastIndexOf('/') + 1);
        } else {
            pattern = classNode.name.substring(classNode.name.lastIndexOf('$') + 1);
        }
    } else {
        searchMode = BACKWARD;
        pattern = methodNode.name;
    }
    return getLocationForLine(findLineNumber(methodNode), pattern, null, SearchHints.create(searchMode).matchJavaSymbol());
}
Also used : SearchDirection(com.android.tools.klint.detector.api.Location.SearchDirection) NonNull(com.android.annotations.NonNull)

Example 87 with NonNull

use of com.android.annotations.NonNull in project kotlin by JetBrains.

the class IssueRegistry method createDetectors.

/**
     * Creates a list of detectors applicable to the given scope, and with the
     * given configuration.
     *
     * @param client the client to report errors to
     * @param configuration the configuration to look up which issues are
     *            enabled etc from
     * @param scope the scope for the analysis, to filter out detectors that
     *            require wider analysis than is currently being performed
     * @param scopeToDetectors an optional map which (if not null) will be
     *            filled by this method to contain mappings from each scope to
     *            the applicable detectors for that scope
     * @return a list of new detector instances
     */
@NonNull
final List<? extends Detector> createDetectors(@NonNull LintClient client, @NonNull Configuration configuration, @NonNull EnumSet<Scope> scope, @Nullable Map<Scope, List<Detector>> scopeToDetectors) {
    List<Issue> issues = getIssuesForScope(scope);
    if (issues.isEmpty()) {
        return Collections.emptyList();
    }
    Set<Class<? extends Detector>> detectorClasses = new HashSet<Class<? extends Detector>>();
    Map<Class<? extends Detector>, EnumSet<Scope>> detectorToScope = new HashMap<Class<? extends Detector>, EnumSet<Scope>>();
    for (Issue issue : issues) {
        Implementation implementation = issue.getImplementation();
        Class<? extends Detector> detectorClass = implementation.getDetectorClass();
        EnumSet<Scope> issueScope = implementation.getScope();
        if (!detectorClasses.contains(detectorClass)) {
            // Determine if the issue is enabled
            if (!configuration.isEnabled(issue)) {
                continue;
            }
            // Ensured by getIssuesForScope above
            assert implementation.isAdequate(scope);
            detectorClass = client.replaceDetector(detectorClass);
            assert detectorClass != null : issue.getId();
            detectorClasses.add(detectorClass);
        }
        if (scopeToDetectors != null) {
            EnumSet<Scope> s = detectorToScope.get(detectorClass);
            if (s == null) {
                detectorToScope.put(detectorClass, issueScope);
            } else if (!s.containsAll(issueScope)) {
                EnumSet<Scope> union = EnumSet.copyOf(s);
                union.addAll(issueScope);
                detectorToScope.put(detectorClass, union);
            }
        }
    }
    List<Detector> detectors = new ArrayList<Detector>(detectorClasses.size());
    for (Class<? extends Detector> clz : detectorClasses) {
        try {
            Detector detector = clz.newInstance();
            detectors.add(detector);
            if (scopeToDetectors != null) {
                EnumSet<Scope> union = detectorToScope.get(clz);
                for (Scope s : union) {
                    List<Detector> list = scopeToDetectors.get(s);
                    if (list == null) {
                        list = new ArrayList<Detector>();
                        scopeToDetectors.put(s, list);
                    }
                    list.add(detector);
                }
            }
        } catch (Throwable t) {
            //$NON-NLS-1$
            client.log(t, "Can't initialize detector %1$s", clz.getName());
        }
    }
    return detectors;
}
Also used : Issue(com.android.tools.klint.detector.api.Issue) HashMap(java.util.HashMap) EnumSet(java.util.EnumSet) ArrayList(java.util.ArrayList) Implementation(com.android.tools.klint.detector.api.Implementation) Detector(com.android.tools.klint.detector.api.Detector) Scope(com.android.tools.klint.detector.api.Scope) HashSet(java.util.HashSet) NonNull(com.android.annotations.NonNull)

Example 88 with NonNull

use of com.android.annotations.NonNull in project kotlin by JetBrains.

the class IssueRegistry method createIdToIssueMap.

@NonNull
private Map<String, Issue> createIdToIssueMap() {
    List<Issue> issues = getIssues();
    Map<String, Issue> map = Maps.newHashMapWithExpectedSize(issues.size() + 2);
    for (Issue issue : issues) {
        map.put(issue.getId(), issue);
    }
    map.put(PARSER_ERROR.getId(), PARSER_ERROR);
    map.put(LINT_ERROR.getId(), LINT_ERROR);
    return map;
}
Also used : Issue(com.android.tools.klint.detector.api.Issue) NonNull(com.android.annotations.NonNull)

Example 89 with NonNull

use of com.android.annotations.NonNull in project kotlin by JetBrains.

the class LintUtils method getFormattedParameters.

/**
     * Splits up the given message coming from a given string format (where the string
     * format follows the very specific convention of having only strings formatted exactly
     * with the format %n$s where n is between 1 and 9 inclusive, and each formatting parameter
     * appears exactly once, and in increasing order.
     *
     * @param format the format string responsible for creating the error message
     * @param errorMessage an error message formatted with the format string
     * @return the specific values inserted into the format
     */
@NonNull
public static List<String> getFormattedParameters(@NonNull String format, @NonNull String errorMessage) {
    StringBuilder pattern = new StringBuilder(format.length());
    int parameter = 1;
    for (int i = 0, n = format.length(); i < n; i++) {
        char c = format.charAt(i);
        if (c == '%') {
            // Only support formats of the form %n$s where n is 1 <= n <=9
            assert i < format.length() - 4 : format;
            assert format.charAt(i + 1) == ('0' + parameter) : format;
            assert Character.isDigit(format.charAt(i + 1)) : format;
            assert format.charAt(i + 2) == '$' : format;
            assert format.charAt(i + 3) == 's' : format;
            parameter++;
            i += 3;
            pattern.append("(.*)");
        } else {
            pattern.append(c);
        }
    }
    try {
        Pattern compile = Pattern.compile(pattern.toString());
        Matcher matcher = compile.matcher(errorMessage);
        if (matcher.find()) {
            int groupCount = matcher.groupCount();
            List<String> parameters = Lists.newArrayListWithExpectedSize(groupCount);
            for (int i = 1; i <= groupCount; i++) {
                parameters.add(matcher.group(i));
            }
            return parameters;
        }
    } catch (PatternSyntaxException pse) {
    // Internal error: string format is not valid. Should be caught by unit tests
    // as a failure to return the formatted parameters.
    }
    return Collections.emptyList();
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) PatternSyntaxException(java.util.regex.PatternSyntaxException) NonNull(com.android.annotations.NonNull)

Aggregations

NonNull (com.android.annotations.NonNull)89 File (java.io.File)23 TextRange (com.intellij.openapi.util.TextRange)13 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)8 OutputFile (com.android.build.OutputFile)5 DefaultPosition (com.android.tools.klint.detector.api.DefaultPosition)5 Position (com.android.tools.klint.detector.api.Position)5 Issue (com.android.tools.klint.detector.api.Issue)4 Position (com.android.tools.lint.detector.api.Position)4 Module (com.intellij.openapi.module.Module)4 StringReader (java.io.StringReader)4 AbstractInsnNode (org.jetbrains.org.objectweb.asm.tree.AbstractInsnNode)4 Node (org.w3c.dom.Node)4 NodeList (org.w3c.dom.NodeList)4 InputSource (org.xml.sax.InputSource)4 Nullable (com.android.annotations.Nullable)3 AaptPackageProcessBuilder (com.android.builder.core.AaptPackageProcessBuilder)3 AtlasBuilder (com.android.builder.core.AtlasBuilder)3 AndroidProject (com.android.builder.model.AndroidProject)3