Example usage for java.lang Class hashCode

List of usage examples for java.lang Class hashCode

Introduction

In this page you can find the example usage for java.lang Class hashCode.

Prototype

@HotSpotIntrinsicCandidate
public native int hashCode();

Source Link

Document

Returns a hash code value for the object.

Usage

From source file:Main.java

/**
 * Returns an empty array of the specified type.  The intent is that
 * it will return the same empty array every time to avoid reallocation,
 * although this is not guaranteed./*  w ww. j  a  va  2s  .  co m*/
 */
@SuppressWarnings("unchecked")
public static <T> T[] emptyArray(Class<T> kind) {
    if (kind == Object.class) {
        //            return (T[]) EmptyArray.OBJECT;
        return (T[]) new Object[0];
    }

    int bucket = (kind.hashCode() & 0x7FFFFFFF) % CACHE_SIZE;
    Object cache = sCache[bucket];

    if (cache == null || cache.getClass().getComponentType() != kind) {
        cache = Array.newInstance(kind, 0);
        sCache[bucket] = cache;

        // Log.e("cache", "new empty " + kind.getName() + " at " + bucket);
    }

    return (T[]) cache;
}

From source file:net.geoprism.context.ServerInitializer.java

@Request
private static void startup(List<ServerContextListenerInfo> infos) {
    for (ServerContextListenerInfo info : infos) {
        try {/*  w  w  w  .ja v a 2  s  .c  om*/

            Class<?> clazz = LoaderDecorator.load(info.getClassName());
            Object newInstance = clazz.newInstance();

            try {
                ServerContextListener listener = (ServerContextListener) newInstance;
                listener.startup();
            } catch (ClassCastException e) {
                log.error("ClassCastException initializer", e);

                Class<? extends Object> class1 = newInstance.getClass();
                ClassLoader loader1 = class1.getClassLoader();

                log.debug("New instance class : " + class1.hashCode());
                log.debug("New instance class loader: " + loader1.hashCode());

                Class<? extends Object> class2 = ServerContextListener.class;
                ClassLoader loader2 = class2.getClassLoader();

                log.debug("Interface class : " + class2.hashCode());
                log.debug("New instance class loader: " + loader2.hashCode());

                clazz.getMethod("startup").invoke(newInstance);
            }

            log.debug("COMLPETE: " + info.getClassName() + ".setup();");
        } catch (Exception e) {
            log.error(e);

            throw new ProgrammingErrorException(
                    "Unable to startup the server context listener [" + info.getClassName() + "]", e);
        }
    }
}

From source file:net.geoprism.context.ServerInitializer.java

@Request
public static void destroy() {
    ServerContextListenerDocumentBuilder builder = new ServerContextListenerDocumentBuilder();
    List<ServerContextListenerInfo> infos = builder.read();

    Collections.reverse(infos);//from   ww w . jav a2 s .  c om

    for (ServerContextListenerInfo info : infos) {
        try {

            Class<?> clazz = LoaderDecorator.load(info.getClassName());
            Object newInstance = clazz.newInstance();

            try {
                ServerContextListener listener = (ServerContextListener) newInstance;
                listener.shutdown();
            } catch (ClassCastException e) {
                log.error("ClassCastException in ServerInitializer.shutdown", e);

                Class<? extends Object> class1 = newInstance.getClass();
                ClassLoader loader1 = class1.getClassLoader();

                log.debug("New instance class : " + class1.hashCode());
                log.debug("New instance class loader: " + loader1.hashCode());

                Class<? extends Object> class2 = ServerContextListener.class;
                ClassLoader loader2 = class2.getClassLoader();

                log.debug("Interface class : " + class2.hashCode());
                log.debug("New instance class loader: " + loader2.hashCode());

                clazz.getMethod("shutdown").invoke(newInstance);
            }

            log.debug("COMLPETE: " + info.getClassName() + ".shutdown();");
        } catch (Exception e) {
            log.error(e);

            throw new ProgrammingErrorException(
                    "Unable to shutdown the server context listener [" + info.getClassName() + "]", e);
        }
    }
}

