Java Code Examples for com.android.tools.lint.detector.api.Location#create()

The following examples show how to use com.android.tools.lint.detector.api.Location#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TodoDetector.java    From linette with Apache License 2.0 6 votes vote down vote up
@Override
public AstVisitor createJavaVisitor(@NonNull JavaContext context) {
    String source = context.getContents();

    // Check validity of source
    if (source == null) {
        return null;
    }

    // Check for uses of to-dos
    int index = source.indexOf(TODO_MATCHER_STRING);
    for (int i = index; i >= 0; i = source.indexOf(TODO_MATCHER_STRING, i + 1)) {
        Location location = Location.create(context.file, source, i, i + TODO_MATCHER_STRING.length());
        context.report(ISSUE, location, ISSUE.getBriefDescription(TextFormat.TEXT));
    }
    return null;
}
 
Example 2
Source File: TodoDetector.java    From Android-Lint-Checks with Apache License 2.0 6 votes vote down vote up
@Override
public AstVisitor createJavaVisitor(@NonNull JavaContext context) {
    String source = context.getContents();

    // Check validity of source
    if (source == null) {
        return null;
    }

    // Check for uses of to-dos
    int index = source.indexOf(TODO_MATCHER_STRING);
    for (int i = index; i >= 0; i = source.indexOf(TODO_MATCHER_STRING, i + 1)) {
        Location location = Location.create(context.file, source, i, i + TODO_MATCHER_STRING.length());
        context.report(ISSUE, location, ISSUE.getBriefDescription(TextFormat.TEXT));
    }
    return null;
}
 
Example 3
Source File: TranslationDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
private void reportMap(Context context, Issue issue, Map<String, Location> map) {
    if (map != null) {
        for (Map.Entry<String, Location> entry : map.entrySet()) {
            Location location = entry.getValue();
            String name = entry.getKey();
            String message = mDescriptions.get(name);

            if (location == null) {
                location = Location.create(context.getProject().getDir());
            }

            // We were prepending locations, but we want to prefer the base folders
            location = Location.reverse(location);

            context.report(issue, location, message);
        }
    }
}
 
Example 4
Source File: StringFormatDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
private static Location refineLocation(Context context, Location location,
        String formatString, int substringStart, int substringEnd) {
    Position startLocation = location.getStart();
    Position endLocation = location.getEnd();
    if (startLocation != null && endLocation != null) {
        int startOffset = startLocation.getOffset();
        int endOffset = endLocation.getOffset();
        if (startOffset >= 0) {
            String contents = context.getClient().readFile(location.getFile());
            if (endOffset <= contents.length() && startOffset < endOffset) {
                int formatOffset = contents.indexOf(formatString, startOffset);
                if (formatOffset != -1 && formatOffset <= endOffset) {
                    return Location.create(location.getFile(), contents,
                            formatOffset + substringStart, formatOffset + substringEnd);
                }
            }
        }
    }

    return location;
}
 
Example 5
Source File: GroovyGradleDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected Location createLocation(@NonNull Context context, @NonNull Object cookie) {
    ASTNode node = (ASTNode) cookie;
    Pair<Integer, Integer> offsets = getOffsets(node, context);
    int fromLine = node.getLineNumber() - 1;
    int fromColumn = node.getColumnNumber() - 1;
    int toLine = node.getLastLineNumber() - 1;
    int toColumn = node.getLastColumnNumber() - 1;
    return Location.create(context.file,
            new DefaultPosition(fromLine, fromColumn, offsets.getFirst()),
            new DefaultPosition(toLine, toColumn, offsets.getSecond()));
}
 
Example 6
Source File: EcjParser.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
@Override
public Location getLocation(@NonNull JavaContext context, @NonNull Node node) {
    lombok.ast.Position position = node.getPosition();
    return Location.create(context.file, context.getContents(),
            position.getStart(), position.getEnd());
}
 
