Class Utils

java.lang.Object
host.anzo.simon.utils.Utils

public class Utils extends Object
A class with some static helper-methods
  • Field Details

    • DEBUG

      @Deprecated public static boolean DEBUG
      Deprecated.
      use JVM argument "java.util.logging.config.file=./log/mylogconfig.properties"
      if this flag is set to TRUE, SIMON tries to load the java.util.logging properties and enabled the debug-mode
  • Constructor Details

    • Utils

      public Utils()
  • Method Details

    • computeMethodHash

      public static long computeMethodHash(Method m)
      Compute the "method hash" of a remote method. The method hash is a long containing the first 64 bits of the SHA digest from the bytes representing the complete method signature.
      Parameters:
      m - the method for which the hash has to be computed
      Returns:
      the computed hash
    • getProtocolFactoryInstance

      public static SimonProtocolCodecFactory getProtocolFactoryInstance(String protocolFactory) throws ClassNotFoundException, InstantiationException, IllegalAccessException
      Loads a protocol codec factory by a given classname
      Parameters:
      protocolFactory - a class name like "com.mydomain.myproject.codec.mySimonProtocolCodecFactory" which points to a class, that extends SimonProtocolCodecFactory. The important thing is, that this class correctly overrides SimonProtocolCodecFactory.setup(boolean). For further details, look at SimonProtocolCodecFactory!
      Returns:
      the protocolcodecfactory instance according to the given protocol factory class name
      Throws:
      IllegalAccessException - if the class or its nullary constructor is not accessible.
      InstantiationException - if this Class represents an abstract class, an interface, an array class, a primitive type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason.
      ClassNotFoundException - if the class is not found by the classloader. if so, please check your classpath.
      ClassCastException - if the given class is no instance of SimonProtocolCodecFactory
    • longToHexString

      public static String longToHexString(long l)
      Converts a long value to a hex string, i.e. 0xF923
      Parameters:
      l -
      Returns:
      return a string showing the hex value of parameter l
    • booleanToByte

      public static byte booleanToByte(boolean bool)
      Converts a boolean value to a byte value.
      Parameters:
      bool -
      Returns:
      0xFF if true, 0x00 if false
    • byteToBoolean

      public static boolean byteToBoolean(byte b) throws IllegalArgumentException
      Converts a byte value to a boolean value.
      Parameters:
      b -
      Returns:
      0xFF if true, 0x00 if false
      Throws:
      IllegalArgumentException - if byte value not 0xFF or 0x00
    • findAllRemoteInterfaces

      public static Class<?> @NotNull [] findAllRemoteInterfaces(Class<?> clazz)
      Method that returns an Class array containing all remote interfaces of a given class
      Parameters:
      clazz - the class to analyze for remote interfaces
      Returns:
      the array with all known remote interfaces
    • isRemoteAnnotated

      public static boolean isRemoteAnnotated(Object remoteObject)
      Checks whether the object is annotated with SimonRemote or not
      Parameters:
      remoteObject - the object to check
      Returns:
      true, if object is annotated, false if not
    • getRemoteAnnotationValue

      public static Class<?>[] getRemoteAnnotationValue(Object remoteObject)
      Returns the value of the SimonRemote annotation.
      Parameters:
      remoteObject - the object to query
      Returns:
      the annotation value
      Throws:
      IllegalArgumentException - in case of remoteObject==null
    • isValidRemote

      public static boolean isValidRemote(Object remoteObject)
      Checks if the given remote object is a valid remote object. Checks for:
      • SimonRemote annotation
      • SimonRemoteMarker proxy
      • implements SimonRemote
      Parameters:
      remoteObject - the object to check
      Returns:
      true, if remote object is valid, false if not
      Throws:
      IllegalRemoteObjectException - thrown in case of a faulty remote object (ie. missing interfaces)
    • isSimonProxy

      public static boolean isSimonProxy(Object o)
      Checks if given object is a simon proxy.
      Parameters:
      o - object to check
      Returns:
      true, if object is a simon proxy, false if not
    • getMarker

      public static SimonRemoteMarker getMarker(Object o)
      Returns the related instance of SimonRemoteMarker of the given object. if the specified object isn't marked, null is returned.
      Parameters:
      o -
      Returns:
      the related instance of SimonRemoteMarker, or null if given object is not marked
    • putAllInterfaceNames

      public static void putAllInterfaceNames(@NotNull @NotNull Object object, List<String> interfaceNames)
      Reads all interfaces and subinterfaces of the given object and add the names to the provided interface name list
      Parameters:
      object - the object to search for interfaces
      interfaceNames - the list to which found interfaces names are added
    • getStackTraceAsString

      public static String getStackTraceAsString(Throwable e)
      Returns the stacktrace of the given throwable as a string. String will be the same as "e.printStackTrace();" woulld print to console
      Parameters:
      e -
      Returns:
      the exceptions stacktrace as a string
    • getRootCause

      public static Throwable getRootCause(Throwable th)
      Looks up and returns the root cause of an exception. If none is found, returns supplied Throwable object unchanged. If root is found, recursively "unwraps" it, and returns the result to the caller.
      Parameters:
      th -
      Returns:
      the exceptions root-cause, if available, otherwise th will be returned unchanged
    • hash

      public static final int hash(Object object)
      Retrieve object hash code and applies a supplemental hash function to the result hash, which defends against poor quality hash functions. This is critical because HashMap uses power-of-two length hash tables, that otherwise encounter collisions for hashCodes that do not differ in lower bits. Note: Null keys always map to hash 0, thus index 0.
    • registerMBean

      public static boolean registerMBean(Object o, String objectNameOfMBean)
      see MBeanServer#registerMBean(Object, ObjectName) This is a workaround to be able to run the code also on android, where the MBeanServer is not available
      Returns:
      true, if registration succeeds, false if not