Package com.android.tools.lint.detector.api

Examples of com.android.tools.lint.detector.api.Location


                    if (context.getDriver().isSuppressed(context, MISSING, element)) {
                        mMissingLocations.remove(name);
                        return;
                    }

                    Location location = context.getLocation(attribute);
                    location.setClientData(element);
                    location.setSecondary(mMissingLocations.get(name));
                    mMissingLocations.put(name, location);
                }
            }
            if (mExtraLocations != null && mExtraLocations.containsKey(name)) {
                if (context.getDriver().isSuppressed(context, EXTRA, element)) {
                    mExtraLocations.remove(name);
                    return;
                }
                Location location = context.getLocation(attribute);
                location.setClientData(element);
                location.setMessage("Also translated here");
                location.setSecondary(mExtraLocations.get(name));
                mExtraLocations.put(name, location);
            }
            return;
        }
View Full Code Here


            return;
        }

        for (Pair<String, Location.Handle> pending : mPending) {
            Handle handle = pending.getSecond();
            Location location = handle.resolve();
            File file = location.getFile();
            String layout = file.getName();
            if (layout.endsWith(DOT_XML)) {
                layout = layout.substring(0, layout.length() - DOT_XML.length());
            }
View Full Code Here

                                            (Node) clientData)) {
                                        return;
                                    }
                                }

                                Location location = handle.resolve();
                                String message = String.format(
                                        "Incorrect formatting string %1$s; missing conversion " +
                                        "character in '%2$s' ?", name, str);
                                context.report(INVALID, location, message, null);
                                //warned = true;
                                continue;
                            }
                        }
                    }

                    if (!checkTypes) {
                        continue;
                    }

                    // Shouldn't throw a number format exception since we've already
                    // matched the pattern in the regexp
                    int number;
                    String numberString = matcher.group(1);
                    if (numberString != null) {
                        // Strip off trailing $
                        numberString = numberString.substring(0, numberString.length() - 1);
                        number = Integer.parseInt(numberString);
                        nextNumber = number + 1;
                    } else {
                        number = nextNumber++;
                    }
                    String format = matcher.group(6);
                    String currentFormat = types.get(number);
                    if (currentFormat == null) {
                        types.put(number, format);
                        typeDefinition.put(number, handle);
                    } else if (!currentFormat.equals(format)
                            && isIncompatible(currentFormat.charAt(0), format.charAt(0))) {

                        Object clientData = handle.getClientData();
                        if (clientData instanceof Node) {
                            if (context.getDriver().isSuppressed(null, ARG_TYPES,
                                    (Node) clientData)) {
                                return;
                            }
                        }

                        Location location = handle.resolve();
                        // Attempt to limit the location range to just the formatting
                        // string in question
                        location = refineLocation(context, location, formatString,
                                matcher.start(), matcher.end());
                        Location otherLocation = typeDefinition.get(number).resolve();
                        otherLocation.setMessage("Conflicting argument type here");
                        location.setSecondary(otherLocation);
                        File f = otherLocation.getFile();
                        String message = String.format(
                                "Inconsistent formatting types for argument #%1$d in " +
                                "format string %2$s ('%3$s'): Found both '%4$s' and '%5$s' " +
                                "(in %6$s)",
                                number, name,
View Full Code Here

                if (clientData instanceof Node) {
                    if (context.getDriver().isSuppressed(null, ARG_COUNT, (Node) clientData)) {
                        return;
                    }
                }
                Location location = handle.resolve();
                Location secondary = list.get(0).getFirst().resolve();
                secondary.setMessage("Conflicting number of arguments here");
                location.setSecondary(secondary);
                String message = String.format(
                        "Inconsistent number of arguments in formatting string %1$s; " +
                        "found both %2$d and %3$d", name, prevCount, count);
                context.report(ARG_COUNT, location, message, null);
                break;
            }

            for (int i = 1; i <= count; i++) {
                if (!indices.contains(i)) {
                    Object clientData = handle.getClientData();
                    if (clientData instanceof Node) {
                        if (context.getDriver().isSuppressed(null, ARG_COUNT,
                                (Node) clientData)) {
                            return;
                        }
                    }

                    Set<Integer> all = new HashSet<Integer>();
                    for (int j = 1; j < count; j++) {
                        all.add(j);
                    }
                    all.removeAll(indices);
                    List<Integer> sorted = new ArrayList<Integer>(all);
                    Collections.sort(sorted);
                    Location location = handle.resolve();
                    String message = String.format(
                            "Formatting string '%1$s' is not referencing numbered arguments %2$s",
                            name, sorted);
                    context.report(ARG_COUNT, location, message, null);
                    break;
View Full Code Here

            if (clientData instanceof Node) {
                if (context.getDriver().isSuppressed(null, INVALID, (Node) clientData)) {
                    return;
                }
            }
            Location location = handle.resolve();
            String message = String.format(
                    "Format string '%1$s' is not a valid format string so it should not be " +
                    "passed to String.format",
                    name);
            context.report(INVALID, call, location, message, null);
            return;
        }

        Iterator<Expression> argIterator = args.iterator();
        Expression first = argIterator.next();
        Expression second = argIterator.hasNext() ? argIterator.next() : null;

        boolean specifiesLocale;
        TypeReference parameterType;
        lombok.ast.Node resolved = context.parser.resolve(context, first);
        if (resolved != null) {
            parameterType = context.parser.getType(context, resolved);
        } else {
            parameterType = context.parser.getType(context, first);
        }
        if (parameterType != null) {
            specifiesLocale = parameterType.getTypeName().equals("java.util.Locale"); //$NON-NLS-1$
        } else if (!call.astName().astValue().equals(FORMAT_METHOD)) {
            specifiesLocale = false;
        } else {
            // No type information with this AST; use string patterns instead to make
            // an educated guess
            String firstName = first.toString();
            specifiesLocale = firstName.startsWith("Locale.")                     //$NON-NLS-1$
                    || firstName.contains("locale")                               //$NON-NLS-1$
                    || firstName.equals("null")                                   //$NON-NLS-1$
                    || (second != null && second.toString().contains("getString") //$NON-NLS-1$
                        && !firstName.contains("getString")                       //$NON-NLS-1$
                        && !firstName.contains(R_PREFIX)
                        && !(first instanceof StringLiteral));
        }

        List<Pair<Handle, String>> list = mFormatStrings.get(name);
        if (list != null) {
            for (Pair<Handle, String> pair : list) {
                String s = pair.getSecond();
                int count = getFormatArgumentCount(s, null);
                Handle handle = pair.getFirst();
                if (count != args.size() - 1 - (specifiesLocale ? 1 : 0)) {
                    Location location = context.parser.getLocation(context, call);
                    Location secondary = handle.resolve();
                    secondary.setMessage(String.format("This definition requires %1$d arguments",
                            count));
                    location.setSecondary(secondary);
                    String message = String.format(
                            "Wrong argument count, format string %1$s requires %2$d but format " +
                            "call supplies %3$d",
                            name, count, args.size() - 1 - (specifiesLocale ? 1 : 0));
                    context.report(ARG_TYPES, method, location, message, null);
                } else {
                    for (int i = 1; i <= count; i++) {
                        int argumentIndex = i + (specifiesLocale ? 1 : 0);
                        Class<?> type = tracker.getArgumentType(argumentIndex);
                        if (type != null) {
                            boolean valid = true;
                            String formatType = getFormatArgumentType(s, i);
                            char last = formatType.charAt(formatType.length() - 1);
                            if (formatType.length() >= 2 &&
                                    Character.toLowerCase(
                                            formatType.charAt(formatType.length() - 2)) == 't') {
                                // Date time conversion.
                                // TODO
                                continue;
                            }
                            switch (last) {
                                // Booleans. It's okay to pass objects to these;
                                // it will print "true" if non-null, but it's
                                // unusual and probably not intended.
                                case 'b':
                                case 'B':
                                    valid = type == Boolean.TYPE;
                                    break;

                                // Numeric: integer and floats in various formats
                                case 'x':
                                case 'X':
                                case 'd':
                                case 'o':
                                case 'e':
                                case 'E':
                                case 'f':
                                case 'g':
                                case 'G':
                                case 'a':
                                case 'A':
                                    valid = type == Integer.TYPE
                                            || type == Float.TYPE;
                                    break;
                                case 'c':
                                case 'C':
                                    // Unicode character
                                    valid = type == Character.TYPE;
                                    break;
                                case 'h':
                                case 'H': // Hex print of hash code of objects
                                case 's':
                                case 'S':
                                    // String. Can pass anything, but warn about
                                    // numbers since you may have meant more
                                    // specific formatting. Use special issue
                                    // explanation for this?
                                    valid = type != Boolean.TYPE &&
                                        !type.isAssignableFrom(Number.class);
                                    break;
                            }

                            if (!valid) {
                                IJavaParser parser = context.parser;
                                Expression argument = tracker.getArgument(argumentIndex);
                                Location location = parser.getLocation(context, argument);
                                Location secondary = handle.resolve();
                                secondary.setMessage("Conflicting argument declaration here");
                                location.setSecondary(secondary);

                                String message = String.format(
                                        "Wrong argument type for formatting argument '#%1$d' " +
                                        "in %2$s: conversion is '%3$s', received %4$s",
View Full Code Here

                    IssueRegistry.PARSER_ERROR, Location.create(context.file),
                    e.getCause() != null ? e.getCause().getLocalizedMessage() :
                        e.getLocalizedMessage(),
                    null);
        } catch (SAXException e) {
            Location location = Location.create(context.file);
            String message = e.getCause() != null ? e.getCause().getLocalizedMessage() :
                    e.getLocalizedMessage();
            if (message.startsWith("The processing instruction target matching "
                    + "\"[xX][mM][lL]\" is not allowed.")) {
                int prologue = xml.indexOf("<?xml ");
View Full Code Here

            if (!haveUpperCase) {
                String fixed = className.charAt(0) + className.substring(1).replace('.','$');
                String message = "Use '$' instead of '.' for inner classes " +
                        "(or use only lowercase letters in package names); replace \"" +
                        className + "\" with \"" + fixed + "\"";
                Location location = context.getLocation(classNameNode);
                context.report(INNERCLASS, element, location, message, null);
            }
        }
    }
View Full Code Here

                }

                String message = String.format(
                        "Class referenced in the manifest, %1$s, was not found in the " +
                                "project or the libraries", fqcn);
                Location location = handle.resolve();
                File parentFile = location.getFile().getParentFile();
                if (parentFile != null) {
                    String parent = parentFile.getName();
                    ResourceFolderType type = ResourceFolderType.getFolderType(parent);
                    if (type == LAYOUT) {
                        message = String.format(
View Full Code Here

                        mWriter.write("\n</pre>");                       //$NON-NLS-1$
                    }
                    mWriter.write('\n');
                    if (warning.location != null && warning.location.getSecondary() != null) {
                        mWriter.write("<ul>");
                        Location l = warning.location.getSecondary();
                        int otherLocations = 0;
                        while (l != null) {
                            String message = l.getMessage();
                            if (message != null && !message.isEmpty()) {
                                Position start = l.getStart();
                                int line = start != null ? start.getLine() : -1;
                                String path = mClient.getDisplayPath(warning.project, l.getFile());
                                writeLocation(l.getFile(), path, line);
                                mWriter.write(':');
                                mWriter.write(' ');
                                mWriter.write("<span class=\"message\">");           //$NON-NLS-1$
                                appendEscapedText(message);
                                mWriter.write("</span>");                            //$NON-NLS-1$
                                mWriter.write("<br />");                         //$NON-NLS-1$

                                String name = l.getFile().getName();
                                if (!(endsWith(name, DOT_PNG) || endsWith(name, DOT_JPG))) {
                                    String s = mClient.readFile(l.getFile());
                                    if (s != null && !s.isEmpty()) {
                                        mWriter.write("<pre class=\"errorlines\">\n");   //$NON-NLS-1$
                                        int offset = start != null ? start.getOffset() : -1;
                                        appendCodeBlock(s, line, offset);
                                        mWriter.write("\n</pre>");                       //$NON-NLS-1$
                                    }
                                }
                            } else {
                                otherLocations++;
                            }

                            l = l.getSecondary();
                        }
                        mWriter.write("</ul>");
                        if (otherLocations > 0) {

                            String id = "Location" + count + "Div";          //$NON-NLS-1$
                            mWriter.write("<button id=\"");                  //$NON-NLS-1$
                            mWriter.write(id);
                            mWriter.write("Link\" onclick=\"reveal('");      //$NON-NLS-1$
                            mWriter.write(id);
                            mWriter.write("');\" />"); //$NON-NLS-1$
                            mWriter.write(String.format("+ %1$d Additional Locations...",
                                    otherLocations));
                            mWriter.write("</button>\n");                    //$NON-NLS-1$
                            mWriter.write("<div id=\"");                     //$NON-NLS-1$
                            mWriter.write(id);
                            mWriter.write("\" style=\"display: none\">\n")//$NON-NLS-1$

                            mWriter.write("Additional locations: ");
                            mWriter.write("<ul>\n"); //$NON-NLS-1$
                            l = warning.location.getSecondary();
                            while (l != null) {
                                Position start = l.getStart();
                                int line = start != null ? start.getLine() : -1;
                                String path = mClient.getDisplayPath(warning.project, l.getFile());
                                mWriter.write("<li> "); //$NON-NLS-1$
                                writeLocation(l.getFile(), path, line);
                                mWriter.write("\n")//$NON-NLS-1$
                                l = l.getSecondary();
                            }
                            mWriter.write("</ul>\n"); //$NON-NLS-1$

                            mWriter.write("</div><br/><br/>\n"); //$NON-NLS-1$
                        }
View Full Code Here

                    IssueRegistry.PARSER_ERROR, Location.create(context.file),
                    e.getCause() != null ? e.getCause().getLocalizedMessage() :
                        e.getLocalizedMessage(),
                    null);
        } catch (SAXException e) {
            Location location = Location.create(context.file);
            String message = e.getCause() != null ? e.getCause().getLocalizedMessage() :
                    e.getLocalizedMessage();
            if (message.startsWith("The processing instruction target matching "
                    + "\"[xX][mM][lL]\" is not allowed.")) {
                int prologue = xml.indexOf("<?xml ");
View Full Code Here

TOP

Related Classes of com.android.tools.lint.detector.api.Location

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.