Example 7
Source File: PrivateResourceDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void beforeCheckFile(@NonNull Context context) {
    File file = context.file;
    boolean isXmlFile = LintUtils.isXmlFile(file);
    if (!isXmlFile && !LintUtils.isBitmapFile(file)) {
        return;
    }
    String parentName = file.getParentFile().getName();
    int dash = parentName.indexOf('-');
    if (dash != -1 || FD_RES_VALUES.equals(parentName)) {
        return;
    }
    ResourceFolderType folderType = ResourceFolderType.getFolderType(parentName);
    if (folderType == null) {
        return;
    }
    List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(folderType);
    if (types.isEmpty()) {
        return;
    }
    ResourceType type = types.get(0);
    String resourceName = getResourceFieldName(getBaseName(file.getName()));
    if (isPrivate(context, type, resourceName)) {
        String message = createOverrideErrorMessage(context, type, resourceName);
        Location location = Location.create(file);
        context.report(ISSUE, location, message);
    }
}
 
Example 8
Source File: InvalidPackageDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void afterCheckProject(@NonNull Context context) {
    if (mCandidates == null) {
        return;
    }

    Set<String> seen = Sets.newHashSet();

    for (Candidate candidate : mCandidates) {
        String type = candidate.mClass;
        if (mJavaxLibraryClasses.contains(type)) {
            continue;
        }
        File jarFile = candidate.mJarFile;
        String referencedIn = candidate.mReferencedIn;

        Location location = Location.create(jarFile);
        String pkg = getPackageName(type);
        if (seen.contains(pkg)) {
            continue;
        }
        seen.add(pkg);

        if (pkg.equals("javax.inject")) {
            String name = jarFile.getName();
            //noinspection SpellCheckingInspection
            if (name.startsWith("dagger-") || name.startsWith("guice-")) {
                // White listed
                return;
            }
        }

        String message = String.format(
                "Invalid package reference in library; not included in Android: `%1$s`. " +
                "Referenced from `%2$s`.", pkg, ClassContext.getFqcn(referencedIn));
        context.report(ISSUE, location, message);
    }
}
 
Example 9
Source File: Utf8Detector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void visitDocument(@NonNull XmlContext context, @NonNull Document document) {
    String xml = context.getContents();
    if (xml == null) {
        return;
    }

    // AAPT: The prologue must be in the first line
    int lineEnd = 0;
    int max = xml.length();
    for (; lineEnd < max; lineEnd++) {
        char c = xml.charAt(lineEnd);
        if (c == '\n' || c == '\r') {
            break;
        }
    }

    for (int i = 16; i < lineEnd - 5; i++) { // +4: Skip at least <?xml encoding="
        if ((xml.charAt(i) == 'u' || xml.charAt(i) == 'U')
                && (xml.charAt(i + 1) == 't' || xml.charAt(i + 1) == 'T')
                && (xml.charAt(i + 2) == 'f' || xml.charAt(i + 2) == 'F')
                && (xml.charAt(i + 3) == '-' || xml.charAt(i + 3) == '_')
                && (xml.charAt(i + 4) == '8')) {
            return;
        }
    }

    int encodingIndex = xml.lastIndexOf("encoding", lineEnd); //$NON-NLS-1$
    if (encodingIndex != -1) {
        Matcher matcher = ENCODING_PATTERN.matcher(xml);
        if (matcher.find(encodingIndex)) {
            String encoding = matcher.group(1);
            Location location = Location.create(context.file, xml,
                    matcher.start(1), matcher.end(1));
            context.report(ISSUE, null, location, String.format(
                    "%1$s: Not using UTF-8 as the file encoding. This can lead to subtle " +
                            "bugs with non-ascii characters", encoding));
        }
    }
}
 
Example 10
Source File: IconDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private static Location chainLocations(List<File> files) {
    // Chain locations together
    Collections.sort(files);
    Location location = null;
    for (File file : files) {
        Location linkedLocation = location;
        location = Location.create(file);
        location.setSecondary(linkedLocation);
    }
    return location;
}
 
Example 11
Source File: ResourcePrefixDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void checkBinaryResource(@NonNull ResourceContext context) {
    if (mPrefix != null) {
        ResourceFolderType folderType = context.getResourceFolderType();
        if (folderType != null && folderType != ResourceFolderType.VALUES) {
            String name = LintUtils.getBaseName(context.file.getName());
            if (!name.startsWith(mPrefix)) {
                Location location = Location.create(context.file);
                context.report(ISSUE, location, getErrorMessage(name));
            }
        }
    }
}
 
Example 12
Source File: LintCliXmlParser.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@NonNull
@Override
public Location getLocation(@NonNull XmlContext context, @NonNull Node node,
        int start, int end) {
    return Location.create(context.file, PositionXmlParser.getPosition(node, start, end));
}
 
