Example usage for java.lang.reflect InvocationHandler getClass

List of usage examples for java.lang.reflect InvocationHandler getClass

Introduction

In this page you can find the example usage for java.lang.reflect InvocationHandler getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:net.sf.beanlib.hibernate3.Hibernate3SequenceGenerator.java

/** Returns the next sequence id from the specified sequence and session. */
public static long nextval(final String sequenceName, final Session session) {
    Object target = session;//from www  .  j  ava  2s . co m

    if (Proxy.isProxyClass(session.getClass())) {
        // Dig out the underlying session.
        InvocationHandler invocationHandler = Proxy.getInvocationHandler(session);

        if (invocationHandler instanceof DtoCentricCloseSuppressingInvocationHandler) {
            // This is faster for we don't need to use reflection.
            DtoCentricCloseSuppressingInvocationHandler dch = (DtoCentricCloseSuppressingInvocationHandler) invocationHandler;
            target = dch.getTarget();
        } else {
            Class<?> invocationHandlerClass = invocationHandler.getClass();
            Class<?> invocationHandlerDeclaringClass = invocationHandlerClass.getDeclaringClass();

            if (invocationHandlerDeclaringClass == HibernateTemplate.class) {
                String className = invocationHandlerClass.getName();

                if (className.endsWith("CloseSuppressingInvocationHandler")) {
                    // Assume this is the private class org.springframework.orm.hibernate3.HibernateTempate$CloseSuppressingInvocationHandler
                    // Dig out the private target.  
                    // I know this is bad, but there doesn't seem to be a better way.  Oh well.
                    try {
                        Field f = invocationHandlerClass.getDeclaredField("target");
                        f.setAccessible(true);
                        target = f.get(invocationHandler);
                    } catch (SecurityException e) {
                        throw new RuntimeException(e);
                    } catch (NoSuchFieldException e) {
                        throw new RuntimeException(e);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }

                }
            }

        }
    }
    SessionImpl sessionImpl;

    if (target instanceof SessionImpl)
        sessionImpl = (SessionImpl) target;
    else
        throw new IllegalStateException("Not yet know how to handle the given session!");
    IdentifierGenerator idGenerator = createIdentifierGenerator(sequenceName, session);
    Serializable id = idGenerator.generate(sessionImpl, null);
    return (Long) id;
}

From source file:com.github.cherimojava.data.mongo.entity.EntityInvocationHandler.java

/**
 * equals method of the entity represented by this EntityInvocationHandler instance. Objects are considered unequal
 * (false) if o is:/*  ww  w  .jav a  2  s  . c  o m*/
 * <ul>
 * <li>null
 * <li>no Proxy
 * <li>different Proxy class
 * <li>Different Entity class
 * <li>Data doesn't match
 * </ul>
 * If all the above is false both entities are considered equal and true will be returned
 *
 * @param o object to compare this instance with
 * @return true if both objects match the before mentioned criteria otherwise false
 */
private boolean _equals(Object o) {
    if (o == null) {
        return false;
    }
    if (!Proxy.isProxyClass(o.getClass())) {
        // for all non proxies we know that we can return false
        return false;
    }
    InvocationHandler ihandler = Proxy.getInvocationHandler(o);
    if (!ihandler.getClass().equals(getClass())) {
        // for all proxies not being EntityInvocationHandler return false
        return false;
    }
    EntityInvocationHandler handler = (EntityInvocationHandler) ihandler;
    if (!handler.properties.getEntityClass().equals(properties.getEntityClass())) {
        // this is not the same entity class, so false
        return false;
    }
    // make sure both have all lazy dependencies resolved
    lazyLoad();
    handler.lazyLoad();
    return data.equals(handler.data);
}