Java Hints
APIs
- Exporting non-public type through public API
- Checks that return types and parameter types of allpublic methods and all types of public fields are publicly available from other packages.Having private or package private types in a package API is useless.
- Utility class with visible constructor
- Finds out classes containing only static elements with public or protected constructors.
- Utility class without constructor
- Finds out classes containing only static elements and still being instantiable.
Assignment Issues
- Assignment replaceable with operator-assignment
- Reports instances of assignment operations which can be replaced by operator-assignment. Code using operator-assignment may be clearer, and theoretically more performant. Since NetBeans 6.9
- Assignment to catch-block parameter
- Reports any instances of assignment to variable declared as a catch block parameter. While occasionally intended, this construct can be confusing. Since NetBeans 6.9
- Assignment to for-loop parameter
- Reports any instances of assignment to variable declared in a for statement in the body of that statement. It also reports any attempt to increment or decrement the variable. While occasionally intended, this construct can be extremely confusing, and is often the result of a typo. Since NetBeans 6.9
- Assignment to method parameter
- Reports any instances of assignment to a variable declared as a method parameter. It also reports any attempt to increment or decrement the variable. While occasionally intended, this construct can be extremely confusing, and is often the result of a typo. Since NetBeans 6.9
- Nested assignment
- Reports any instances of assignment expressions nested inside other expressions. While admirably terse, such expressions may be confusing, and violate the general design principle that a given construct should do precisely one thing. Since NetBeans 6.9
- Value of increment/decrement expression used
- Reports any instances of increment or decrement expressions nested inside other expressions. While admirably terse, such expressions may be confusing, and violate the general design principle that a given construct should do precisely one thing. Since NetBeans 6.9
Bitwise Operations
- Incompatible mask
- This inspection reports any instances of bitwise mask expressions which are guaranteed to evaluate to false. Expressions checked are of the form (var & constant1) == constant2 or (var | constant1) == constant2, where constant1and constant2 are incompatible bitmask constants. Since NetBeans 6.9
- Pointless bitwise expression
- This inspection reports any instances of pointless bitwise expressions. Such expressions include anding with zero, oring by zero, and shift by zero. Such expressions may be the result of automated refactorings not completely followed through to completion, and in any case are unlikely to be what the developer intended to do. Since NetBeans 6.9
- Shift operation outside of the reasonable range
- This inspection reports any instances of shift operations where the value shifted by is constant and outside of the reasonable range. Integer shift operations outside of the range 0..31 and long shift operations outside of therange 0..63 are reported. Shifting by negative or overly large values is almost certainly a coding error. Since NetBeans 6.9
Braces
- Do-While Loops Must Use Braces
- Do-While Loops Must Use Braces
- For Loops Must Use Braces
- Warns if a
for loop does not have its body wrapped in curly braces.
- If-Else Statements Must Use Braces
- If-Else Statements Must Use Braces
- While Loops Must Use Braces
- While Loops Must Use Braces
Class Structure
- Class may be interface
- Reports any concrete or abstract classes which may be simplified to be interfaces. This occurs if the class has no superclass (other than Object), has no fields declared that are not static, final, and public, and has no methods declared that are not public and abstract, and no inner classes that cannot themselves be interfaces. Since NetBeans 6.9
- Final class
- Reports any instances of classes being declared final. Some coding standards discourage final classes. Since NetBeans 6.9
- Final method
- Reports any instances of methods being declared final. Some coding standards discourage final classes. Since NetBeans 6.9
- Final method in final class
- Reports any instances of methods being declared final in classes that are declared final. This is unnecessary, and may be confusing. Since NetBeans 6.9
- Final private method
- Reports any instances of methods being declared final and private. As private methods cannot be meaningfully overridden, declaring them final is redundant. Since NetBeans 6.9
- Final static method
- Reports any instances of methods being declared final and static. As static methods cannot be meaningfully overridden, declaring them final is redundant. Since NetBeans 6.9
- Marker interface
- Reports marker interfaces which have no methods or fields. Such interfaces may be confusing, and normally indicate a design failure. Interfaces which extend two or more other interfaces will not be reported. Since NetBeans 6.9
- Multiple top-level classes in file
- Reports any instances of multiple top-level classes in a single java file. Putting multiple top-level classes in a file can be confusing, and may degrade the usefulness of various software tools. Since NetBeans 6.9
- No-op method in abstract class
- Reports any instances of no-op methods in abstract classes. It is usually a better design to make such methods abstract themselves, so that classes which inherit the methods will not forget to provide their own implementations. Since NetBeans 6.9
- Protected member in final class
- Reports any instances of members being declared protected in classes that are declared final. Such members may be declared private or package-visible instead. Since NetBeans 6.9
- Public constructor in non-public class
- Reports all constructors in non-public classes that are declared public. Since NetBeans 6.9
Code Maturity Issues
- Obsolete Collection
- This inspection reports any uses of java.util.Vector or java.util.Hashtable. While still supported, these classes were made obsolete by the JDK1.2 collection classes, and should probably not be used in new development. Since NetBeans 6.9
- Print Stack Trace
- This inspection reports any uses Throwable.printStackTrace() without arguments. These are often temporary debugging statements, and should probably be either removed from production code, or replaced by a more robust logging facility. Since NetBeans 6.9
- System out / err
- This inspection reports any uses of System.out or System.err. These are often temporary debugging statements, and should probably be either removed from production code, or replaced by a more robust logging facility. Since NetBeans 6.9
- Thread Dump Stack
- This inspection reports any uses Thread.dumpStack().These are often temporary debugging statements, and should probably be either removed from production code,or replaced by a more robust logging facility. Since NetBeans 6.9
Empty Statements
-
-
- Empty statement
- Checks for empty statements in blocks usually representedas superfluous semicolon. Since NetBeans 6.9
- Empty statement after 'do'
- Checks for 'do/while' statements in form
do; while. Since NetBeans 6.9
- Empty statement after 'for'
- Checks for 'for' statements in form
for(...);. Since NetBeans 6.9
- Empty statement after 'if/else'
- Checks for 'if/else' statements in form
if(...); else;. Since NetBeans 6.9
- Empty statement after 'while'
- Checks for 'while' statements in form
while(...);. Since NetBeans 6.9
Encapsulation
- Access of Private Field of Another Object
- Warns about access of private fields of another objects Since NetBeans 6.9
- Assignment to Array Field from Parameter
- Warns about assignment of array into fields Since NetBeans 6.9
- Assignment to Collection Field from Parameter
- Warns about assignment of java.util.Collection into fields Since NetBeans 6.9
- Assignment to Date or Calendar Field from Parameter
- Warns about assignment of java.util.Date or java.util.Calendar into fields Since NetBeans 6.9
- Package Field
- Warns about existence of package visible variables Since NetBeans 6.9
- Package Visible Inner Class
- Warns about existence of package visible inner class Since NetBeans 6.9
- Protected Field
- Warns about existence of protected variables Since NetBeans 6.9
- Protected Inner Class
- Warns about existence of protected visible inner class Since NetBeans 6.9
- Public Field
- Warns about existence of public variables Since NetBeans 6.9
- Public Inner Class
- Warns about existence of public visible inner class Since NetBeans 6.9
- Return of Array Field
- Warns about return of array fields Since NetBeans 6.9
- Return of Collection Field
- Warns about return of collection fields Since NetBeans 6.9
- Return of Date or Calendar Field
- Warns about return of java.util.Date or java.util.Calendar fields Since NetBeans 6.9
Error Fixes
- Create Field
- Settings for Create Field Fix Since NetBeans 6.9
- Create Local Variable
- Settings for the Create Local Variable error fix Since NetBeans 6.9
- Surround with try-catch
- Settings for the Surround with try-catch error fix Since NetBeans 6.9
Finalization
- finalize() called explicitly
- Warns about an explicit call of the Object.finalize() Since NetBeans 6.9
- finalize() declared
- Warns about implementations of Object.finalize() Since NetBeans 6.9
- finalize() does not call super.finalize()
- Warns about implementations of Object.finalize() which do not call supertype implementation Since NetBeans 6.9
- finalize() not declared protected
- Warns about non protected implementations of Object.finalize() Since NetBeans 6.9
General
- .equals Method not Checking Type
- Implementation of .equals methods not checking the type of the input parameter
- Accessing static via reference
- Java language allows access to static fields thru instance variables, however this is often misleading and harder to read.
- Assignment To Itself
- Assignment To Itself
- Comparing Strings using == or !=
- Checks for usages of == or != operator for comparing Strings.
String comparisons should generally be done using the equals() method.
- Field hides another field
- Declaration of a field in a class can hide declaration of another field in superclasses. Although possible, this is not very good programming style.
- Generate missing hashCode or equals
- Checks whether a class which overrides equals method also overrides hashCode.
- Local variable hides a field
- Declaration of a variable in a method can hide declaration of a field declared in the surrounding class. Although possible, this is not very good programming style.
- Remove Unnecessary Return Statement
- Remove Unnecessary Return Statement Since NetBeans 7.1
- Unnecessary Throwable.initCause
- Finds invocations of Throwable.initCause which can be replaced with simple constructor invocation. When the "Never alter result of getMessage()" checkbox is unchecked,
(IllegalStateException) new IllegalStateException().initCause(ex) will be rewritten to new IllegalStateException(ex), which will alter the value of getMessage(). When the checkbox is checked, the code will become new IllegalStateException(null, ex). Similar rule holds for creating the exception from getMessage() or getLocalizedMessage() of the cause. Since NetBeans 6.9
- Wrong Package
- Wrong Package
Imports
- Import From The Same Package
- Import From The Same Package
- Import From java.lang Package
- Import From java.lang Package
- Import from Excluded
- Import from package or class which has been labelled "Excluded" in the Code Completer
- Organize imports
- Checks whether import statements correspond to the specified code style rules Since NetBeans 7.1
- Star import
- Star import
- Unused Import
- Unused Import
Initialization
- Passing suspicious parameter in the constructor
- Using this as parameter can be dangerous in the contructor because the object is not fully initialized. Since NetBeans 6.9
- Problematic call in the constructor
- Calling methods that can be overridden can be dangerous in the contructor because in the moment when the overridden method is called the object is not fully initialized. Since NetBeans 6.9
- Static non-final variable used during initialization
- Using static non-final variables can be dangerous in the initialization code because their values may depend on the order of initialization statements Since NetBeans 6.9
JDK 1.5 and later
- AbstractProcessor.getSupportedAnnotations() is overridden
- Overriding Processor.getSupportedAnnotations() may lead to unnecessary classloading during development, and may prevent important optimalizations. consider using @javax.annotation.processing.SupportedAnnotationTypes In current development version
- Add @Override Annotation
- Add @Override Annotation
- Add Underscores
- Proposed to add underscores to integer literals to improve their readability In current development version
- Can Use Diamond
- Warns about places where the diamond operator in JDK 7 can be used instead of explicit type parameters Since NetBeans 7.1
- Convert to try-with-resources
- Converts try finally block to try-with-resources Since NetBeans 7.0
- Don't use Annotation as super interface
- Despite the compiler permitting such constructs,Annotations should not be used as superinterfaces.
- Join catch sections using multicatch
- Join catch sections using multicatch Since NetBeans 7.0
- Static imports
- Convert method to static import. Feedback to http://www.netbeans.org/issues/show_bug.cgi?id=89258 Since NetBeans 6.9
- Use specific catch
- Converts catch (Throwable) or catch (Exception) to multicatch catching the exceptions thrown by the try body. Since NetBeans 7.0
- Use switch over Strings where possible.
- Marks cascades of ifs which can be converted to switch over Strings . Since NetBeans 6.9
Javadoc
- Create Javadoc
- Create Javadoc
- Error in Javadoc
- Error in Javadoc
Logging
- Logger declaration is not static final
- Each class should have one unique logger. If declared as a field it should be static and final. Since NetBeans 6.9
- Multiple loggers
- There are several loggers declared for a single class. Since NetBeans 6.9
- No loggers
- There is no logger declared for a class. Since NetBeans 6.9
- String concatenation in logger
- It is not performance efficient to concatenate strings in logger messages. It is better to use a template message with placeholders that are replaced by concrete values only when the message is really going to be logged. Since NetBeans 6.9
Maven
- Resolve Missing Class Dependency
- Resolve missing class dependency and add to project POM Since NetBeans 6.9
NetBeans Development
- Empty cancel() for cancelable tasks
- Warn about empty cancel() methods for cancelable tasks
- HelpCtx issues
- Warnings about misuse of org.openide.util.HelpCtx. In current development version
- Illegal Use of instanceOf operator
- Show illegal use of instanceof on javax.lang.model.elements, javax.lang.model.type and com.sun.source.tree
- Use @NbBundle.Messages
- Use @NbBundle.Messages in preference to Bundle.properties plus NbBundle.getMessage(...). Since NetBeans 7.0
Performance
- .getClass() replaceable with .class
- Finds instantions of a class directly followed by invocation of .getClass() on the newly constructed object Since NetBeans 6.9
- Collections without initial capacity
- Looks for instatiations of collections with missing initial capacity. Only collections backed-up with an array are tested. Since NetBeans 6.9
- Creating new Boolean
- Creating new Boolean is inefficient and typically useless. Since NetBeans 6.9
- Length one String in String.indexOf
- Length one String literal in String.indexOf can be replaced with a character literal Since NetBeans 6.9
- Manual array copy
- Finds occurrences of manual array copying via for loop Since NetBeans 6.9
- Map replaceable with EnumMap
- Finds instantiations of Maps that can be replaced with EnumMap Since NetBeans 6.9
- Set replaceable with EnumSet
- Finds instantiations of Sets that can be replaced with EnumSet Since NetBeans 6.9
- String concatenation in StringBuilder.append
- Looks for string concatenation in the parameter of an invocation of the append method of StringBuilder or StringBuffer. Since NetBeans 6.9
- String constructor
- Use of java.lang.String constructor is usually useless. Since NetBeans 6.9
- String.equals("")
- Use of String.equals("") can be replaced with with String.length() == 0 (for JDK5 and lower) or String.isEmpty() (for JDK6 and higher) Since NetBeans 6.9
- String.intern() called on constant
- Invocations of String.intern() on compile-time constants are superfluous. Since NetBeans 6.9
- StringBuilder without initial capacity
- Looks for instantiations of StringBuilder or StringBuffer with missing initial capacity. Since NetBeans 6.9
- Usage of .size() == 0
- Use .isEmpty() or !.isEmpty() instead of .size() == 0 or .size() != 0 where possible. Since NetBeans 6.9
- Useless use of StringBuffer
- Use StringBuilder instead of StringBuffer where possible. Since NetBeans 6.9
- Zero element array passed to Collection.toArray
- Passing zero element array to Collection.toArray may affect performance In current development version
Probable Bugs
- .equals on Array
- .equals on array Since NetBeans 6.9
- .equals on Incompatible Types
- .equals on incompatible types
- .equals(null)
- Finds invocations of the Object.equals method with literal parameter 'null'. Since NetBeans 6.9
- @CheckReturnValue
- Verifies that a result of method marked with @CheckReturnValue is really checked. In current development version
- Annotations without runtime Retention
- Warns about reflective access to annotations with CLASS or SOURCE retentions Since NetBeans 6.9
- Dead Branch
- Dead Branch Since NetBeans 7.1
- Incompatible cast/instanceof
- Incompatible cast surrounded with incompatible instanceof Since NetBeans 6.9
- Incorrect column index in ResultSet
- Reports Iincorrect column indices passed to various methods of java.sql.ResultSet Since NetBeans 6.9
- Malformed regular expression
- Warns about malformed regular expressions Since NetBeans 6.9
- Result of new Object ignored
- Result of new Object ignored. Since NetBeans 6.9
- String.replaceAll(".", )
- Finds occurrences of calls to String.replaceAll(".", $target), which would replace all characters of the source string with $target. Since NetBeans 6.9
- Suspicious invocation of System.arraycopy
- Finds invocations of System.arraycopy with negative offsets, length or used on non-array objects. Since NetBeans 6.9
- Suspicious method call
- Warns about suspicious calls to Collection.remove/contains and Map.containsKey/containsValue/remove Since NetBeans 6.9
- Synchronizing on this in anonymous class
- <html>Synchronizing on this in anonymous or local class is probably a result of refactoring and possibly a mistake Since NetBeans 6.9
- Unbalanced read/write with arrays
- Unbalanced read/write with arrays Since NetBeans 7.1
- Unbalanced read/write with collections
- Unbalanced read/write with collections Since NetBeans 7.1
- Unused Assignment
- Unused Assignment Since NetBeans 7.1
Standard Javac Warnings
- Deprecated
- Warn when code uses deprecated API. Since NetBeans 6.9
- Division By Zero
- Division By Zero Since NetBeans 6.9
- Empty Statement After If
- Empty Statement After If Since NetBeans 6.9
- Fallthrough
- Warn when a case can fall through to the next case. Since NetBeans 6.9
- Finally
- Warn when a finally block interrupts flow of a try/catch block. Since NetBeans 6.9
- Overrides
- Warn when an overriding method is not annotated with @Overrides Since NetBeans 6.9
- Raw Types
- Raw Types Since NetBeans 6.9
- Serialization
- Warn when a class which implements java.io.Serializable does not declare a serialVersionUID. Since NetBeans 6.9
- Unchecked
- Warn when unchecked conversions may cause ClassCastExceptions at runtime. Since NetBeans 6.9
- Unnecessary Cast
- Warn when an object is cast unnecessarily to the same type or a supertype. Since NetBeans 6.9
Suggestions
- Assign Return Value To New Variable
- Assign Return Value To New Variable
- Assign Unused Constructor Parameter to Field
- Assign Unused Constructor Parameter to Field
- Convert Anonymous to Member
- Convert Anonymous to Member
- Convert integer constant to different base
- Convert integer constant to different base Since NetBeans 7.0
- Create Subclass
- Create Subclass In current development version
- Declaration for instanceof
- Declaration for instanceof
- Expand Enhanced For Loop
- Expand Enhanced For Loop Since NetBeans 7.1
- Fill Missing Cases to Switch
- Adds missing cases to switch statement. The default clause template should be one Java statement, occurrences of $expression will be replaced with the expression over which the switch statement works. In current development version
- Flip .equals
- Allows to flip .equals parameter and call site Since NetBeans 6.9
- Split Declaration
- Splits declaration with initializer to a declaration and assignment In current development version
Threading
- .wait invoked outside a synchronized context
- .wait invoked outside a synchronized context Since NetBeans 6.9
- .wait invoked outside a synchronized context
- .wait invoked outside a synchronized context Since NetBeans 6.9
- Double-checked locking
- Searches for examples of double checked locking - e.g.when a variable is tested before as well as inside a synchronized block.
- Empty synchronized block
- Empty synchronized block are usually useless Since NetBeans 6.9
- Invoking Condition.notify()
- Invoking notify or notifyAll on java.util.concurrent.locks.Condition should probably be replaced with invoking signal or signalAll. Since NetBeans 6.9
- Invoking Condition.wait()
- Invoking notify or notifyAll on java.util.concurrent.locks.Condition is probably unintended. Since NetBeans 6.9
- Invoking Thread.run()
- Invoking run on java.lang.Thread should be probably replaced with invocation of method start() Since NetBeans 6.9
- Invoking Thread.stop()/suspend()/resume()
- Methods stop(), suspend() and resume() of java.lang.Thread are dangerous and should not be used. Since NetBeans 6.9
- Invoking Thread.yield()
- Invocation of method yield() on java.lang.Thread is usually used to masquerade synchronization problems and should be avoided. Since NetBeans 6.9
- Lock not unlocked in finally
- Finds occurrences of Lock.lock()-Lock.unlock() not properly wrapped in try-finally. Since NetBeans 6.9
- Nested synchronized blocks
- Nesting synchronized blocks is either useless (if they use the same lock object) or dangerous. Since NetBeans 6.9
- Starting Thread in constructor
- Starting a new Thread in constructor is dangerous and should be prevented. Since NetBeans 6.9
- Synchronization on non-final field
- Synchronization on non-final field
- Synchronizing on Lock
- Synchronizing on java.util.concurrent.locks.Lock is usually unintended and should be replaced with Lock.lock()-Lock.unlock() Since NetBeans 6.9
- Thread.sleep in loop
- Invoking Thread.sleep in loop can cause performance problems Since NetBeans 6.9
- Thread.sleep in synchronized context
- Invoking Thread.sleep in synchronized context can cause performance problems Since NetBeans 6.9
- Volatile array field
- Finds declarations of volatile array fields. Since NetBeans 6.9
|
|