Example 13
Source File: LintCliXmlParser.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@NonNull
@Override
public Location resolve() {
    return Location.create(mFile, PositionXmlParser.getPosition(mNode));
}
 
Example 14
Source File: ExtraTextDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
private void visitNode(XmlContext context, Node node) {
    short nodeType = node.getNodeType();
    if (nodeType == Node.TEXT_NODE && !mFoundText) {
        String text = node.getNodeValue();
        for (int i = 0, n = text.length(); i < n; i++) {
            char c = text.charAt(i);
            if (!Character.isWhitespace(c)) {
                String snippet = text.trim();
                int maxLength = 100;
                if (snippet.length() > maxLength) {
                    snippet = snippet.substring(0, maxLength) + "...";
                }
                Location location = context.getLocation(node);
                if (i > 0) {
                    // Adjust the error position to point to the beginning of
                    // the text rather than the beginning of the text node
                    // (which is often the newline at the end of the previous
                    // line and the indentation)
                    Position start = location.getStart();
                    if (start != null) {
                        int line = start.getLine();
                        int column = start.getColumn();
                        int offset = start.getOffset();

                        for (int j = 0; j < i; j++) {
                            offset++;

                            if (text.charAt(j) == '\n') {
                                if (line != -1) {
                                    line++;
                                }
                                if (column != -1) {
                                    column = 0;
                                }
                            } else if (column != -1) {
                                column++;
                            }
                        }

                        start = new DefaultPosition(line, column, offset);
                        location = Location.create(context.file, start, location.getEnd());
                    }
                }
                context.report(ISSUE, node, location,
                        String.format("Unexpected text found in layout file: \"%1$s\"",
                                snippet));
                mFoundText = true;
                break;
            }
        }
    }

    // Visit children
    NodeList childNodes = node.getChildNodes();
    for (int i = 0, n = childNodes.getLength(); i < n; i++) {
        Node child = childNodes.item(i);
        visitNode(context, child);
    }
}
 
