org.jadira.reflection.cloning.implementor.UnsafeCloneStrategy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cloning Show documentation
Show all versions of cloning Show documentation
Cloning for Jadira Framework
/*
* Copyright 2013 Chris Pheby
*
* 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
*
* http://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.jadira.reflection.cloning.implementor;
import java.util.IdentityHashMap;
import org.jadira.reflection.access.model.ClassModel;
import org.jadira.reflection.access.model.FieldModel;
import org.jadira.reflection.access.unsafe.UnsafeClassAccess;
import org.jadira.reflection.access.unsafe.UnsafeOperations;
import org.jadira.reflection.cloning.api.CloneDriver;
import org.jadira.reflection.cloning.api.CloneStrategy;
import org.objenesis.ObjenesisException;
/**
* A CloneStrategy that uses sun.misc.Unsafe
*/
public class UnsafeCloneStrategy extends AbstractCloneStrategy implements CloneStrategy {
private static final UnsafeOperations UNSAFE_OPERATIONS = UnsafeOperations.getUnsafeOperations();
@Override
public T newInstance(Class c) {
try {
return UNSAFE_OPERATIONS.allocateInstance(c);
} catch (IllegalStateException e) {
throw new ObjenesisException(e.getCause());
}
}
private static UnsafeCloneStrategy instance = new UnsafeCloneStrategy();
/**
* Returns a shared instance of UnsafeCloneStrategy
* @return The instance
*/
public static UnsafeCloneStrategy getInstance() {
return instance;
}
@Override
protected ClassModel getClassModel(Class clazz) {
return UnsafeClassAccess.get(clazz).getClassModel();
}
@Override
protected void handleTransientField(T copy, FieldModel f) {
Class> type = f.getFieldClass();
if (type.isPrimitive()) {
if (java.lang.Boolean.TYPE == type) {
f.getFieldAccess().putBooleanValue(copy, false);
} else if (java.lang.Byte.TYPE == type) {
f.getFieldAccess().putByteValue(copy, (byte) 0);
} else if (java.lang.Character.TYPE == type) {
f.getFieldAccess().putCharValue(copy, '\u0000');
} else if (java.lang.Short.TYPE == type) {
f.getFieldAccess().putShortValue(copy, (short) 0);
} else if (java.lang.Integer.TYPE == type) {
f.getFieldAccess().putIntValue(copy, 0);
} else if (java.lang.Long.TYPE == type) {
f.getFieldAccess().putLongValue(copy, 0L);
} else if (java.lang.Float.TYPE == type) {
f.getFieldAccess().putFloatValue(copy, 0.0f);
} else if (java.lang.Double.TYPE == type) {
f.getFieldAccess().putDoubleValue(copy, 0.0d);
}
} else {
f.getFieldAccess().putValue(copy, null);
}
}
@Override
protected void handleClonePrimitiveField(T obj, T copy, CloneDriver driver, FieldModel f,
IdentityHashMap