|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.android.tools.lint.detector.api.LintUtils
@Beta public class LintUtils
Useful utility methods related to lint.
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.
Field Summary | |
---|---|
static boolean |
sTryPrefixLookup
Whether we should attempt to look up the prefix from the model. |
Method Summary | |
---|---|
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 |
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 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 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 |
getBaseName(java.lang.String fileName)
Returns the basename of the given filename, unless it's a dot-file such as ".svn". |
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.String |
getEncodedString(byte[] data)
Returns the String corresponding to the given data. |
static java.lang.String |
getEncodedString(LintClient client,
java.io.File file)
Returns the encoded String for the given file. |
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 |
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.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 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 |
isEnglishResource(XmlContext context,
boolean assumeForBase)
Check whether the given resource file is in an English locale |
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)
Returns true if the given class (specified by a fully qualified class name) name is imported in the given compilation unit either through a fully qualified import or by a wildcard import. |
static boolean |
isManifestFolder(java.io.File dir)
Returns true if the given directory is a lint manifest file directory. |
static boolean |
isModelOlderThan(com.android.builder.model.AndroidProject project,
int major,
int minor,
int micro)
Returns true if the given Gradle model is older than the given version number |
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 |
isXmlFile(java.io.File file)
Returns true if the given file represents an XML file |
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 |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static boolean sTryPrefixLookup
This is public such that code which for example syncs to a new gradle model can reset it.
Method Detail |
---|
@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 print
public static boolean isFileBasedResourceType(@NonNull com.android.resources.ResourceType type)
type
- the resource type to check
public static boolean isXmlFile(@NonNull java.io.File file)
file
- the file to be checked
public static boolean isBitmapFile(@NonNull java.io.File file)
file
- the file to be checked
public 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 check
string
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 with
string
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.util.List<org.w3c.dom.Element> getChildren(@NonNull org.w3c.dom.Node node)
node
- the parent node
public static int getChildCount(@NonNull org.w3c.dom.Node node)
node
- the parent node
public static boolean isRootElement(org.w3c.dom.Element element)
element
- the element to test
@NonNull public static java.lang.String stripIdPrefix(@Nullable java.lang.String id)
@id/
or @+id
prefix
id
- the id to strip
public 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 compare
public 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 compare
public 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.String getEncodedString(@NonNull LintClient client, @NonNull java.io.File file) 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 from
java.io.IOException
- if the file cannot be read properly@NonNull public static java.lang.String getEncodedString(@Nullable byte[] data)
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 from
public static boolean isStaticInnerClass(@NonNull org.objectweb.asm.tree.ClassNode classNode)
classNode
- the inner class to be checked
public static boolean isAnonymousClass(@NonNull org.objectweb.asm.tree.ClassNode classNode)
classNode
- the class to be checked
public static int getPrevOpcode(@NonNull org.objectweb.asm.tree.AbstractInsnNode node)
node
- the node to look up the previous opcode for
Opcodes.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 for
public static int getNextOpcode(@NonNull org.objectweb.asm.tree.AbstractInsnNode node)
node
- the node to look up the next opcode for
Opcodes.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 for
public 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
public static boolean isImported(@Nullable lombok.ast.Node compilationUnit, @NonNull java.lang.String fullyQualifiedName)
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)
@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(@Nullable com.android.builder.model.AndroidProject 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 for
public 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 English
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |