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

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


    private void runFileDetectors(Project project, File projectDir) {
        // Look up manifest information
        File manifestFile = new File(project.getDir(), ANDROID_MANIFEST_XML);
        if (manifestFile.exists()) {
            Context context = new Context(mClient, project, manifestFile, mScope);
            context.location = new Location(manifestFile, null, null);
            IDomParser parser = mClient.getParser();
            context.document = parser.parse(context);
            if (context.document != null) {
                project.readManifest(context.document);

                if (mScope.contains(Scope.MANIFEST)) {
                    List<Detector> detectors = mScopeDetectors.get(Scope.MANIFEST);
                    if (detectors != null) {
                        XmlVisitor v = new XmlVisitor(parser, detectors);
                        fireEvent(EventType.SCANNING_FILE, context);
                        v.visitFile(context, manifestFile);
                    }
                }
            }
        }

        // Process both Scope.RESOURCE_FILE and Scope.ALL_RESOURCE_FILES detectors together
        // in a single pass through the resource directories.
        if (mScope.contains(Scope.ALL_RESOURCE_FILES) || mScope.contains(Scope.RESOURCE_FILE)) {
            List<Detector> checks = union(mScopeDetectors.get(Scope.RESOURCE_FILE),
                    mScopeDetectors.get(Scope.ALL_RESOURCE_FILES));
            if (checks.size() > 0) {
                List<ResourceXmlDetector> xmlDetectors =
                        new ArrayList<ResourceXmlDetector>(checks.size());
                for (Detector detector : checks) {
                    if (detector instanceof ResourceXmlDetector) {
                        xmlDetectors.add((ResourceXmlDetector) detector);
                    }
                }
                if (xmlDetectors.size() > 0) {
                    if (project.getSubset() != null) {
                        checkIndividualResources(project, xmlDetectors, project.getSubset());
                    } else {
                        File res = new File(projectDir, RES_FOLDER_NAME);
                        if (res.exists() && xmlDetectors.size() > 0) {
                            checkResFolder(project, res, xmlDetectors);
                        }
                    }
                }
            }
        }

        if (mCanceled) {
            return;
        }

        if (mScope.contains(Scope.JAVA_FILE) || mScope.contains(Scope.ALL_JAVA_FILES)) {
            List<Detector> checks = union(mScopeDetectors.get(Scope.JAVA_FILE),
                    mScopeDetectors.get(Scope.ALL_JAVA_FILES));
            if (checks.size() > 0) {
                List<File> sourceFolders = project.getJavaSourceFolders();
                checkJava(project, sourceFolders, checks);
            }
        }

        if (mCanceled) {
            return;
        }

        if (mScope.contains(Scope.CLASS_FILE)) {
            List<Detector> detectors = mScopeDetectors.get(Scope.CLASS_FILE);
            if (detectors != null) {
                List<File> binFolders = project.getJavaClassFolders();
                checkClasses(project, binFolders, detectors);
            }
        }

        if (mCanceled) {
            return;
        }

        if (mScope.contains(Scope.PROGUARD)) {
            List<Detector> detectors = mScopeDetectors.get(Scope.PROGUARD);
            if (detectors != null) {
                File file = new File(project.getDir(), PROGUARD_CFG);
                if (file.exists()) {
                    Context context = new Context(mClient, project, file, mScope);
                    fireEvent(EventType.SCANNING_FILE, context);
                    context.location = new Location(file, null, null);
                    for (Detector detector : detectors) {
                        if (detector.appliesTo(context, file)) {
                            detector.beforeCheckFile(context);
                            detector.run(context);
                            detector.afterCheckFile(context);
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

                if (project.isGradleProject()) {
                    @SuppressWarnings("SpellCheckingInspection")
                    String message = String.format("\"%1$s\" is a Gradle project. To correctly "
                            + "analyze Gradle projects, you should run \"gradlew :lint\" instead.",
                            project.getName());
                    Location location = Location.create(project.getDir());
                    Context context = new Context(mDriver, project, project, project.getDir());
                    if (context.isEnabled(IssueRegistry.LINT_ERROR)) {
                        report(context,
                               IssueRegistry.LINT_ERROR,
                               project.getConfiguration().getSeverity(IssueRegistry.LINT_ERROR),
View Full Code Here

                fileMap.put(name, file);
            } else if (!count.equals(current)) {
                //Location location = getLocation(language, parentFolderToLanguage);
                String thisName = file.getParentFile().getName() + File.separator + file.getName();
                File otherFile = fileMap.get(name);
                Location location = new Location(otherFile, null, null);
                location.setSecondary(new Location(file, null, null));
                String otherName = otherFile.getParentFile().getName() + File.separator
                        + otherFile.getName();
                context.client.report(context, INCONSISTENT, location,
                    String.format(
                     "Array %1$s has an inconsistent number of items (%2$d in %3$s, %4$d in %5$s)",
View Full Code Here

        }
        Collections.sort(sorted);

        for (String resource : sorted) {
            String message = String.format("The resource %1$s appears to be unused", resource);
            Location location = null;
            Attr attr = mIdToAttr.get(resource);
            if (attr != null) {
                location = mAttrToLocation.get(attr);
                if (location == null) {
                    File f = mAttrToFile.get(attr);
                    IDomParser parser = context.client.getParser();
                    Position start = parser.getStartPosition(context, attr);
                    Position end = null;
                    if (start != null) {
                        end = parser.getEndPosition(context, attr);
                    }
                    location = new Location(f, start, end);
                }
            } else {
                // Try to figure out the file if it's a file based resource (such as R.layout) --
                // in that case we can figure out the filename since it has a simple mapping
                // from the resource name (though the presence of qualifiers like -land etc
                // makes it a little tricky if there's no base file provided)
                int secondDot = resource.indexOf('.', 2);
                String typeName = resource.substring(2, secondDot); // 2: Skip R.
                ResourceType type = ResourceType.getEnum(typeName);
                if (type != null && LintUtils.isFileBasedResourceType(type)) {
                    String name = resource.substring(secondDot + 1);
                    File file = new File(context.project.getDir(),
                            "res" + File.separator + typeName + File.separator + //$NON-NLS-1$
                            name + ".xml"); //$NON-NLS-1$
                    if (file.exists()) {
                        location = new Location(file, null, null);
                    }
                }
            }
            if (location == null) {
                File file = mDeclarationToFile.get(resource);
                if (file != null) {
                    location = new Location(file, null, null);
                }
            }
            context.client.report(context, ISSUE, location, message, resource);
        }
View Full Code Here

            msg = String.format(
                "Duplicate id %1$s, already defined in layout %2$s which is included in this layout",
                id, includer);
        }

        Location location = new Location(first, null, null);
        if (second != null) {
            // Also record the secondary location
            location.setSecondary(new Location(second, null, null));
        }
        context.client.report(context, CROSS_LAYOUT, location, msg, null);
    }
View Full Code Here

    @Override
    public void afterCheckProject(Context context) {
        if (mRootAttributes != null) {
            for (Pair<Location, String> pair : mRootAttributes) {
                Location location = pair.getFirst();

                String layoutName = location.getFile().getName();
                if (endsWith(layoutName, DOT_XML)) {
                    layoutName = layoutName.substring(0, layoutName.length() - DOT_XML.length());
                }

                String theme = getTheme(context, layoutName);
View Full Code Here

            Position start = parser.getStartPosition(context, attribute);
            Position end = null;
            if (start != null) {
                end = parser.getEndPosition(context, attribute);
            }
            Location location = new Location(context.file, start, end);
            if (mRootAttributes == null) {
                mRootAttributes = new ArrayList<Pair<Location,String>>();
            }
            mRootAttributes.add(Pair.of(location, attribute.getValue()));
        }
View Full Code Here

                            return list1.get(0).compareTo(list2.get(0));
                        }
                    });

                    for (List<File> sameFiles : lists) {
                        Location location = null;
                        boolean sameNames = true;
                        String lastName = null;
                        for (File file : sameFiles) {
                             if (lastName != null && !lastName.equals(file.getName())) {
                                sameNames = false;
                            }
                            lastName = file.getName();
                            // Chain locations together
                            Location linkedLocation = location;
                            location = new Location(file, null, null);
                            location.setSecondary(linkedLocation);
                        }

                        if (sameNames) {
                            StringBuilder sb = new StringBuilder();
View Full Code Here

                }
                double widthStdDev = Math.sqrt(squareWidthSum / count);
                double heightStdDev = Math.sqrt(squareHeightSum / count);

                if (widthStdDev > meanWidth / 10 || heightStdDev > meanHeight) {
                    Location location = null;
                    StringBuilder sb = new StringBuilder();

                    // Sort entries by decreasing dip size
                    List<Map.Entry<File, Dimension>> entries =
                            new ArrayList<Map.Entry<File,Dimension>>();
                    for (Map.Entry<File, Dimension> entry2 : dipSizes.entrySet()) {
                        entries.add(entry2);
                    }
                    Collections.sort(entries,
                            new Comparator<Map.Entry<File, Dimension>>() {
                        public int compare(Entry<File, Dimension> e1,
                                Entry<File, Dimension> e2) {
                            Dimension d1 = e1.getValue();
                            Dimension d2 = e2.getValue();
                            if (d1.width != d2.width) {
                                return d2.width - d1.width;
                            }

                            return d2.height - d1.height;
                        }
                    });
                    for (Map.Entry<File, Dimension> entry2 : entries) {
                        if (sb.length() > 0) {
                            sb.append(", ");
                        }
                        File file = entry2.getKey();

                        // Chain locations together
                        Location linkedLocation = location;
                        location = new Location(file, null, null);
                        location.setSecondary(linkedLocation);
                        Dimension dip = entry2.getValue();
                        Dimension px = pixelSizes.get(file);
                        String fileName = file.getParentFile().getName() + File.separator
                                + file.getName();
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.