Example 15
Source File: XmlReporterTest.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
public void test() throws Exception {
    File file = new File(getTargetDir(), "report");
    try {
        LintCliClient client = new LintCliClient() {
            @Override
            String getRevision() {
                return "unittest"; // Hardcode version to keep unit test output stable
            }
        };
        //noinspection ResultOfMethodCallIgnored
        file.getParentFile().mkdirs();
        XmlReporter reporter = new XmlReporter(client, file);
        Project project = Project.create(client, new File("/foo/bar/Foo"),
                new File("/foo/bar/Foo"));

        Warning warning1 = new Warning(ManifestDetector.USES_SDK,
                "<uses-sdk> tag should specify a target API level (the highest verified " +
                "version; when running on later versions, compatibility behaviors may " +
                "be enabled) with android:targetSdkVersion=\"?\"",
                Severity.WARNING, project);
        warning1.line = 6;
        warning1.file = new File("/foo/bar/Foo/AndroidManifest.xml");
        warning1.errorLine = "    <uses-sdk android:minSdkVersion=\"8\" />\n    ^\n";
        warning1.path = "AndroidManifest.xml";
        warning1.location = Location.create(warning1.file,
                new DefaultPosition(6, 4, 198), new DefaultPosition(6, 42, 236));

        Warning warning2 = new Warning(HardcodedValuesDetector.ISSUE,
                "[I18N] Hardcoded string \"Fooo\", should use @string resource",
                Severity.WARNING, project);
        warning2.line = 11;
        warning2.file = new File("/foo/bar/Foo/res/layout/main.xml");
        warning2.errorLine = "        android:text=\"Fooo\" />\n" +
                      "        ~~~~~~~~~~~~~~~~~~~\n";
        warning2.path = "res/layout/main.xml";
        warning2.location = Location.create(warning2.file,
                new DefaultPosition(11, 8, 377), new DefaultPosition(11, 27, 396));

        List<Warning> warnings = new ArrayList<Warning>();
        warnings.add(warning1);
        warnings.add(warning2);

        reporter.write(0, 2, warnings);

        String report = Files.toString(file, Charsets.UTF_8);
        assertEquals(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "<issues format=\"4\" by=\"lint unittest\">\n" +
            "\n" +
            "    <issue\n" +
            "        id=\"UsesMinSdkAttributes\"\n" +
            "        severity=\"Warning\"\n" +
            "        message=\"&lt;uses-sdk> tag should specify a target API level (the highest verified version; when running on later versions, compatibility behaviors may be enabled) with android:targetSdkVersion=&quot;?&quot;\"\n" +
            "        category=\"Correctness\"\n" +
            "        priority=\"9\"\n" +
            "        summary=\"Minimum SDK and target SDK attributes not defined\"\n" +
            "        explanation=\"The manifest should contain a `&lt;uses-sdk>` element which defines the minimum API Level required for the application to run, as well as the target version (the highest API level you have tested the version for.)\"\n" +
            "        url=\"http://developer.android.com/guide/topics/manifest/uses-sdk-element.html\"\n" +
            "        urls=\"http://developer.android.com/guide/topics/manifest/uses-sdk-element.html\"\n" +
            "        errorLine1=\"    &lt;uses-sdk android:minSdkVersion=&quot;8&quot; />\"\n" +
            "        errorLine2=\"    ^\">\n" +
            "        <location\n" +
            "            file=\"AndroidManifest.xml\"\n" +
            "            line=\"7\"\n" +
            "            column=\"5\"/>\n" +
            "    </issue>\n" +
            "\n" +
            "    <issue\n" +
            "        id=\"HardcodedText\"\n" +
            "        severity=\"Warning\"\n" +
            "        message=\"[I18N] Hardcoded string &quot;Fooo&quot;, should use @string resource\"\n" +
            "        category=\"Internationalization\"\n" +
            "        priority=\"5\"\n" +
            "        summary=\"Hardcoded text\"\n" +
            "        explanation=\"Hardcoding text attributes directly in layout files is bad for several reasons:\n" +
            "\n" +
            "* When creating configuration variations (for example for landscape or portrait)you have to repeat the actual text (and keep it up to date when making changes)\n" +
            "\n" +
            "* The application cannot be translated to other languages by just adding new translations for existing string resources.\n" +
            "\n" +
            "In Android Studio and Eclipse there are quickfixes to automatically extract this hardcoded string into a resource lookup.\"\n" +
            "        errorLine1=\"        android:text=&quot;Fooo&quot; />\"\n" +
            "        errorLine2=\"        ~~~~~~~~~~~~~~~~~~~\">\n" +
            "        <location\n" +
            "            file=\"res/layout/main.xml\"\n" +
            "            line=\"12\"\n" +
            "            column=\"9\"/>\n" +
            "    </issue>\n" +
            "\n" +
            "</issues>\n",
            report);

        // Make sure the XML is valid
        Document document = PositionXmlParser.parse(report);
        assertNotNull(document);
        assertEquals(2, document.getElementsByTagName("issue").getLength());
    } finally {
        //noinspection ResultOfMethodCallIgnored
        file.delete();
    }
}
 
