View Javadoc

    1   /*
    2    * Copyright 2006 John G. Wilson
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17
    18  package org.codehaus.groovy.runtime.wrappers;
    19
    20  import groovy.lang.GroovyObject;
    21  import groovy.lang.MetaClass;
    22
    23  import org.codehaus.groovy.runtime.InvokerHelper;
    24
    25  /***
    26   * @author John Wilson
    27   *
    28   */
    29
    30  public class PojoWrapper extends Wrapper {
    31    protected MetaClass delegate;
    32    protected final Object wrapped;
    33
    34    public PojoWrapper(final Object wrapped, final Class constrainedType) {
    35      super(constrainedType);
    36      this.wrapped = wrapped;
    37      this.delegate = InvokerHelper.getMetaClass(wrapped);
    38
    39      /*
    40       * This check is temporary - remove before 1.0 release
    41       */
    42
    43      if (wrapped instanceof GroovyObject) {
    44        throw new RuntimeException("trying to wrap the groovyObject "
    45                                   + wrapped.getClass().getName()
    46                                   + " in a PojoWrapper");
    47      }
    48    }
    49
    50    public Object unwrap() {
    51      return this.wrapped;
    52    }
    53
    54    /***
    55     * Note the rest of these method will only be used post 1.0
    56     */
    57
    58    /* (non-Javadoc)
    59     * @see groovy.lang.GroovyObject#getProperty(java.lang.String)
    60     */
    61    public Object getProperty(final String property) {
    62      return this.delegate.getProperty(this.wrapped, property);
    63    }
    64
    65    /* (non-Javadoc)
    66     * @see groovy.lang.GroovyObject#invokeMethod(java.lang.String, java.lang.Object)
    67     */
    68    public Object invokeMethod(final String methodName, final Object arguments) {
    69      return this.delegate.invokeMethod(this.wrapped, methodName, arguments);
    70    }
    71
    72    /* (non-Javadoc)
    73     * @see groovy.lang.GroovyObject#setMetaClass(groovy.lang.MetaClass)
    74     */
    75    public void setMetaClass(final MetaClass metaClass) {
    76      this.delegate = metaClass;
    77    }
    78
    79    /* (non-Javadoc)
    80     * @see groovy.lang.GroovyObject#setProperty(java.lang.String, java.lang.Object)
    81     */
    82    public void setProperty(final String property, final Object newValue) {
    83      this.delegate.setProperty(this.wrapped, property, newValue);
    84    }
    85
    86    protected Object getWrapped() {
    87      return this.wrapped;
    88    }
    89
    90    protected MetaClass getDelegatedMetaClass() {
    91      return this.delegate;
    92    }
    93  }