com.sun.xml.bind.v2.runtime.reflect.NullSafeAccessor Maven / Gradle / Ivy
/*
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package com.sun.xml.bind.v2.runtime.reflect;
import com.sun.xml.bind.api.AccessorException;
/**
* {@link Accessor} wrapper that replaces a null with an empty collection.
*
*
* This is so that JAX-WS property accessor will work like an ordinary getter.
*
*
* @author Kohsuke Kawaguchi
*/
public class NullSafeAccessor extends Accessor {
private final Accessor core;
private final Lister lister;
public NullSafeAccessor(Accessor core, Lister lister) {
super(core.getValueType());
this.core = core;
this.lister = lister;
}
public V get(B bean) throws AccessorException {
V v = core.get(bean);
if(v==null) {
// creates a new object
P pack = lister.startPacking(bean,core);
lister.endPacking(pack,bean,core);
v = core.get(bean);
}
return v;
}
public void set(B bean, V value) throws AccessorException {
core.set(bean,value);
}
}