Package org.djunits.util
Class ClassUtil
java.lang.Object
org.djunits.util.ClassUtil
ClassUtil is a utility class providing assistance for Java Classes.
Copyright (c) 2002-2009 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
See for project information www.getSI()mulation.tudelft.nl.
The DSOL project is distributed under the following BSD-style license:
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of Delft University of Technology, nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
- Since:
- 1.5
- Version:
- $Revision: 1.3 $ $Date: 2010/08/10 11:39:11 $
- Author:
- Peter Jacobs, Niels Lang, Alexander Verbraeck
-
Method Summary
Modifier and TypeMethodDescriptionstatic Constructor<?>[]
getAllConstructors
(Class<?> clazz, Constructor<?>[] result) gets all the constructors of a class and adds the result to result.getAllFields
(Class<?> clazz) gets all the fields of a class (public, protected, package, and private).getAllFields
(Class<?> clazz, Set<Field> result) gets all the fields of a class (public, protected, package, and private) and adds the result to the return value.static Method[]
getAllMethods
(Class<?> clazz, String name, Method[] result) gets all the methods of a class and adds the result to result.static Class<?>[]
converts an array of objects to their corresponding classes.static boolean
isMoreSpecific
(Class<?>[] a, Class<?>[] b) Determines & returns whether constructor 'a' is more specific than constructor 'b', as defined in the Java Language Specification ???static boolean
isMoreSpecific
(Constructor<?> a, Constructor<?> b) Determines & returns whether constructor 'a' is more specific than constructor 'b', as defined in the Java Language Specification ???static boolean
isMoreSpecific
(Method a, Method b) Determines & returns whether constructor 'a' is more specific than constructor 'b', as defined in the Java Language Specification ???static boolean
Returns whether a declaringClass is accessible according to the modifiers.static boolean
isVisible
(Constructor<?> constructor, Class<?> caller) Returns whether a constructor is visible for a caller.static boolean
Returns whether a field is visible for a caller.static boolean
Returns whether a method is visible for a caller.static Constructor<?>[]
matchSignature
(Constructor<?>[] constructors, Class<?>... argTypes) Filters an array methods for signatures that are compatible with a given signature.static boolean
matchSignature
(Constructor<?> constructor, Class<?>... argTypes) Filters an array methods for signatures that are compatible with a given signature.static Method[]
matchSignature
(Method[] methods, String name, Class<?>... argTypes) Filters an array methods for signatures that are compatible with a given signature.static boolean
matchSignature
(Method method, String name, Class<?>... argTypes) Filters an array methods for signatures that are compatible with a given signature.static Constructor<?>
resolveConstructor
(Class<?> clazz, Class<?>... parameterTypes) returns the interface method.static Constructor<?>
resolveConstructor
(Class<?> clazz, Class<?> callerClass, Class<?>... parameterTypes) returns the interface method.static Constructor<?>
resolveConstructor
(Class<?> clazz, Object[] arguments) returns the constructor.static Field
resolveField
(Class<?> clazz, Class<?> callerClass, String name) returns the field.static Field
resolveField
(Class<?> clazz, String fieldName) resolves the field for a class, taking into account inner classes.static Field
resolveField
(Object object, String fieldName) resolves the field for a given object instance.static Method
resolveMethod
(Class<?> clazz, Class<?> callerClass, String name, Class<?>... parameterTypes) returns the interface method.static Method
resolveMethod
(Class<?> clazz, String name, Class<?>... parameterTypes) returns the interface method.static Method
resolveMethod
(Object object, String name, Class<?>... parameterTypes) resolves a method the method.static Method
resolveMethod
(Object object, String name, Object[] arguments) returns the method.
-
Method Details
-
getAllConstructors
gets all the constructors of a class and adds the result to result.- Parameters:
clazz
- the classresult
- the resulting set- Returns:
- result
-
resolveConstructor
public static Constructor<?> resolveConstructor(Class<?> clazz, Class<?> callerClass, Class<?>... parameterTypes) throws NoSuchMethodException returns the interface method.- Parameters:
clazz
- the class to start withcallerClass
- the calling classparameterTypes
- the parameterTypes- Returns:
- Constructor
- Throws:
NoSuchMethodException
- if the method cannot be resolved
-
resolveConstructor
public static Constructor<?> resolveConstructor(Class<?> clazz, Class<?>... parameterTypes) throws NoSuchMethodException returns the interface method.- Parameters:
clazz
- the class to start withparameterTypes
- the parameterTypes- Returns:
- Constructor
- Throws:
NoSuchMethodException
- if the method cannot be resolved
-
resolveConstructor
public static Constructor<?> resolveConstructor(Class<?> clazz, Object[] arguments) throws NoSuchMethodException returns the constructor.- Parameters:
clazz
- the clazz to start witharguments
- the arguments- Returns:
- Constructor
- Throws:
NoSuchMethodException
- on lookup failure
-
getAllFields
gets all the fields of a class (public, protected, package, and private) and adds the result to the return value.- Parameters:
clazz
- the classresult
- the resulting set- Returns:
- the set of fields including all fields of the field clazz
-
getAllFields
gets all the fields of a class (public, protected, package, and private).- Parameters:
clazz
- the class- Returns:
- all fields of the class
-
resolveField
resolves the field for a class, taking into account inner classes.- Parameters:
clazz
- the class to resolve the field for, including inner classesfieldName
- name of the field- Returns:
- Field the field
- Throws:
NoSuchFieldException
- on no such field
-
resolveField
public static Field resolveField(Class<?> clazz, Class<?> callerClass, String name) throws NoSuchFieldException returns the field.- Parameters:
clazz
- the class to start withcallerClass
- the calling classname
- the fieldName- Returns:
- Constructor
- Throws:
NoSuchFieldException
- if the method cannot be resolved
-
resolveField
resolves the field for a given object instance.- Parameters:
object
- the object to resolve the field forfieldName
- name of the field to resolve- Returns:
- the field (if found)
- Throws:
NoSuchFieldException
- if the field cannot be resolved
-
getAllMethods
gets all the methods of a class and adds the result to result.- Parameters:
clazz
- the classname
- the name of the methodresult
- the resulting set- Returns:
- result
-
resolveMethod
public static Method resolveMethod(Class<?> clazz, Class<?> callerClass, String name, Class<?>... parameterTypes) throws NoSuchMethodException returns the interface method.- Parameters:
clazz
- the class to start withcallerClass
- the caller classname
- the name of the methodparameterTypes
- the parameterTypes- Returns:
- Method
- Throws:
NoSuchMethodException
- on lookup failure
-
resolveMethod
public static Method resolveMethod(Class<?> clazz, String name, Class<?>... parameterTypes) throws NoSuchMethodException returns the interface method.- Parameters:
clazz
- the class to start withname
- the name of the methodparameterTypes
- the parameterTypes- Returns:
- Method
- Throws:
NoSuchMethodException
- on lookup failure
-
resolveMethod
public static Method resolveMethod(Object object, String name, Class<?>... parameterTypes) throws NoSuchMethodException resolves a method the method.- Parameters:
object
- the object to start withname
- the name of the methodparameterTypes
- the parameterTypes- Returns:
- Method
- Throws:
NoSuchMethodException
- on lookup failure
-
resolveMethod
public static Method resolveMethod(Object object, String name, Object[] arguments) throws NoSuchMethodException returns the method.- Parameters:
object
- the object to start withname
- the name of the methodarguments
- the arguments- Returns:
- Method
- Throws:
NoSuchMethodException
- on lookup failure
-
isVisible
Returns whether a declaringClass is accessible according to the modifiers.- Parameters:
modifiers
- the modifiersdeclaringClass
- the declaringClasscaller
- the caller- Returns:
- boolean isVisible
-
isMoreSpecific
Determines & returns whether constructor 'a' is more specific than constructor 'b', as defined in the Java Language Specification ???15.12.- Parameters:
a
- reflects the first constructorb
- reflects the second constructor- Returns:
- true if 'a' is more specific than b, false otherwise. 'false' is also returned when constructors are incompatible, e.g. have different names or a different number of parameters.
-
isMoreSpecific
Determines & returns whether constructor 'a' is more specific than constructor 'b', as defined in the Java Language Specification ???15.12.- Parameters:
a
- reflects the first constructorb
- reflects the second constructor- Returns:
- true if 'a' is more specific than b, false otherwise. 'false' is also returned when constructors are incompatible, e.g. have different names or a different number of parameters.
-
isMoreSpecific
Determines & returns whether constructor 'a' is more specific than constructor 'b', as defined in the Java Language Specification ???15.12.- Parameters:
a
- reflects the first methodb
- reflects the second method- Returns:
- true if 'a' is more specific than b, false otherwise. 'false' is also returned when constructors are incompatible, e.g. have different names or a different number of parameters.
-
isVisible
Returns whether a field is visible for a caller.- Parameters:
field
- The fieldcaller
- The class of the caller for whom invocation visibility is checked.- Returns:
- boolean yes or no
-
isVisible
Returns whether a constructor is visible for a caller.- Parameters:
constructor
- The constructorcaller
- The class of the caller for whom invocation visibility is checked.- Returns:
- boolean yes or no
-
isVisible
Returns whether a method is visible for a caller.- Parameters:
method
- The methodcaller
- The class of the caller for whom invocation visibility is checked.- Returns:
- boolean yes or no
-
matchSignature
Filters an array methods for signatures that are compatible with a given signature.- Parameters:
methods
- which are methods to be filtered.name
- reflects the method's name, part of the signatureargTypes
- are the method's argument types- Returns:
- Method[] An unordered Method-array consisting of the elements of 'methods' that match with the given signature. An array with 0 elements is returned when no matching Method objects are found.
-
matchSignature
Filters an array methods for signatures that are compatible with a given signature.- Parameters:
method
- The method to be filtered.name
- reflects the method's name, part of the signatureargTypes
- are the method's argument types- Returns:
- boolean if methodParameters assignable from argTypes
-
matchSignature
Filters an array methods for signatures that are compatible with a given signature.- Parameters:
constructor
- which are constructors to be filtered.argTypes
- are the constructor's argument types- Returns:
- boolean if methodParameters assignable from argTypes
-
matchSignature
Filters an array methods for signatures that are compatible with a given signature.- Parameters:
constructors
- which are constructors to be filtered.argTypes
- are the constructor's argument types- Returns:
- Constructor<?>[] An unordered Constructor-array consisting of the elements of 'constructors' that match with the given signature. An array with 0 elements is returned when no matching Method objects are found.
-
getClass
converts an array of objects to their corresponding classes.- Parameters:
array
- the array to invoke- Returns:
- Class<?>[] the result;
-