@Beta
public class LintUtils
extends java.lang.Object
NOTE: This is not a public or final API; if you rely on this be prepared to adjust your code for the next tools release.
Modifier and Type | Field and Description |
---|---|
static boolean |
sTryPrefixLookup
Whether we should attempt to look up the prefix from the model.
|
Modifier and Type | Method and Description |
---|---|
static boolean |
assertionsEnabled()
Returns true if assertions are enabled
|
static java.lang.String |
computeResourceName(java.lang.String prefix,
java.lang.String name)
Computes a suggested name given a resource prefix and resource name
|
static java.lang.String |
computeResourceName(java.lang.String prefix,
java.lang.String name,
com.android.resources.ResourceFolderType folderType)
Computes a suggested name given a resource prefix and resource name
|
static java.lang.String |
computeResourcePrefix(com.android.builder.model.AndroidProject project)
Looks up the resource prefix for the given Gradle project, if possible
|
static com.android.sdklib.AndroidVersion |
convertVersion(com.android.builder.model.ApiVersion api,
com.android.sdklib.IAndroidTarget[] targets)
Convert an
ApiVersion to a AndroidVersion . |
static java.lang.String |
describeCounts(int errorCount,
int warningCount,
boolean comma)
Returns a description of counts for errors and warnings, such as
"5 errors and 2 warnings" or "3 errors" or "2 warnings"
|
static int |
editDistance(java.lang.String s,
java.lang.String t)
Computes the edit distance (number of insertions, deletions or substitutions
to edit one string into the other) between two strings.
|
static int |
editDistance(java.lang.String s,
java.lang.String t,
int max)
Computes the edit distance (number of insertions, deletions or substitutions
to edit one string into the other) between two strings.
|
static boolean |
endsWith(java.lang.String string,
java.lang.String suffix)
Case insensitive ends with
|
static java.lang.String |
findSubstring(java.lang.String string,
java.lang.String prefix,
java.lang.String suffix)
Looks for a certain string within a larger string, which should immediately follow
the given prefix and immediately precede the given suffix.
|
static java.lang.String |
formatList(java.util.List<java.lang.String> strings,
int maxItems)
Format a list of strings, and cut of the list at
maxItems if the
number of items are greater. |
static java.lang.String |
getAutoBoxedType(java.lang.String primitive) |
static java.lang.String |
getBaseName(java.lang.String fileName)
Returns the basename of the given filename, unless it's a dot-file such as ".svn".
|
static com.intellij.psi.PsiElement |
getCallName(com.intellij.psi.PsiCallExpression expression) |
static int |
getChildCount(org.w3c.dom.Node node)
Returns the number of children of the given node
|
static java.util.List<org.w3c.dom.Element> |
getChildren(org.w3c.dom.Node node)
Returns the children elements of the given node
|
static java.io.File |
getCommonParent(java.io.File file1,
java.io.File file2)
Computes the closest common parent path between two files.
|
static java.io.File |
getCommonParent(java.util.List<java.io.File> files)
Computes the shared parent among a set of files (which may be null).
|
static java.lang.CharSequence |
getEncodedString(byte[] data,
boolean createString)
Returns the String corresponding to the given data.
|
static java.lang.CharSequence |
getEncodedString(LintClient client,
java.io.File file,
boolean createString)
Returns the encoded String for the given file.
|
static java.lang.String |
getFieldName(java.lang.String styleName)
Returns the corresponding R field name for the given XML resource name
|
static java.util.List<java.lang.String> |
getFormattedParameters(java.lang.String format,
java.lang.String errorMessage)
Splits up the given message coming from a given string format (where the string
format follows the very specific convention of having only strings formatted exactly
with the format %n$s where n is between 1 and 9 inclusive, and each formatting parameter
appears exactly once, and in increasing order.
|
static java.util.List<com.android.ide.common.rendering.api.StyleResourceValue> |
getInheritedStyles(Project project,
LintClient client,
java.lang.String styleUrl) |
static java.lang.String |
getInternalMethodName(com.intellij.psi.PsiMethod method)
Returns the internal method name
|
static java.lang.String |
getLayoutName(java.io.File layoutFile)
Returns the layout resource name for the given layout file
|
static com.android.ide.common.resources.configuration.LocaleQualifier |
getLocale(java.lang.String parent)
Returns the locale for the given parent folder.
|
static com.android.ide.common.resources.configuration.LocaleQualifier |
getLocale(XmlContext context)
Returns the locale for the given context.
|
static java.lang.String |
getLocaleAndRegion(java.lang.String folderName)
Look up the locale and region from the given parent folder name and
return it as a combined string, such as "en", "en-rUS", b+eng-US, etc, or null if
no language is specified.
|
static org.objectweb.asm.tree.AbstractInsnNode |
getNextInstruction(org.objectweb.asm.tree.AbstractInsnNode node)
Returns the next instruction after to the given node, ignoring label and
line number nodes.
|
static int |
getNextOpcode(org.objectweb.asm.tree.AbstractInsnNode node)
Returns the next opcode after to the given node, ignoring label and line
number nodes
|
static org.objectweb.asm.tree.AbstractInsnNode |
getPrevInstruction(org.objectweb.asm.tree.AbstractInsnNode node)
Returns the previous instruction prior to the given node, ignoring label
and line number nodes.
|
static int |
getPrevOpcode(org.objectweb.asm.tree.AbstractInsnNode node)
Returns the previous opcode prior to the given node, ignoring label and
line number nodes
|
static java.lang.String |
getPrimitiveType(java.lang.String autoBoxedType) |
static java.util.List<com.android.ide.common.rendering.api.ResourceValue> |
getStyleAttributes(Project project,
LintClient client,
java.lang.String styleUrl,
java.lang.String namespace,
java.lang.String attribute)
Looks up the resource values for the given attribute given a style.
|
static Location |
guessGradleLocation(Project project)
Create a
Location for an error in the top level build.gradle file. |
static boolean |
idReferencesMatch(java.lang.String id1,
java.lang.String id2)
Returns true if the given two id references match.
|
static boolean |
isAnonymousClass(org.objectweb.asm.tree.ClassNode classNode)
Returns true if the given class node represents an anonymous inner class
|
static boolean |
isBitmapFile(java.io.File file)
Returns true if the given file represents a bitmap drawable file
|
static boolean |
isEditableTo(java.lang.String s,
java.lang.String t,
int max)
Returns true if the first string can be edited (Via insertions, deletions or
substitutions) into the second string in at most the given number of editing
operations.
|
static boolean |
isEnglishResource(XmlContext context,
boolean assumeForBase)
Check whether the given resource file is in an English locale
|
static boolean |
isFalseLiteral(com.intellij.psi.PsiElement element) |
static boolean |
isFileBasedResourceType(com.android.resources.ResourceType type)
Determine if the given type corresponds to a resource that has a unique
file
|
static boolean |
isImported(lombok.ast.Node compilationUnit,
java.lang.String fullyQualifiedName)
Deprecated.
Use PSI element hierarchies instead where type resolution is more directly
available (call
PsiImportStatementBase.resolve() ) |
static boolean |
isManifestFolder(java.io.File dir)
Returns true if the given directory is a lint manifest file directory.
|
static boolean |
isModelOlderThan(Project project,
int major,
int minor,
int micro)
Returns true if the given Gradle model is older than the given version number
|
static boolean |
isNullLiteral(com.intellij.psi.PsiElement element)
Returns true if the given element is the null literal
|
static boolean |
isRootElement(org.w3c.dom.Element element)
Returns true if the given element is the root element of its document
|
static boolean |
isSameResourceFile(java.io.File file1,
java.io.File file2)
Returns true if the given two paths point to the same logical resource file within
a source set.
|
static boolean |
isStaticInnerClass(org.objectweb.asm.tree.ClassNode classNode)
Returns true if the given class node represents a static inner class.
|
static boolean |
isString(com.intellij.psi.PsiType type) |
static boolean |
isTrueLiteral(com.intellij.psi.PsiElement element) |
static boolean |
isXmlFile(java.io.File file)
Returns true if the given file represents an XML file
|
static com.intellij.psi.PsiElement |
nextNonWhitespace(com.intellij.psi.PsiElement element) |
static com.intellij.psi.PsiElement |
prevNonWhitespace(com.intellij.psi.PsiElement element) |
static com.intellij.psi.PsiElement |
skipParentheses(com.intellij.psi.PsiElement element) |
static java.lang.Iterable<java.lang.String> |
splitPath(java.lang.String path)
Splits the given path into its individual parts, attempting to be
tolerant about path separators (: or ;).
|
static boolean |
startsWith(java.lang.String string,
java.lang.String prefix,
int offset)
Case insensitive starts with
|
static java.lang.String |
stripIdPrefix(java.lang.String id)
Returns the given id without an
@id/ or @+id prefix |
public static boolean sTryPrefixLookup
This is public such that code which for example syncs to a new gradle model can reset it.
@NonNull public static java.lang.String getInternalMethodName(@NonNull com.intellij.psi.PsiMethod method)
@Nullable public static com.intellij.psi.PsiElement getCallName(@NonNull com.intellij.psi.PsiCallExpression expression)
@NonNull public static java.lang.String formatList(@NonNull java.util.List<java.lang.String> strings, int maxItems)
maxItems
if the
number of items are greater.strings
- the list of strings to print out as a comma separated listmaxItems
- the maximum number of items to printpublic static boolean isFileBasedResourceType(@NonNull com.android.resources.ResourceType type)
type
- the resource type to checkpublic static boolean isXmlFile(@NonNull java.io.File file)
file
- the file to be checkedpublic static boolean isBitmapFile(@NonNull java.io.File file)
file
- the file to be checkedpublic static boolean endsWith(@NonNull java.lang.String string, @NonNull java.lang.String suffix)
string
- the string to be tested whether it ends with the given
suffixsuffix
- the suffix to checkstring
ends with suffix
,
case-insensitively.public static boolean startsWith(@NonNull java.lang.String string, @NonNull java.lang.String prefix, int offset)
string
- the string to be tested whether it starts with the given prefixprefix
- the prefix to checkoffset
- the offset to start checking withstring
starts with prefix
,
case-insensitively.public static java.lang.String getBaseName(@NonNull java.lang.String fileName)
fileName
- the file name to extract the basename from@NonNull public static java.lang.String describeCounts(int errorCount, int warningCount, boolean comma)
errorCount
- the count of errorswarningCount
- the count of warningscomma
- if true, use a comma to separate messages, otherwise "and"@NonNull public static java.util.List<org.w3c.dom.Element> getChildren(@NonNull org.w3c.dom.Node node)
node
- the parent nodepublic static int getChildCount(@NonNull org.w3c.dom.Node node)
node
- the parent nodepublic static boolean isRootElement(org.w3c.dom.Element element)
element
- the element to testpublic static java.lang.String getFieldName(@NonNull java.lang.String styleName)
styleName
- the XML name@NonNull public static java.lang.String stripIdPrefix(@Nullable java.lang.String id)
@id/
or @+id
prefixid
- the id to strippublic static boolean idReferencesMatch(@Nullable java.lang.String id1, @Nullable java.lang.String id2)
@+id/foo == @id/foo
.id1
- the first id to compareid2
- the second id to comparepublic static boolean isEditableTo(@NonNull java.lang.String s, @NonNull java.lang.String t, int max)
s
- the first string to comparet
- the second string to comparemax
- the maximum number of edit operations allowedpublic static int editDistance(@NonNull java.lang.String s, @NonNull java.lang.String t)
See http://en.wikipedia.org/wiki/Levenshtein_distance for details.
s
- the first string to comparet
- the second string to comparepublic static int editDistance(@NonNull java.lang.String s, @NonNull java.lang.String t, int max)
See http://en.wikipedia.org/wiki/Levenshtein_distance for details.
s
- the first string to comparet
- the second string to comparemax
- the maximum edit distance that we care about; if for example the string length
delta is greater than this we don't bother computing the exact edit distance since
the caller has indicated they're not interested in the resultmax
parameterpublic static boolean assertionsEnabled()
public static java.lang.String getLayoutName(java.io.File layoutFile)
layoutFile
- the file pointing to the layout@layout
prefixpublic static java.lang.Iterable<java.lang.String> splitPath(@NonNull java.lang.String path)
c:\foo\bar:\other
, though of course these are to
be avoided if possible.path
- the path variable to split, which can use both : and ; as
path separators.@Nullable public static java.io.File getCommonParent(@NonNull java.util.List<java.io.File> files)
files
- the set of files to be checked@Nullable public static java.io.File getCommonParent(@NonNull java.io.File file1, @NonNull java.io.File file2)
file1
- the first file to be comparedfile2
- the second file to be compared@NonNull public static java.lang.CharSequence getEncodedString(@NonNull LintClient client, @NonNull java.io.File file, boolean createString) throws java.io.IOException
Files.toString(file, Charsets.UTF8
, but if there's a UTF byte order mark
(for UTF8, UTF_16 or UTF_16LE), use that instead.client
- the client to use for I/O operationsfile
- the file to read fromcreateString
- If true, create a String
instead of a general CharSequence
java.io.IOException
- if the file cannot be read properly@NonNull public static java.lang.CharSequence getEncodedString(@Nullable byte[] data, boolean createString)
new String(data)
, but if there's a UTF byte order mark
(for UTF8, UTF_16 or UTF_16LE), use that instead.
NOTE: For XML files, there is the additional complication that there
could be a encoding=
attribute in the prologue. For those files,
use PositionXmlParser.getXmlString(byte[])
instead.
data
- the byte array to construct the string fromcreateString
- If true, create a String
instead of a general CharSequence
public static boolean isStaticInnerClass(@NonNull org.objectweb.asm.tree.ClassNode classNode)
classNode
- the inner class to be checkedpublic static boolean isAnonymousClass(@NonNull org.objectweb.asm.tree.ClassNode classNode)
classNode
- the class to be checkedpublic static int getPrevOpcode(@NonNull org.objectweb.asm.tree.AbstractInsnNode node)
node
- the node to look up the previous opcode forOpcodes.NOP
if no previous node
was found@Nullable public static org.objectweb.asm.tree.AbstractInsnNode getPrevInstruction(@NonNull org.objectweb.asm.tree.AbstractInsnNode node)
node
- the node to look up the previous instruction forpublic static int getNextOpcode(@NonNull org.objectweb.asm.tree.AbstractInsnNode node)
node
- the node to look up the next opcode forOpcodes.NOP
if no next node was found@Nullable public static org.objectweb.asm.tree.AbstractInsnNode getNextInstruction(@NonNull org.objectweb.asm.tree.AbstractInsnNode node)
node
- the node to look up the next node forpublic static boolean isManifestFolder(java.io.File dir)
dir
- the directory to check@Nullable public static java.lang.String getLocaleAndRegion(@NonNull java.lang.String folderName)
folderName
- the folder name@Deprecated public static boolean isImported(@Nullable lombok.ast.Node compilationUnit, @NonNull java.lang.String fullyQualifiedName)
PsiImportStatementBase.resolve()
)compilationUnit
- the compilation unitfullyQualifiedName
- the fully qualified class name@Nullable public static java.util.List<com.android.ide.common.rendering.api.ResourceValue> getStyleAttributes(@NonNull Project project, @NonNull LintClient client, @NonNull java.lang.String styleUrl, @NonNull java.lang.String namespace, @NonNull java.lang.String attribute)
@Nullable public static java.util.List<com.android.ide.common.rendering.api.StyleResourceValue> getInheritedStyles(@NonNull Project project, @NonNull LintClient client, @NonNull java.lang.String styleUrl)
public static boolean isSameResourceFile(@Nullable java.io.File file1, @Nullable java.io.File file2)
file1
- the first file to comparefile2
- the second file to compare@Nullable public static java.lang.String computeResourcePrefix(@Nullable com.android.builder.model.AndroidProject project)
public static java.lang.String computeResourceName(@NonNull java.lang.String prefix, @NonNull java.lang.String name)
public static java.lang.String computeResourceName(@NonNull java.lang.String prefix, @NonNull java.lang.String name, @Nullable com.android.resources.ResourceFolderType folderType)
@NonNull public static com.android.sdklib.AndroidVersion convertVersion(@NonNull com.android.builder.model.ApiVersion api, @Nullable com.android.sdklib.IAndroidTarget[] targets)
ApiVersion
to a AndroidVersion
. The chief problem here is that the ApiVersion
, when using a codename, will not encode the
corresponding API level (it just reflects the string entered by the user in the gradle file)
so we perform a search here (since lint really wants to know the actual numeric API level)api
- the api version to converttargets
- if known, the installed targets (used to resolve platform codenames, only
needed to resolve platforms newer than the tools since SdkVersionInfo
knows the rest)public static boolean isModelOlderThan(@NonNull Project project, int major, int minor, int micro)
@Nullable public static java.lang.String findSubstring(@NonNull java.lang.String string, @Nullable java.lang.String prefix, @Nullable java.lang.String suffix)
string
- the full string to searchprefix
- the optional prefix to followsuffix
- the optional suffix to precede@NonNull public static java.util.List<java.lang.String> getFormattedParameters(@NonNull java.lang.String format, @NonNull java.lang.String errorMessage)
format
- the format string responsible for creating the error messageerrorMessage
- an error message formatted with the format string@Nullable public static com.android.ide.common.resources.configuration.LocaleQualifier getLocale(@NonNull java.lang.String parent)
parent
- the name of the parent folder@Nullable public static com.android.ide.common.resources.configuration.LocaleQualifier getLocale(@NonNull XmlContext context)
context
- the context to look up the locale forpublic static boolean isEnglishResource(@NonNull XmlContext context, boolean assumeForBase)
context
- the XML context for the resource fileassumeForBase
- whether the base folder (e.g. no locale specified) should be
treated as Englishpublic static Location guessGradleLocation(@NonNull Project project)
Location
for an error in the top level build.gradle file.
This is necessary when we're doing an analysis based on the Gradle interpreted model,
not from parsing Gradle files - and the model doesn't provide source positions.project
- the project containing the gradle file being analyzedpublic static boolean isNullLiteral(@Nullable com.intellij.psi.PsiElement element)
element
- the element to checkpublic static boolean isTrueLiteral(@Nullable com.intellij.psi.PsiElement element)
public static boolean isFalseLiteral(@Nullable com.intellij.psi.PsiElement element)
@Nullable public static com.intellij.psi.PsiElement skipParentheses(@Nullable com.intellij.psi.PsiElement element)
@Nullable public static com.intellij.psi.PsiElement nextNonWhitespace(@Nullable com.intellij.psi.PsiElement element)
@Nullable public static com.intellij.psi.PsiElement prevNonWhitespace(@Nullable com.intellij.psi.PsiElement element)
public static boolean isString(@NonNull com.intellij.psi.PsiType type)
@Nullable public static java.lang.String getAutoBoxedType(@NonNull java.lang.String primitive)
@Nullable public static java.lang.String getPrimitiveType(@NonNull java.lang.String autoBoxedType)