From source file:Main.java

/**
 * Format a string buffer containing the Class, Interfaces, CodeSource, and
 * ClassLoader information for the given object clazz.
 * /*from   w ww  . ja  v a  2s  .c om*/
 * @param clazz
 *          the Class
 * @param results -
 *          the buffer to write the info to
 */
public static void displayClassInfo(Class clazz, StringBuffer results) {
    // Print out some codebase info for the clazz
    ClassLoader cl = clazz.getClassLoader();
    results.append("\n");
    results.append(clazz.getName());
    results.append("(");
    results.append(Integer.toHexString(clazz.hashCode()));
    results.append(").ClassLoader=");
    results.append(cl);
    ClassLoader parent = cl;
    while (parent != null) {
        results.append("\n..");
        results.append(parent);
        URL[] urls = getClassLoaderURLs(parent);
        int length = urls != null ? urls.length : 0;
        for (int u = 0; u < length; u++) {
            results.append("\n....");
            results.append(urls[u]);
        }
        if (parent != null)
            parent = parent.getParent();
    }
    CodeSource clazzCS = clazz.getProtectionDomain().getCodeSource();
    if (clazzCS != null) {
        results.append("\n++++CodeSource: ");
        results.append(clazzCS);
    } else
        results.append("\n++++Null CodeSource");

    results.append("\nImplemented Interfaces:");
    Class[] ifaces = clazz.getInterfaces();
    for (int i = 0; i < ifaces.length; i++) {
        Class iface = ifaces[i];
        results.append("\n++");
        results.append(iface);
        results.append("(");
        results.append(Integer.toHexString(iface.hashCode()));
        results.append(")");
        ClassLoader loader = ifaces[i].getClassLoader();
        results.append("\n++++ClassLoader: ");
        results.append(loader);
        ProtectionDomain pd = ifaces[i].getProtectionDomain();
        CodeSource cs = pd.getCodeSource();
        if (cs != null) {
            results.append("\n++++CodeSource: ");
            results.append(cs);
        } else
            results.append("\n++++Null CodeSource");
    }
}

From source file:org.bonitasoft.engine.commons.ClassReflector.java

public static Method getMethod(final Class<?> clazz, final String methodName, final Class<?>... parameterTypes)
        throws NoSuchMethodException {
    final StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(clazz.hashCode());
    stringBuilder.append(':');
    stringBuilder.append(clazz.getName());
    stringBuilder.append('.');
    stringBuilder.append(methodName);//from  w w w  .  j  a v  a 2 s .c om
    stringBuilder.append('(');
    if (parameterTypes != null) {
        for (final Class<?> class1 : parameterTypes) {
            stringBuilder.append(class1.getName());
            stringBuilder.append(',');
        }
    }
    stringBuilder.append(')');
    final String key = stringBuilder.toString();
    putIfAbsent(clazz, methodName, key, parameterTypes);
    return methods.get(key);
}

From source file:org.openmrs.module.maternalsummary.impl.MaternalSummaryServiceImpl.java

@SuppressWarnings("rawtypes")
private static Integer buildKey(Class clazz, int patientId) {
    return clazz.getName().hashCode() * clazz.hashCode() + patientId;
}

From source file:org.apache.crunch.types.writable.Writables.java

/**
 * Registers a {@code WritableComparable} class so that it can be used for comparing the fields inside of
 * tuple types (e.g., {@code pairs}, {@code trips}, {@code tupleN}, etc.) for use in sorts and
 * secondary sorts./*from w ww .  j av  a 2s .c o  m*/
 *
 * @param clazz The WritableComparable class to register
 * @return the integer code that was assigned to serialized instances of this class
 */