Example 16
Source File: DefaultConfigurationTest.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
public void testPathIgnore() throws Exception {
    File projectDir = getProjectDir(null,
            "res/layout/onclick.xml=>res/layout/onclick.xml",
            "res/layout/onclick.xml=>res/layout-xlarge/onclick.xml",
            "res/layout/onclick.xml=>res/layout-xlarge/activation.xml"
    );
    LintClient client = new TestLintClient();
    Project project = Project.create(client, projectDir, projectDir);
    LintDriver driver = new LintDriver(new BuiltinIssueRegistry(), client);
    File plainFile = new File(projectDir,
            "res" + File.separator + "layout" + File.separator + "onclick.xml");
    assertTrue(plainFile.exists());
    File largeFile = new File(projectDir,
            "res" + File.separator + "layout-xlarge" + File.separator + "onclick.xml");
    assertTrue(largeFile.exists());
    File windowsFile = new File(projectDir,
            "res" + File.separator + "layout-xlarge" + File.separator + "activation.xml");
    assertTrue(windowsFile.exists());
    Context plainContext = new Context(driver, project, project, plainFile);
    Context largeContext = new Context(driver, project, project, largeFile);
    Context windowsContext = new Context(driver, project, project, windowsFile);
    Location plainLocation = Location.create(plainFile);
    Location largeLocation = Location.create(largeFile);
    Location windowsLocation = Location.create(windowsFile);

    assertEquals(Severity.WARNING, ObsoleteLayoutParamsDetector.ISSUE.getDefaultSeverity());
    assertEquals(Severity.ERROR, ApiDetector.UNSUPPORTED.getDefaultSeverity());

    DefaultConfiguration configuration = getConfiguration(""
            + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
            + "<lint>\n"
            + "    <issue id=\"ObsoleteLayoutParam\">\n"
            + "        <ignore path=\"res/layout-xlarge/onclick.xml\" />\n"
            + "        <ignore path=\"res\\layout-xlarge\\activation.xml\" />\n"
            + "    </issue>\n"
            + "    <issue id=\"NewApi\">\n"
            + "        <ignore path=\"res/layout-xlarge\" />\n"
            + "    </issue>\n"
            + "</lint>");

    assertFalse(configuration
            .isIgnored(plainContext, ApiDetector.UNSUPPORTED, plainLocation, ""));
    assertFalse(configuration
            .isIgnored(plainContext, ObsoleteLayoutParamsDetector.ISSUE, plainLocation,
                    ""));

    assertTrue(configuration
            .isIgnored(windowsContext, ObsoleteLayoutParamsDetector.ISSUE, windowsLocation,
                    ""));
    assertTrue(
            configuration
                    .isIgnored(largeContext, ApiDetector.UNSUPPORTED, largeLocation, ""));
    assertTrue(configuration
            .isIgnored(largeContext, ObsoleteLayoutParamsDetector.ISSUE, largeLocation,
                    ""));
}
 
Example 17
Source File: DefaultConfigurationTest.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
public void testPatternIgnore() throws Exception {
    File projectDir = getProjectDir(null,
            "res/layout/onclick.xml=>res/layout/onclick.xml",
            "res/layout/onclick.xml=>res/layout-xlarge/onclick.xml",
            "res/layout/onclick.xml=>res/layout-xlarge/activation.xml"
    );
    LintClient client = new TestLintClient();
    Project project = Project.create(client, projectDir, projectDir);
    LintDriver driver = new LintDriver(new BuiltinIssueRegistry(), client);
    File plainFile = new File(projectDir,
            "res" + File.separator + "layout" + File.separator + "onclick.xml");
    assertTrue(plainFile.exists());
    File largeFile = new File(projectDir,
            "res" + File.separator + "layout-xlarge" + File.separator + "onclick.xml");
    assertTrue(largeFile.exists());
    File windowsFile = new File(projectDir,
            "res" + File.separator + "layout-xlarge" + File.separator + "activation.xml");
    assertTrue(windowsFile.exists());
    Context plainContext = new Context(driver, project, project, plainFile);
    Context largeContext = new Context(driver, project, project, largeFile);
    Context windowsContext = new Context(driver, project, project, windowsFile);
    Location plainLocation = Location.create(plainFile);
    Location largeLocation = Location.create(largeFile);
    Location windowsLocation = Location.create(windowsFile);

    assertEquals(Severity.WARNING, ObsoleteLayoutParamsDetector.ISSUE.getDefaultSeverity());
    assertEquals(Severity.ERROR, ApiDetector.UNSUPPORTED.getDefaultSeverity());

    DefaultConfiguration configuration = getConfiguration(""
            + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
            + "<lint>\n"
            + "    <issue id=\"ObsoleteLayoutParam\">\n"
            + "        <ignore regexp=\"x.*onclick\" />\n"
            + "        <ignore regexp=\"res/.*layout.*/activation.xml\" />\n"
            + "    </issue>\n"
            + "</lint>");

    assertFalse(configuration.isIgnored(plainContext, ApiDetector.UNSUPPORTED,
            plainLocation, ""));
    assertFalse(configuration.isIgnored(plainContext, ObsoleteLayoutParamsDetector.ISSUE,
            plainLocation, ""));
    assertTrue(configuration.isIgnored(windowsContext, ObsoleteLayoutParamsDetector.ISSUE,
            windowsLocation, ""));
    assertTrue(configuration.isIgnored(largeContext, ObsoleteLayoutParamsDetector.ISSUE,
            largeLocation, ""));
}
 
