Top Description Fields Constructors Methods
org.testng.internal

public final Class ClassHelper

extends Object
Class Inheritance
Imports
java.lang.reflect.Constructor, .Executable, .Method, .Modifier, java.util.ArrayList, .Collection, .List, .Map, .Set, .Vector, java.util.function.BiConsumer, java.util.stream.Collectors, org.testng.TestNGException, org.testng.annotations.IFactoryAnnotation, org.testng.collections.Lists, .Maps, .Sets, org.testng.internal.annotations.IAnnotationFinder, org.testng.internal.reflect.ReflectionHelper, org.testng.xml.XmlClass, .XmlSuite, .XmlTest

Utility class for different class manipulations.

Field Summary

Modifier and TypeField and Description
private static final String
private static final List<ClassLoader>
classLoaders

The additional class loaders to find classes in.

private static int
lastGoodRootIndex

When given a file name to form a class name, the file name is parsed and divided into segments.

Constructor Summary

AccessConstructor and Description
private
ClassHelper()

Hide constructor.

Method Summary

Modifier and TypeMethod and Description
public static void
pack-priv static List<ClassLoader>
private static void
appendMethod(Map<String, Set<Method>> methods, Method declaredMethod)

private static boolean
canInclude(boolean isSamePackage, Method method, Map<String, Set<Method>> collected)

private static Map<String, Set<Method>>
extractMethods(Class<?> childClass, Class<?> clazz, Map<String, Set<Method>> collected)

public static Class<?>

Returns:

the class corresponding to the name specified.
fileToClass
(String
the class name.
file
)

Returns the Class object corresponding to the given name.

public static XmlClass[]

Returns:

- All the XmlClass objects that share the same <test> tag as the class.
findClassesInSameTest
(Class<?>
- The class to look for.
cls
,
XmlSuite
- The XmlSuite whose <test> tags needs to be searched in.
suite
)

private static Collection<XmlClass>
public static List<ConstructorOrMethod>

Returns:

the @Factory methods
findDeclaredFactoryMethods
(Class<?>
The class to search for the @Factory annotation.
cls
,
IAnnotationFinder
The finder (JDK 1.4 or JDK 5.0+) use to search for the annotation.
finder
)

For the given class, returns the method annotated with @Factory or null if none is found.

public static Class<?>

Returns:

the class or null if the class is not found.
forName
(final String
the class name to be loaded.
className
)

Tries to load the specified class using the context ClassLoader or if none, than from the default ClassLoader.

public static Set<Method>
public static Set<Method>

Returns:

- A Set of Method excluding default methods from base class interfaces.
getAvailableMethodsExcludingDefaults
(Class<?>
- The Class in which the search is to be done.
clazz
)

private static boolean
isOverridden(Method method, Map<String, Set<Method>> methodsByName)

private static boolean
isSamePackage(Package childPackage, Package classPackage)

private static void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CLASS_HELPERback to summary
private static final String CLASS_HELPER
classLoadersback to summary
private static final List<ClassLoader> classLoaders

The additional class loaders to find classes in.

lastGoodRootIndexback to summary
private static int lastGoodRootIndex

When given a file name to form a class name, the file name is parsed and divided into segments. For example, "c:/java/classes/com/foo/A.class" would be divided into 6 segments {"C:" "java", "classes", "com", "foo", "A"}. The first segment actually making up the class name is [3]. This value is saved in lastGoodRootIndex so that when we parse the next file name, we will try 3 right away. If 3 fails we will take the long approach. This is just a optimization cache value.

Constructor Detail

ClassHelperback to summary
private ClassHelper()

Hide constructor.

Method Detail

addClassLoaderback to summary
public static void addClassLoader(final ClassLoader loader)
appendContextualClassLoadersback to summary
pack-priv static List<ClassLoader> appendContextualClassLoaders(List<ClassLoader> currentLoaders)
appendMethodback to summary
private static void appendMethod(Map<String, Set<Method>> methods, Method declaredMethod)
canIncludeback to summary
private static boolean canInclude(boolean isSamePackage, Method method, Map<String, Set<Method>> collected)
extractMethodsback to summary
private static Map<String, Set<Method>> extractMethods(Class<?> childClass, Class<?> clazz, Map<String, Set<Method>> collected)
fileToClassback to summary
public static Class<?> fileToClass(String file)

Returns the Class object corresponding to the given name. The name may be of the following form:

  • A class name: "org.testng.TestNG"
  • A class file name: "/testng/src/org/testng/TestNG.class"
  • A class source name: "d:\testng\src\org\testng\TestNG.java"
Parameters
file:String

the class name.

Returns:Class<?>

the class corresponding to the name specified.

findClassesInSameTestback to summary
public static XmlClass[] findClassesInSameTest(Class<?> cls, XmlSuite suite)
Parameters
cls:Class<?>

- The class to look for.

suite:XmlSuite

- The XmlSuite whose <test> tags needs to be searched in.

Returns:XmlClass[]

- All the XmlClass objects that share the same <test> tag as the class.

findClassesInSameTestback to summary
private static Collection<XmlClass> findClassesInSameTest(Class<?> cls, XmlTest xmlTest)
findDeclaredFactoryMethodsback to summary
public static List<ConstructorOrMethod> findDeclaredFactoryMethods(Class<?> cls, IAnnotationFinder finder)

For the given class, returns the method annotated with @Factory or null if none is found. This method does not search up the superclass hierarchy. If more than one method is @Factory annotated, a TestNGException is thrown.

Parameters
cls:Class<?>

The class to search for the @Factory annotation.

finder:IAnnotationFinder

The finder (JDK 1.4 or JDK 5.0+) use to search for the annotation.

Returns:List<ConstructorOrMethod>

the @Factory methods

forNameback to summary
public static Class<?> forName(final String className)

Tries to load the specified class using the context ClassLoader or if none, than from the default ClassLoader. This method differs from the standard class loading methods in that it does not throw an exception if the class is not found but returns null instead.

Parameters
className:String

the class name to be loaded.

Returns:Class<?>

the class or null if the class is not found.

getAvailableMethodsback to summary
public static Set<Method> getAvailableMethods(Class<?> clazz)
getAvailableMethodsExcludingDefaultsback to summary
public static Set<Method> getAvailableMethodsExcludingDefaults(Class<?> clazz)
Parameters
clazz:Class<?>

- The Class in which the search is to be done.

Returns:Set<Method>

- A Set of Method excluding default methods from base class interfaces.

isOverriddenback to summary
private static boolean isOverridden(Method method, Map<String, Set<Method>> methodsByName)
isSamePackageback to summary
private static boolean isSamePackage(Package childPackage, Package classPackage)
logClassNotFoundErrorback to summary
private static void logClassNotFoundError(String className, Throwable ex)