Java Hints

(Difference between revisions)
m
Line 10: Line 10:
:Checks that return types and parameter types of all public 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. '''In NetBeans 6.8 or earlier'''
:Checks that return types and parameter types of all public 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. '''In NetBeans 6.8 or earlier'''
-
;Utility class with visible constructor
+
;'''Utility class with visible constructor'''&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-size:100%;color:#204a87">[ Disabled by default ]</span>
-
:Finds out classes containing only static elements with public or protected constructors. '''Disabled by default''' '''In NetBeans 6.8 or earlier'''
+
:Finds out classes containing only static elements with public or protected constructors.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;''In NetBeans 6.8 or earlier''
;Utility class without constructor
;Utility class without constructor

Revision as of 08:55, 23 October 2012

There are 163 total hints/inspections, 3 of them in current development version only.

There are 13 total suggestions/actions, 3 of them in current development version only.

Contents

APIs

Be aware that this API will be optional in Java EE 7 platform.
Warn user about usage of APIs from technologies which will be made optional in Java EE 7 specification. These APIs are not deprecated and can be used but because they will be optional they may or may not be available in future Java EE 7 compliant platforms. Since NetBeans 7.0
Exporting non-public type through public API
Checks that return types and parameter types of all public 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. In NetBeans 6.8 or earlier
Utility class with visible constructor            [ Disabled by default ]
Finds out classes containing only static elements with public or protected constructors.            In NetBeans 6.8 or earlier
Utility class without constructor
Finds out classes containing only static elements and still being instantiable. Disabled by default In NetBeans 6.8 or earlier

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. Disabled by default 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. Disabled by default 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. Disabled by default 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. Disabled by default 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. Disabled by default 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. Disabled by default 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 In NetBeans 6.8 or earlier
For Loops Must Use Braces
Warns if a for loop does not have its body wrapped in curly braces.
In NetBeans 6.8 or earlier
If-Else Statements Must Use Braces
If-Else Statements Must Use Braces In NetBeans 6.8 or earlier
While Loops Must Use Braces
While Loops Must Use Braces In NetBeans 6.8 or earlier

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. Disabled by default Since NetBeans 6.9
Final class
Reports any instances of classes being declared final. Some coding standards discourage final classes. Disabled by default Since NetBeans 6.9
Final method
Reports any instances of methods being declared final. Some coding standards discourage final classes. Disabled by default 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. Disabled by default 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. Disabled by default 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. Disabled by default 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. Disabled by default Since NetBeans 6.9
Organize members
Checks whether members order corresponds to the specified code style rules Disabled by default In current development version
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. Disabled by default Since NetBeans 6.9
Public constructor in non-public class
Reports all constructors in non-public classes that are declared public. Disabled by default 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. Disabled by default 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

In NetBeans 6.8 or earlier
Empty statement
Checks for empty statements in blocks usually represented as 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;
. Disabled by default 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 Disabled by default Since NetBeans 6.9
Assignment to Array Field from Parameter
Warns about assignment of array into fields Disabled by default Since NetBeans 6.9
Assignment to Collection Field from Parameter
Warns about assignment of java.util.Collection into fields Disabled by default 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 Disabled by default Since NetBeans 6.9
Package Field
Warns about existence of package visible variables Disabled by default Since NetBeans 6.9
Package Visible Inner Class
Warns about existence of package visible inner class Disabled by default Since NetBeans 6.9
Protected Field
Warns about existence of protected variables Disabled by default Since NetBeans 6.9
Protected Inner Class
Warns about existence of protected visible inner class Disabled by default Since NetBeans 6.9
Public Field
Warns about existence of public variables Disabled by default Since NetBeans 6.9
Public Inner Class
Warns about existence of public visible inner class Disabled by default Since NetBeans 6.9
Return of Array Field
Warns about return of array fields Disabled by default Since NetBeans 6.9
Return of Collection Field
Warns about return of collection fields Disabled by default Since NetBeans 6.9
Return of Date or Calendar Field
Warns about return of java.util.Date or java.util.Calendar fields Disabled by default 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
Implementation of EJBContainer is missing on project's classpath
Find out whether implementation of EJBContainer necessary for successful execution of unit test is available. Since NetBeans 7.0
Java EE API is missing on project classpath.
Find out whether Java EE API is available on project classpath. Java EE API is necessary for successful test execution. This hint also warns about incorrect usage of javaee-web-api artifact - that artifact is suitable only for compilation but not for test execution because body of all API methods was removed from the bytecode. Since NetBeans 7.0
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() Disabled by default Since NetBeans 6.9

General

.equals Method not Checking Type
Implementation of .equals methods not checking the type of the input parameter In NetBeans 6.8 or earlier
Accessing static via reference
Java language allows access to static fields thru instance variables, however this is often misleading and harder to read. In NetBeans 6.8 or earlier
Assignment To Itself
Assignment To Itself In NetBeans 6.8 or earlier
Comparing Strings using == or !=
Checks for usages of == or != operator for comparing Strings.
String comparisons should generally be done using the equals() method. In NetBeans 6.8 or earlier
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. In NetBeans 6.8 or earlier
Generate missing hashCode or equals
Checks whether a class which overrides equals method also overrides hashCode. In NetBeans 6.8 or earlier
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. In NetBeans 6.8 or earlier
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 In NetBeans 6.8 or earlier

