Search in sources :

Example 11 with NonNull

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

the class Project method getGradleBuildScripts.

/**
     * Returns the Gradle build script files configured for this project, if any
     *
     * @return the Gradle files, if any
     */
@NonNull
public List<File> getGradleBuildScripts() {
    if (mGradleFiles == null) {
        if (isGradleProject()) {
            mGradleFiles = Lists.newArrayListWithExpectedSize(2);
            File build = new File(mDir, SdkConstants.FN_BUILD_GRADLE);
            if (build.exists()) {
                mGradleFiles.add(build);
            }
            File settings = new File(mDir, SdkConstants.FN_SETTINGS_GRADLE);
            if (settings.exists()) {
                mGradleFiles.add(settings);
            }
        } else {
            mGradleFiles = Collections.emptyList();
        }
    }
    return mGradleFiles;
}
Also used : OutputFile(com.android.build.OutputFile) File(java.io.File) NonNull(com.android.annotations.NonNull)

Example 12 with NonNull

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

the class Project method getJavaLibraries.

/**
     * Returns the list of Java libraries (typically .jar files) that this
     * project depends on. Note that this refers to jar libraries, not Android
     * library projects which are processed in a separate pass with their own
     * source and class folders.
     *
     * @param includeProvided If true, included provided libraries too (libraries
     *                        that are not packaged with the app, but are provided
     *                        for compilation purposes and are assumed to be present
     *                        in the running environment)
     * @return a list of .jar files (or class folders) that this project depends
     *         on.
     */
@NonNull
public List<File> getJavaLibraries(boolean includeProvided) {
    if (includeProvided) {
        if (mJavaLibraries == null) {
            // AOSP builds already merge libraries and class folders into
            // the single classes.jar file, so these have already been processed
            // in getJavaClassFolders.
            mJavaLibraries = mClient.getJavaLibraries(this, true);
            if (isAospBuildEnvironment()) {
                // We still need to add the support-annotations library in the case of AOSP
                File out = new File(getAospTop(), "out");
                String relative = "target/common/obj/JAVA_LIBRARIES/" + "android-support-annotations_intermediates/classes";
                File annotationsDir = new File(out, relative.replace('/', File.separatorChar));
                if (annotationsDir.exists()) {
                    mJavaLibraries.add(annotationsDir);
                }
            }
        }
        return mJavaLibraries;
    } else {
        if (mNonProvidedJavaLibraries == null) {
            mNonProvidedJavaLibraries = mClient.getJavaLibraries(this, false);
        }
        return mNonProvidedJavaLibraries;
    }
}
Also used : OutputFile(com.android.build.OutputFile) File(java.io.File) NonNull(com.android.annotations.NonNull)

Example 13 with NonNull

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

the class LintUtils method getChildren.

/**
     * Returns the children elements of the given node
     *
     * @param node the parent node
     * @return a list of element children, never null
     */
@NonNull
public static List<Element> getChildren(@NonNull Node node) {
    NodeList childNodes = node.getChildNodes();
    List<Element> children = new ArrayList<Element>(childNodes.getLength());
    for (int i = 0, n = childNodes.getLength(); i < n; i++) {
        Node child = childNodes.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            children.add((Element) child);
        }
    }
    return children;
}
Also used : NodeList(org.w3c.dom.NodeList) UElement(org.jetbrains.uast.UElement) Element(org.w3c.dom.Element) AbstractInsnNode(org.jetbrains.org.objectweb.asm.tree.AbstractInsnNode) ClassNode(org.jetbrains.org.objectweb.asm.tree.ClassNode) FieldNode(org.jetbrains.org.objectweb.asm.tree.FieldNode) Node(org.w3c.dom.Node) NonNull(com.android.annotations.NonNull)

Example 14 with NonNull

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

the class Location method create.

/**
     * Creates a new location for the given file, with the given contents, for
     * the given line number.
     *
     * @param file the file containing the location
     * @param contents the current contents of the file
     * @param line the line number (0-based) for the position
     * @param patternStart an optional pattern to search for from the line
     *            match; if found, adjust the column and offsets to begin at the
     *            pattern start
     * @param patternEnd an optional pattern to search for behind the start
     *            pattern; if found, adjust the end offset to match the end of
     *            the pattern
     * @param hints optional additional information regarding the pattern search
     * @return a new location
     */