public static void registerComparable(Class<? extends WritableComparable> clazz) {
    int code = clazz.hashCode();
    if (code < 0) {
        code = -code;
    }
    if (code < WRITABLE_CODES.size()) {
        code += WRITABLE_CODES.size();
    }
    registerComparable(clazz, code);
}

From source file:ca.oson.json.util.ObjectUtil.java

public static int hashCode(Object obj, Class valueType) {
    int hash = 7;

    try {/*from  www  . j  a  v a  2 s. c  o  m*/
        if (obj == null) {
            hash = valueType.hashCode();
        } else {
            hash = obj.hashCode();
        }

    } catch (Exception ex) {
    }

    if (obj != null && Math.abs(hash) < 100) {
        String str = obj.toString();
        hash += str.hashCode();
    }

    return hash;
}

From source file:grails.plugin.springsecurity.acl.AclAutoProxyCreator.java

@Override
protected Object getCacheKey(Class<?> beanClass, String beanName) {
    return beanClass.hashCode() + "_" + beanName;
}

From source file:rubah.runtime.classloader.RubahClassloader.java

@Override
protected synchronized Class<?> loadClass(String className, boolean resolve) throws ClassNotFoundException {
    byte[] classBytes = null;
    VersionManager updateManager = VersionManager.getInstance();

    Class<?> ret = findLoadedClass(className);

    if (ret != null) {
        // Do nothing
    } else if (className.startsWith("rubah.") || className.startsWith("java") ||
    //            className.startsWith("com.sun.jna") ||
            className.startsWith("org.xml.sax") || className.startsWith("sun")) {
        ret = super.loadClass(className, resolve);
        resolve = false;/*from   w  ww . ja va 2s .com*/
    } else if (className.startsWith(PureConversionClassLoader.PURE_CONVERSION_PREFFIX)) {
        try {
            Version version = updateManager.getRunningVersion();
            UpdateClass updateClass = updateManager.getUpdateClass(version);
            classBytes = new PureConversionClassLoader(version, updateClass).getClass(className);
            writeClassFile(className, classBytes);
            ret = this.defineClass(className, classBytes, 0, classBytes.length);
            //            return ret;
        } catch (IOException e) {
            throw new Error(e);
        }
    } else if (ProxyGenerator.isProxyName(className)) {
        Version latest = updateManager.getLatestVersion();
        String proxiedName = ProxyGenerator.getOriginalName(className);
        String originalName = latest.getOriginalName(proxiedName);
        originalName = (originalName == null ? proxiedName : originalName);
        Clazz c = latest.getNamespace().getClass(Type.getObjectType(originalName.replace('.', '/')));
        classBytes = new ProxyGenerator(c, updateManager.getRunningVersion()).generateProxy(className);
        writeClassFile(className, classBytes);
        ret = this.defineClass(className, classBytes, 0, classBytes.length);
    } else {

        try {
            classBytes = Rubah.getClassBytes(className);
            writeClassFile(className, classBytes);
            ret = this.defineClass(className, classBytes, 0, classBytes.length);
            redefinableClasses.add(ret);
        } catch (IOException e) {
            throw new ClassNotFoundException();
        }

        Version latest = VersionManager.getInstance().getLatestVersion();
        Version prev = latest.getPrevious();

        if (prev != null) {
            String prevName = prev.getUpdatableName(latest.getOriginalName(className));
            Class<?> prevClass = this.findLoadedClass(prevName);

            if (prevClass != null) {
                setHashCode(ret, prevClass.hashCode());
            }
        }
    }

    if (AddTraverseMethod.isAllowed(className)) {
        loadedClasses.add(ret);
        loadedClassNames.add(ret.getName());
    }

    if (resolve)
        this.resolveClass(ret);

    if (ret.hashCode() == 0)
        setHashCode(ret);

    Boolean isResolved = this.resolved.get(ret.getName());
    if (isResolved == null)
        this.resolved.put(ret.getName(), false);

    return ret;
}