Imports

Import From The Same Package
Import From The Same Package In NetBeans 6.8 or earlier
Import From java.lang Package
Import From java.lang Package In NetBeans 6.8 or earlier
Import from Excluded
Import from package or class which has been labelled "Excluded" in the Code Completer In NetBeans 6.8 or earlier
Organize imports
Checks whether import statements correspond to the specified code style rules Disabled by default Since NetBeans 7.1
Star import
Star import Disabled by default In NetBeans 6.8 or earlier
Unused Import
Unused Import In NetBeans 6.8 or earlier

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.getSupportedAnnotationTypes() is overridden
Overriding Processor.getSupportedAnnotationTypes() may lead to unnecessary classloading during development, and may prevent important optimalizations. consider using @javax.annotation.processing.SupportedAnnotationTypes Since NetBeans 7.2
Add @Override Annotation
Add @Override Annotation In NetBeans 6.8 or earlier
Add Underscores
Proposed to add underscores to integer literals to improve their readability Disabled by default Since NetBeans 7.2
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. In NetBeans 6.8 or earlier
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 Disabled by default Since NetBeans 6.9
Use JDK 5 for-loop
Replaces simple uses of Iterator with a corresponding for-loop. In current development version
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

JavaEE

Instantiation replaceable with @EJB injection
Finds instantiations of a bean which can be injected by @EJB annotation Since NetBeans 7.2

Javadoc

Create Javadoc
Create Javadoc In NetBeans 6.8 or earlier
Error in Javadoc
Error in Javadoc Disabled by default In NetBeans 6.8 or earlier

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. Disabled by default 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 Disabled by default In NetBeans 6.8 or earlier
HelpCtx issues
Warnings about misuse of org.openide.util.HelpCtx. Since NetBeans 7.2
Illegal Use of instanceOf operator
Show illegal use of instanceof on javax.lang.model.elements, javax.lang.model.type and com.sun.source.tree Disabled by default In NetBeans 6.8 or earlier
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 Disabled by default 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. Disabled by default 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 Disabled by default 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) Disabled by default Since NetBeans 6.9
String.intern() called on constant
Invocations of String.intern() on compile-time constants are superfluous. Disabled by default Since NetBeans 6.9
StringBuilder without initial capacity
Looks for instantiations of StringBuilder or StringBuffer with missing initial capacity. Disabled by default 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 Disabled by default Since NetBeans 7.2

Probable Bugs

.equals on Array
.equals on array Since NetBeans 6.9
.equals on Incompatible Types
.equals on incompatible types In NetBeans 6.8 or earlier
.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. Since NetBeans 7.2
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
Null Pointer Dereference
Checks various problems related to dereferencing nulls In current development version
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. Disabled by default Since NetBeans 6.9
Division By Zero
Division By Zero Disabled by default Since NetBeans 6.9
Empty Statement After If
Empty Statement After If Disabled by default Since NetBeans 6.9
Fallthrough
Warn when a case can fall through to the next case. Disabled by default Since NetBeans 6.9
Finally
Warn when a finally block interrupts flow of a try/catch block. Disabled by default Since NetBeans 6.9
Overrides
Warn when an overriding method is not annotated with @Overrides Disabled by default Since NetBeans 6.9
Raw Types
Raw Types Disabled by default Since NetBeans 6.9
Serialization
Warn when a class which implements java.io.Serializable does not declare a serialVersionUID. Disabled by default Since NetBeans 6.9
Unchecked
Warn when unchecked conversions may cause ClassCastExceptions at runtime. Disabled by default Since NetBeans 6.9
Unnecessary Cast
Warn when an object is cast unnecessarily to the same type or a supertype. Disabled by default Since NetBeans 6.9

Suggestions

Assign Return Value To New Variable
Assign Return Value To New Variable In NetBeans 6.8 or earlier
Assign Unused Constructor Parameter to Field
Assign Unused Constructor Parameter to Field In NetBeans 6.8 or earlier
Convert Anonymous to Member
Convert Anonymous to Member In NetBeans 6.8 or earlier
Convert integer constant to different base
Convert integer constant to different base Since NetBeans 7.0
Create Subclass
Create Subclass Since NetBeans 7.2
Declaration for instanceof
Declaration for instanceof In NetBeans 6.8 or earlier
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. Since NetBeans 7.2
Flip .equals
Allows to flip .equals parameter and call site Since NetBeans 6.9
Implement Abstract Methods
Implement Abstract Methods. In current development version
Invert If
Will invert an if statement; negate the condition and switch the statements from the then and else sections. In current development version
Move initilization to constructor
Moves a fields initilization expression to the constructors. In current development version
Split Declaration
Splits declaration with initializer to a declaration and assignment Since NetBeans 7.2

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. In NetBeans 6.8 or earlier
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 In NetBeans 6.8 or earlier
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
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo