Package org.testng.internal
Class ClassHelper
- java.lang.Object
-
- org.testng.internal.ClassHelper
-
public final class ClassHelper extends java.lang.Object
Utility class for different class manipulations.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
CANNOT_INSTANTIATE_CLASS
private static java.lang.String
CLASS_HELPER
private static java.util.List<java.lang.ClassLoader>
classLoaders
The additional class loaders to find classes in.private static java.lang.String
JUNIT_4_TESTRUNNER
private static java.lang.String
JUNIT_TESTRUNNER
private static int
lastGoodRootIndex
When given a file name to form a class name, the file name is parsed and divided into segments.private static java.lang.String
SKIP_CALLER_CLS_LOADER
-
Constructor Summary
Constructors Modifier Constructor Description private
ClassHelper()
Hide constructor.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addClassLoader(java.lang.ClassLoader loader)
Add a class loader to the searchable loaders.private static void
appendMethod(java.util.Map<java.lang.String,java.util.Set<java.lang.reflect.Method>> methods, java.lang.reflect.Method declaredMethod)
private static boolean
canInclude(boolean isSamePackage, java.lang.reflect.Method method, java.util.Map<java.lang.String,java.util.Set<java.lang.reflect.Method>> collected)
static java.lang.Object
createInstance(java.lang.Class<?> declaringClass, java.util.Map<java.lang.Class<?>,IClass> classes, XmlTest xmlTest, IAnnotationFinder finder, ITestObjectFactory objectFactory)
Create an instance for the given class.static java.lang.Object
createInstance1(java.lang.Class<?> declaringClass, java.util.Map<java.lang.Class<?>,IClass> classes, XmlTest xmlTest, IAnnotationFinder finder, IObjectFactory objectFactory)
private static java.lang.Object
createInstance2(java.lang.Class<?> declaringClass, IObjectFactory2 objectFactory)
static IMethodSelector
createSelector(XmlMethodSelector selector)
static IJUnitTestRunner
createTestRunner(TestRunner runner)
private static java.util.Map<java.lang.String,java.util.Set<java.lang.reflect.Method>>
extractMethods(java.lang.Class<?> childClass, java.lang.Class<?> clazz, java.util.Map<java.lang.String,java.util.Set<java.lang.reflect.Method>> collected)
static java.lang.Class<?>
fileToClass(java.lang.String file)
Returns the Class object corresponding to the given name.private static java.lang.reflect.Constructor<?>
findAnnotatedConstructor(IAnnotationFinder finder, java.lang.Class<?> declaringClass)
Find the best constructor given the parameters found on the annotationstatic XmlClass[]
findClassesInSameTest(java.lang.Class<?> cls, XmlSuite suite)
private static java.util.Collection<XmlClass>
findClassesInSameTest(java.lang.Class<?> cls, XmlTest xmlTest)
static java.util.List<ConstructorOrMethod>
findDeclaredFactoryMethods(java.lang.Class<?> cls, IAnnotationFinder finder)
For the given class, returns the method annotated with @Factory or null if none is found.static java.lang.Class<?>
forName(java.lang.String className)
Tries to load the specified class using the context ClassLoader or if none, than from the default ClassLoader.static java.util.Set<java.lang.reflect.Method>
getAvailableMethods(java.lang.Class<?> clazz)
Extract all callable methods of a class and all its super (keeping in mind the Java access rules).private static java.lang.Class<?>
getEnclosingClass(java.lang.Class<?> declaringClass)
Class.getEnclosingClass() only exists on JDK5, so reimplementing it here.private static boolean
isOverridden(java.lang.reflect.Method method, java.util.Map<java.lang.String,java.util.Set<java.lang.reflect.Method>> methodsByName)
private static boolean
isSamePackage(java.lang.Package childPackage, java.lang.Package classPackage)
private static void
logClassNotFoundError(java.lang.String className, java.lang.Exception ex)
static <T> T
newInstance(java.lang.Class<T> clazz)
static <T> T
newInstance(java.lang.reflect.Constructor<T> constructor, java.lang.Object... parameters)
static <T> T
newInstanceOrNull(java.lang.Class<T> clazz)
static <T> T
tryOtherConstructor(java.lang.Class<T> declaringClass)
-
-
-
Field Detail
-
JUNIT_TESTRUNNER
private static final java.lang.String JUNIT_TESTRUNNER
- See Also:
- Constant Field Values
-
JUNIT_4_TESTRUNNER
private static final java.lang.String JUNIT_4_TESTRUNNER
- See Also:
- Constant Field Values
-
classLoaders
private static final java.util.List<java.lang.ClassLoader> classLoaders
The additional class loaders to find classes in.
-
CANNOT_INSTANTIATE_CLASS
private static final java.lang.String CANNOT_INSTANTIATE_CLASS
- See Also:
- Constant Field Values
-
CLASS_HELPER
private static final java.lang.String CLASS_HELPER
-
SKIP_CALLER_CLS_LOADER
private static final java.lang.String SKIP_CALLER_CLS_LOADER
- See Also:
- Constant Field Values
-
lastGoodRootIndex
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.
-
-
Method Detail
-
addClassLoader
public static void addClassLoader(java.lang.ClassLoader loader)
Add a class loader to the searchable loaders.
-
newInstance
public static <T> T newInstance(java.lang.Class<T> clazz)
-
newInstanceOrNull
public static <T> T newInstanceOrNull(java.lang.Class<T> clazz)
-
newInstance
public static <T> T newInstance(java.lang.reflect.Constructor<T> constructor, java.lang.Object... parameters)
-
forName
public static java.lang.Class<?> forName(java.lang.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
- the class name to be loaded.- Returns:
- the class or null if the class is not found.
-
logClassNotFoundError
private static void logClassNotFoundError(java.lang.String className, java.lang.Exception ex)
-
findDeclaredFactoryMethods
public static java.util.List<ConstructorOrMethod> findDeclaredFactoryMethods(java.lang.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
- The class to search for the @Factory annotation.finder
- The finder (JDK 1.4 or JDK 5.0+) use to search for the annotation.- Returns:
- the @Factory
methods
-
getAvailableMethods
public static java.util.Set<java.lang.reflect.Method> getAvailableMethods(java.lang.Class<?> clazz)
Extract all callable methods of a class and all its super (keeping in mind the Java access rules).
-
createTestRunner
public static IJUnitTestRunner createTestRunner(TestRunner runner)
-
appendMethod
private static void appendMethod(java.util.Map<java.lang.String,java.util.Set<java.lang.reflect.Method>> methods, java.lang.reflect.Method declaredMethod)
-
extractMethods
private static java.util.Map<java.lang.String,java.util.Set<java.lang.reflect.Method>> extractMethods(java.lang.Class<?> childClass, java.lang.Class<?> clazz, java.util.Map<java.lang.String,java.util.Set<java.lang.reflect.Method>> collected)
-
canInclude
private static boolean canInclude(boolean isSamePackage, java.lang.reflect.Method method, java.util.Map<java.lang.String,java.util.Set<java.lang.reflect.Method>> collected)
-
isSamePackage
private static boolean isSamePackage(java.lang.Package childPackage, java.lang.Package classPackage)
-
isOverridden
private static boolean isOverridden(java.lang.reflect.Method method, java.util.Map<java.lang.String,java.util.Set<java.lang.reflect.Method>> methodsByName)
-
createSelector
public static IMethodSelector createSelector(XmlMethodSelector selector)
-
createInstance
public static java.lang.Object createInstance(java.lang.Class<?> declaringClass, java.util.Map<java.lang.Class<?>,IClass> classes, XmlTest xmlTest, IAnnotationFinder finder, ITestObjectFactory objectFactory)
Create an instance for the given class.
-
createInstance2
private static java.lang.Object createInstance2(java.lang.Class<?> declaringClass, IObjectFactory2 objectFactory)
-
createInstance1
public static java.lang.Object createInstance1(java.lang.Class<?> declaringClass, java.util.Map<java.lang.Class<?>,IClass> classes, XmlTest xmlTest, IAnnotationFinder finder, IObjectFactory objectFactory)
-
getEnclosingClass
private static java.lang.Class<?> getEnclosingClass(java.lang.Class<?> declaringClass)
Class.getEnclosingClass() only exists on JDK5, so reimplementing it here.
-
findAnnotatedConstructor
private static java.lang.reflect.Constructor<?> findAnnotatedConstructor(IAnnotationFinder finder, java.lang.Class<?> declaringClass)
Find the best constructor given the parameters found on the annotation
-
tryOtherConstructor
public static <T> T tryOtherConstructor(java.lang.Class<T> declaringClass)
-
fileToClass
public static java.lang.Class<?> fileToClass(java.lang.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
- the class name.- Returns:
- the class corresponding to the name specified.
-
findClassesInSameTest
public static XmlClass[] findClassesInSameTest(java.lang.Class<?> cls, XmlSuite suite)
-
-