com.android.tools.lint.checks
Class StringFormatDetector

java.lang.Object
  extended by com.android.tools.lint.detector.api.Detector
      extended by com.android.tools.lint.detector.api.ResourceXmlDetector
          extended by com.android.tools.lint.checks.StringFormatDetector
All Implemented Interfaces:
com.android.tools.lint.detector.api.Detector.JavaScanner, com.android.tools.lint.detector.api.Detector.XmlScanner

public class StringFormatDetector
extends com.android.tools.lint.detector.api.ResourceXmlDetector
implements com.android.tools.lint.detector.api.Detector.JavaScanner

Check which looks for problems with formatting strings such as inconsistencies between translations or between string declaration and string usage in Java.

TODO: Handle Resources.getQuantityString as well


Nested Class Summary
 
Nested classes/interfaces inherited from class com.android.tools.lint.detector.api.Detector
com.android.tools.lint.detector.api.Detector.ClassScanner, com.android.tools.lint.detector.api.Detector.GradleScanner, com.android.tools.lint.detector.api.Detector.JavaScanner, com.android.tools.lint.detector.api.Detector.OtherFileScanner, com.android.tools.lint.detector.api.Detector.XmlScanner
 
Field Summary
static com.android.tools.lint.detector.api.Issue ARG_COUNT
          Whether formatting argument types are consistent across translations
static com.android.tools.lint.detector.api.Issue ARG_TYPES
          Whether the string format supplied in a call to String.format matches the format string
static com.android.tools.lint.detector.api.Issue INVALID
          Whether formatting strings are invalid
 
Fields inherited from interface com.android.tools.lint.detector.api.Detector.XmlScanner
ALL
 
Constructor Summary
StringFormatDetector()
          Constructs a new StringFormatDetector check
 
Method Summary
 void afterCheckProject(com.android.tools.lint.detector.api.Context context)
           
 boolean appliesTo(com.android.tools.lint.detector.api.Context context, java.io.File file)
           
 boolean appliesTo(com.android.resources.ResourceFolderType folderType)
           
 java.util.Collection<java.lang.String> getApplicableElements()
           
 java.util.List<java.lang.String> getApplicableMethodNames()
           
static boolean isLocaleSpecific(java.lang.String format)
          Determines whether the given String.format(String, Object...) formatting string is "locale dependent", meaning that its output depends on the locale.
 void visitElement(com.android.tools.lint.detector.api.XmlContext context, org.w3c.dom.Element element)
           
 void visitMethod(com.android.tools.lint.detector.api.JavaContext context, lombok.ast.AstVisitor visitor, lombok.ast.MethodInvocation node)
           
 
Methods inherited from class com.android.tools.lint.detector.api.ResourceXmlDetector
run
 
Methods inherited from class com.android.tools.lint.detector.api.Detector
afterCheckFile, afterCheckLibraryProject, appliesToFolder, appliesToResourceRefs, beforeCheckFile, beforeCheckLibraryProject, beforeCheckProject, checkCall, checkClass, checkInstruction, createJavaVisitor, getApplicableAsmNodeTypes, getApplicableAttributes, getApplicableCallNames, getApplicableCallOwners, getApplicableFiles, getApplicableNodeTypes, getSpeed, visitAttribute, visitBuildScript, visitDocument, visitElementAfter, visitResourceReference
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.android.tools.lint.detector.api.Detector.JavaScanner
appliesToResourceRefs, createJavaVisitor, getApplicableNodeTypes, visitResourceReference
 
Methods inherited from interface com.android.tools.lint.detector.api.Detector.XmlScanner
getApplicableAttributes, visitAttribute, visitDocument, visitElementAfter
 

Field Detail

INVALID

public static final com.android.tools.lint.detector.api.Issue INVALID
Whether formatting strings are invalid


ARG_COUNT

public static final com.android.tools.lint.detector.api.Issue ARG_COUNT
Whether formatting argument types are consistent across translations


ARG_TYPES

public static final com.android.tools.lint.detector.api.Issue ARG_TYPES
Whether the string format supplied in a call to String.format matches the format string

Constructor Detail

StringFormatDetector

public StringFormatDetector()
Constructs a new StringFormatDetector check

Method Detail

appliesTo

public boolean appliesTo(@NonNull
                         com.android.resources.ResourceFolderType folderType)
Overrides:
appliesTo in class com.android.tools.lint.detector.api.ResourceXmlDetector

appliesTo

public boolean appliesTo(@NonNull
                         com.android.tools.lint.detector.api.Context context,
                         @NonNull
                         java.io.File file)
Overrides:
appliesTo in class com.android.tools.lint.detector.api.ResourceXmlDetector

getApplicableElements

public java.util.Collection<java.lang.String> getApplicableElements()
Specified by:
getApplicableElements in interface com.android.tools.lint.detector.api.Detector.XmlScanner
Overrides:
getApplicableElements in class com.android.tools.lint.detector.api.Detector

visitElement

public void visitElement(@NonNull
                         com.android.tools.lint.detector.api.XmlContext context,
                         @NonNull
                         org.w3c.dom.Element element)
Specified by:
visitElement in interface com.android.tools.lint.detector.api.Detector.XmlScanner
Overrides:
visitElement in class com.android.tools.lint.detector.api.Detector

afterCheckProject

public void afterCheckProject(@NonNull
                              com.android.tools.lint.detector.api.Context context)
Overrides:
afterCheckProject in class com.android.tools.lint.detector.api.Detector

isLocaleSpecific

public static boolean isLocaleSpecific(@NonNull
                                       java.lang.String format)
Determines whether the given String.format(String, Object...) formatting string is "locale dependent", meaning that its output depends on the locale. This is the case if it for example references decimal numbers of dates and times.

Parameters:
format - the format string
Returns:
true if the format is locale sensitive, false otherwise

getApplicableMethodNames

public java.util.List<java.lang.String> getApplicableMethodNames()
Specified by:
getApplicableMethodNames in interface com.android.tools.lint.detector.api.Detector.JavaScanner
Overrides:
getApplicableMethodNames in class com.android.tools.lint.detector.api.Detector

visitMethod

public void visitMethod(@NonNull
                        com.android.tools.lint.detector.api.JavaContext context,
                        @Nullable
                        lombok.ast.AstVisitor visitor,
                        @NonNull
                        lombok.ast.MethodInvocation node)
Specified by:
visitMethod in interface com.android.tools.lint.detector.api.Detector.JavaScanner
Overrides:
visitMethod in class com.android.tools.lint.detector.api.Detector