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