Example 18
Source File: DefaultConfigurationTest.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
public void testGlobbing() throws Exception {
    File projectDir = getProjectDir(null,
            "res/layout/onclick.xml=>res/layout/onclick.xml",
            "res/layout/onclick.xml=>res/layout-xlarge/onclick.xml",
            "res/layout/onclick.xml=>res/layout-xlarge/activation.xml"
    );
    LintClient client = new TestLintClient();
    Project project = Project.create(client, projectDir, projectDir);
    LintDriver driver = new LintDriver(new BuiltinIssueRegistry(), client);
    File plainFile = new File(projectDir,
            "res" + File.separator + "layout" + File.separator + "onclick.xml");
    assertTrue(plainFile.exists());
    File largeFile = new File(projectDir,
            "res" + File.separator + "layout-xlarge" + File.separator + "onclick.xml");
    assertTrue(largeFile.exists());
    File windowsFile = new File(projectDir,
            "res" + File.separator + "layout-xlarge" + File.separator + "activation.xml");
    assertTrue(windowsFile.exists());
    Context plainContext = new Context(driver, project, project, plainFile);
    Context largeContext = new Context(driver, project, project, largeFile);
    Context windowsContext = new Context(driver, project, project, windowsFile);
    Location plainLocation = Location.create(plainFile);
    Location largeLocation = Location.create(largeFile);
    Location windowsLocation = Location.create(windowsFile);

    assertEquals(Severity.WARNING, ObsoleteLayoutParamsDetector.ISSUE.getDefaultSeverity());
    assertEquals(Severity.ERROR, ApiDetector.UNSUPPORTED.getDefaultSeverity());

    DefaultConfiguration configuration = getConfiguration(""
            + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
            + "<lint>\n"
            + "    <issue id=\"ObsoleteLayoutParam\">\n"
            + "        <ignore path=\"**/layout-x*/onclick.xml\" />\n"
            + "        <ignore path=\"res/**/activation.xml\" />\n"
            + "    </issue>\n"
            + "</lint>");

    assertFalse(configuration.isIgnored(plainContext, ApiDetector.UNSUPPORTED,
            plainLocation, ""));
    assertFalse(configuration.isIgnored(plainContext, ObsoleteLayoutParamsDetector.ISSUE,
            plainLocation, ""));
    assertTrue(configuration.isIgnored(windowsContext, ObsoleteLayoutParamsDetector.ISSUE,
            windowsLocation, ""));
    assertTrue(configuration.isIgnored(largeContext, ObsoleteLayoutParamsDetector.ISSUE,
            largeLocation, ""));
}
 
Example 19
Source File: LintDriver.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
/** Check the classes in this project (and if applicable, in any library projects */
private void checkClasses(Project project, Project main) {
    List<File> files = project.getSubset();
    if (files != null) {
        checkIndividualClassFiles(project, main, files);
        return;
    }

    // We need to read in all the classes up front such that we can initialize
    // the parent chains (such that for example for a virtual dispatch, we can
    // also check the super classes).

    List<File> libraries = project.getJavaLibraries();
    List<ClassEntry> libraryEntries = ClassEntry.fromClassPath(mClient, libraries, true);

    List<File> classFolders = project.getJavaClassFolders();
    List<ClassEntry> classEntries;
    if (classFolders.isEmpty()) {
        String message = String.format("No `.class` files were found in project \"%1$s\", "
                + "so none of the classfile based checks could be run. "
                + "Does the project need to be built first?", project.getName());
        Location location = Location.create(project.getDir());
        mClient.report(new Context(this, project, main, project.getDir()),
                IssueRegistry.LINT_ERROR,
                project.getConfiguration(this).getSeverity(IssueRegistry.LINT_ERROR),
                location, message, TextFormat.RAW);
        classEntries = Collections.emptyList();
    } else {
        classEntries = ClassEntry.fromClassPath(mClient, classFolders, true);
    }

    // Actually run the detectors. Libraries should be called before the
    // main classes.
    runClassDetectors(Scope.JAVA_LIBRARIES, libraryEntries, project, main);

    if (mCanceled) {
        return;
    }

    runClassDetectors(Scope.CLASS_FILE, classEntries, project, main);
    runClassDetectors(Scope.ALL_CLASS_FILES, classEntries, project, main);
}
 
Example 20
Source File: LintCliXmlParser.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@NonNull
@Override
public Location getLocation(@NonNull XmlContext context, @NonNull Node node) {
    return Location.create(context.file, PositionXmlParser.getPosition(node));
}