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 groovy.lang.MetaClass; 021 022 023 /** 024 * @author John Wilson 025 * 026 */ 027 028 public class PojoWrapper extends Wrapper { 029 protected MetaClass delegate; 030 protected final Object wrapped; 031 032 public PojoWrapper(final Object wrapped, final Class constrainedType) { 033 super(constrainedType); 034 this.wrapped = wrapped; 035 036 /* 037 * This check is temporary - remove before 1.0 release 038 */ 039 // if (wrapped instanceof GroovyObject) { 040 // throw new RuntimeException("trying to wrap the groovyObject " 041 // + wrapped.getClass().getName() 042 // + " in a PojoWrapper"); 043 // } 044 } 045 046 public Object unwrap() { 047 return this.wrapped; 048 } 049 050 /** 051 * Note the rest of these method will only be used post 1.0 052 */ 053 054 /* (non-Javadoc) 055 * @see groovy.lang.GroovyObject#getProperty(java.lang.String) 056 */ 057 public Object getProperty(final String property) { 058 return this.delegate.getProperty(this.wrapped, property); 059 } 060 061 /* (non-Javadoc) 062 * @see groovy.lang.GroovyObject#invokeMethod(java.lang.String, java.lang.Object) 063 */ 064 public Object invokeMethod(final String methodName, final Object arguments) { 065 return this.delegate.invokeMethod(this.wrapped, methodName, arguments); 066 } 067 068 /* (non-Javadoc) 069 * @see groovy.lang.GroovyObject#setMetaClass(groovy.lang.MetaClass) 070 */ 071 public void setMetaClass(final MetaClass metaClass) { 072 this.delegate = metaClass; 073 } 074 075 /* (non-Javadoc) 076 * @see groovy.lang.GroovyObject#setProperty(java.lang.String, java.lang.Object) 077 */ 078 public void setProperty(final String property, final Object newValue) { 079 this.delegate.setProperty(this.wrapped, property, newValue); 080 } 081 082 protected Object getWrapped() { 083 return this.wrapped; 084 } 085 086 protected MetaClass getDelegatedMetaClass() { 087 return this.delegate; 088 } 089 }