Search in sources :

Example 31 with NonNull

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

the class LayoutConsistencyDetector method chainLocations.

@NonNull
private static Location chainLocations(@NonNull List<Location> locations) {
    assert !locations.isEmpty();
    // Sort locations by the file parent folders
    if (locations.size() > 1) {
        Collections.sort(locations, new Comparator<Location>() {

            @Override
            public int compare(Location location1, Location location2) {
                File file1 = location1.getFile();
                File file2 = location2.getFile();
                String folder1 = file1.getParentFile().getName();
                String folder2 = file2.getParentFile().getName();
                return folder1.compareTo(folder2);
            }
        });
        // Chain locations together
        Iterator<Location> iterator = locations.iterator();
        assert iterator.hasNext();
        Location prev = iterator.next();
        while (iterator.hasNext()) {
            Location next = iterator.next();
            prev.setSecondary(next);
            prev = next;
        }
    }
    return locations.get(0);
}
Also used : File(java.io.File) Location(com.android.tools.klint.detector.api.Location) NonNull(com.android.annotations.NonNull)

Example 32 with NonNull

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

the class DomPsiParser method getLocation.

@NonNull
@Override
public Location getLocation(@NonNull XmlContext context, @NonNull Node node, int startDelta, int endDelta) {
    TextRange textRange = DomPsiConverter.getTextRange(node);
    Position start = new DefaultPosition(-1, -1, textRange.getStartOffset() + startDelta);
    Position end = new DefaultPosition(-1, -1, textRange.getStartOffset() + endDelta);
    return Location.create(context.file, start, end);
}
Also used : Position(com.android.tools.klint.detector.api.Position) DefaultPosition(com.android.tools.klint.detector.api.DefaultPosition) DefaultPosition(com.android.tools.klint.detector.api.DefaultPosition) TextRange(com.intellij.openapi.util.TextRange) NonNull(com.android.annotations.NonNull)

Example 33 with NonNull

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

the class IntellijLintClient method getConfiguration.

@NonNull
@Override
public Configuration getConfiguration(@NonNull com.android.tools.klint.detector.api.Project project, @Nullable final LintDriver driver) {
    if (project.isGradleProject() && project.isAndroidProject() && !project.isLibrary()) {
        AndroidProject model = project.getGradleProjectModel();
        if (model != null) {
            try {
                LintOptions lintOptions = model.getLintOptions();
                final Map<String, Integer> overrides = lintOptions.getSeverityOverrides();
                if (overrides != null && !overrides.isEmpty()) {
                    return new DefaultConfiguration(this, project, null) {

                        @NonNull
                        @Override
                        public Severity getSeverity(@NonNull Issue issue) {
                            Integer severity = overrides.get(issue.getId());
                            if (severity != null) {
                                switch(severity.intValue()) {
                                    case LintOptions.SEVERITY_FATAL:
                                        return Severity.FATAL;
                                    case LintOptions.SEVERITY_ERROR:
                                        return Severity.ERROR;
                                    case LintOptions.SEVERITY_WARNING:
                                        return Severity.WARNING;
                                    case LintOptions.SEVERITY_INFORMATIONAL:
                                        return Severity.INFORMATIONAL;
                                    case LintOptions.SEVERITY_IGNORE:
                                    default:
                                        return Severity.IGNORE;
                                }
                            }
                            // This is a LIST lookup. I should make this faster!
                            if (!getIssues().contains(issue) && (driver == null || !driver.isCustomIssue(issue))) {
                                return Severity.IGNORE;
                            }
                            return super.getSeverity(issue);
                        }
                    };
                }
            } catch (Exception e) {
                LOG.error(e);
            }
        }
    }
    return new DefaultConfiguration(this, project, null) {

        @Override
        public boolean isEnabled(@NonNull Issue issue) {
            if (getIssues().contains(issue) && super.isEnabled(issue)) {
                return true;
            }
            return driver != null && driver.isCustomIssue(issue);
        }
    };
}
Also used : NonNull(com.android.annotations.NonNull) LintOptions(com.android.builder.model.LintOptions) AndroidProject(com.android.builder.model.AndroidProject) IOException(java.io.IOException) NonNull(com.android.annotations.NonNull)

Example 34 with NonNull

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

the class IntellijLintProject method createForSingleFile.

