org.grails.web.converters.marshaller.ProxyUnwrappingMarshaller Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of grace-plugin-converters Show documentation
Show all versions of grace-plugin-converters Show documentation
Grace Framework : Grace Plugin Converters
/*
* Copyright 2004-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.grails.web.converters.marshaller;
import groovy.lang.GroovySystem;
import groovy.lang.MetaClass;
import groovy.lang.MetaClassRegistry;
import grails.util.GrailsNameUtils;
import org.grails.web.converters.Converter;
import org.grails.web.converters.exceptions.ConverterException;
/**
* Unwraps Hibernate proxies with no direct references to the Hibernate APIs
*
* @author Siegfried Puchbauer
* @author Graeme Rocher
*
* @since 1.1
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public class ProxyUnwrappingMarshaller implements ObjectMarshaller, NameAwareMarshaller {
private static final String HIBERNATE_LAZY_INITIALIZER_PROP = "hibernateLazyInitializer";
private static final String IMPLEMENTATION_PROP = "implementation";
public boolean supports(Object object) {
if (object == null) {
return false;
}
MetaClass mc = GroovySystem.getMetaClassRegistry().getMetaClass(object.getClass());
return mc.hasProperty(object, HIBERNATE_LAZY_INITIALIZER_PROP) != null;
}
public void marshalObject(Object object, C converter) throws ConverterException {
Object unwrapped = unwrap(object);
converter.lookupObjectMarshaller(unwrapped).marshalObject(unwrapped, converter);
}
private Object unwrap(Object o) {
if (o == null) {
return o;
}
MetaClassRegistry registry = GroovySystem.getMetaClassRegistry();
MetaClass mc = registry.getMetaClass(o.getClass());
Object hibernateLazyInitializer = mc.getProperty(o, HIBERNATE_LAZY_INITIALIZER_PROP);
return registry.getMetaClass(hibernateLazyInitializer.getClass()).getProperty(
hibernateLazyInitializer, IMPLEMENTATION_PROP);
}
public String getElementName(Object o) {
return GrailsNameUtils.getPropertyName(unwrap(o).getClass().getName());
}
}