net.sf.qualitytest
Class StaticCheck

java.lang.Object
  extended by net.sf.qualitytest.StaticCheck

public final class StaticCheck
extends Object

This class offers simple static methods to test static properties of your classes. These checks are typically only called from within unit tests, because they are costly. Static tests in unit tests ensure that certain intended properties of a class (e.g. immutability, thread-safeness, no non static finals, etc.) are still true after future changes.

Author:
Dominik Seichter

Method Summary
static Class<?> classIsFinal(Class<?> clazz)
          Check if a class is final.
static Class<?> noNonFinalStatic(Class<?> clazz)
          Check if a class contains a non-final static variable.
static Class<?> noNonFinalStaticInHierarchy(Class<?> clazz)
          Check if a class or super-class contains a non-final static variable.
static Class<?> noPublicDefaultConstructor(Class<?> clazz)
          Check that a class contains no public default constructor.
static Class<?> publicMethodsAnnotated(Class<?> clazz, Class<? extends Annotation> annotation)
          Check that all declared public methods of a class are annotated using a certain annotation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

classIsFinal

public static Class<?> classIsFinal(@Nonnull
                                    Class<?> clazz)
Check if a class is final.

Parameters:
clazz - A class that must be final.
Returns:
clazz The class that was passed to this method.
Throws:
IllegalNonFinalClassException - If the passed class is not final.

noNonFinalStatic

public static Class<?> noNonFinalStatic(@Nonnull
                                        Class<?> clazz)
Check if a class contains a non-final static variable. Non-final static fields are dangerous in multi-threaded environments and should therefore not be used. This method only checks the passed class and not any super-classes.

Parameters:
clazz - A class which is checked for non-final statics.
Returns:
clazz The class that was passed to this method.
Throws:
IllegalNonFinalStaticException - If the passed class contains and non-final static field.

noNonFinalStaticInHierarchy

public static Class<?> noNonFinalStaticInHierarchy(@Nonnull
                                                   Class<?> clazz)
Check if a class or super-class contains a non-final static variable. Non-final static fields are dangerous in multi-threaded environments and should therefore not be used.

Parameters:
clazz - A class which is checked for non-final statics.
Returns:
clazz The class that was passed to this method.
Throws:
IllegalNonFinalStaticException - If the passed class contains and non-final static field.

noPublicDefaultConstructor

public static Class<?> noPublicDefaultConstructor(@Nonnull
                                                  Class<?> clazz)
Check that a class contains no public default constructor. It is recommended to hide the public default constructor of utility classes by providing a private default construct. This method assures that the given class cannot be intantiated

Parameters:
clazz - A class which is checked for not having a public default constructor
Returns:
clazz The class that was passed to this method.
Throws:
IllegalClassWithPublicDefaultConstructorException - If the passed class contains a public default constructor.

publicMethodsAnnotated

public static Class<?> publicMethodsAnnotated(@Nonnull
                                              Class<?> clazz,
                                              @Nonnull
                                              Class<? extends Annotation> annotation)
Check that all declared public methods of a class are annotated using a certain annotation.

Parameters:
clazz - A class that must have annotations on all public methods.
annotation - An annotation that must be present on all public methods in a class
Returns:
the checked class
Throws:
IllegalMissingAnnotationOnMethodException - if the one or more public methods of a Class are not annotated with a specific Annotation


Copyright © 2012-2013. All Rights Reserved.