/**
   * Creates a project for a single file. Also optionally creates a main project for the file, if applicable.
   *
   * @param client the lint client
   * @param file the file to create a project for
   * @param module the module to create a project for
   * @return a project for the file, as well as a project (or null) for the main Android module
   */
@NonNull
public static Pair<Project, Project> createForSingleFile(@NonNull IntellijLintClient client, @Nullable VirtualFile file, @NonNull Module module) {
    // TODO: Can make this method even more lightweight: we don't need to initialize anything in the project (source paths etc)
    // other than the metadata necessary for this file's type
    LintModuleProject project = createModuleProject(client, module);
    LintModuleProject main = null;
    Map<Project, Module> projectMap = Maps.newHashMap();
    if (project != null) {
        project.setDirectLibraries(Collections.<Project>emptyList());
        if (file != null) {
            project.addFile(VfsUtilCore.virtualToIoFile(file));
        }
        projectMap.put(project, module);
        // using the library, not "1" (the default for a module without a manifest)
        if (!project.isAndroidProject()) {
            Module androidModule = findAndroidModule(module);
            if (androidModule != null) {
                main = createModuleProject(client, androidModule);
                if (main != null) {
                    projectMap.put(main, androidModule);
                    main.setDirectLibraries(Collections.<Project>singletonList(project));
                }
            }
        }
    }
    client.setModuleMap(projectMap);
    //noinspection ConstantConditions
    return Pair.<Project, Project>create(project, main);
}
Also used : Project(com.android.tools.klint.detector.api.Project) Module(com.intellij.openapi.module.Module) NonNull(com.android.annotations.NonNull)

Example 35 with NonNull

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

the class IntellijLintUtils method getUastLocation.

/**
   * Gets the location of the given element
   *
   * @param file the file containing the location
   * @param element the element to look up the location for
   * @return the location of the given element
   */
@NonNull
public static Location getUastLocation(@NonNull File file, @NonNull UElement element) {
    //noinspection ConstantConditions
    PsiFile containingPsiFile = UastUtils.getContainingFile(element).getPsi();
    assert containingPsiFile.getVirtualFile() == null || FileUtil.filesEqual(VfsUtilCore.virtualToIoFile(containingPsiFile.getVirtualFile()), file);
    if (element instanceof UClass) {
        // Point to the name rather than the beginning of the javadoc
        UClass clz = (UClass) element;
        UElement nameIdentifier = clz.getUastAnchor();
        if (nameIdentifier != null) {
            element = nameIdentifier;
        }
    }
    TextRange textRange = null;
    PsiElement psi = element.getPsi();
    if (psi != null) {
        textRange = psi.getTextRange();
    } else if (element instanceof UElementWithLocation) {
        UElementWithLocation elementWithLocation = (UElementWithLocation) element;
        textRange = new TextRange(elementWithLocation.getStartOffset(), elementWithLocation.getEndOffset());
    }
    if (textRange == null) {
        return Location.NONE;
    }
    Position start = new DefaultPosition(-1, -1, textRange.getStartOffset());
    Position end = new DefaultPosition(-1, -1, textRange.getEndOffset());
    return Location.create(file, start, end);
}
Also used : UElementWithLocation(org.jetbrains.uast.psi.UElementWithLocation) TextRange(com.intellij.openapi.util.TextRange) NonNull(com.android.annotations.NonNull)

Aggregations

NonNull (com.android.annotations.NonNull)113 File (java.io.File)38 TextRange (com.intellij.openapi.util.TextRange)13 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)11 ImmutableList (com.google.common.collect.ImmutableList)8 Nullable (com.android.annotations.Nullable)7 DefaultPosition (com.android.tools.klint.detector.api.DefaultPosition)5 Position (com.android.tools.klint.detector.api.Position)5 Matcher (java.util.regex.Matcher)5 Pattern (java.util.regex.Pattern)5 SdkConstants (com.android.SdkConstants)4 OutputFile (com.android.build.OutputFile)4 Issue (com.android.tools.klint.detector.api.Issue)4 Position (com.android.tools.lint.detector.api.Position)4 FileUtils (com.android.utils.FileUtils)4 Splitter (com.google.common.base.Splitter)4 Module (com.intellij.openapi.module.Module)4 StringReader (java.io.StringReader)4 Node (org.w3c.dom.Node)4