@NonNull
public static Location create(@NonNull File file, @NonNull String contents, int line, @Nullable String patternStart, @Nullable String patternEnd, @Nullable SearchHints hints) {
    int currentLine = 0;
    int offset = 0;
    while (currentLine < line) {
        offset = contents.indexOf('\n', offset);
        if (offset == -1) {
            return create(file);
        }
        currentLine++;
        offset++;
    }
    if (line == currentLine) {
        if (patternStart != null) {
            SearchDirection direction = SearchDirection.NEAREST;
            if (hints != null) {
                direction = hints.mDirection;
            }
            int index;
            if (direction == SearchDirection.BACKWARD) {
                index = findPreviousMatch(contents, offset, patternStart, hints);
                line = adjustLine(contents, line, offset, index);
            } else if (direction == SearchDirection.EOL_BACKWARD) {
                int lineEnd = contents.indexOf('\n', offset);
                if (lineEnd == -1) {
                    lineEnd = contents.length();
                }
                index = findPreviousMatch(contents, lineEnd, patternStart, hints);
                line = adjustLine(contents, line, offset, index);
            } else if (direction == SearchDirection.FORWARD) {
                index = findNextMatch(contents, offset, patternStart, hints);
                line = adjustLine(contents, line, offset, index);
            } else {
                assert direction == SearchDirection.NEAREST || direction == SearchDirection.EOL_NEAREST;
                int lineEnd = contents.indexOf('\n', offset);
                if (lineEnd == -1) {
                    lineEnd = contents.length();
                }
                offset = lineEnd;
                int before = findPreviousMatch(contents, offset, patternStart, hints);
                int after = findNextMatch(contents, offset, patternStart, hints);
                if (before == -1) {
                    index = after;
                    line = adjustLine(contents, line, offset, index);
                } else if (after == -1) {
                    index = before;
                    line = adjustLine(contents, line, offset, index);
                } else {
                    int newLinesBefore = 0;
                    for (int i = before; i < offset; i++) {
                        if (contents.charAt(i) == '\n') {
                            newLinesBefore++;
                        }
                    }
                    int newLinesAfter = 0;
                    for (int i = offset; i < after; i++) {
                        if (contents.charAt(i) == '\n') {
                            newLinesAfter++;
                        }
                    }
                    if (newLinesBefore < newLinesAfter || newLinesBefore == newLinesAfter && offset - before < after - offset) {
                        index = before;
                        line = adjustLine(contents, line, offset, index);
                    } else {
                        index = after;
                        line = adjustLine(contents, line, offset, index);
                    }
                }
            }
            if (index != -1) {
                int lineStart = contents.lastIndexOf('\n', index);
                if (lineStart == -1) {
                    lineStart = 0;
                } else {
                    // was pointing to the previous line's CR, not line start
                    lineStart++;
                }
                int column = index - lineStart;
                if (patternEnd != null) {
                    int end = contents.indexOf(patternEnd, offset + patternStart.length());
                    if (end != -1) {
                        return new Location(file, new DefaultPosition(line, column, index), new DefaultPosition(line, -1, end + patternEnd.length()));
                    }
                } else if (hints != null && (hints.isJavaSymbol() || hints.isWholeWord())) {
                    if (hints.isConstructor() && contents.startsWith(SUPER_KEYWORD, index)) {
                        patternStart = SUPER_KEYWORD;
                    }
                    return new Location(file, new DefaultPosition(line, column, index), new DefaultPosition(line, column + patternStart.length(), index + patternStart.length()));
                }
                return new Location(file, new DefaultPosition(line, column, index), new DefaultPosition(line, column, index + patternStart.length()));
            }
        }
        Position position = new DefaultPosition(line, -1, offset);
        return new Location(file, position, position);
    }
    return create(file);
}
Also used : SourcePosition(com.android.ide.common.blame.SourcePosition) NonNull(com.android.annotations.NonNull)

Example 15 with NonNull

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

the class Location method create.

/**
     * Creates a new location for the given file, with the given contents, for
     * the given offset range.
     *
     * @param file the file containing the location
     * @param contents the current contents of the file
     * @param startOffset the starting offset
     * @param endOffset the ending offset
     * @return a new location
     */
@NonNull
public static Location create(@NonNull File file, @Nullable String contents, int startOffset, int endOffset) {
    if (startOffset < 0 || endOffset < startOffset) {
        throw new IllegalArgumentException("Invalid offsets");
    }
    if (contents == null) {
        return new Location(file, new DefaultPosition(-1, -1, startOffset), new DefaultPosition(-1, -1, endOffset));
    }
    int size = contents.length();
    endOffset = Math.min(endOffset, size);
    startOffset = Math.min(startOffset, endOffset);
    Position start = null;
    int line = 0;
    int lineOffset = 0;
    char prev = 0;
    for (int offset = 0; offset <= size; offset++) {
        if (offset == startOffset) {
            start = new DefaultPosition(line, offset - lineOffset, offset);
        }
        if (offset == endOffset) {
            Position end = new DefaultPosition(line, offset - lineOffset, offset);
            return new Location(file, start, end);
        }
        char c = contents.charAt(offset);
        if (c == '\n') {
            lineOffset = offset + 1;
            if (prev != '\r') {
                line++;
            }
        } else if (c == '\r') {
            line++;
            lineOffset = offset + 1;
        }
        prev = c;
    }
    return create(file);
}
Also used : SourcePosition(com.android.ide.common.blame.SourcePosition) 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