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 java.lang.reflect.Constructor;
    21  import java.lang.reflect.Method;
    22  import java.util.List;
    23  import java.util.Map;
    24
    25  import org.codehaus.groovy.ast.ClassNode;
    26
    27  import groovy.lang.GroovyObject;
    28  import groovy.lang.MetaClass;
    29  import groovy.lang.MetaMethod;
    30
    31  /***
    32   * @author John Wilson
    33   *
    34   */
    35
    36  public abstract class Wrapper implements GroovyObject {
    37    protected MetaClass delegatingMetaClass = new MetaClass(Object.class) {
    38      /***
    39       * @param obj
    40       * @return
    41       * @see java.lang.Object#equals(java.lang.Object)
    42       */
    43      public boolean equals(Object obj) {
    44        return Wrapper.this.getDelegatedMetaClass().equals(obj);
    45      }
    46
    47      /***
    48       * @param object
    49       * @param attribute
    50       * @return
    51       * @see groovy.lang.MetaClass#getAttribute(java.lang.Object, java.lang.String)
    52       */
    53      public Object getAttribute(Object object, String attribute) {
    54        return Wrapper.this.getDelegatedMetaClass().getAttribute(Wrapper.this.getWrapped(), attribute);
    55      }
    56
    57      /***
    58       * @return
    59       * @see groovy.lang.MetaClass#getClassNode()
    60       */
    61      public ClassNode getClassNode() {
    62        return Wrapper.this.getDelegatedMetaClass().getClassNode();
    63      }
    64
    65      /***
    66       * @return
    67       * @see groovy.lang.MetaClass#getMetaMethods()
    68       */
    69      public List getMetaMethods() {
    70        return Wrapper.this.getDelegatedMetaClass().getMetaMethods();
    71      }
    72
    73      /***
    74       * @return
    75       * @see groovy.lang.MetaClass#getMethods()
    76       */
    77      public List getMethods() {
    78        return Wrapper.this.getDelegatedMetaClass().getMethods();
    79      }
    80
    81      /***
    82       * @return
    83       * @see groovy.lang.MetaClass#getProperties()
    84       */
    85      public List getProperties() {
    86        return Wrapper.this.getDelegatedMetaClass().getProperties();
    87      }
    88
    89      /***
    90       * @param object
    91       * @param property
    92       * @return
    93       * @see groovy.lang.MetaClass#getProperty(java.lang.Object, java.lang.String)
    94       */
    95      public Object getProperty(Object object, String property) {
    96        return Wrapper.this.getDelegatedMetaClass().getProperty(Wrapper.this.getWrapped(), property);
    97      }
    98
    99      /***
    100      * @return
    101      * @see java.lang.Object#hashCode()
    102      */
    103     public int hashCode() {
    104       return Wrapper.this.getDelegatedMetaClass().hashCode();
    105     }
    106
    107     /***
    108      * @param arguments
    109      * @return
    110      * @see groovy.lang.MetaClass#invokeConstructor(java.lang.Object[])
    111      */
    112     public Object invokeConstructor(Object[] arguments) {
    113       return Wrapper.this.getDelegatedMetaClass().invokeConstructor(arguments);
    114     }
    115
    116     /***
    117      * @param at
    118      * @param arguments
    119      * @return
    120      * @see groovy.lang.MetaClass#invokeConstructorAt(java.lang.Class, java.lang.Object[])
    121      */
    122     public Object invokeConstructorAt(Class at, Object[] arguments) {
    123       return Wrapper.this.getDelegatedMetaClass().invokeConstructorAt(at, arguments);
    124     }
    125
    126     /***
    127      * @param object
    128      * @param methodName
    129      * @param arguments
    130      * @return
    131      * @see groovy.lang.MetaClass#invokeMethod(java.lang.Object, java.lang.String, java.lang.Object)
    132      */
    133     public Object invokeMethod(Object object, String methodName, Object arguments) {
    134       return Wrapper.this.getDelegatedMetaClass().invokeMethod(Wrapper.this.getWrapped(), methodName, arguments);
    135     }
    136
    137     /***
    138      * @param object
    139      * @param methodName
    140      * @param arguments
    141      * @return
    142      * @see groovy.lang.MetaClass#invokeMethod(java.lang.Object, java.lang.String, java.lang.Object[])
    143      */
    144     public Object invokeMethod(Object object, String methodName, Object[] arguments) {
    145       return Wrapper.this.getDelegatedMetaClass().invokeMethod(Wrapper.this.getWrapped(), methodName, arguments);
    146     }
    147
    148     /***
    149      * @param object
    150      * @param methodName
    151      * @param arguments
    152      * @return
    153      * @see groovy.lang.MetaClass#invokeStaticMethod(java.lang.Object, java.lang.String, java.lang.Object[])
    154      */
    155     public Object invokeStaticMethod(Object object, String methodName, Object[] arguments) {
    156       return Wrapper.this.getDelegatedMetaClass().invokeStaticMethod(Wrapper.this.getWrapped(), methodName, arguments);
    157     }
    158
    159     /***
    160      * @param arguments
    161      * @return
    162      * @see groovy.lang.MetaClass#retrieveConstructor(java.lang.Class[])
    163      */
    164     public Constructor retrieveConstructor(Class[] arguments) {
    165       return Wrapper.this.getDelegatedMetaClass().retrieveConstructor(arguments);
    166     }
    167
    168     /***
    169      * @param owner
    170      * @param methodName
    171      * @param arguments
    172      * @return
    173      * @see groovy.lang.MetaClass#retrieveMethod(java.lang.Object, java.lang.String, java.lang.Object[])
    174      */
    175     public MetaMethod retrieveMethod(Object owner, String methodName, Object[] arguments) {
    176       return Wrapper.this.getDelegatedMetaClass().retrieveMethod(owner, methodName, arguments);
    177     }
    178
    179     /***
    180      * @param methodName
    181      * @param arguments
    182      * @return
    183      * @see groovy.lang.MetaClass#retrieveMethod(java.lang.String, java.lang.Class[])
    184      */
    185     public MetaMethod retrieveMethod(String methodName, Class[] arguments) {
    186       return Wrapper.this.getDelegatedMetaClass().retrieveMethod(methodName, arguments);
    187     }
    188
    189     /***
    190      * @param methodName
    191      * @param arguments
    192      * @return
    193      * @see groovy.lang.MetaClass#retrieveStaticMethod(java.lang.String, java.lang.Class[])
    194      */
    195     public MetaMethod retrieveStaticMethod(String methodName, Class[] arguments) {
    196       return Wrapper.this.getDelegatedMetaClass().retrieveStaticMethod(methodName, arguments);
    197     }
    198
    199     /***
    200      * @param object
    201      * @param attribute
    202      * @param newValue
    203      * @see groovy.lang.MetaClass#setAttribute(java.lang.Object, java.lang.String, java.lang.Object)
    204      */
    205     public void setAttribute(Object object, String attribute, Object newValue) {
    206       Wrapper.this.getDelegatedMetaClass().setAttribute(Wrapper.this.getWrapped(), attribute, newValue);
    207     }
    208
    209     /***
    210      * @param bean
    211      * @param map
    212      * @see groovy.lang.MetaClass#setProperties(java.lang.Object, java.util.Map)
    213      */
    214     public void setProperties(Object bean, Map map) {
    215       Wrapper.this.getDelegatedMetaClass().setProperties(Wrapper.this.getWrapped(), map);
    216     }
    217
    218     /***
    219      * @param object
    220      * @param property
    221      * @param newValue
    222      * @see groovy.lang.MetaClass#setProperty(java.lang.Object, java.lang.String, java.lang.Object)
    223      */
    224     public void setProperty(Object object, String property, Object newValue) {
    225       Wrapper.this.getDelegatedMetaClass().setProperty(Wrapper.this.getWrapped(), property, newValue);
    226     }
    227
    228     /***
    229      * @return
    230      * @see java.lang.Object#toString()
    231      */
    232     public String toString() {
    233       return Wrapper.this.getDelegatedMetaClass().toString();
    234     }
    235
    236     /* (non-Javadoc)
    237      * @see groovy.lang.MetaClass#addNewInstanceMethod(java.lang.reflect.Method)
    238      */
    239     public void addNewInstanceMethod(Method method) {
    240       Wrapper.this.getDelegatedMetaClass().addNewInstanceMethod(method);
    241     }
    242
    243     /* (non-Javadoc)
    244      * @see groovy.lang.MetaClass#addNewStaticMethod(java.lang.reflect.Method)
    245      */
    246     public void addNewStaticMethod(Method method) {
    247       Wrapper.this.getDelegatedMetaClass().addNewStaticMethod(method);
    248     }
    249
    250     /* (non-Javadoc)
    251      * @see groovy.lang.MetaClass#checkInitialised()
    252      */
    253     public void checkInitialised() {
    254       Wrapper.this.getDelegatedMetaClass().checkInitialised();
    255     }
    256
    257     /* (non-Javadoc)
    258      * @see groovy.lang.MetaClass#pickMethod(java.lang.Object, java.lang.String, java.lang.Object[])
    259      */
    260     public MetaMethod pickMethod(Object object, String methodName, Object[] arguments) {
    261       return Wrapper.this.getDelegatedMetaClass().pickMethod(object, methodName, arguments);
    262     }
    263
    264     /* (non-Javadoc)
    265      * @see groovy.lang.MetaClass#pickMethod(java.lang.String, java.lang.Class[])
    266      */
    267     public MetaMethod pickMethod(String methodName, Class[] arguments) {
    268       return Wrapper.this.getDelegatedMetaClass().pickMethod(methodName, arguments);
    269     }
    270   };
    271
    272   protected final Class constrainedType;
    273
    274   public Wrapper(final Class constrainedType) {
    275     this.constrainedType = constrainedType;
    276   }
    277
    278   /* (non-Javadoc)
    279    * @see groovy.lang.GroovyObject#getMetaClass()
    280    *
    281    * This will only be useful post 1.0
    282    */
    283   public MetaClass getMetaClass() {
    284     return this.delegatingMetaClass;
    285   }
    286
    287   public abstract Object unwrap();
    288
    289   public Class getType() {
    290     return this.constrainedType;
    291   }
    292
    293   protected abstract Object getWrapped();
    294   protected abstract MetaClass getDelegatedMetaClass();
    295 }