Example usage for java.util Iterator getClass

List of usage examples for java.util Iterator getClass

Introduction

In this page you can find the example usage for java.util Iterator getClass.

Prototype

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

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.broadleafcommerce.common.util.BLCCollectionUtils.java

/**
 * Create a collection proxy that will perform some piece of work whenever modification methods are called on the
 * proxy. This includes the add, allAll, remove, removeAll, clear methods. Additionally, calling remove on an iterator
 * created from this collection is also covered.
 *
 * @param work the work to perform on collection modification
 * @param original the original collection to make change aware
 * @param <T> the collection type (e.g. List, Set, etc...)
 * @return the proxied collection//w  w w  . j  a v  a  2s  .c om
 */
public static <T extends Collection> T createChangeAwareCollection(final WorkOnChange work,
        final Collection original) {
    T proxy = (T) Proxy.newProxyInstance(BLCCollectionUtils.class.getClassLoader(),
            ClassUtils.getAllInterfacesForClass(original.getClass()), new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    if (method.getName().startsWith("add") || method.getName().startsWith("remove")
                            || method.getName().startsWith("clear")) {
                        work.doWork(original);
                    }
                    if (method.getName().equals("iterator")) {
                        final Iterator itr = (Iterator) method.invoke(original, args);
                        Iterator proxyItr = (Iterator) Proxy.newProxyInstance(getClass().getClassLoader(),
                                ClassUtils.getAllInterfacesForClass(itr.getClass()), new InvocationHandler() {
                                    @Override
                                    public Object invoke(Object proxy, Method method, Object[] args)
                                            throws Throwable {
                                        if (method.getName().equals("remove")) {
                                            work.doWork(original);
                                        }
                                        return method.invoke(itr, args);
                                    }
                                });
                        return proxyItr;
                    }
                    return method.invoke(original, args);
                }
            });
    return proxy;
}