001 /*
002 * Copyright 2006 John G. Wilson
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 */
017
018 package org.codehaus.groovy.runtime.wrappers;
019
020 //import java.lang.reflect.Constructor;
021 //import java.lang.reflect.Method;
022 //import java.util.List;
023 //import java.util.Map;
024 //
025 //import org.codehaus.groovy.ast.ClassNode;
026
027 import groovy.lang.GroovyObject;
028 import groovy.lang.MetaClass;
029 //import groovy.lang.MetaMethod;
030
031 /**
032 * @author John Wilson
033 *
034 */
035
036 public abstract class Wrapper implements GroovyObject {
037 protected MetaClass delegatingMetaClass;
038
039 // protected MetaClass delegatingMetaClass = new MetaClass(Object.class) {
040 // /**
041 // * @param obj
042 // * @see java.lang.Object#equals(java.lang.Object)
043 // */
044 // public boolean equals(Object obj) {
045 // return Wrapper.this.getDelegatedMetaClass().equals(obj);
046 // }
047 //
048 // /**
049 // * @param object
050 // * @param attribute
051 // * @see groovy.lang.MetaClass#getAttribute(java.lang.Object, java.lang.String)
052 // */
053 // public Object getAttribute(Object object, String attribute) {
054 // return Wrapper.this.getDelegatedMetaClass().getAttribute(Wrapper.this.getWrapped(), attribute);
055 // }
056 //
057 // /**
058 // * @see groovy.lang.MetaClass#getClassNode()
059 // */
060 // public ClassNode getClassNode() {
061 // return Wrapper.this.getDelegatedMetaClass().getClassNode();
062 // }
063 //
064 // /**
065 // * @see groovy.lang.MetaClass#getMetaMethods()
066 // */
067 // public List getMetaMethods() {
068 // return Wrapper.this.getDelegatedMetaClass().getMetaMethods();
069 // }
070 //
071 // /**
072 // * @see groovy.lang.MetaClass#getMethods()
073 // */
074 // public List getMethods() {
075 // return Wrapper.this.getDelegatedMetaClass().getMethods();
076 // }
077 //
078 // /**
079 // * @see groovy.lang.MetaClass#getProperties()
080 // */
081 // public List getProperties() {
082 // return Wrapper.this.getDelegatedMetaClass().getProperties();
083 // }
084 //
085 // /**
086 // * @param object
087 // * @param property
088 // * @see groovy.lang.MetaClass#getProperty(java.lang.Object, java.lang.String)
089 // */
090 // public Object getProperty(Object object, String property) {
091 // return Wrapper.this.getDelegatedMetaClass().getProperty(Wrapper.this.getWrapped(), property);
092 // }
093 //
094 // /**
095 // * @see java.lang.Object#hashCode()
096 // */
097 // public int hashCode() {
098 // return Wrapper.this.getDelegatedMetaClass().hashCode();
099 // }
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 }