Class JavaClassHelper

java.lang.Object
com.espertech.esper.common.internal.util.JavaClassHelper

public class JavaClassHelper extends Object
Helper for questions about EPType and Classes.
  • Field Details

    • APACHE_AVRO_GENERIC_RECORD_CLASSNAME

      public static final String APACHE_AVRO_GENERIC_RECORD_CLASSNAME
      See Also:
  • Constructor Details

    • JavaClassHelper

      public JavaClassHelper()
  • Method Details

    • isImplementsCharSequence

      public static boolean isImplementsCharSequence(EPType type)
    • isArrayTypeCompatible

      public static boolean isArrayTypeCompatible(Class<?> target, Class<?> provided)
    • isCollectionMapOrArray

      public static boolean isCollectionMapOrArray(EPType returnType)
    • getBoxedType

      public static EPType getBoxedType(EPType type)
    • getBoxedType

      public static EPTypeClass getBoxedType(EPTypeClass clazz)
    • getBoxedType

      public static Class<?> getBoxedType(Class<?> clazz)
      Returns the boxed class for the given class, or the class itself if already boxed or not a primitive type. For primitive unboxed types returns the boxed types, e.g. returns java.lang.Integer for passing int.class. For any other class, returns the class passed.
      Parameters:
      clazz - is the class to return the boxed class for
      Returns:
      boxed variant of the same class
    • getPrimitiveType

      public static EPTypeClass getPrimitiveType(Class<?> clazz)
      Returns the un-boxed class for the given class, or the class itself if already un-boxed or not a primitive type. For primitive boxed types returns the unboxed primitive type, e.g. returns int.class for passing Integer.class. For any other class, returns the class passed.
      Parameters:
      clazz - is the class to return the unboxed (or primitive) class for
      Returns:
      primitive variant of the same class
    • getPrimitiveType

      public static EPTypeClass getPrimitiveType(EPTypeClass clazz)
      Returns the un-boxed class for the given class, or the class itself if already un-boxed or not a primitive type. For primitive boxed types returns the unboxed primitive type, e.g. returns int.class for passing Integer.class. For any other class, returns the class passed.
      Parameters:
      clazz - is the class to return the unboxed (or primitive) class for
      Returns:
      primitive variant of the same class
    • isNumeric

      public static boolean isNumeric(EPType clazz)
    • isNumeric

      public static boolean isNumeric(Class<?> clazz)
      Determines if the class passed in is one of the numeric classes.
      Parameters:
      clazz - to check
      Returns:
      true if numeric, false if not
    • isNumericNonFP

      public static boolean isNumericNonFP(EPType type)
      Determines if the type passed in is one of the numeric classes and not a floating point.
      Parameters:
      type - to check
      Returns:
      true if numeric and not a floating point, false if not
    • isNumericNonFP

      public static boolean isNumericNonFP(Class<?> clazz)
      Determines if the class passed in is one of the numeric classes and not a floating point.
      Parameters:
      clazz - to check
      Returns:
      true if numeric and not a floating point, false if not
    • isAssignmentCompatible

      public static boolean isAssignmentCompatible(EPType invocationType, Class<?> declarationType)
    • isAssignmentCompatible

      public static boolean isAssignmentCompatible(Class<?> invocationType, Class<?> declarationType)
      Returns true if 2 classes are assignment compatible.
      Parameters:
      invocationType - type to assign from
      declarationType - type to assign to
      Returns:
      true if assignment compatible, false if not
    • isTypeBoolean

      public static boolean isTypeBoolean(EPType type)
      Determines if the class passed in is a boolean boxed or unboxed type.
      Parameters:
      type - to check
      Returns:
      true if boolean, false if not
    • isTypeInteger

      public static boolean isTypeInteger(EPType type)
      Determines if the class passed in is a boolean boxed or unboxed type.
      Parameters:
      type - to check
      Returns:
      true if boolean, false if not
    • isTypeLong

      public static boolean isTypeLong(EPType type)
      Determines if the class passed in is a boolean boxed or unboxed type.
      Parameters:
      type - to check
      Returns:
      true if boolean, false if not
    • isTypeDouble

      public static boolean isTypeDouble(EPType type)
      Determines if the class passed in is a boolean boxed or unboxed type.
      Parameters:
      type - to check
      Returns:
      true if boolean, false if not
    • isTypeString

      public static boolean isTypeString(EPType type)
      Determines if the class passed in is a boolean boxed or unboxed type.
      Parameters:
      type - to check
      Returns:
      true if boolean, false if not
    • isTypePrimitive

      public static boolean isTypePrimitive(EPType type)
    • isType

      public static boolean isType(EPType type, Class<?> clazz)
    • isTypeBoolean

      public static boolean isTypeBoolean(Class<?> clazz)
      Determines if the class passed in is a boolean boxed or unboxed type.
      Parameters:
      clazz - to check
      Returns:
      true if boolean, false if not
    • isTypeInteger

      public static boolean isTypeInteger(Class<?> clazz)
      Determines if the class passed in is a boolean boxed or unboxed type.
      Parameters:
      clazz - to check
      Returns:
      true if boolean, false if not
    • isTypeLong

      public static boolean isTypeLong(Class<?> clazz)
      Determines if the class passed in is a boolean boxed or unboxed type.
      Parameters:
      clazz - to check
      Returns:
      true if boolean, false if not
    • isTypeDouble

      public static boolean isTypeDouble(Class<?> clazz)
      Determines if the class passed in is a boolean boxed or unboxed type.
      Parameters:
      clazz - to check
      Returns:
      true if boolean, false if not
    • getArithmaticCoercionType

      public static EPTypeClass getArithmaticCoercionType(EPTypeClass typeOne, EPTypeClass typeTwo) throws CoercionException
      Returns the coercion type for the 2 numeric types for use in arithmatic. Note: byte and short types always result in integer.
      Parameters:
      typeOne - is the first type
      typeTwo - is the second type
      Returns:
      coerced type
      Throws:
      CoercionException - if types don't allow coercion
    • getArithmaticCoercionType

      public static Class<?> getArithmaticCoercionType(Class<?> typeOne, Class<?> typeTwo) throws CoercionException
      Returns the coercion type for the 2 numeric types for use in arithmatic. Note: byte and short types always result in integer.
      Parameters:
      typeOne - is the first type
      typeTwo - is the second type
      Returns:
      coerced type
      Throws:
      CoercionException - if types don't allow coercion
    • coerceBoxed

      public static Number coerceBoxed(Number numToCoerce, Class<?> resultBoxedType)
      Coerce the given number to the given type, assuming the type is a Boxed type. Allows coerce to lower resultion number. Does't coerce to primitive types.

      Meant for statement compile-time use, not for runtime use.

      Parameters:
      numToCoerce - is the number to coerce to the given type
      resultBoxedType - is the boxed result type to return
      Returns:
      the numToCoerce as a value in the given result type
    • coerceNumberBoxedToBoxedCodegen

      public static CodegenExpression coerceNumberBoxedToBoxedCodegen(CodegenExpression exprReturningBoxed, EPTypeClass fromTypeBoxed, EPTypeClass targetTypeBoxed)
    • coerceNumberToBoxedCodegen

      public static CodegenExpression coerceNumberToBoxedCodegen(CodegenExpression expr, EPTypeClass fromType, EPTypeClass targetTypeBoxed)
    • isFloatingPointNumber

      public static boolean isFloatingPointNumber(Number number)
      Returns true if the Number instance is a floating point number.
      Parameters:
      number - to check
      Returns:
      true if number is Float or Double type
    • isFloatingPointClass

      public static boolean isFloatingPointClass(EPType type)
      Returns true if the supplied type is a floating point number.
      Parameters:
      type - to check
      Returns:
      true if primitive or boxed float or double
    • isFloatingPointClass

      public static boolean isFloatingPointClass(Class<?> clazz)
      Returns true if the supplied type is a floating point number.
      Parameters:
      clazz - to check
      Returns:
      true if primitive or boxed float or double
    • getCompareToCoercionType

      public static EPTypeClass getCompareToCoercionType(EPType typeOne, EPType typeTwo) throws CoercionException
      Returns for 2 classes to be compared via relational operator the Class type of common comparison. The output is always Long.class, Double.class, String.class or Boolean.class depending on whether the passed types are numeric and floating-point. Accepts primitive as well as boxed types.
      Parameters:
      typeOne - is the first type
      typeTwo - is the second type
      Returns:
      One of Long.class, Double.class or String.class
      Throws:
      CoercionException - if the types cannot be compared
    • canCoerce

      public static boolean canCoerce(Class<?> numberClassToBeCoerced, Class<?> numberClassToCoerceTo)
      Determines if a number can be coerced upwards to another number class without loss.

      Clients must pass in two classes that are numeric types.

      Any number class can be coerced to double, while only double cannot be coerced to float. Any non-floating point number can be coerced to long. Integer can be coerced to Byte and Short even though loss is possible, for convenience.

      Parameters:
      numberClassToBeCoerced - the number class to be coerced
      numberClassToCoerceTo - the number class to coerce to
      Returns:
      true if numbers can be coerced without loss, false if not
    • getBoxedClassName

      public static String getBoxedClassName(String className)
      Returns for the class name given the class name of the boxed (wrapped) type if the class name is one of the Java primitive types.
      Parameters:
      className - is a class name, a Java primitive type or other class
      Returns:
      boxed class name if Java primitive type, or just same class name passed in if not a primitive type
    • isJavaBuiltinDataType

      public static boolean isJavaBuiltinDataType(EPType type)
      Returns true if the class passed in is a Java built-in data type (primitive or wrapper) including String and 'null'.
      Parameters:
      type - to check
      Returns:
      true if built-in data type, or false if not
    • isJavaBuiltinDataType

      public static boolean isJavaBuiltinDataType(Class<?> clazz)
      Returns true if the class passed in is a Java built-in data type (primitive or wrapper) including String and 'null'.
      Parameters:
      clazz - to check
      Returns:
      true if built-in data type, or false if not
    • getCommonCoercionType

      public static EPType getCommonCoercionType(EPType[] types) throws CoercionException
      Determines a common denominator type to which one or more types can be casted or coerced. For use in determining the result type in certain expressions (coalesce, case).

      Null values are allowed as part of the input and indicate a 'null' constant value in an expression tree. Such as value doesn't have any type and can be ignored in determining a result type.

      For numeric types, determines a coercion type that all types can be converted to via the method getArithmaticCoercionType.

      Indicates that there is no common denominator type by throwing CoercionException.

      Parameters:
      types - is an array of one or more types, which can be Java built-in (primitive or wrapper) or user types
      Returns:
      common denominator type if any can be found, for use in comparison
      Throws:
      CoercionException - when no coercion type could be determined
    • getClassForName

      public static Class<?> getClassForName(String className, ClassForNameProvider classForNameProvider) throws ClassNotFoundException
      Returns the class given a fully-qualified class name.
      Parameters:
      className - is the fully-qualified class name, java primitive types included.
      classForNameProvider - lookup of class for class name
      Returns:
      class for name
      Throws:
      ClassNotFoundException - if the class cannot be found
    • getClassForSimpleName

      public static Class<?> getClassForSimpleName(String className, ClassForNameProvider classForNameProvider)
      Returns the boxed class for the given classname, recognizing all primitive and abbreviations, uppercase and lowercase.

      Recognizes "int" as Integer.class and "strIng" as String.class, and "Integer" as Integer.class, and so on.

      Parameters:
      className - is the name to recognize
      classForNameProvider - lookup of class for class name
      Returns:
      class
    • getSimpleNameForClass

      public static String getSimpleNameForClass(EPTypeClass clazz)
    • getSimpleNameForClass

      public static String getSimpleNameForClass(Class<?> clazz)
    • getPrimitiveClassForName

      public static Class<?> getPrimitiveClassForName(String typeName)
      Returns the class for a Java primitive type name, ignoring case, and considering String as a primitive.
      Parameters:
      typeName - is a potential primitive Java type, or some other type name
      Returns:
      class for primitive type name, or null if not a primitive type.
    • parse

      public static Object parse(Class<?> clazz, String text)
      Parse the String using the given Java built-in class for parsing.
      Parameters:
      clazz - is the class to parse the value to
      text - is the text to parse
      Returns:
      value matching the type passed in
    • isImplementsInterface

      public static boolean isImplementsInterface(EPType type, Class<?> interfaceClass)
      Method to check if a given class, and its superclasses and interfaces (deep), implement a given interface.
      Parameters:
      type - to check, including all its superclasses and their interfaces and extends
      interfaceClass - is the interface class to look for
      Returns:
      true if such interface is implemented by any of the clazz or its superclasses or extends by any interface and superclasses (deep check)
    • isImplementsInterface

      public static boolean isImplementsInterface(Class<?> clazz, Class<?> interfaceClass)
      Method to check if a given class, and its superclasses and interfaces (deep), implement a given interface.
      Parameters:
      clazz - to check, including all its superclasses and their interfaces and extends
      interfaceClass - is the interface class to look for
      Returns:
      true if such interface is implemented by any of the clazz or its superclasses or extends by any interface and superclasses (deep check)
    • isSubclassOrImplementsInterface

      public static boolean isSubclassOrImplementsInterface(EPType extendorOrImplementor, EPTypeClass extendedOrImplemented)
    • isSubclassOrImplementsInterface

      public static boolean isSubclassOrImplementsInterface(EPType extendorOrImplementor, Class<?> extendedOrImplemented)
    • isSubclassOrImplementsInterface

      public static boolean isSubclassOrImplementsInterface(Class<?> extendorOrImplementor, Class<?> extendedOrImplemented)
      Method to check if a given class, and its superclasses and interfaces (deep), implement a given interface or extend a given class.
      Parameters:
      extendorOrImplementor - is the class to inspect its extends and implements clauses
      extendedOrImplemented - is the potential interface, or superclass, to check
      Returns:
      true if such interface is implemented by any of the clazz or its superclasses or extends by any interface and superclasses (deep check)
    • instantiate

      public static <T> T instantiate(Class<T> implementedOrExtendedClass, String className, ClassForNameProvider classForNameProvider) throws ClassInstantiationException
      Looks up the given class and checks that it implements or extends the required interface, and instantiates an object.
      Type Parameters:
      T - type
      Parameters:
      implementedOrExtendedClass - is the class that the looked-up class should extend or implement
      className - of the class to load, check type and instantiate
      classForNameProvider - lookup of class for class name
      Returns:
      instance of given class, via newInstance
      Throws:
      ClassInstantiationException - if the type does not match or the class cannot be loaded or an object instantiated
    • instantiate

      public static Object instantiate(Class<?> implementedOrExtendedClass, Class<?> clazz) throws ClassInstantiationException
      Checks that the given class implements or extends the required interface (first parameter), and instantiates an object.
      Parameters:
      implementedOrExtendedClass - is the class that the looked-up class should extend or implement
      clazz - to check type and instantiate
      Returns:
      instance of given class, via newInstance
      Throws:
      ClassInstantiationException - if the type does not match or the class cannot be loaded or an object instantiated
    • getSuper

      public static void getSuper(Class<?> clazz, Set<Class> result)
      Populates all interface and superclasses for the given class, recursivly.
      Parameters:
      clazz - to reflect upon
      result - set of classes to populate
    • isSimpleNameFullyQualfied

      public static boolean isSimpleNameFullyQualfied(String simpleClassName, String fullyQualifiedClassname)
      Returns true if the simple class name is the class name of the fully qualified classname.

      This method does not verify validity of class and package names, it uses simple string compare inspecting the trailing part of the fully qualified class name.

      Parameters:
      simpleClassName - simple class name
      fullyQualifiedClassname - fully qualified class name contains package name and simple class name
      Returns:
      true if simple class name of the fully qualified class name, false if not
    • isFragmentableType

      public static boolean isFragmentableType(EPType type)
      Returns true if the Class is a fragmentable type, i.e. not a primitive or boxed type or any of the common built-in types or does not implement Map.
      Parameters:
      type - type to check
      Returns:
      true if fragmentable
    • isFragmentableType

      public static boolean isFragmentableType(Class<?> propertyType)
      Returns true if the Class is a fragmentable type, i.e. not a primitive or boxed type or any of the common built-in types or does not implement Map.
      Parameters:
      propertyType - type to check
      Returns:
      true if fragmentable
    • getSuperInterfaces

      public static Class[] getSuperInterfaces(Class<?> clazz)
    • getSuperInterfaces

      public static void getSuperInterfaces(Class<?> clazz, Set<Class> result)
    • getGenericFieldType

      public static Class<?> getGenericFieldType(Field field, boolean isAllowNull)
      Returns the generic type parameter of a return value by a field.
      Parameters:
      field - field or null if method
      isAllowNull - whether null is allowed as a return value or expected Object.class
      Returns:
      generic type parameter
    • getGenericFieldTypeMap

      public static Class<?> getGenericFieldTypeMap(Field field, boolean isAllowNull)
      Returns the generic type parameter of a return value by a field or method.
      Parameters:
      field - field or null if method
      isAllowNull - whether null is allowed as a return value or expected Object.class
      Returns:
      generic type parameter
    • getGenericType

      public static Class<?> getGenericType(Type t, int index)
    • getArrayType

      public static Class<?> getArrayType(Class<?> resultType)
    • getArrayDimensions

      public static int getArrayDimensions(Class<?> clazz)
    • getArrayComponentTypeInnermost

      public static Class<?> getArrayComponentTypeInnermost(Class<?> clazz)
    • getArrayType

      public static Class<?> getArrayType(Class<?> resultType, int numberOfDimensions)
    • getMethodByName

      public static Method getMethodByName(Class<?> clazz, String methodName)
    • printInstance

      public static String printInstance(Object instance, boolean fullyQualified)
    • writeInstance

      public static void writeInstance(StringWriter writer, Object instance, boolean fullyQualified)
    • writeInstance

      public static void writeInstance(StringWriter writer, String title, Object instance)
    • getMessageInvocationTarget

      public static String getMessageInvocationTarget(String optionalStatementName, String methodName, Class[] methodParameters, String classOrPropertyName, Object[] args, Throwable targetException)
    • isDatetimeClass

      public static boolean isDatetimeClass(EPType inputType)
    • isDatetimeClass

      public static boolean isDatetimeClass(Class<?> inputType)
    • getClassObjectFromPropertyTypeNames

      public static Map<String,Object> getClassObjectFromPropertyTypeNames(Properties properties, ClassForNameProvider classForNameProvider) throws ClassNotFoundException
      Throws:
      ClassNotFoundException
    • getClassInClasspath

      public static Class<?> getClassInClasspath(String classname, ClassForNameProvider classForNameProvider)
    • isSignatureCompatible

      public static boolean isSignatureCompatible(Class<?>[] one, Class<?>[] two)
    • findRequiredMethod

      public static Method findRequiredMethod(Class<?> clazz, String methodName)
    • getAnnotations

      public static List<Annotation> getAnnotations(Class<? extends Annotation> annotationClass, Annotation[] annotations)
    • isAnnotationListed

      public static boolean isAnnotationListed(Class<? extends Annotation> annotationClass, Annotation[] annotations)
    • findAnnotatedFields

      public static Set<Field> findAnnotatedFields(Class<?> targetClass, Class<? extends Annotation> annotation)
    • findAnnotatedMethods

      public static Set<Method> findAnnotatedMethods(Class<?> targetClass, Class<? extends Annotation> annotation)
    • setFieldForAnnotation

      public static void setFieldForAnnotation(Object target, Class<? extends Annotation> annotation, Object value)
    • takeFirstN

      public static Type[] takeFirstN(Type[] types, int numToTake)
    • getObjectValuePretty

      public static void getObjectValuePretty(Object value, StringWriter writer)
    • isTypeVoid

      public static boolean isTypeVoid(EPType type)
    • isTypeVoid

      public static boolean isTypeVoid(Class<?> clazz)
    • isTypeOrNull

      public static boolean isTypeOrNull(EPType type, Class<?> expected)
    • traverseAnnotations

      public static <T> void traverseAnnotations(List<Class> classes, Class<T> annotationClass, BiConsumer<Class,T> consumer)
    • getArrayComponentType

      public static EPTypeClass getArrayComponentType(EPTypeClass type)
    • getArrayComponentTypeInnermost

      public static EPTypeClass getArrayComponentTypeInnermost(EPTypeClass type)
    • getSingleParameterTypeOrObject

      public static EPTypeClass getSingleParameterTypeOrObject(EPTypeClass returnType)
    • getSecondParameterTypeOrObject

      public static EPTypeClass getSecondParameterTypeOrObject(EPTypeClass returnType)
    • getTypeClassOrObjectType

      public static EPTypeClass getTypeClassOrObjectType(EPType evaluationType)
    • getArrayType

      public static EPTypeClass getArrayType(EPTypeClass type)
    • getArrayType

      public static EPTypeClass getArrayType(EPTypeClass type, int numDimensions)
    • getClassNameNormalized

      public static String getClassNameNormalized(EPTypeClass clazz)