alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (GroovyResultSetProxy.java)

This example Groovy source code file (GroovyResultSetProxy.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

class, groovyresultset, groovyresultset, groovyresultsetextension, groovyresultsetproxy, groovyresultsetproxy, invocationhandler, jdbc, metaclass, metaclass, method, object, object, reflection, sql, string, throwable

The Groovy GroovyResultSetProxy.java source code

/*
 * Copyright 2003-2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package groovy.sql;

import groovy.lang.GroovyObject;
import groovy.lang.GroovySystem;
import groovy.lang.MetaClass;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.ResultSet;

import org.codehaus.groovy.runtime.InvokerHelper;

/**
 * GroovyResultSetProxy is used to create a proxy for GroovyResultSet.
 * Due to the version incompatibility between java 6 and older versions
 * methods with additional logic were moved into an extension class. When
 * getting properties or calling methods, the runtime will try to first
 * execute these on the extension and then on the ResultSet itself.
 * This way it is possible to replace and add methods. To overload methods
 * from ResultSet all methods have to be implemented on the extension
 * class.
 *
 * @author Jochen Theodorou
 */
public final class GroovyResultSetProxy implements InvocationHandler {

    private GroovyResultSetExtension extension;

    /**
     * Creates a new proxy instance.
     * This will create the extension automatically using
     * GroovyResultSetExtension
     *
     * @param set the result set to delegate to
     * @see GroovyResultSetExtension
     */
    public GroovyResultSetProxy(ResultSet set) {
        extension = new GroovyResultSetExtension(set);
    }

    /**
     * Creates a new proxy instance with a custom extension.
     *
     * @param ext the extension
     * @see GroovyResultSetExtension
     */
    public GroovyResultSetProxy(GroovyResultSetExtension ext) {
        extension = ext;
    }

    /**
     * Invokes a method for the GroovyResultSet.
     * This will try to invoke the given method first on the extension
     * and then on the result set given as proxy parameter.
     *
     * @param proxy  the result set
     * @param method the method name of this method will be used
     *               to make a call on the extension. If this fails the call will be
     *               done on the proxy instead
     * @param args   for the call
     * @see ResultSet
     * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
     */
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String name = method.getName();
        if (method.getDeclaringClass() == GroovyObject.class) {
            if (name.equals("getMetaClass")) {
                return getMetaClass();
            } else if (name.equals("setMetaClass")) {
                return setMetaClass((MetaClass) args[0]);
            }
        }

        return InvokerHelper.invokeMethod(extension, method.getName(), args);
    }

    private MetaClass metaClass;

    private MetaClass setMetaClass(MetaClass mc) {
        metaClass = mc;
        return mc;
    }

    private MetaClass getMetaClass() {
        if (metaClass == null) {
            metaClass = GroovySystem.getMetaClassRegistry().getMetaClass(GroovyResultSet.class);
        }
        return metaClass;
    }

    /**
     * Gets a proxy instance that can be used as GroovyResultSet.
     *
     * @return the proxy
     */
    public GroovyResultSet getImpl() {
        return (GroovyResultSet)
                Proxy.newProxyInstance(
                        this.getClass().getClassLoader(),
                        new Class[]{GroovyResultSet.class},
                        this
                );
    }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy GroovyResultSetProxy.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.