Some common problems with Android FindBugs

Source: Internet
Author: User

FindBugs warning 26. There are 9 main types of problems.  1, Bug:hard coded reference to an absolute pathnamebug description: This code constructs a File object using a hard coded to an Absolute Pathname (This code contains the file object as an absolute pathname)   problem reason: hard-coded point to absolute path.  file prefile = new File (Preferences_file_full_path);  and private static final String Preferences_file_full_ PATH =         "/data/data/com.android.mms/shared_prefs/auto_download.xml";  Preferences_file_full_path declaration for final type, immutable. If the subsequent file path is changed, the reference is not available, but the path is not changed, the problem is introduced.   Workaround: Remove final.  2, Bug:Pattern:Dead Store to local variable bug description: This instruction assigns a value to a local variable, but th E value is not read or used in any subsequent instruction. Often, this indicates a error, because the value computed is never used. (the directive assigns a value to a local variable, but does not make any use of it afterwards.) Typically, this indicates an error because the value has never been used.   Problem Reason: The lock screen prompts dead store to Velocityx, parsing code  case MOTIONEVENT.ACTION_POINTER_1_UP statement defines the local variable Velocityx, and only if ( (Mdirectionflag && velocityx > 0) | | (!mdirectionflag && Velocityx < 0)                      Velocityx =-velocityx; The value is not used again. Therefore there is no need to assign a value, and the parsing code does not require the variable and can be removed.   WORKAROUND: Remove the definition of Velocityx variable and assign value.  3, bug:inconsistent synchronizationbug Description: The fields of this class appear to being accessed inconsistently with respect to synchronization. (unreasonable sync)   problem reason: According to the description Configloader file, Munlockappdatamap is locked for 46% of the time. The parsing code Munlockappdatamap is locked in the Checkunlockappconfigchange function. And the method public synchronized Boolean Checkunlockappconfigchange (context context) has no place to call.   Workaround: Remove the Synchronized keyword.  4, Bug:incorrect lazy initialization of static Field bug Description: This method contains a unsynchronized lazy Initiali Zation of a non-volatile static field. Because the compiler or processor may reorder instructions, threads is not guaranteed-see a completely initialized obj ECT, if the method can be called by multiple threads. (This method contains a non-volatile static field that does not synchronize lazy initialization.) Because the compiler or processor may rearrange the instructions, if the method can be called by more than one thread, the thread is not guaranteed to see a fully initialized object. )   Problem Reason: sinstanceis a static type, the clean () method may be called by multiple threads, and after Sinstance is judged to be non-empty, there may be a problem when empty null is cleared.   WORKAROUND: Add the keyword synchronized. public static synchronized void clean ()  5, bug:redundant Nullcheck of value Known to be Non-null bug Description: This method contains a redundant check of a known Non-null value against the constant null . (The method is judged to be empty for values that are not NULL.)   Problem Reason: Analysis findbugs error of this code  if (Minputstream = = null) {&NBSP;LOG.I (TAG, "Minputstream is null");  return;& nbsp;}  mdbuilder = Mdbuilderfactory.newdocumentbuilder ();  if (Minputstream! = null) { mdocument = Mdbuilder.parse (Minputstream);  }else { log.i (TAG, "minputstream = =  null");  return; }  minputstream if NULL, it is returned directly. There is no need to judge if (Minputstream! = null) later.   Solution: After minputstream sentence empty, will not be repeatedly sentenced to empty, the next if judgment in the Minputstream changed to Mdbuilder.  6, bug:should be a static inner Class bug description: This class was an inner class, but does does use its embedded Referen Ce to the object which created it.  this reference makes the instances of theClass larger, and may keep the reference to the Creator object alive longer than necessary.  if possible, the class should be made static. (If non-static members of the perimeter class are not accessed in the member class, it is recommended to use static member classes instead of additional space and time overhead.)   Problem Reason: the difference between a non-static member class and a static member class is that a non-static member class is an object, and a static member class is a class. A non-static member class can access any member of the perimeter class, but only if the perimeter class object must exist. Java requires extra maintenance of the relationships of non-static member classes and peripheral class objects. The analysis Code private class Icctext and private class Mediametadata {Do not have access to the non-static members of the perimeter class, so findbugs recommends setting it as a static type.   WORKAROUND: Change these 2 inner classes to a static type.  7, Bug:switch statement found where one case falls through to the next Casebug Description: This method contains a Switch state ment where one case branch would fall through to the next case. Usually you need to end this case with a break or return. (the switch statement executes the next branch after one of the branches executes.) Usually the case is followed by a break or return statement to jump out.   Problem Reason: Case MOTIONEVENT.ACTION_UP does not break after execution, will continue to walk case Motionevent.action_cancel branch. Parsing the code logic, when the finger is lifted, the lock screen icon needs to go back to the initial position, and the logic to return to the initial position is done in Action_cancel. The logic behind the ACTION_UP also needs to action_cancel the logic inside.   WORKAROUND: Pull the logic out of the action_cancel to make a function, and then call this function after the action_up logic to do the break operation.  8, Bug:unread fieldbug Description: This fiEld is never read.  consider removing it from the class. (Properties defined in the class have never been called, it is recommended to delete.)   Problem Reason: The member variable is defined in the class private hwviewproperty mcondition = null; The code has only the assignment operation mcondition = new Hwviewproperty (Mcontext, Value, Viewpropertytype.type_condition, mcallback); but there is no place to use this variable mcondition.   WORKAROUND: Remove the definition of mcondition and the assignment statement. Be aware, however, that mcondition = new Hwviewproperty (Mcontext,value, viewpropertytype.type_condition, Mcallback); Although the mcondition variable is not used later, but the new Hwviewproperty object calls the Hwviewproperty constructor method, actually does the function operation. Therefore, remove mcondition, but need to keep new Hwviewproperty (Mcontext,value, viewpropertytype.type_condition, mcallback);  9, BUG: Write to static field from instance Methodbug Description: This instance method writes to a static field. This was tricky to get correct if multiple instances was being manipulated, and generally bad practice. (Instance methods write static variables directly.)   Problem Reason: Sinstance is a static member variable of a class, the non-static method Unregistercallbaks directly assigns it, the non-static method is associated with the object, and the problem may occur when multiple objects are assigned to the variable at the same time.   WORKAROUND: Use the Get and set methods when using static member variables.

Some common problems with Android FindBugs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.